@tsslint/config 1.3.6 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from '@tsslint/types';
2
+ export { create as createIgnorePlugin } from './lib/plugins/ignore.js';
2
3
  import type { Config, Plugin, Rule } from '@tsslint/types';
3
4
  export declare function defineRule(rule: Rule): Rule;
4
5
  export declare function definePlugin(plugin: Plugin): Plugin;
package/index.js CHANGED
@@ -14,10 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.createIgnorePlugin = void 0;
17
18
  exports.defineRule = defineRule;
18
19
  exports.definePlugin = definePlugin;
19
20
  exports.defineConfig = defineConfig;
20
21
  __exportStar(require("@tsslint/types"), exports);
22
+ var ignore_js_1 = require("./lib/plugins/ignore.js");
23
+ Object.defineProperty(exports, "createIgnorePlugin", { enumerable: true, get: function () { return ignore_js_1.create; } });
21
24
  function defineRule(rule) {
22
25
  return rule;
23
26
  }
@@ -0,0 +1,2 @@
1
+ import type { Plugin } from '@tsslint/types';
2
+ export declare function create(cmd: string, reportsUnusedComments: boolean, reg?: RegExp, completeReg1?: RegExp, completeReg2?: RegExp): Plugin;
@@ -0,0 +1,213 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.create = create;
4
+ const ts_api_utils_1 = require("ts-api-utils");
5
+ function create(cmd, reportsUnusedComments, reg = new RegExp(`//\\s*${cmd}\\b[ \\t]*(?<ruleId>\\S*)\\b`), completeReg1 = /^\s*\/\/(\s*)([\S]*)?$/, completeReg2 = new RegExp(`//\\s*${cmd}\\b[ \\t]*(\\S*)?$`)) {
6
+ return ({ typescript: ts, languageService }) => {
7
+ const reportedRulesOfFile = new Map();
8
+ const { getCompletionsAtPosition } = languageService;
9
+ languageService.getCompletionsAtPosition = (fileName, position, ...rest) => {
10
+ let result = getCompletionsAtPosition(fileName, position, ...rest);
11
+ const sourceFile = languageService.getProgram()?.getSourceFile(fileName);
12
+ if (!sourceFile) {
13
+ return result;
14
+ }
15
+ const reportedRules = reportedRulesOfFile.get(fileName);
16
+ const line = sourceFile.getLineAndCharacterOfPosition(position).line;
17
+ const lineStart = sourceFile.getPositionOfLineAndCharacter(line, 0);
18
+ const prefix = sourceFile.text.slice(lineStart, position);
19
+ const matchCmd = completeReg1
20
+ ? prefix.match(completeReg1)
21
+ : undefined;
22
+ if (matchCmd) {
23
+ const nextLineRules = reportedRules?.filter(([, reportedLine]) => reportedLine === line + 1) ?? [];
24
+ const item = {
25
+ name: cmd,
26
+ insertText: matchCmd[1].length ? cmd : ` ${cmd}`,
27
+ kind: ts.ScriptElementKind.keyword,
28
+ sortText: 'a',
29
+ replacementSpan: matchCmd[2]
30
+ ? {
31
+ start: position - matchCmd[2].length,
32
+ length: matchCmd[2].length,
33
+ }
34
+ : undefined,
35
+ labelDetails: {
36
+ description: nextLineRules.length >= 2
37
+ ? `Ignore ${nextLineRules.length} issues in next line`
38
+ : nextLineRules.length
39
+ ? 'Ignore 1 issue in next line'
40
+ : undefined,
41
+ }
42
+ };
43
+ if (result) {
44
+ result.entries.push(item);
45
+ }
46
+ else {
47
+ result = {
48
+ isGlobalCompletion: false,
49
+ isMemberCompletion: false,
50
+ isNewIdentifierLocation: false,
51
+ entries: [item],
52
+ };
53
+ }
54
+ }
55
+ else if (reportedRules?.length) {
56
+ const matchRule = completeReg2
57
+ ? prefix.match(completeReg2)
58
+ : undefined;
59
+ if (matchRule) {
60
+ const visited = new Set();
61
+ for (const [ruleId] of reportedRules) {
62
+ if (visited.has(ruleId)) {
63
+ continue;
64
+ }
65
+ visited.add(ruleId);
66
+ const reportedLines = reportedRules
67
+ .filter(([r]) => r === ruleId)
68
+ .map(([, l]) => l + 1);
69
+ const item = {
70
+ name: ruleId,
71
+ kind: ts.ScriptElementKind.keyword,
72
+ sortText: ruleId,
73
+ replacementSpan: matchRule[1]
74
+ ? {
75
+ start: position - matchRule[1].length,
76
+ length: matchRule[1].length,
77
+ }
78
+ : undefined,
79
+ labelDetails: {
80
+ description: `Reported in line${reportedLines.length >= 2 ? 's' : ''} ${reportedLines.join(', ')}`,
81
+ },
82
+ };
83
+ if (result) {
84
+ result.entries.push(item);
85
+ }
86
+ else {
87
+ result = {
88
+ isGlobalCompletion: false,
89
+ isMemberCompletion: false,
90
+ isNewIdentifierLocation: false,
91
+ entries: [item],
92
+ };
93
+ }
94
+ }
95
+ }
96
+ }
97
+ return result;
98
+ };
99
+ return {
100
+ resolveDiagnostics(sourceFile, results) {
101
+ if (!reportsUnusedComments &&
102
+ !results.some(error => error.source === 'tsslint')) {
103
+ return results;
104
+ }
105
+ const disabledLines = new Map();
106
+ const disabledLinesByRules = new Map();
107
+ (0, ts_api_utils_1.forEachComment)(sourceFile, (fullText, { pos, end }) => {
108
+ const commentText = fullText.substring(pos, end);
109
+ const comment = commentText.match(reg);
110
+ if (comment?.index === undefined) {
111
+ return;
112
+ }
113
+ const index = comment.index + pos;
114
+ const line = sourceFile.getLineAndCharacterOfPosition(index).line + 1;
115
+ const ruleId = comment.groups?.ruleId;
116
+ if (ruleId) {
117
+ if (!disabledLinesByRules.has(ruleId)) {
118
+ disabledLinesByRules.set(ruleId, new Map());
119
+ }
120
+ disabledLinesByRules.get(ruleId).set(line, {
121
+ start: index,
122
+ end: index + comment[0].length,
123
+ });
124
+ }
125
+ else {
126
+ disabledLines.set(line, {
127
+ start: index,
128
+ end: index + comment[0].length,
129
+ });
130
+ }
131
+ });
132
+ let reportedRules = reportedRulesOfFile.get(sourceFile.fileName);
133
+ if (!reportedRules) {
134
+ reportedRules = [];
135
+ reportedRulesOfFile.set(sourceFile.fileName, reportedRules);
136
+ }
137
+ reportedRules.length = 0;
138
+ results = results.filter(error => {
139
+ if (error.source !== 'tsslint') {
140
+ return true;
141
+ }
142
+ const line = sourceFile.getLineAndCharacterOfPosition(error.start).line;
143
+ reportedRules.push([error.code, line]);
144
+ if (disabledLines.has(line)) {
145
+ disabledLines.get(line).used = true;
146
+ return false;
147
+ }
148
+ const disabledLinesByRule = disabledLinesByRules.get(error.code);
149
+ if (disabledLinesByRule?.has(line)) {
150
+ disabledLinesByRule.get(line).used = true;
151
+ return false;
152
+ }
153
+ return true;
154
+ });
155
+ if (reportsUnusedComments) {
156
+ for (const state of disabledLines.values()) {
157
+ if (!state.used) {
158
+ results.push({
159
+ file: sourceFile,
160
+ start: state.start,
161
+ length: state.end - state.start,
162
+ code: 'tsslint:unused-ignore-comment',
163
+ messageText: `Unused ${cmd} comment.`,
164
+ source: 'tsslint',
165
+ category: 1,
166
+ });
167
+ }
168
+ }
169
+ for (const disabledLinesByRule of disabledLinesByRules.values()) {
170
+ for (const state of disabledLinesByRule.values()) {
171
+ if (!state.used) {
172
+ results.push({
173
+ file: sourceFile,
174
+ start: state.start,
175
+ length: state.end - state.start,
176
+ code: 'tsslint:unused-ignore-comment',
177
+ messageText: `Unused ${cmd} comment.`,
178
+ source: 'tsslint',
179
+ category: 1,
180
+ });
181
+ }
182
+ }
183
+ }
184
+ }
185
+ return results;
186
+ },
187
+ resolveCodeFixes(sourceFile, diagnostic, codeFixes) {
188
+ if (diagnostic.source !== 'tsslint' || diagnostic.start === undefined) {
189
+ return codeFixes;
190
+ }
191
+ const line = sourceFile.getLineAndCharacterOfPosition(diagnostic.start).line;
192
+ codeFixes.push({
193
+ fixName: cmd,
194
+ description: `Ignore with ${cmd}`,
195
+ changes: [
196
+ {
197
+ fileName: sourceFile.fileName,
198
+ textChanges: [{
199
+ newText: `// ${cmd} ${diagnostic.code}\n`,
200
+ span: {
201
+ start: sourceFile.getPositionOfLineAndCharacter(line, 0),
202
+ length: 0,
203
+ },
204
+ }],
205
+ },
206
+ ],
207
+ });
208
+ return codeFixes;
209
+ },
210
+ };
211
+ };
212
+ }
213
+ //# sourceMappingURL=ignore.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsslint/config",
3
- "version": "1.3.6",
3
+ "version": "1.4.1",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -12,7 +12,8 @@
12
12
  "directory": "packages/config"
13
13
  },
14
14
  "dependencies": {
15
- "@tsslint/types": "1.3.6"
15
+ "@tsslint/types": "1.4.1",
16
+ "ts-api-utils": "^2.0.0"
16
17
  },
17
- "gitHead": "d153aa87c92803b4c20fef1f5cf2799f1a599099"
18
+ "gitHead": "54f42ec9414029a356fa19a762260f03392563fa"
18
19
  }