@tsslint/core 1.0.11 → 1.0.13

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 +75 -4
  3. package/package.json +3 -3
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
@@ -24,6 +24,7 @@ function createLinter(ctx, config, withStack) {
24
24
  const ts = ctx.typescript;
25
25
  const fileRules = new Map();
26
26
  const fileFixes = new Map();
27
+ const fileRefactors = new Map();
27
28
  const sourceFiles = new Map();
28
29
  const plugins = (config.plugins ?? []).map(plugin => plugin(ctx));
29
30
  const excludes = [];
@@ -50,9 +51,11 @@ function createLinter(ctx, config, withStack) {
50
51
  const token = ctx.languageServiceHost.getCancellationToken?.();
51
52
  const rules = getFileRules(sourceFile.fileName);
52
53
  const fixes = getFileFixes(sourceFile.fileName);
53
- let result = [];
54
+ const refactors = getFileRefactors(sourceFile.fileName);
55
+ let diagnostics = [];
54
56
  let currentRuleId;
55
57
  fixes.clear();
58
+ refactors.clear();
56
59
  for (const [id, rule] of Object.entries(rules)) {
57
60
  if (token?.isCancellationRequested()) {
58
61
  break;
@@ -68,10 +71,29 @@ function createLinter(ctx, config, withStack) {
68
71
  }
69
72
  for (const plugin of plugins) {
70
73
  if (plugin.resolveDiagnostics) {
71
- result = plugin.resolveDiagnostics(sourceFile.fileName, result);
74
+ diagnostics = plugin.resolveDiagnostics(sourceFile.fileName, diagnostics);
72
75
  }
73
76
  }
74
- return result;
77
+ const diagnosticSet = new Set(diagnostics);
78
+ for (const [ruleId, fix] of [...fixes]) {
79
+ const final = fix.filter(fix => diagnosticSet.has(fix.diagnostic));
80
+ if (final.length) {
81
+ fixes.set(ruleId, final);
82
+ }
83
+ else {
84
+ fixes.delete(ruleId);
85
+ }
86
+ }
87
+ for (const [ruleId, refactor] of [...refactors]) {
88
+ const final = refactor.filter(fix => diagnosticSet.has(fix.diagnostic));
89
+ if (final.length) {
90
+ refactors.set(ruleId, final);
91
+ }
92
+ else {
93
+ refactors.delete(ruleId);
94
+ }
95
+ }
96
+ return diagnostics;
75
97
  function reportError(message, start, end, traceOffset = 0) {
76
98
  return report(ts.DiagnosticCategory.Error, message, start, end, traceOffset);
77
99
  }
@@ -103,7 +125,7 @@ function createLinter(ctx, config, withStack) {
103
125
  }
104
126
  }
105
127
  }
106
- result.push(error);
128
+ diagnostics.push(error);
107
129
  return {
108
130
  withDeprecated() {
109
131
  error.reportsDeprecated = true;
@@ -126,6 +148,19 @@ function createLinter(ctx, config, withStack) {
126
148
  }));
127
149
  return this;
128
150
  },
151
+ withRefactor(title, getEdits) {
152
+ if (!refactors.has(currentRuleId)) {
153
+ refactors.set(currentRuleId, []);
154
+ }
155
+ refactors.get(currentRuleId).push(({
156
+ diagnostic: error,
157
+ title,
158
+ start,
159
+ end,
160
+ getEdits,
161
+ }));
162
+ return this;
163
+ },
129
164
  };
130
165
  }
131
166
  function pushRelatedInformation(error, stack) {
@@ -197,6 +232,36 @@ function createLinter(ctx, config, withStack) {
197
232
  }
198
233
  return result;
199
234
  },
235
+ getRefactors(fileName, start, end) {
236
+ const refactorsMap = getFileRefactors(fileName);
237
+ let result = [];
238
+ for (const [ruleId, refactors] of refactorsMap) {
239
+ for (let i = 0; i < refactors.length; i++) {
240
+ const refactor = refactors[i];
241
+ if ((refactor.start >= start && refactor.start <= end) ||
242
+ (refactor.end >= start && refactor.end <= end) ||
243
+ (start >= refactor.start && start <= refactor.end) ||
244
+ (end >= refactor.start && end <= refactor.end)) {
245
+ result.push({
246
+ name: `tsslint:${ruleId}:${i}`,
247
+ description: refactor.title + ' (' + ruleId + ')',
248
+ kind: 'quickfix',
249
+ });
250
+ }
251
+ }
252
+ }
253
+ return result;
254
+ },
255
+ getRefactorEdits(fileName, name) {
256
+ if (name.startsWith('tsslint:')) {
257
+ const [ruleId, index] = name.slice('tsslint:'.length).split(':');
258
+ const refactorsMap = getFileRefactors(fileName);
259
+ const refactor = refactorsMap.get(ruleId)?.[Number(index)];
260
+ if (refactor) {
261
+ return refactor.getEdits();
262
+ }
263
+ }
264
+ },
200
265
  };
201
266
  function getFileRules(fileName) {
202
267
  let rules = fileRules.get(fileName);
@@ -217,6 +282,12 @@ function createLinter(ctx, config, withStack) {
217
282
  }
218
283
  return fileFixes.get(fileName);
219
284
  }
285
+ function getFileRefactors(fileName) {
286
+ if (!fileRefactors.has(fileName)) {
287
+ fileRefactors.set(fileName, new Map());
288
+ }
289
+ return fileRefactors.get(fileName);
290
+ }
220
291
  }
221
292
  exports.createLinter = createLinter;
222
293
  function combineCodeFixes(fileName, fixes) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsslint/core",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -17,7 +17,7 @@
17
17
  "source-map-support": "^0.5.21"
18
18
  },
19
19
  "devDependencies": {
20
- "@tsslint/config": "1.0.11"
20
+ "@tsslint/config": "1.0.13"
21
21
  },
22
- "gitHead": "5797507a1ef19b82bafa6f43cdf412905723374f"
22
+ "gitHead": "47f0f4bdbb68d8343c2967d46bd4b7bf9fb00e7e"
23
23
  }