8000 feat(eslint-plugin): new extended rule 'no-invalid-this' (#1823) · DudaGod/typescript-eslint@b18bc35 · GitHub
[go: up one dir, main page]

Skip to content

Commit b18bc35

Browse files
feat(eslint-plugin): new extended rule 'no-invalid-this' (typescript-eslint#1823)
Co-Authored-By: Brad Zacher <brad.zacher@gmail.com>
1 parent 2f0824b commit b18bc35

File tree

7 files changed

+1030
-0
lines changed

7 files changed

+1030
-0
lines changed

packages/eslint-plugin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ In these cases, we create what we call an extension rule; a rule within our plug
192192
| [`@typescript-eslint/no-empty-function`](./docs/rules/no-empty-function.md) | Disallow empty functions | :heavy_check_mark: | | |
193193
| [`@typescript-eslint/no-extra-parens`](./docs/rules/no-extra-parens.md) | Disallow unnecessary parentheses | | :wrench: | |
194194
| [`@typescript-eslint/no-extra-semi`](./docs/rules/no-extra-semi.md) | Disallow unnecessary semicolons | | :wrench: | |
195+
| [`@typescript-eslint/no-invalid-this`](./docs/rules/no-invalid-this.md) | disallow `this` keywords outside of classes or class-like objects | | | |
195196
| [`@typescript-eslint/no-magic-numbers`](./docs/rules/no-magic-numbers.md) | Disallow magic numbers | | | |
196197
| [`@typescript-eslint/no-unused-expressions`](./docs/rules/no-unused-expressions.md) | Disallow unused expressions | | | |
197198
| [`@typescript-eslint/no-unused-vars`](./docs/rules/no-unused-vars.md) | Disallow unused variables | :heavy_check_mark: | | |
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# disallow `this` keywords outside of classes or class-like objects (`no-invalid-this`)
2+
3+
## Rule Details
4+
5+
This rule extends the base [`eslint/no-invalid-this`](https://eslint.org/docs/rules/no-invalid-this) rule.
6+
It adds support for TypeScript's `this` parameters.
7+
8+
## How to use
9+
10+
```cjson
11+
{
12+
// note you must disable the base rule as it can report incorrect errors
13+
"no-invalid-this": "off",
14+
"@typescript-eslint/no-invalid-this": ["error"]
15+
}
16+
```
17+
18+
## Options
19+
20+
See [`eslint/no-invalid-this` options](https://eslint.org/docs/rules/no-invalid-this#options).
21+
22+
## When Not To Use It
23+
24+
When you are indifferent as to how your variables are initialized.
25+
26+
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-invalid-this.md)</sup>

packages/eslint-plugin/src/configs/all.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
"@typescript-eslint/no-for-in-array": "error",
5353
"@typescript-eslint/no-implied-eval": "error",
5454
"@typescript-eslint/no-inferrable-types": "error",
55+
"no-invalid-this": "off",
56+
"@typescript-eslint/no-invalid-this": "error",
5557
"@typescript-eslint/no-invalid-void-type": "error",
5658
"no-magic-numbers": "off",
5759
"@typescript-eslint/no-magic-numbers": "error",

packages/eslint-plugin/src/rules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import noFloatingPromises from './no-floating-promises';
4141
import noForInArray from './no-for-in-array';
4242
import noImpliedEval from './no-implied-eval';
4343
import noInferrableTypes from './no-inferrable-types';
44+
import noInvalidThis from './no-invalid-this';
4445
import noInvalidVoidType from './no-invalid-void-type';
4546
import noMagicNumbers from './no-magic-numbers';
4647
import noMisusedNew from './no-misused-new';
@@ -145,6 +146,7 @@ export default {
145146
'no-for-in-array': noForInArray,
146147
'no-implied-eval': noImpliedEval,
147148
'no-inferrable-types': noInferrableTypes,
149+
'no-invalid-this': noInvalidThis,
148150
'no-invalid-void-type': noInvalidVoidType,
149151
'no-magic-numbers': noMagicNumbers,
150152
'no-misused-new': noMisusedNew,
Lines changed: 78 additions & 0 deletions
+
category: 'Best Practices',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {
2+
TSESTree,
3+
AST_NODE_TYPES,
4+
} from '@typescript-eslint/experimental-utils';
5+
import baseRule from 'eslint/lib/rules/no-invalid-this';
6+
import {
7+
InferOptionsTypeFromRule,
8+
createRule,
9+
InferMessageIdsTypeFromRule,
10+
} from '../util';
11+
12+
export type Options = InferOptionsTypeFromRule<typeof baseRule>;
13+
export type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>;
14+
15+
export default createRule<Options, MessageIds>({
16+
name: 'no-invalid-this',
17+
meta: {
18+
type: 'suggestion',
19+
docs: {
20+
description:
21+
'disallow `this` keywords outside of classes or class-like objects',
22
23+
recommended: false,
24+
extendsBaseRule: true,
25+
},
26+
messages: baseRule.meta.messages,
27+
schema: baseRule.meta.schema,
28+
},
29+
defaultOptions: [{ capIsConstructor: true }],
30+
create(context) {
31+
const rules = baseRule.create(context);
32+
const argList: boolean[] = [];
33+
34+
return {
35+
...rules,
36+
FunctionDeclaration(node: TSESTree.FunctionDeclaration): void {
37+
argList.push(
38+
node.params.some(
39+
param =>
40+
param.type === AST_NODE_TYPES.Identifier && param.name === 'this',
41+
),
42+
);
43+
// baseRule's work
44+
rules.FunctionDeclaration(node);
45+
},
46+
'FunctionDeclaration:exit'(node: TSESTree.FunctionDeclaration): void {
47+
argList.pop();
48+
// baseRule's work
49+
rules['FunctionDeclaration:exit'](node);
50+
},
51+
FunctionExpression(node: TSESTree.FunctionExpression): void {
52+
argList.push(
53+
node.params.some(
54+
param =>
55+
param.type === AST_NODE_TYPES.Identifier && param.name === 'this',
56+
),
57+
);
58+
// baseRule's work
59+
rules.FunctionExpression(node);
60+
},
61+
'FunctionExpression:exit'(node: TSESTree.FunctionExpression): void {
62+
argList.pop();
63+
// baseRule's work
64+
rules['FunctionExpression:exit'](node);
65+
},
66+
ThisExpression(node: TSESTree.ThisExpression): void {
67+
const lastFnArg = argList[argList.length - 1];
68+
69+
if (lastFnArg) {
70+
return;
71+
}
72+
73+
// baseRule's work
74+
rules.ThisExpression(node);
75+
},
76+
};
77+
},
78+
});

0 commit comments

Comments
 (0)
0