1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 |
|
20 | var utils = require("tsutils");
|
21 | var ts = require("typescript");
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | exports.ENABLE_DISABLE_REGEX = /^\s*tslint:(enable|disable)(?:-(line|next-line))?(:|\s|$)/;
|
30 | function removeDisabledFailures(sourceFile, failures) {
|
31 | if (failures.length === 0) {
|
32 |
|
33 | return failures;
|
34 | }
|
35 | var failingRules = new Set(failures.map(function (f) { return f.getRuleName(); }));
|
36 | var map = getDisableMap(sourceFile, failingRules);
|
37 | return failures.filter(function (failure) {
|
38 | var disabledIntervals = map.get(failure.getRuleName());
|
39 | return (disabledIntervals === undefined ||
|
40 | !disabledIntervals.some(function (_a) {
|
41 | var pos = _a.pos, end = _a.end;
|
42 | var failPos = failure.getStartPosition().getPosition();
|
43 | var failEnd = failure.getEndPosition().getPosition();
|
44 | return failEnd >= pos && (end === -1 || failPos < end);
|
45 | }));
|
46 | });
|
47 | }
|
48 | exports.removeDisabledFailures = removeDisabledFailures;
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function getDisableMap(sourceFile, failingRules) {
|
54 | var map = new Map();
|
55 | utils.forEachComment(sourceFile, function (fullText, comment) {
|
56 | var commentText = comment.kind === ts.SyntaxKind.SingleLineCommentTrivia
|
57 | ? fullText.substring(comment.pos + 2, comment.end)
|
58 | : fullText.substring(comment.pos + 2, comment.end - 2);
|
59 | var parsed = parseComment(commentText);
|
60 | if (parsed !== undefined) {
|
61 | var rulesList = parsed.rulesList, isEnabled = parsed.isEnabled, modifier = parsed.modifier;
|
62 | var switchRange = getSwitchRange(modifier, comment, sourceFile);
|
63 | if (switchRange !== undefined) {
|
64 | var rulesToSwitch = rulesList === "all"
|
65 | ? Array.from(failingRules)
|
66 | : rulesList.filter(function (r) { return failingRules.has(r); });
|
67 | for (var _i = 0, rulesToSwitch_1 = rulesToSwitch; _i < rulesToSwitch_1.length; _i++) {
|
68 | var ruleToSwitch = rulesToSwitch_1[_i];
|
69 | switchRuleState(ruleToSwitch, isEnabled, switchRange.pos, switchRange.end);
|
70 | }
|
71 | }
|
72 | }
|
73 | });
|
74 | return map;
|
75 | function switchRuleState(ruleName, isEnable, start, end) {
|
76 | var disableRanges = map.get(ruleName);
|
77 | if (isEnable) {
|
78 | if (disableRanges !== undefined) {
|
79 | var lastDisable = disableRanges[disableRanges.length - 1];
|
80 | if (lastDisable.end === -1) {
|
81 | lastDisable.end = start;
|
82 | if (end !== -1) {
|
83 |
|
84 | disableRanges.push({ pos: end, end: -1 });
|
85 | }
|
86 | }
|
87 | }
|
88 | }
|
89 | else {
|
90 |
|
91 | if (disableRanges === undefined) {
|
92 | map.set(ruleName, [{ pos: start, end: end }]);
|
93 | }
|
94 | else if (disableRanges[disableRanges.length - 1].end !== -1) {
|
95 | disableRanges.push({ pos: start, end: end });
|
96 | }
|
97 | }
|
98 | }
|
99 | }
|
100 |
|
101 | function getSwitchRange(modifier, range, sourceFile) {
|
102 | var lineStarts = sourceFile.getLineStarts();
|
103 | switch (modifier) {
|
104 | case "line":
|
105 | return {
|
106 |
|
107 | pos: getStartOfLinePosition(range.pos),
|
108 |
|
109 | end: getStartOfLinePosition(range.end, 1),
|
110 | };
|
111 | case "next-line":
|
112 |
|
113 | var pos = getStartOfLinePosition(range.end, 1);
|
114 | if (pos === -1) {
|
115 |
|
116 | return undefined;
|
117 | }
|
118 |
|
119 | return { pos: pos, end: getStartOfLinePosition(range.end, 2) };
|
120 | default:
|
121 |
|
122 |
|
123 | return { pos: range.pos, end: -1 };
|
124 | }
|
125 |
|
126 | function getStartOfLinePosition(position, lineOffset) {
|
127 | if (lineOffset === void 0) { lineOffset = 0; }
|
128 | var line = ts.getLineAndCharacterOfPosition(sourceFile, position).line + lineOffset;
|
129 | return line >= lineStarts.length ? -1 : lineStarts[line];
|
130 | }
|
131 | }
|
132 | function parseComment(commentText) {
|
133 | var match = exports.ENABLE_DISABLE_REGEX.exec(commentText);
|
134 | if (match === null) {
|
135 | return undefined;
|
136 | }
|
137 |
|
138 |
|
139 |
|
140 | var rulesList = splitOnSpaces(commentText.substr(match[0].length));
|
141 | if (rulesList.length === 0 && match[3] === ":") {
|
142 |
|
143 | return undefined;
|
144 | }
|
145 | if (rulesList.length === 0 || rulesList.indexOf("all") !== -1) {
|
146 |
|
147 |
|
148 | rulesList = "all";
|
149 | }
|
150 | return { rulesList: rulesList, isEnabled: match[1] === "enable", modifier: match[2] };
|
151 | }
|
152 | function splitOnSpaces(str) {
|
153 | return str.split(/\s+/).filter(function (s) { return s !== ""; });
|
154 | }
|