forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enableDisableRules.ts
172 lines (145 loc) · 7.4 KB
/
enableDisableRules.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* @license
* Copyright 2014 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 * as ts from "typescript";
import {AbstractRule} from "./language/rule/abstractRule";
import {IOptions} from "./language/rule/rule";
import {scanAllTokens} from "./language/utils";
import {SkippableTokenAwareRuleWalker} from "./language/walker/skippableTokenAwareRuleWalker";
import {IEnableDisablePosition} from "./ruleLoader";
export class EnableDisableRulesWalker extends SkippableTokenAwareRuleWalker {
public enableDisableRuleMap: {[rulename: string]: IEnableDisablePosition[]} = {};
constructor(sourceFile: ts.SourceFile, options: IOptions, rules: {[name: string]: any}) {
super(sourceFile, options);
if (rules) {
for (const rule in rules) {
if (rules.hasOwnProperty(rule) && AbstractRule.isRuleEnabled(rules[rule])) {
this.enableDisableRuleMap[rule] = [{
isEnabled: true,
position: 0,
}];
}
}
}
}
public visitSourceFile(node: ts.SourceFile) {
super.visitSourceFile(node);
const scan = ts.createScanner(ts.ScriptTarget.ES5, false, ts.LanguageVariant.Standard, node.text);
scanAllTokens(scan, (scanner: ts.Scanner) => {
const startPos = scanner.getStartPos();
if (this.tokensToSkipStartEndMap[startPos] != null) {
// tokens to skip are places where the scanner gets confused about what the token is, without the proper context
// (specifically, regex, identifiers, and templates). So skip those tokens.
scanner.setTextPos(this.tokensToSkipStartEndMap[startPos]);
return;
}
if (scanner.getToken() === ts.SyntaxKind.MultiLineCommentTrivia ||
scanner.getToken() === ts.SyntaxKind.SingleLineCommentTrivia) {
const commentText = scanner.getTokenText();
const startPosition = scanner.getTokenPos();
this.handlePossibleTslintSwitch(commentText, startPosition, node, scanner);
}
});
}
private getStartOfLinePosition(node: ts.SourceFile, position: number, lineOffset = 0) {
const line = ts.getLineAndCharacterOfPosition(node, position).line + lineOffset;
const lineStarts = node.getLineStarts();
if (line >= lineStarts.length) {
// next line ends with eof or there is no next line
return node.getFullWidth();
}
return lineStarts[line];
}
private switchRuleState(ruleName: string, isEnabled: boolean, start: number, end?: number): void {
const ruleStateMap = this.enableDisableRuleMap[ruleName];
ruleStateMap.push({
isEnabled,
position: start,
});
if (end) {
// switchRuleState method is only called when rule state changes therefore we can safely use opposite state
ruleStateMap.push({
isEnabled: !isEnabled,
position: end,
});
}
}
private getLatestRuleState(ruleName: string): boolean {
const ruleStateMap = this.enableDisableRuleMap[ruleName];
return ruleStateMap[ruleStateMap.length - 1].isEnabled;
}
private handlePossibleTslintSwitch(commentText: string, startingPosition: number, node: ts.SourceFile, scanner: ts.Scanner) {
// regex is: start of string followed by "/*" or "//" followed by any amount of whitespace followed by "tslint:"
if (commentText.match(/^(\/\*|\/\/)\s*tslint:/)) {
const commentTextParts = commentText.split(":");
// regex is: start of string followed by either "enable" or "disable"
// followed optionally by -line or -next-line
// followed by either whitespace or end of string
const enableOrDisableMatch = commentTextParts[1].match(/^(enable|disable)(-(line|next-line))?(\s|$)/);
if (enableOrDisableMatch != null) {
const isEnabled = enableOrDisableMatch[1] === "enable";
const isCurrentLine = enableOrDisableMatch[3] === "line";
const isNextLine = enableOrDisableMatch[3] === "next-line";
let rulesList = ["all"];
if (commentTextParts.length === 2) {
// an implicit whitespace separator is used for the rules list.
rulesList = commentTextParts[1].split(/\s+/).slice(1);
// remove empty items and potential comment end.
rulesList = rulesList.filter((item) => !!item && item.indexOf("*/") === -1);
// potentially there were no items, so default to `all`.
rulesList = rulesList.length > 0 ? rulesList : ["all"];
} else if (commentTextParts.length > 2) {
// an explicit separator was specified for the rules list.
rulesList = commentTextParts[2].split(/\s+/);
}
if (rulesList.indexOf("all") !== -1) {
// iterate over all enabled rules
rulesList = Object.keys(this.enableDisableRuleMap);
}
for (const ruleToSwitch of rulesList) {
if (!(ruleToSwitch in this.enableDisableRuleMap)) {
// all rules enabled in configuration are already in map - skip switches for disabled rules
continue;
}
const previousState = this.getLatestRuleState(ruleToSwitch);
if (previousState === isEnabled) {
// no need to add switch points if there is no change in rule state
continue;
}
let start: number;
let end: number | undefined;
if (isCurrentLine) {
// start at the beginning of the current line
start = this.getStartOfLinePosition(node, startingPosition);
// end at the beginning of the next line
end = scanner.getTextPos() + 1;
} else if (isNextLine) {
// start at the current position
start = startingPosition;
// end at the beginning of the line following the next line
end = this.getStartOfLinePosition(node, startingPosition, 2);
} else {
// disable rule for the rest of the file
// start at the current position, but skip end position
start = startingPosition;
end = undefined;
}
this.switchRuleState(ruleToSwitch, isEnabled, start, end);
}
}
}
}
}