forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noInvalidThisRule.ts
128 lines (115 loc) · 4.82 KB
/
noInvalidThisRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* @license
* Copyright 2018 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { isThisParameter } from "tsutils";
import * as ts from "typescript";
import * as Lint from "..";
const OPTION_FUNCTION_IN_METHOD = "check-function-in-method";
const DEPRECATED_OPTION_FUNCTION_IN_METHOD = "no-this-in-function-in-method";
export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-invalid-this",
description: "Disallows using the `this` keyword outside of classes.",
rationale:
"See [the rule's author's rationale here.](https://github.com/palantir/tslint/pull/1105#issue-147549402)",
optionsDescription: Lint.Utils.dedent`
One argument may be optionally provided:
* \`${OPTION_FUNCTION_IN_METHOD}\` disallows using the \`this\` keyword in functions within class methods.`,
options: {
type: "array",
items: {
type: "string",
enum: [OPTION_FUNCTION_IN_METHOD],
},
minLength: 0,
maxLength: 1,
},
optionExamples: [true, [true, OPTION_FUNCTION_IN_METHOD]],
type: "functionality",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */
public static FAILURE_STRING_OUTSIDE =
'the "this" keyword is disallowed outside of a class body';
public static FAILURE_STRING_INSIDE =
'the "this" keyword is disallowed in function bodies inside class methods, ' +
"use arrow functions instead";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const hasOption = (name: string) => this.ruleArguments.indexOf(name) !== -1;
const checkFuncInMethod =
hasOption(DEPRECATED_OPTION_FUNCTION_IN_METHOD) || hasOption(OPTION_FUNCTION_IN_METHOD);
return this.applyWithFunction(sourceFile, walk, checkFuncInMethod);
}
}
const enum ParentType {
None,
Class,
ClassMethod,
BoundRegularFunction,
UnboundRegularFunction,
}
const thisAllowedParents = new Set([ParentType.ClassMethod, ParentType.BoundRegularFunction]);
function walk(ctx: Lint.WalkContext<boolean>): void {
const { sourceFile, options: checkFuncInMethod } = ctx;
let currentParent: ParentType = ParentType.None;
let inClass = false;
ts.forEachChild(sourceFile, function cb(node: ts.Node) {
const originalParent = currentParent;
const originalInClass = inClass;
switch (node.kind) {
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ClassExpression:
inClass = true;
currentParent = ParentType.Class;
ts.forEachChild(node, cb);
currentParent = originalParent;
inClass = originalInClass;
return;
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.GetAccessor:
case ts.SyntaxKind.SetAccessor:
case ts.SyntaxKind.Constructor:
case ts.SyntaxKind.PropertyDeclaration:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.FunctionExpression:
if (currentParent === ParentType.Class) {
currentParent = ParentType.ClassMethod;
ts.forEachChild(node, cb);
currentParent = originalParent;
return;
}
currentParent = (node as ts.FunctionLikeDeclaration).parameters.some(
isThisParameter,
)
? ParentType.BoundRegularFunction
: ParentType.UnboundRegularFunction;
ts.forEachChild(node, cb);
currentParent = originalParent;
return;
case ts.SyntaxKind.ThisKeyword:
if (!thisAllowedParents.has(currentParent)) {
if (!inClass) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING_OUTSIDE);
} else if (checkFuncInMethod) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING_INSIDE);
}
}
return;
}
ts.forEachChild(node, cb);
});
}