@tsslint/core 1.0.17 → 1.0.19
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/lib/watch.js +2 -2
- package/package.json +3 -3
- package/lib/linter.d.ts +0 -12
- package/lib/linter.js +0 -342
package/lib/watch.js
CHANGED
|
@@ -13,7 +13,7 @@ async function watchConfigFile(configFilePath, onBuild, watch = true, createHash
|
|
|
13
13
|
let config;
|
|
14
14
|
if (!result.errors.length) {
|
|
15
15
|
try {
|
|
16
|
-
config = (await import(outFile + '?time=' + Date.now())).default;
|
|
16
|
+
config = (await import('file://' + outFile + '?time=' + Date.now())).default;
|
|
17
17
|
}
|
|
18
18
|
catch (e) {
|
|
19
19
|
result.errors.push({ text: String(e) });
|
|
@@ -46,7 +46,7 @@ async function watchConfigFile(configFilePath, onBuild, watch = true, createHash
|
|
|
46
46
|
if (!path.endsWith('.ts')) {
|
|
47
47
|
try {
|
|
48
48
|
const maybeJsPath = require.resolve(path, { paths: [resolveDir] });
|
|
49
|
-
if (!maybeJsPath.endsWith('.ts')) {
|
|
49
|
+
if (maybeJsPath !== path && !maybeJsPath.endsWith('.ts')) {
|
|
50
50
|
return {
|
|
51
51
|
path: maybeJsPath,
|
|
52
52
|
external: true,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsslint/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.19",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"**/*.js",
|
|
@@ -12,11 +12,11 @@
|
|
|
12
12
|
"directory": "packages/core"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@tsslint/types": "1.0.
|
|
15
|
+
"@tsslint/types": "1.0.19",
|
|
16
16
|
"error-stack-parser": "^2.1.4",
|
|
17
17
|
"esbuild": "^0.23.0",
|
|
18
18
|
"minimatch": "^10.0.1",
|
|
19
19
|
"source-map-support": "^0.5.21"
|
|
20
20
|
},
|
|
21
|
-
"gitHead": "
|
|
21
|
+
"gitHead": "fbb15b9115e3106a7c8d336023883ea5273cbe65"
|
|
22
22
|
}
|
package/lib/linter.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Config, ProjectContext } from '@tsslint/types';
|
|
2
|
-
import type * as ts from 'typescript';
|
|
3
|
-
export type Linter = ReturnType<typeof createLinter>;
|
|
4
|
-
export declare function createLinter(ctx: ProjectContext, config: Config, withStack: boolean): {
|
|
5
|
-
lint(fileName: string): ts.DiagnosticWithLocation[];
|
|
6
|
-
hasCodeFixes(fileName: string): boolean;
|
|
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;
|
|
10
|
-
};
|
|
11
|
-
export declare function combineCodeFixes(fileName: string, fixes: ts.CodeFixAction[]): ts.TextChange[];
|
|
12
|
-
export declare function applyTextChanges(baseSnapshot: ts.IScriptSnapshot, textChanges: ts.TextChange[]): ts.IScriptSnapshot;
|
package/lib/linter.js
DELETED
|
@@ -1,342 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createLinter = createLinter;
|
|
4
|
-
exports.combineCodeFixes = combineCodeFixes;
|
|
5
|
-
exports.applyTextChanges = applyTextChanges;
|
|
6
|
-
const ErrorStackParser = require("error-stack-parser");
|
|
7
|
-
const path = require("path");
|
|
8
|
-
const minimatch = require("minimatch");
|
|
9
|
-
function createLinter(ctx, config, withStack) {
|
|
10
|
-
if (withStack) {
|
|
11
|
-
require('source-map-support').install({
|
|
12
|
-
retrieveFile(path) {
|
|
13
|
-
// monkey-fix, refs: https://github.com/typescript-eslint/typescript-eslint/issues/9352
|
|
14
|
-
if (path.replace(/\\/g, '/').includes('/@typescript-eslint/eslint-plugin/dist/rules/') && path.endsWith('.js.map')) {
|
|
15
|
-
return JSON.stringify({
|
|
16
|
-
version: 3,
|
|
17
|
-
sources: [],
|
|
18
|
-
sourcesContent: [],
|
|
19
|
-
mappings: '',
|
|
20
|
-
names: [],
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
},
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
const ts = ctx.typescript;
|
|
27
|
-
const fileRules = new Map();
|
|
28
|
-
const fileFixes = new Map();
|
|
29
|
-
const fileRefactors = new Map();
|
|
30
|
-
const sourceFiles = new Map();
|
|
31
|
-
const plugins = (config.plugins ?? []).map(plugin => plugin(ctx));
|
|
32
|
-
const excludes = [];
|
|
33
|
-
for (let exclude of config.exclude ?? []) {
|
|
34
|
-
const basePath = path.dirname(ctx.configFile);
|
|
35
|
-
excludes.push(path.resolve(basePath, exclude));
|
|
36
|
-
}
|
|
37
|
-
return {
|
|
38
|
-
lint(fileName) {
|
|
39
|
-
if (excludes.some(pattern => minimatch.minimatch(fileName, pattern))) {
|
|
40
|
-
return [];
|
|
41
|
-
}
|
|
42
|
-
const sourceFile = ctx.languageService.getProgram()?.getSourceFile(fileName);
|
|
43
|
-
if (!sourceFile) {
|
|
44
|
-
throw new Error(`No source file found for ${fileName}`);
|
|
45
|
-
}
|
|
46
|
-
const rulesContext = {
|
|
47
|
-
...ctx,
|
|
48
|
-
sourceFile,
|
|
49
|
-
reportError,
|
|
50
|
-
reportWarning,
|
|
51
|
-
reportSuggestion,
|
|
52
|
-
};
|
|
53
|
-
const token = ctx.languageServiceHost.getCancellationToken?.();
|
|
54
|
-
const rules = getFileRules(sourceFile.fileName);
|
|
55
|
-
const fixes = getFileFixes(sourceFile.fileName);
|
|
56
|
-
const refactors = getFileRefactors(sourceFile.fileName);
|
|
57
|
-
let diagnostics = [];
|
|
58
|
-
let currentRuleId;
|
|
59
|
-
fixes.clear();
|
|
60
|
-
refactors.clear();
|
|
61
|
-
for (const [id, rule] of Object.entries(rules)) {
|
|
62
|
-
if (token?.isCancellationRequested()) {
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
currentRuleId = id;
|
|
66
|
-
try {
|
|
67
|
-
rule(rulesContext);
|
|
68
|
-
}
|
|
69
|
-
catch (err) {
|
|
70
|
-
diagnostics.push({
|
|
71
|
-
category: ts.DiagnosticCategory.Error,
|
|
72
|
-
code: id,
|
|
73
|
-
messageText: String(err),
|
|
74
|
-
file: sourceFile,
|
|
75
|
-
start: 0,
|
|
76
|
-
length: 0,
|
|
77
|
-
source: 'tsslint',
|
|
78
|
-
relatedInformation: [],
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
for (const plugin of plugins) {
|
|
83
|
-
if (plugin.resolveDiagnostics) {
|
|
84
|
-
diagnostics = plugin.resolveDiagnostics(sourceFile.fileName, diagnostics);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
const diagnosticSet = new Set(diagnostics);
|
|
88
|
-
for (const [ruleId, fix] of [...fixes]) {
|
|
89
|
-
const final = fix.filter(fix => diagnosticSet.has(fix.diagnostic));
|
|
90
|
-
if (final.length) {
|
|
91
|
-
fixes.set(ruleId, final);
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
fixes.delete(ruleId);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
for (const [ruleId, refactor] of [...refactors]) {
|
|
98
|
-
const final = refactor.filter(fix => diagnosticSet.has(fix.diagnostic));
|
|
99
|
-
if (final.length) {
|
|
100
|
-
refactors.set(ruleId, final);
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
refactors.delete(ruleId);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return diagnostics;
|
|
107
|
-
function reportError(message, start, end, traceOffset = 0) {
|
|
108
|
-
return report(ts.DiagnosticCategory.Error, message, start, end, traceOffset);
|
|
109
|
-
}
|
|
110
|
-
function reportWarning(message, start, end, traceOffset = 0) {
|
|
111
|
-
return report(ts.DiagnosticCategory.Warning, message, start, end, traceOffset);
|
|
112
|
-
}
|
|
113
|
-
function reportSuggestion(message, start, end, traceOffset = 0) {
|
|
114
|
-
return report(ts.DiagnosticCategory.Suggestion, message, start, end, traceOffset);
|
|
115
|
-
}
|
|
116
|
-
function report(category, message, start, end, traceOffset) {
|
|
117
|
-
const error = {
|
|
118
|
-
category,
|
|
119
|
-
code: currentRuleId,
|
|
120
|
-
messageText: message,
|
|
121
|
-
file: sourceFile,
|
|
122
|
-
start,
|
|
123
|
-
length: end - start,
|
|
124
|
-
source: 'tsslint',
|
|
125
|
-
relatedInformation: [],
|
|
126
|
-
};
|
|
127
|
-
if (withStack) {
|
|
128
|
-
const stacks = traceOffset === false
|
|
129
|
-
? []
|
|
130
|
-
: ErrorStackParser.parse(new Error());
|
|
131
|
-
if (typeof traceOffset === 'number') {
|
|
132
|
-
const baseOffset = 2 + traceOffset;
|
|
133
|
-
if (stacks.length > baseOffset) {
|
|
134
|
-
pushRelatedInformation(error, stacks[baseOffset]);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
diagnostics.push(error);
|
|
139
|
-
return {
|
|
140
|
-
withDeprecated() {
|
|
141
|
-
error.reportsDeprecated = true;
|
|
142
|
-
return this;
|
|
143
|
-
},
|
|
144
|
-
withUnnecessary() {
|
|
145
|
-
error.reportsUnnecessary = true;
|
|
146
|
-
return this;
|
|
147
|
-
},
|
|
148
|
-
withFix(title, getEdits) {
|
|
149
|
-
if (!fixes.has(currentRuleId)) {
|
|
150
|
-
fixes.set(currentRuleId, []);
|
|
151
|
-
}
|
|
152
|
-
fixes.get(currentRuleId).push(({
|
|
153
|
-
diagnostic: error,
|
|
154
|
-
title,
|
|
155
|
-
start,
|
|
156
|
-
end,
|
|
157
|
-
getEdits,
|
|
158
|
-
}));
|
|
159
|
-
return this;
|
|
160
|
-
},
|
|
161
|
-
withRefactor(title, getEdits) {
|
|
162
|
-
if (!refactors.has(currentRuleId)) {
|
|
163
|
-
refactors.set(currentRuleId, []);
|
|
164
|
-
}
|
|
165
|
-
refactors.get(currentRuleId).push(({
|
|
166
|
-
diagnostic: error,
|
|
167
|
-
title,
|
|
168
|
-
start,
|
|
169
|
-
end,
|
|
170
|
-
getEdits,
|
|
171
|
-
}));
|
|
172
|
-
return this;
|
|
173
|
-
},
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
function pushRelatedInformation(error, stack) {
|
|
177
|
-
if (stack.fileName && stack.lineNumber !== undefined && stack.columnNumber !== undefined) {
|
|
178
|
-
let fileName = stack.fileName.replace(/\\/g, '/');
|
|
179
|
-
if (fileName.startsWith('file://')) {
|
|
180
|
-
fileName = fileName.substring('file://'.length);
|
|
181
|
-
}
|
|
182
|
-
if (fileName.includes('http-url:')) {
|
|
183
|
-
fileName = fileName.split('http-url:')[1];
|
|
184
|
-
}
|
|
185
|
-
if (!sourceFiles.has(fileName)) {
|
|
186
|
-
const text = ctx.languageServiceHost.readFile(fileName) ?? '';
|
|
187
|
-
sourceFiles.set(fileName, ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true));
|
|
188
|
-
}
|
|
189
|
-
const stackFile = sourceFiles.get(fileName);
|
|
190
|
-
let pos = 0;
|
|
191
|
-
try {
|
|
192
|
-
pos = stackFile?.getPositionOfLineAndCharacter(stack.lineNumber - 1, stack.columnNumber - 1) ?? 0;
|
|
193
|
-
}
|
|
194
|
-
catch { }
|
|
195
|
-
error.relatedInformation?.push({
|
|
196
|
-
category: ts.DiagnosticCategory.Message,
|
|
197
|
-
code: 0,
|
|
198
|
-
file: stackFile,
|
|
199
|
-
start: pos,
|
|
200
|
-
length: 0,
|
|
201
|
-
messageText: 'at ' + (stack.functionName ?? '<anonymous>'),
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
},
|
|
206
|
-
hasCodeFixes(fileName) {
|
|
207
|
-
const fixesMap = getFileFixes(fileName);
|
|
208
|
-
for (const [_ruleId, fixes] of fixesMap) {
|
|
209
|
-
if (fixes.length) {
|
|
210
|
-
return true;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
return false;
|
|
214
|
-
},
|
|
215
|
-
getCodeFixes(fileName, start, end, diagnostics) {
|
|
216
|
-
const fixesMap = getFileFixes(fileName);
|
|
217
|
-
let result = [];
|
|
218
|
-
for (const [ruleId, fixes] of fixesMap) {
|
|
219
|
-
for (let i = 0; i < fixes.length; i++) {
|
|
220
|
-
const fix = fixes[i];
|
|
221
|
-
if (diagnostics?.length && !diagnostics.includes(fix.diagnostic)) {
|
|
222
|
-
continue;
|
|
223
|
-
}
|
|
224
|
-
if ((fix.start >= start && fix.start <= end) ||
|
|
225
|
-
(fix.end >= start && fix.end <= end) ||
|
|
226
|
-
(start >= fix.start && start <= fix.end) ||
|
|
227
|
-
(end >= fix.start && end <= fix.end)) {
|
|
228
|
-
result.push({
|
|
229
|
-
fixName: `tsslint:${ruleId}`,
|
|
230
|
-
description: fix.title,
|
|
231
|
-
changes: fix.getEdits(),
|
|
232
|
-
fixId: 'tsslint',
|
|
233
|
-
fixAllDescription: 'Fix all TSSLint errors'
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
for (const plugin of plugins) {
|
|
239
|
-
if (plugin.resolveCodeFixes) {
|
|
240
|
-
result = plugin.resolveCodeFixes(fileName, result);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
return result;
|
|
244
|
-
},
|
|
245
|
-
getRefactors(fileName, start, end) {
|
|
246
|
-
const refactorsMap = getFileRefactors(fileName);
|
|
247
|
-
let result = [];
|
|
248
|
-
for (const [ruleId, refactors] of refactorsMap) {
|
|
249
|
-
for (let i = 0; i < refactors.length; i++) {
|
|
250
|
-
const refactor = refactors[i];
|
|
251
|
-
if ((refactor.start >= start && refactor.start <= end) ||
|
|
252
|
-
(refactor.end >= start && refactor.end <= end) ||
|
|
253
|
-
(start >= refactor.start && start <= refactor.end) ||
|
|
254
|
-
(end >= refactor.start && end <= refactor.end)) {
|
|
255
|
-
result.push({
|
|
256
|
-
name: `tsslint:${ruleId}:${i}`,
|
|
257
|
-
description: refactor.title + ' (' + ruleId + ')',
|
|
258
|
-
kind: 'quickfix',
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
return result;
|
|
264
|
-
},
|
|
265
|
-
getRefactorEdits(fileName, name) {
|
|
266
|
-
if (name.startsWith('tsslint:')) {
|
|
267
|
-
const [ruleId, index] = name.slice('tsslint:'.length).split(':');
|
|
268
|
-
const refactorsMap = getFileRefactors(fileName);
|
|
269
|
-
const refactor = refactorsMap.get(ruleId)?.[Number(index)];
|
|
270
|
-
if (refactor) {
|
|
271
|
-
return refactor.getEdits();
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
},
|
|
275
|
-
};
|
|
276
|
-
function getFileRules(fileName) {
|
|
277
|
-
let rules = fileRules.get(fileName);
|
|
278
|
-
if (!rules) {
|
|
279
|
-
rules = { ...config.rules };
|
|
280
|
-
for (const plugin of plugins) {
|
|
281
|
-
if (plugin.resolveRules) {
|
|
282
|
-
rules = plugin.resolveRules(fileName, rules);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
fileRules.set(fileName, rules);
|
|
286
|
-
}
|
|
287
|
-
return rules;
|
|
288
|
-
}
|
|
289
|
-
function getFileFixes(fileName) {
|
|
290
|
-
if (!fileFixes.has(fileName)) {
|
|
291
|
-
fileFixes.set(fileName, new Map());
|
|
292
|
-
}
|
|
293
|
-
return fileFixes.get(fileName);
|
|
294
|
-
}
|
|
295
|
-
function getFileRefactors(fileName) {
|
|
296
|
-
if (!fileRefactors.has(fileName)) {
|
|
297
|
-
fileRefactors.set(fileName, new Map());
|
|
298
|
-
}
|
|
299
|
-
return fileRefactors.get(fileName);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
function combineCodeFixes(fileName, fixes) {
|
|
303
|
-
const changes = fixes
|
|
304
|
-
.map(fix => fix.changes)
|
|
305
|
-
.flat()
|
|
306
|
-
.filter(change => change.fileName === fileName && change.textChanges.length)
|
|
307
|
-
.sort((a, b) => b.textChanges[0].span.start - a.textChanges[0].span.start);
|
|
308
|
-
let lastChangeAt = Number.MAX_VALUE;
|
|
309
|
-
let finalTextChanges = [];
|
|
310
|
-
for (const change of changes) {
|
|
311
|
-
const textChanges = [...change.textChanges].sort((a, b) => a.span.start - b.span.start);
|
|
312
|
-
const firstChange = textChanges[0];
|
|
313
|
-
const lastChange = textChanges[textChanges.length - 1];
|
|
314
|
-
if (lastChangeAt >= lastChange.span.start + lastChange.span.length) {
|
|
315
|
-
lastChangeAt = firstChange.span.start;
|
|
316
|
-
finalTextChanges = finalTextChanges.concat(textChanges);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
return finalTextChanges;
|
|
320
|
-
}
|
|
321
|
-
function applyTextChanges(baseSnapshot, textChanges) {
|
|
322
|
-
textChanges = [...textChanges].sort((a, b) => b.span.start - a.span.start);
|
|
323
|
-
let text = baseSnapshot.getText(0, baseSnapshot.getLength());
|
|
324
|
-
for (const change of textChanges) {
|
|
325
|
-
text = text.slice(0, change.span.start) + change.newText + text.slice(change.span.start + change.span.length);
|
|
326
|
-
}
|
|
327
|
-
return {
|
|
328
|
-
getText(start, end) {
|
|
329
|
-
return text.substring(start, end);
|
|
330
|
-
},
|
|
331
|
-
getLength() {
|
|
332
|
-
return text.length;
|
|
333
|
-
},
|
|
334
|
-
getChangeRange(oldSnapshot) {
|
|
335
|
-
if (oldSnapshot === baseSnapshot) {
|
|
336
|
-
// TODO
|
|
337
|
-
}
|
|
338
|
-
return undefined;
|
|
339
|
-
},
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
//# sourceMappingURL=linter.js.map
|