@tsslint/core 1.0.12 → 1.0.14

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.
Files changed (3) hide show
  1. package/index.d.ts +2 -0
  2. package/index.js +67 -7
  3. package/package.json +4 -4
package/index.d.ts CHANGED
@@ -5,6 +5,8 @@ export declare function createLinter(ctx: ProjectContext, config: Config, withSt
5
5
  lint(fileName: string): ts.DiagnosticWithLocation[];
6
6
  hasCodeFixes(fileName: string): boolean;
7
7
  getCodeFixes(fileName: string, start: number, end: number, diagnostics?: ts.Diagnostic[]): ts.CodeFixAction[];
8
+ getRefactors(fileName: string, start: number, end: number): ts.RefactorActionInfo[];
9
+ getRefactorEdits(fileName: string, name: string): ts.FileTextChanges[] | undefined;
8
10
  };
9
11
  export declare function combineCodeFixes(fileName: string, fixes: ts.CodeFixAction[]): ts.TextChange[];
10
12
  export declare function applyTextChanges(baseSnapshot: ts.IScriptSnapshot, textChanges: ts.TextChange[]): ts.IScriptSnapshot;
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.applyTextChanges = exports.combineCodeFixes = exports.createLinter = void 0;
3
+ exports.createLinter = createLinter;
4
+ exports.combineCodeFixes = combineCodeFixes;
5
+ exports.applyTextChanges = applyTextChanges;
4
6
  const ErrorStackParser = require("error-stack-parser");
5
7
  const path = require("path");
6
8
  const minimatch = require("minimatch");
@@ -24,6 +26,7 @@ function createLinter(ctx, config, withStack) {
24
26
  const ts = ctx.typescript;
25
27
  const fileRules = new Map();
26
28
  const fileFixes = new Map();
29
+ const fileRefactors = new Map();
27
30
  const sourceFiles = new Map();
28
31
  const plugins = (config.plugins ?? []).map(plugin => plugin(ctx));
29
32
  const excludes = [];
@@ -50,9 +53,11 @@ function createLinter(ctx, config, withStack) {
50
53
  const token = ctx.languageServiceHost.getCancellationToken?.();
51
54
  const rules = getFileRules(sourceFile.fileName);
52
55
  const fixes = getFileFixes(sourceFile.fileName);
56
+ const refactors = getFileRefactors(sourceFile.fileName);
53
57
  let diagnostics = [];
54
58
  let currentRuleId;
55
59
  fixes.clear();
60
+ refactors.clear();
56
61
  for (const [id, rule] of Object.entries(rules)) {
57
62
  if (token?.isCancellationRequested()) {
58
63
  break;
@@ -73,14 +78,23 @@ function createLinter(ctx, config, withStack) {
73
78
  }
74
79
  const diagnosticSet = new Set(diagnostics);
75
80
  for (const [ruleId, fix] of [...fixes]) {
76
- const finalFixes = fix.filter(fix => diagnosticSet.has(fix.diagnostic));
77
- if (finalFixes.length) {
78
- fixes.set(ruleId, finalFixes);
81
+ const final = fix.filter(fix => diagnosticSet.has(fix.diagnostic));
82
+ if (final.length) {
83
+ fixes.set(ruleId, final);
79
84
  }
80
85
  else {
81
86
  fixes.delete(ruleId);
82
87
  }
83
88
  }
89
+ for (const [ruleId, refactor] of [...refactors]) {
90
+ const final = refactor.filter(fix => diagnosticSet.has(fix.diagnostic));
91
+ if (final.length) {
92
+ refactors.set(ruleId, final);
93
+ }
94
+ else {
95
+ refactors.delete(ruleId);
96
+ }
97
+ }
84
98
  return diagnostics;
85
99
  function reportError(message, start, end, traceOffset = 0) {
86
100
  return report(ts.DiagnosticCategory.Error, message, start, end, traceOffset);
@@ -136,6 +150,19 @@ function createLinter(ctx, config, withStack) {
136
150
  }));
137
151
  return this;
138
152
  },
153
+ withRefactor(title, getEdits) {
154
+ if (!refactors.has(currentRuleId)) {
155
+ refactors.set(currentRuleId, []);
156
+ }
157
+ refactors.get(currentRuleId).push(({
158
+ diagnostic: error,
159
+ title,
160
+ start,
161
+ end,
162
+ getEdits,
163
+ }));
164
+ return this;
165
+ },
139
166
  };
140
167
  }
141
168
  function pushRelatedInformation(error, stack) {
@@ -207,6 +234,36 @@ function createLinter(ctx, config, withStack) {
207
234
  }
208
235
  return result;
209
236
  },
237
+ getRefactors(fileName, start, end) {
238
+ const refactorsMap = getFileRefactors(fileName);
239
+ let result = [];
240
+ for (const [ruleId, refactors] of refactorsMap) {
241
+ for (let i = 0; i < refactors.length; i++) {
242
+ const refactor = refactors[i];
243
+ if ((refactor.start >= start && refactor.start <= end) ||
244
+ (refactor.end >= start && refactor.end <= end) ||
245
+ (start >= refactor.start && start <= refactor.end) ||
246
+ (end >= refactor.start && end <= refactor.end)) {
247
+ result.push({
248
+ name: `tsslint:${ruleId}:${i}`,
249
+ description: refactor.title + ' (' + ruleId + ')',
250
+ kind: 'quickfix',
251
+ });
252
+ }
253
+ }
254
+ }
255
+ return result;
256
+ },
257
+ getRefactorEdits(fileName, name) {
258
+ if (name.startsWith('tsslint:')) {
259
+ const [ruleId, index] = name.slice('tsslint:'.length).split(':');
260
+ const refactorsMap = getFileRefactors(fileName);
261
+ const refactor = refactorsMap.get(ruleId)?.[Number(index)];
262
+ if (refactor) {
263
+ return refactor.getEdits();
264
+ }
265
+ }
266
+ },
210
267
  };
