-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add
no-typeof
rule
- Loading branch information
Showing
17 changed files
with
113 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export function tuiIsNumber(value: unknown): value is number { | ||
// eslint-disable-next-line @taiga-ui/no-typeof | ||
return typeof value === `number`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export function tuiIsObject<T extends Record<any, any> | object>( | ||
value: unknown, | ||
): value is NonNullable<T> { | ||
// eslint-disable-next-line @taiga-ui/no-typeof | ||
return typeof value === `object` && !!value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export function tuiIsString(value: unknown): value is string { | ||
// eslint-disable-next-line @taiga-ui/no-typeof | ||
return typeof value === `string`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @type {import('eslint').Rule.RuleModule} | ||
*/ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
schema: [], | ||
}, | ||
create(context) { | ||
return { | ||
/** | ||
* @type {import('eslint').Rule.Node} | ||
* @return {*} | ||
*/ | ||
"BinaryExpression[operator='==='][left.operator='typeof']"(node) { | ||
markTypeOfString(context, node); | ||
markTypeOfNumber(context, node); | ||
markTypeOfObject(context, node); | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
/** | ||
* @param {import('eslint').Rule.RuleContext} context | ||
* @param {import('eslint').Rule.Node} node | ||
*/ | ||
function markTypeOfString(context, node) { | ||
if (checkRightOperandIs(node, `string`)) { | ||
context.report({ | ||
node, | ||
message: `Don't use "typeof value === 'string'" instead of tuiIsString(value)`, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @param {import('eslint').Rule.RuleContext} context | ||
* @param {import('eslint').Rule.Node} node | ||
*/ | ||
function markTypeOfNumber(context, node) { | ||
if (checkRightOperandIs(node, `number`)) { | ||
context.report({ | ||
node, | ||
message: `Don't use "typeof value === 'number'" instead of tuiIsNumber(value)`, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @param {import('eslint').Rule.RuleContext} context | ||
* @param {import('eslint').Rule.Node} node | ||
*/ | ||
function markTypeOfObject(context, node) { | ||
if (checkRightOperandIs(node, `object`)) { | ||
context.report({ | ||
node, | ||
message: `Don't use "typeof value === 'object'" instead of tuiIsObject(value)`, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @param {import('eslint').Rule.Node} node | ||
* @param {string} type | ||
*/ | ||
function checkRightOperandIs(node, type) { | ||
return node?.right?.quasis?.[0]?.value?.raw === type || node?.right?.value === type; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import {tuiIsString} from '../../projects/cdk'; | ||
|
||
export function bumpTuiVersionInPackageJson( | ||
packageJson: Record<string, unknown>, | ||
version: string, | ||
): void { | ||
if (`version` in packageJson && typeof packageJson[`version`] === `string`) { | ||
if (`version` in packageJson && tuiIsString(packageJson[`version`])) { | ||
packageJson[`version`] = version; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters