@tsslint/config 1.4.6 → 1.5.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.
@@ -1,2 +1,2 @@
1
1
  import type { Plugin } from '@tsslint/types';
2
- export declare function create(cmd: string, reportsUnusedComments: boolean, reg?: RegExp, completeReg1?: RegExp, completeReg2?: RegExp): Plugin;
2
+ export declare function create(cmdOption: string | [string, string], reportsUnusedComments: boolean): Plugin;
@@ -2,7 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.create = create;
4
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*)?$`)) {
5
+ function create(cmdOption, reportsUnusedComments) {
6
+ const mode = typeof cmdOption === 'string' ? 'singleLine' : 'multiLine';
7
+ const [cmd, endCmd] = Array.isArray(cmdOption) ? cmdOption : [cmdOption, undefined];
8
+ const cmdText = cmd.replace(/\?/g, '');
9
+ const withRuleId = '(\\b[ \\t]*|[ \\t]+)(?<ruleId>\\S*\\b)?';
10
+ const reg = new RegExp(`\\s*${cmd}${withRuleId}`);
11
+ const endReg = endCmd ? new RegExp(`\\s*${endCmd}${withRuleId}`) : undefined;
12
+ const completeReg1 = /^\s*\/\/(\s*)([\S]*)?$/;
13
+ const completeReg2 = new RegExp(`//\\s*${cmd}(\\S*)?$`);
6
14
  return ({ typescript: ts, languageService }) => {
7
15
  const reportedRulesOfFile = new Map();
8
16
  const { getCompletionsAtPosition } = languageService;
@@ -22,8 +30,8 @@ function create(cmd, reportsUnusedComments, reg = new RegExp(`//\\s*${cmd}\\b[ \
22
30
  if (matchCmd) {
23
31
  const nextLineRules = reportedRules?.filter(([, reportedLine]) => reportedLine === line + 1) ?? [];
24
32
  const item = {
25
- name: cmd,
26
- insertText: matchCmd[1].length ? cmd : ` ${cmd}`,
33
+ name: cmdText,
34
+ insertText: matchCmd[1].length ? cmdText : ` ${cmdText}`,
27
35
  kind: ts.ScriptElementKind.keyword,
28
36
  sortText: 'a',
29
37
  replacementSpan: matchCmd[2]
@@ -102,31 +110,40 @@ function create(cmd, reportsUnusedComments, reg = new RegExp(`//\\s*${cmd}\\b[ \
102
110
  !results.some(error => error.source === 'tsslint')) {
103
111
  return results;
104
112
  }
105
- const disabledLines = new Map();
106
- const disabledLinesByRules = new Map();
113
+ const comments = new Map();
114
+ const logs = [];
107
115
  (0, ts_api_utils_1.forEachComment)(sourceFile, (fullText, { pos, end }) => {
116
+ pos += 2; // Trim the // or /* characters
108
117
  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());
118
+ logs.push(commentText);
119
+ const startComment = commentText.match(reg);
120
+ if (startComment?.index !== undefined) {
121
+ const index = startComment.index + pos;
122
+ const nextLine = sourceFile.getLineAndCharacterOfPosition(index).line + 1;
123
+ const ruleId = startComment.groups?.ruleId;
124
+ if (!comments.has(ruleId)) {
125
+ comments.set(ruleId, []);
119
126
  }
120
- disabledLinesByRules.get(ruleId).set(line, {
121
- start: index,
122
- end: index + comment[0].length,
127
+ const disabledLines = comments.get(ruleId);
128
+ disabledLines.push({
129
+ commentRange: [
130
+ index - 2,
131
+ index + startComment[0].length,
132
+ ],
133
+ nextLine,
123
134
  });
124
135
  }
125
- else {
126
- disabledLines.set(line, {
127
- start: index,
128
- end: index + comment[0].length,
129
- });
136
+ else if (endReg) {
137
+ const endComment = commentText.match(endReg);
138
+ if (endComment?.index !== undefined) {
139
+ const index = endComment.index + pos;
140
+ const prevLine = sourceFile.getLineAndCharacterOfPosition(index).line - 1;
141
+ const ruleId = endComment.groups?.ruleId;
142
+ const disabledLines = comments.get(ruleId);
143
+ if (disabledLines) {
144
+ disabledLines[disabledLines.length - 1].lastLine = prevLine;
145
+ }
146
+ }
130
147
  }
131
148
  });
132
149
  let reportedRules = reportedRulesOfFile.get(sourceFile.fileName);
@@ -141,40 +158,45 @@ function create(cmd, reportsUnusedComments, reg = new RegExp(`//\\s*${cmd}\\b[ \
141
158
  }
142
159
  const line = sourceFile.getLineAndCharacterOfPosition(error.start).line;
143
160
  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;
161
+ for (const code of [undefined, error.code]) {
162
+ const states = comments.get(code);
163
+ if (states) {
164
+ if (mode === 'singleLine') {
165
+ if (states.some(({ nextLine }) => nextLine === line)) {
166
+ for (const state of states) {
167
+ if (state.nextLine === line) {
168
+ state.used = true;
169
+ break;
170
+ }
171
+ }
172
+ return false;
173
+ }
174
+ }
175
+ else {
176
+ if (states.some(({ nextLine, lastLine }) => line >= nextLine && line <= (lastLine ?? Number.MAX_VALUE))) {
177
+ for (const state of states) {
178
+ if (line >= state.nextLine && line <= (state.lastLine ?? Number.MAX_VALUE)) {
179
+ state.used = true;
180
+ break;
181
+ }
182
+ }
183
+ return false;
184
+ }
185
+ }
186
+ }
152
187
  }
153
188
  return true;
154
189
  });
155
190
  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()) {
191
+ for (const comment of comments.values()) {
192
+ for (const state of comment.values()) {
171
193
  if (!state.used) {
172
194
  results.push({
173
195
  file: sourceFile,
174
- start: state.start,
175
- length: state.end - state.start,
196
+ start: state.commentRange[0],
197
+ length: state.commentRange[1] - state.commentRange[0],
176
198
  code: 'tsslint:unused-ignore-comment',
177
- messageText: `Unused ${cmd} comment.`,
199
+ messageText: `Unused comment.`,
178
200
  source: 'tsslint',
179
201
  category: 1,
180
202
  });
@@ -191,12 +213,14 @@ function create(cmd, reportsUnusedComments, reg = new RegExp(`//\\s*${cmd}\\b[ \
191
213
  const line = sourceFile.getLineAndCharacterOfPosition(diagnostic.start).line;
192
214
  codeFixes.push({
193
215
  fixName: cmd,
194
- description: `Ignore with ${cmd}`,
216
+ description: `Ignore with ${cmdText}`,
195
217
  changes: [
196
218
  {
197
219
  fileName: sourceFile.fileName,
198
220
  textChanges: [{
199
- newText: `// ${cmd} ${diagnostic.code}\n`,
221
+ newText: reg.test(`${cmdText}${diagnostic.code}`)
222
+ ? `// ${cmdText}${diagnostic.code}\n`
223
+ : `// ${cmdText} ${diagnostic.code}\n`,
200
224
  span: {
201
225
  start: sourceFile.getPositionOfLineAndCharacter(line, 0),
202
226
  length: 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsslint/config",
3
- "version": "1.4.6",
3
+ "version": "1.5.1",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -12,8 +12,8 @@
12
12
  "directory": "packages/config"
13
13
  },
14
14
  "dependencies": {
15
- "@tsslint/types": "1.4.6",
15
+ "@tsslint/types": "1.5.1",
16
16
  "ts-api-utils": "^2.0.0"
17
17
  },
18
- "gitHead": "9c5bc3471bb1c737144f90a1a70470975088e7fc"
18
+ "gitHead": "a0c7b85e3ba032d5c9a92f69efbba51a9f6ce06e"
19
19
  }