1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.TslintLineSwitchParser = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const inversify_1 = require("inversify");
|
6 | const ts = require("typescript");
|
7 | const tsutils_1 = require("tsutils");
|
8 | const matchAll = /^/;
|
9 | let TslintLineSwitchParser = class TslintLineSwitchParser {
|
10 | parse({ sourceFile }) {
|
11 | const result = [];
|
12 | const re = /\/[/*]\s*tslint:(enable|disable)(-line|-next-line)?(:)?/gm;
|
13 | for (let match = re.exec(sourceFile.text); match !== null; match = re.exec(sourceFile.text)) {
|
14 |
|
15 | const comment = tsutils_1.getCommentAtPosition(sourceFile, match.index);
|
16 | if (comment === undefined || comment.pos !== match.index)
|
17 | continue;
|
18 | const ruleNames = sourceFile.text.substring(comment.pos + match[0].length, comment.kind === ts.SyntaxKind.SingleLineCommentTrivia ? comment.end : { pos: comment.pos, end: comment.end }.end - 2);
|
19 | let rules;
|
20 | if (/\S/.test(ruleNames)) {
|
21 | rules = parseRules(ruleNames, comment.pos + match[0].length);
|
22 | }
|
23 | else {
|
24 | rules = match[3] === ':' ? [] : [{ predicate: matchAll }];
|
25 | }
|
26 | const enable = match[1] === 'enable';
|
27 | switch (match[2]) {
|
28 | case '-line': {
|
29 | const lineStarts = sourceFile.getLineStarts();
|
30 | const { line } = ts.getLineAndCharacterOfPosition(sourceFile, comment.pos);
|
31 | result.push({
|
32 | rules,
|
33 | enable,
|
34 | pos: lineStarts[line],
|
35 |
|
36 | end: lineStarts.length === line + 1 ? undefined : lineStarts[line + 1],
|
37 | location: { pos: comment.pos, end: comment.end },
|
38 | });
|
39 | break;
|
40 | }
|
41 | case '-next-line': {
|
42 | const lineStarts = sourceFile.getLineStarts();
|
43 | const line = ts.getLineAndCharacterOfPosition(sourceFile, comment.pos).line + 1;
|
44 | if (lineStarts.length === line) {
|
45 |
|
46 | result.push({
|
47 | rules, enable,
|
48 | pos: sourceFile.end + 1,
|
49 | end: undefined,
|
50 | location: { pos: comment.pos, end: comment.end },
|
51 | });
|
52 | }
|
53 | else {
|
54 | result.push({
|
55 | rules,
|
56 | enable,
|
57 | pos: lineStarts[line],
|
58 |
|
59 | end: lineStarts.length === line + 1 ? undefined : lineStarts[line + 1],
|
60 | location: { pos: comment.pos, end: comment.end },
|
61 | });
|
62 | }
|
63 | break;
|
64 | }
|
65 | default:
|
66 | result.push({ rules, enable, pos: comment.pos, end: undefined, location: { pos: comment.pos, end: comment.end } });
|
67 | }
|
68 | }
|
69 | return result;
|
70 | }
|
71 | };
|
72 | TslintLineSwitchParser = tslib_1.__decorate([
|
73 | inversify_1.injectable()
|
74 | ], TslintLineSwitchParser);
|
75 | exports.TslintLineSwitchParser = TslintLineSwitchParser;
|
76 | function parseRules(raw, offset) {
|
77 | const re = /(\s+|$)/g;
|
78 | const skippedWhitespace = raw.search(/\S/);
|
79 | offset += skippedWhitespace;
|
80 | raw = raw.substr(skippedWhitespace);
|
81 | let pos = 0;
|
82 | let fixPos = -skippedWhitespace;
|
83 | const result = [];
|
84 | for (let match = re.exec(raw);; match = re.exec(raw)) {
|
85 | const rule = raw.slice(pos, match.index);
|
86 | result.push({
|
87 | predicate: rule === 'all' ? matchAll : rule,
|
88 | location: { pos: pos + offset, end: match.index + offset },
|
89 | fixLocation: { pos: fixPos + offset, end: match.index + offset },
|
90 | });
|
91 | if (match[0].length === 0)
|
92 | break;
|
93 | pos = re.lastIndex;
|
94 | fixPos = match.index;
|
95 | }
|
96 | return result;
|
97 | }
|
98 |
|
\ | No newline at end of file |