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): new extendedrule 'no-invalid-this'
- Loading branch information
1 parent
f3160b4
commit 951faa1
Showing
7 changed files
with
601 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# disallow `this` keywords outside of classes or class-like objects (`no-invalid-this`) | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [`eslint/no-invalid-this`](https://eslint.org/docs/rules/no-invalid-this) rule. | ||
It supports all options and features of the base rule. | ||
|
||
## How to use | ||
|
||
```cjson | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"no-invalid-this": "off", | ||
"@typescript-eslint/no-invalid-this": ["error"] | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
See [`eslint/no-invalid-this` options](https://eslint.org/docs/rules/no-invalid-this#options). | ||
|
||
## When Not To Use It | ||
|
||
When you are indifferent as to how your variables are initialized. | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-invalid-this.md)</sup> |
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,86 @@ | ||
import { TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import baseRule from 'eslint/lib/rules/no-invalid-this'; | ||
import { | ||
InferOptionsTypeFromRule, | ||
InferMessageIdsTypeFromRule, | ||
createRule, | ||
deepMerge, | ||
} from '../util'; | ||
|
||
export type Options = InferOptionsTypeFromRule<typeof baseRule>; | ||
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
const schema = deepMerge( | ||
Array.isArray(baseRule.meta.schema) | ||
? baseRule.meta.schema[0] | ||
: baseRule.meta.schema, | ||
{ | ||
properties: { | ||
capIsConstructor: { | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
}, | ||
}, | ||
); | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: 'no-invalid-this', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'disallow `this` keywords outside of classes or class-like objects', | ||
category: 'Best Practices', | ||
recommended: false, | ||
extendsBaseRule: true, | ||
}, | ||
messages: baseRule.meta.messages, | ||
schema: [schema], | ||
}, | ||
defaultOptions: [{ capIsConstructor: true }], | ||
create(context) { | ||
const rules = baseRule.create(context); | ||
let argList: Array<string[]> = []; | ||
|
||
return { | ||
...rules, | ||
FunctionDeclaration(node: TSESTree.FunctionDeclaration): void { | ||
const names = node?.params.map( | ||
(param: TSESTree.Parameter) => param?.name, | ||
); | ||
argList.push(names); | ||
// baseRule's work | ||
rules.FunctionDeclaration(node); | ||
}, | ||
'FunctionDeclaration:exit'(node: TSESTree.FunctionDeclaration): void { | ||
argList.pop(); | ||
// baseRule's work | ||
rules['FunctionDeclaration:exit'](node); | ||
}, | ||
FunctionExpression(node: TSESTree.FunctionExpression): void { | ||
const names = node?.params.map( | ||
(param: TSESTree.Parameter) => param?.name, | ||
); | ||
argList.push(names); | ||
// baseRule's work | ||
rules.FunctionExpression(node); | ||
}, | ||
'FunctionExpression:exit'(node: TSESTree.FunctionExpression): void { | ||
argList.pop(); | ||
// baseRule's work | ||
rules['FunctionExpression:exit'](node); | ||
}, | ||
ThisExpression(node: TSESTree.ThisExpression) { | ||
const lastFnArg = argList[argList.length - 1]; | ||
|
||
if (lastFnArg?.some((name: string) => name === 'this')) { | ||
return; | ||
} | ||
|
||
// baseRule's work | ||
rules.ThisExpression(node); | ||
}, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.