@tsslint/core 3.0.0-alpha.0 → 3.0.0

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
@@ -9,7 +9,7 @@ export type FileLintCache = [
9
9
  minimatchResult: Record<string, boolean>
10
10
  ];
11
11
  export type Linter = ReturnType<typeof createLinter>;
12
- export declare function createLinter(ctx: LinterContext, rootDir: string, config: Config | Config[], handleError: (diag: ts.DiagnosticWithLocation, err: Error, stackOffset: number) => void, syntaxOnlyLanguageService?: ts.LanguageService & {
12
+ export declare function createLinter(ctx: LinterContext, rootDir: string, config: Config | Config[], getRelatedInformations: (err: Error, stackIndex: number) => ts.DiagnosticRelatedInformation[], syntaxOnlyLanguageService?: ts.LanguageService & {
13
13
  getNonBoundSourceFile?(fileName: string): ts.SourceFile;
14
14
  }): {
15
15
  lint(fileName: string, cache?: FileLintCache): ts.DiagnosticWithLocation[];
package/index.js CHANGED
@@ -5,7 +5,7 @@ exports.combineCodeFixes = combineCodeFixes;
5
5
  exports.applyTextChanges = applyTextChanges;
6
6
  const path = require("path");
7
7
  const minimatch = require("minimatch");
8
- function createLinter(ctx, rootDir, config, handleError, syntaxOnlyLanguageService) {
8
+ function createLinter(ctx, rootDir, config, getRelatedInformations, syntaxOnlyLanguageService) {
9
9
  const ts = ctx.typescript;
10
10
  const fileRules = new Map();
11
11
  const fileConfigs = new Map();
@@ -35,30 +35,21 @@ function createLinter(ctx, rootDir, config, handleError, syntaxOnlyLanguageServi
35
35
  const program = ctx.languageService.getProgram();
36
36
  const file = ctx.languageService.getProgram().getSourceFile(fileName);
37
37
  rulesContext = {
38
- ...ctx,
38
+ typescript: ctx.typescript,
39
39
  file,
40
- sourceFile: file,
41
40
  program,
42
41
  report,
43
- reportError: report,
44
- reportWarning: report,
45
- reportSuggestion: report,
46
42
  };
47
43
  }
48
44
  else {
49
45
  const file = getNonBoundSourceFile(fileName);
50
46
  rulesContext = {
51
- ...ctx,
52
- languageService: syntaxOnlyLanguageService,
47
+ typescript: ctx.typescript,
53
48
  get program() {
54
49
  throw new Error('Not supported');
55
50
  },
56
51
  file,
57
- sourceFile: file,
58
52
  report,
59
- reportError: report,
60
- reportWarning: report,
61
- reportSuggestion: report,
62
53
  };
63
54
  }
64
55
  lintResults.set(fileName, [rulesContext.file, new Map(), []]);
@@ -102,10 +93,10 @@ function createLinter(ctx, rootDir, config, handleError, syntaxOnlyLanguageServi
102
93
  shouldRetry = true;
103
94
  }
104
95
  else if (err instanceof Error) {
105
- report(err.stack ?? err.message, 0, 0, ts.DiagnosticCategory.Message, 0, err);
96
+ report(err.stack ?? err.message, 0, 0).at(err, 0);
106
97
  }
107
98
  else {
108
- report(String(err), 0, 0, ts.DiagnosticCategory.Message, Number.MAX_VALUE);
99
+ report(String(err), 0, 0).at(new Error(), Number.MAX_VALUE);
109
100
  }
110
101
  }
111
102
  if (cache && !rule2Mode.get(currentRuleId)) {
@@ -158,29 +149,37 @@ function createLinter(ctx, rootDir, config, handleError, syntaxOnlyLanguageServi
158
149
  }
159
150
  lintResult[2] = lintResult[2].filter(refactor => diagnosticSet.has(refactor.diagnostic));
160
151
  return diagnostics;
161
- function report(message, start, end, category = ts.DiagnosticCategory.Message, stackOffset = 1, err) {
152
+ function report(message, start, end) {
162
153
  const error = {
163
- category,
154
+ category: ts.DiagnosticCategory.Message,
164
155
  code: currentRuleId,
165
156
  messageText: message,
166
157
  file: rulesContext.file,
167
158
  start,
168
159
  length: end - start,
169
160
  source: 'tsslint',
170
- relatedInformation: [],
161
+ get relatedInformation() {
162
+ return relatedInformation ??= getRelatedInformations(location[0], location[1]);
163
+ },
164
+ set relatedInformation(value) {
165
+ relatedInformation = value;
166
+ },
171
167
  };
168
+ let location = [new Error(), 1];
169
+ let relatedInformation;
170
+ let cachedObj;
172
171
  if (cache && !rule2Mode.get(currentRuleId)) {
173
- cache[1][currentRuleId] ??= [false, []];
174
- cache[1][currentRuleId][1].push({
172
+ cachedObj = {
175
173
  ...error,
176
174
  file: undefined,
177
175
  relatedInformation: error.relatedInformation?.map(info => ({
178
176
  ...info,
179
177
  file: info.file ? { fileName: info.file.fileName } : undefined,
180
178
  })),
181
- });
179
+ };
180
+ cache[1][currentRuleId] ??= [false, []];
181
+ cache[1][currentRuleId][1].push(cachedObj);
182
182
  }
183
- handleError(error, err ?? new Error(), stackOffset);
184
183
  let lintResult = lintResults.get(fileName);
185
184
  if (!lintResult) {
186
185
  lintResults.set(fileName, lintResult = [rulesContext.file, new Map(), []]);
@@ -190,6 +189,22 @@ function createLinter(ctx, rootDir, config, handleError, syntaxOnlyLanguageServi
190
189
  diagnostic2Fixes.set(error, []);
191
190
  const fixes = diagnostic2Fixes.get(error);
192
191
  return {
192
+ at(err, stack) {
193
+ location = [err, stack];
194
+ return this;
195
+ },
196
+ asWarning() {
197
+ error.category = ts.DiagnosticCategory.Warning;
198
+ return this;
199
+ },
200
+ asError() {
201
+ error.category = ts.DiagnosticCategory.Error;
202
+ return this;
203
+ },
204
+ asSuggestion() {
205
+ error.category = ts.DiagnosticCategory.Suggestion;
206
+ return this;
207
+ },
193
208
  withDeprecated() {
194
209
  error.reportsDeprecated = true;
195
210
  return this;
@@ -199,15 +214,27 @@ function createLinter(ctx, rootDir, config, handleError, syntaxOnlyLanguageServi
199
214
  return this;
200
215
  },
201
216
  withFix(title, getEdits) {
202
- fixes.push(({ title, getEdits }));
217
+ fixes.push({ title, getEdits });
203
218
  return this;
204
219
  },
205
220
  withRefactor(title, getEdits) {
206
- refactors.push(({
221
+ refactors.push({
207
222
  diagnostic: error,
208
223
  title,
209
224
  getEdits,
210
- }));
225
+ });
226
+ return this;
227
+ },
228
+ withoutCache() {
229
+ if (cachedObj) {
230
+ const ruleCache = cache?.[1][currentRuleId];
231
+ if (ruleCache) {
232
+ const index = ruleCache[1].indexOf(cachedObj);
233
+ if (index >= 0) {
234
+ ruleCache[1].splice(index, 1);
235
+ }
236
+ }
237
+ }
211
238
  return this;
212
239
  },
213
240
  };
@@ -239,10 +266,10 @@ function createLinter(ctx, rootDir, config, handleError, syntaxOnlyLanguageServi
239
266
  }
240
267
  const diagStart = diagnostic.start;
241
268
  const diagEnd = diagStart + diagnostic.length;
242
- if ((diagStart >= start && diagStart <= end) ||
243
- (diagEnd >= start && diagEnd <= end) ||
244
- (start >= diagStart && start <= diagEnd) ||
245
- (end >= diagStart && end <= diagEnd)) {
269
+ if ((diagStart >= start && diagStart <= end)
270
+ || (diagEnd >= start && diagEnd <= end)
271
+ || (start >= diagStart && start <= diagEnd)
272
+ || (end >= diagStart && end <= diagEnd)) {
246
273
  let codeFixes = [];
247
274
  for (const action of actions) {
248
275
  codeFixes.push({
@@ -250,7 +277,7 @@ function createLinter(ctx, rootDir, config, handleError, syntaxOnlyLanguageServi
250
277
  description: action.title,
251
278
  changes: action.getEdits(),
252
279
  fixId: 'tsslint',
253
- fixAllDescription: 'Fix all TSSLint errors'
280
+ fixAllDescription: 'Fix all TSSLint errors',
254
281
  });
255
282
  }
256
283
  for (const { plugins } of configs) {
@@ -275,10 +302,10 @@ function createLinter(ctx, rootDir, config, handleError, syntaxOnlyLanguageServi
275
302
  const refactor = lintResult[2][i];
276
303
  const diagStart = refactor.diagnostic.start;
277
304
  const diagEnd = diagStart + refactor.diagnostic.length;
278
- if ((diagStart >= start && diagStart <= end) ||
279
- (diagEnd >= start && diagEnd <= end) ||
280
- (start >= diagStart && start <= diagEnd) ||
281
- (end >= diagStart && end <= diagEnd)) {
305
+ if ((diagStart >= start && diagStart <= end)
306
+ || (diagEnd >= start && diagEnd <= end)
307
+ || (start >= diagStart && start <= diagEnd)
308
+ || (end >= diagStart && end <= diagEnd)) {
282
309
  result.push({
283
310
  name: `tsslint:${i}`,
284
311
  description: refactor.title,
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@tsslint/core",
3
- "version": "3.0.0-alpha.0",
3
+ "version": "3.0.0",
4
4
  "license": "MIT",
5
5
  "engines": {
6
- "node": ">=23.6.0"
6
+ "node": ">=22.6.0"
7
7
  },
8
8
  "files": [
9
9
  "**/*.js",
@@ -15,11 +15,8 @@
15
15
  "directory": "packages/core"
16
16
  },
17
17
  "dependencies": {
18
- "@tsslint/types": "3.0.0-alpha.0",
18
+ "@tsslint/types": "3.0.0",
19
19
  "minimatch": "^10.0.1"
20
20
  },
21
- "scripts": {
22
- "postinstall": "node scripts/cleanCache.js"
23
- },
24
- "gitHead": "ec683ca05f4360fac720bd8c567f4d9460d255e1"
25
- }
21
+ "gitHead": "69bc86cf2dd81f9e48fc15ed9b9f0351fa2fc19e"
22
+ }
@@ -1,20 +0,0 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
-
4
- let dir = __dirname;
5
-
6
- while (true) {
7
- const cachePath = path.join(dir, 'node_modules', '.tsslint');
8
- if (fs.existsSync(cachePath)) {
9
- console.log(`Removing ${cachePath}`);
10
- fs.rmSync(cachePath, { recursive: true });
11
- break;
12
- }
13
-
14
- const parentDir = path.resolve(dir, '..');
15
- if (parentDir === dir) {
16
- break;
17
- }
18
-
19
- dir = parentDir;
20
- }