211
268
  function getFileRules(fileName) {
212
269
  let rules = fileRules.get(fileName);
@@ -227,8 +284,13 @@ function createLinter(ctx, config, withStack) {
227
284
  }
228
285
  return fileFixes.get(fileName);
229
286
  }
287
+ function getFileRefactors(fileName) {
288
+ if (!fileRefactors.has(fileName)) {
289
+ fileRefactors.set(fileName, new Map());
290
+ }
291
+ return fileRefactors.get(fileName);
292
+ }
230
293
  }
231
- exports.createLinter = createLinter;
232
294
  function combineCodeFixes(fileName, fixes) {
233
295
  const changes = fixes
234
296
  .map(fix => fix.changes)
@@ -248,7 +310,6 @@ function combineCodeFixes(fileName, fixes) {
248
310
  }
249
311
  return finalTextChanges;
250
312
  }
251
- exports.combineCodeFixes = combineCodeFixes;
252
313
  function applyTextChanges(baseSnapshot, textChanges) {
253
314
  textChanges = [...textChanges].sort((a, b) => b.span.start - a.span.start);
254
315
  let text = baseSnapshot.getText(0, baseSnapshot.getLength());
@@ -270,5 +331,4 @@ function applyTextChanges(baseSnapshot, textChanges) {
270
331
  },
271
332
  };
272
333
  }
273
- exports.applyTextChanges = applyTextChanges;
274
334
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsslint/core",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -13,11 +13,11 @@
13
13
  },
14
14
  "dependencies": {
15
15
  "error-stack-parser": "^2.1.4",
16
- "minimatch": "^9.0.4",
16
+ "minimatch": "^10.0.1",
17
17
  "source-map-support": "^0.5.21"
18
18
  },
19
19
  "devDependencies": {
20
- "@tsslint/config": "1.0.12"
20
+ "@tsslint/config": "1.0.14"
21
21
  },
22
- "gitHead": "b34fbb2c8408616eb00536f00e525cb4fc8d8913"
22
+ "gitHead": "84ea99432b44827a79ac034896202652bf21c912"
23
23
  }