Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(eslint-plugin): new extended rule 'no-invalid-this' (#1823)
Co-Authored-By: Brad Zacher <brad.zacher@gmail.com>
- Loading branch information
1 parent
2f0824b
commit b18bc35
Showing
7 changed files
with
1,030 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 adds support for TypeScript's `this` parameters. | ||
|
||
## 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,78 @@ | ||
import { | ||
TSESTree, | ||
AST_NODE_TYPES, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import baseRule from 'eslint/lib/rules/no-invalid-this'; | ||
import { | ||
InferOptionsTypeFromRule, | ||
createRule, | ||
InferMessageIdsTypeFromRule, | ||
} from '../util'; | ||
|
||
export type Options = InferOptionsTypeFromRule<typeof baseRule>; | ||
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
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: baseRule.meta.schema, | ||
}, | ||
defaultOptions: [{ capIsConstructor: true }], | ||
create(context) { | ||
const rules = baseRule.create(context); | ||
const argList: boolean[] = []; | ||
|
||
return { | ||
...rules, | ||
FunctionDeclaration(node: TSESTree.FunctionDeclaration): void { | ||
argList.push( | ||
node.params.some( | ||
param => | ||
param.type === AST_NODE_TYPES.Identifier && param.name === 'this', | ||
), | ||
); | ||
// 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 { | ||
argList.push( | ||
node.params.some( | ||
param => | ||
param.type === AST_NODE_TYPES.Identifier && param.name === 'this', | ||
), | ||
); | ||
// 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): void { | ||
const lastFnArg = argList[argList.length - 1]; | ||
|
||
if (lastFnArg) { | ||
return; | ||
} | ||
|
||
// baseRule's work | ||
rules.ThisExpression(node); | ||
}, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.