|
17 | 17 |
|
18 | 18 | import * as ts from "typescript";
|
19 | 19 |
|
| 20 | +import {AbstractRule} from "./language/rule/abstractRule"; |
| 21 | +import {IOptions} from "./language/rule/rule"; |
20 | 22 | import {scanAllTokens} from "./language/utils";
|
21 | 23 | import {SkippableTokenAwareRuleWalker} from "./language/walker/skippableTokenAwareRuleWalker";
|
22 | 24 | import {IEnableDisablePosition} from "./ruleLoader";
|
23 | 25 |
|
24 | 26 | export class EnableDisableRulesWalker extends SkippableTokenAwareRuleWalker {
|
25 | 27 | public enableDisableRuleMap: {[rulename: string]: IEnableDisablePosition[]} = {};
|
26 | 28 |
|
| 29 | + constructor(sourceFile: ts.SourceFile, options: IOptions, rules: {[name: string]: any}) { |
| 30 | + super(sourceFile, options); |
| 31 | + |
| 32 | + if (rules) { |
| 33 | + for (const rule in rules) { |
| 34 | + if (rules.hasOwnProperty(rule) && AbstractRule.isRuleEnabled(rules[rule])) { |
| 35 | + this.enableDisableRuleMap[rule] = [{ |
| 36 | + isEnabled: true, |
| 37 | + position: 0, |
| 38 | + }]; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
27 | 44 | public visitSourceFile(node: ts.SourceFile) {
|
28 | 45 | super.visitSourceFile(node);
|
29 | 46 | const scan = ts.createScanner(ts.ScriptTarget.ES5, false, ts.LanguageVariant.Standard, node.text);
|
@@ -52,6 +69,29 @@ export class EnableDisableRulesWalker extends SkippableTokenAwareRuleWalker {
|
52 | 69 | );
|
53 | 70 | }
|
54 | 71 |
|
| 72 | + private switchRuleState(ruleName: string, isEnabled: boolean, start: number, end?: number): void { |
| 73 | + const ruleStateMap = this.enableDisableRuleMap[ruleName]; |
| 74 | + |
| 75 | + ruleStateMap.push({ |
| 76 | + isEnabled, |
| 77 | + position: start, |
| 78 | + }); |
| 79 | + |
| 80 | + if (end) { |
| 81 | + // switchRuleState method is only called when rule state changes therefore we can safely use opposite state |
| 82 | + ruleStateMap.push({ |
| 83 | + isEnabled: !isEnabled, |
| 84 | + position: end, |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private getLatestRuleState(ruleName: string): boolean { |
| 90 | + const ruleStateMap = this.enableDisableRuleMap[ruleName]; |
| 91 | + |
| 92 | + return ruleStateMap[ruleStateMap.length - 1].isEnabled; |
| 93 | + } |
| 94 | + |
55 | 95 | private handlePossibleTslintSwitch(commentText: string, startingPosition: number, node: ts.SourceFile, scanner: ts.Scanner) {
|
56 | 96 | // regex is: start of string followed by "/*" or "//" followed by any amount of whitespace followed by "tslint:"
|
57 | 97 | if (commentText.match(/^(\/\*|\/\/)\s*tslint:/)) {
|
@@ -82,35 +122,44 @@ export class EnableDisableRulesWalker extends SkippableTokenAwareRuleWalker {
|
82 | 122 | rulesList = commentTextParts[2].split(/\s+/);
|
83 | 123 | }
|
84 | 124 |
|
85 |
| - for (const ruleToAdd of rulesList) { |
86 |
| - if (!(ruleToAdd in this.enableDisableRuleMap)) { |
87 |
| - this.enableDisableRuleMap[ruleToAdd] = []; |
| 125 | + if (rulesList.indexOf("all") !== -1) { |
| 126 | + // iterate over all enabled rules |
| 127 | + rulesList = Object.keys(this.enableDisableRuleMap); |
| 128 | + } |
| 129 | + |
| 130 | + for (const ruleToSwitch of rulesList) { |
| 131 | + if (!(ruleToSwitch in this.enableDisableRuleMap)) { |
| 132 | + // all rules enabled in configuration are already in map - skip switches for disabled rules |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + const previousState = this.getLatestRuleState(ruleToSwitch); |
| 137 | + |
| 138 | + if (previousState === isEnabled) { |
| 139 | + // no need to add switch points if there is no change in rule state |
| 140 | + continue; |
88 | 141 | }
|
| 142 | + |
| 143 | + let start: number; |
| 144 | + let end: number; |
| 145 | + |
89 | 146 | if (isCurrentLine) {
|
90 | 147 | // start at the beginning of the current line
|
91 |
| - this.enableDisableRuleMap[ruleToAdd].push({ |
92 |
| - isEnabled, |
93 |
| - position: this.getStartOfLinePosition(node, startingPosition), |
94 |
| - }); |
| 148 | + start = this.getStartOfLinePosition(node, startingPosition); |
95 | 149 | // end at the beginning of the next line
|
96 |
| - this.enableDisableRuleMap[ruleToAdd].push({ |
97 |
| - isEnabled: !isEnabled, |
98 |
| - position: scanner.getTextPos() + 1, |
99 |
| - }); |
100 |
| - } else { |
| 150 | + end = scanner.getTextPos() + 1; |
| 151 | + } else if (isNextLine) { |
101 | 152 | // start at the current position
|
102 |
| - this.enableDisableRuleMap[ruleToAdd].push({ |
103 |
| - isEnabled, |
104 |
| - position: startingPosition, |
105 |
| - }); |
| 153 | + start = startingPosition; |
106 | 154 | // end at the beginning of the line following the next line
|
107 |
| - if (isNextLine) { |
108 |
| - this.enableDisableRuleMap[ruleToAdd].push({ |
109 |
| - isEnabled: !isEnabled, |
110 |
| - position: this.getStartOfLinePosition(node, startingPosition, 2), |
111 |
| - }); |
112 |
| - } |
| 155 | + end = this.getStartOfLinePosition(node, startingPosition, 2); |
| 156 | + } else { |
| 157 | + // disable rule for the rest of the file |
| 158 | + // start at the current position, but skip end position |
| 159 | + start = startingPosition; |
113 | 160 | }
|
| 161 | + |
| 162 | + this.switchRuleState(ruleToSwitch, isEnabled, start, end); |
114 | 163 | }
|
115 | 164 | }
|
116 | 165 | }
|
|
0 commit comments