@tsslint/compat-eslint 3.0.0-alpha.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.
- package/LICENSE +21 -0
- package/index.d.ts +4 -0
- package/index.js +372 -0
- package/package.json +24 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present Johnson Chu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type * as TSSLint from '@tsslint/types';
|
|
2
|
+
import type * as ESLint from 'eslint';
|
|
3
|
+
import type * as ts from 'typescript';
|
|
4
|
+
export declare function convertRule(eslintRule: ESLint.Rule.RuleModule, options?: any[], context?: Partial<ESLint.Rule.RuleContext>, category?: ts.DiagnosticCategory): TSSLint.Rule;
|
package/index.js
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertRule = convertRule;
|
|
4
|
+
const estrees = new WeakMap();
|
|
5
|
+
function convertRule(eslintRule, options = [], context = {}, category = 3) {
|
|
6
|
+
// ESLint internal scripts
|
|
7
|
+
let createEmitter;
|
|
8
|
+
let NodeEventGenerator;
|
|
9
|
+
let Traverser;
|
|
10
|
+
try {
|
|
11
|
+
createEmitter = require('../../eslint/lib/linter/safe-emitter.js');
|
|
12
|
+
NodeEventGenerator = require('../../eslint/lib/linter/node-event-generator.js');
|
|
13
|
+
Traverser = require('../../eslint/lib/shared/traverser.js');
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
createEmitter = require(require.resolve('./node_modules/eslint/lib/linter/safe-emitter.js'));
|
|
17
|
+
NodeEventGenerator = require(require.resolve('./node_modules/eslint/lib/linter/node-event-generator.js'));
|
|
18
|
+
Traverser = require(require.resolve('./node_modules/eslint/lib/shared/traverser.js'));
|
|
19
|
+
}
|
|
20
|
+
const tsslintRule = ({ file, report, ...ctx }) => {
|
|
21
|
+
const { sourceCode, eventQueue } = getEstree(file, () => ctx.program);
|
|
22
|
+
const emitter = createEmitter();
|
|
23
|
+
if (eslintRule.meta?.defaultOptions) {
|
|
24
|
+
for (let i = 0; i < eslintRule.meta.defaultOptions.length; i++) {
|
|
25
|
+
options[i] ??= eslintRule.meta.defaultOptions[i];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
let currentNode;
|
|
29
|
+
const ruleListeners = eslintRule.create({
|
|
30
|
+
get cwd() {
|
|
31
|
+
return ctx.program.getCurrentDirectory();
|
|
32
|
+
},
|
|
33
|
+
getCwd() {
|
|
34
|
+
return ctx.program.getCurrentDirectory();
|
|
35
|
+
},
|
|
36
|
+
filename: file.fileName,
|
|
37
|
+
getFilename() {
|
|
38
|
+
return file.fileName;
|
|
39
|
+
},
|
|
40
|
+
physicalFilename: file.fileName,
|
|
41
|
+
getPhysicalFilename() {
|
|
42
|
+
return file.fileName;
|
|
43
|
+
},
|
|
44
|
+
sourceCode,
|
|
45
|
+
getSourceCode() {
|
|
46
|
+
return sourceCode;
|
|
47
|
+
},
|
|
48
|
+
settings: {},
|
|
49
|
+
parserOptions: {},
|
|
50
|
+
languageOptions: {},
|
|
51
|
+
parserPath: undefined,
|
|
52
|
+
id: 'unknown',
|
|
53
|
+
options,
|
|
54
|
+
report(descriptor) {
|
|
55
|
+
let message = 'message' in descriptor
|
|
56
|
+
? descriptor.message
|
|
57
|
+
: getMessage(descriptor.messageId);
|
|
58
|
+
message = message.replace(/\{\{\s*(\w+)\s*\}\}/gu, key => {
|
|
59
|
+
return descriptor.data?.[key.slice(2, -2).trim()] ?? key;
|
|
60
|
+
});
|
|
61
|
+
let start = 0;
|
|
62
|
+
let end = 0;
|
|
63
|
+
try {
|
|
64
|
+
if ('loc' in descriptor) {
|
|
65
|
+
if ('line' in descriptor.loc) {
|
|
66
|
+
start = file.getPositionOfLineAndCharacter(descriptor.loc.line - 1, descriptor.loc.column);
|
|
67
|
+
end = start;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
start = file.getPositionOfLineAndCharacter(descriptor.loc.start.line - 1, descriptor.loc.start.column);
|
|
71
|
+
end = file.getPositionOfLineAndCharacter(descriptor.loc.end.line - 1, descriptor.loc.end.column);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else if ('node' in descriptor) {
|
|
75
|
+
if (descriptor.node.loc) {
|
|
76
|
+
start = file.getPositionOfLineAndCharacter(descriptor.node.loc.start.line - 1, descriptor.node.loc.start.column);
|
|
77
|
+
end = file.getPositionOfLineAndCharacter(descriptor.node.loc.end.line - 1, descriptor.node.loc.end.column);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch { }
|
|
82
|
+
const reporter = report(message, start, end).at(new Error(), 1);
|
|
83
|
+
if (category === 0) {
|
|
84
|
+
reporter.asWarning();
|
|
85
|
+
}
|
|
86
|
+
else if (category === 1) {
|
|
87
|
+
reporter.asError();
|
|
88
|
+
}
|
|
89
|
+
else if (category === 2) {
|
|
90
|
+
reporter.asSuggestion();
|
|
91
|
+
}
|
|
92
|
+
if (descriptor.fix) {
|
|
93
|
+
// @ts-expect-error
|
|
94
|
+
const textChanges = getTextChanges(descriptor.fix);
|
|
95
|
+
reporter.withFix(getTextChangeMessage(textChanges), () => [{
|
|
96
|
+
fileName: file.fileName,
|
|
97
|
+
textChanges,
|
|
98
|
+
}]);
|
|
99
|
+
}
|
|
100
|
+
for (const suggest of descriptor.suggest ?? []) {
|
|
101
|
+
if ('messageId' in suggest) {
|
|
102
|
+
let message = getMessage(suggest.messageId);
|
|
103
|
+
message = message.replace(/\{\{\s*(\w+)\s*\}\}/gu, key => {
|
|
104
|
+
return suggest.data?.[key.slice(2, -2).trim()] ?? key;
|
|
105
|
+
});
|
|
106
|
+
reporter.withRefactor(message, () => [{
|
|
107
|
+
fileName: file.fileName,
|
|
108
|
+
// @ts-expect-error
|
|
109
|
+
textChanges: getTextChanges(suggest.fix),
|
|
110
|
+
}]);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
// @ts-expect-error
|
|
114
|
+
const textChanges = getTextChanges(suggest.fix);
|
|
115
|
+
reporter.withRefactor(getTextChangeMessage(textChanges), () => [{
|
|
116
|
+
fileName: file.fileName,
|
|
117
|
+
textChanges,
|
|
118
|
+
}]);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
getAncestors() {
|
|
123
|
+
return sourceCode.getAncestors(currentNode);
|
|
124
|
+
},
|
|
125
|
+
getDeclaredVariables(node) {
|
|
126
|
+
return sourceCode.getDeclaredVariables(node);
|
|
127
|
+
},
|
|
128
|
+
getScope() {
|
|
129
|
+
return sourceCode.getScope(currentNode);
|
|
130
|
+
},
|
|
131
|
+
markVariableAsUsed(name) {
|
|
132
|
+
return sourceCode.markVariableAsUsed(name, currentNode);
|
|
133
|
+
},
|
|
134
|
+
...context,
|
|
135
|
+
});
|
|
136
|
+
for (const selector in ruleListeners) {
|
|
137
|
+
emitter.on(selector, ruleListeners[selector]);
|
|
138
|
+
}
|
|
139
|
+
const eventGenerator = new NodeEventGenerator(emitter, {
|
|
140
|
+
visitorKeys: sourceCode.visitorKeys,
|
|
141
|
+
fallback: Traverser.getKeys,
|
|
142
|
+
});
|
|
143
|
+
for (const step of eventQueue) {
|
|
144
|
+
switch (step.kind) {
|
|
145
|
+
case 1: {
|
|
146
|
+
try {
|
|
147
|
+
if (step.phase === 1) {
|
|
148
|
+
currentNode = step.target;
|
|
149
|
+
eventGenerator.enterNode(step.target);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
eventGenerator.leaveNode(step.target);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch (err) {
|
|
156
|
+
throw err;
|
|
157
|
+
}
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
case 2: {
|
|
161
|
+
emitter.emit(step.target, ...step.args);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
default:
|
|
165
|
+
throw new Error(`Invalid traversal step found: "${step.type}".`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function getTextChangeMessage(textChanges) {
|
|
169
|
+
if (textChanges.length === 1) {
|
|
170
|
+
const change = textChanges[0];
|
|
171
|
+
const originalText = file.text.substring(change.span.start, change.span.start + change.span.length);
|
|
172
|
+
if (change.newText.length === 0) {
|
|
173
|
+
return `Remove \`${originalText}\`.`;
|
|
174
|
+
}
|
|
175
|
+
else if (change.span.length === 0) {
|
|
176
|
+
const line = file.getLineAndCharacterOfPosition(change.span.start).line;
|
|
177
|
+
const lineStart = file.getPositionOfLineAndCharacter(line, 0);
|
|
178
|
+
const lineText = file.text.substring(lineStart, change.span.start).trimStart();
|
|
179
|
+
return `Insert \`${change.newText}\` after \`${lineText}\`.`;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const changes = [...textChanges].sort((a, b) => a.span.start - b.span.start);
|
|
183
|
+
let text = '';
|
|
184
|
+
let newText = '';
|
|
185
|
+
for (let i = 0; i < changes.length; i++) {
|
|
186
|
+
const change = changes[i];
|
|
187
|
+
text += file.text.substring(change.span.start, change.span.start + change.span.length);
|
|
188
|
+
newText += change.newText;
|
|
189
|
+
if (i !== changes.length - 1) {
|
|
190
|
+
text += '…';
|
|
191
|
+
newText += '…';
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (text.length + newText.length <= 50) {
|
|
195
|
+
return `Replace \`${text}\` with \`${newText}\`.`;
|
|
196
|
+
}
|
|
197
|
+
let removeLeft = 0;
|
|
198
|
+
let removeRight = 0;
|
|
199
|
+
let removedLeft = false;
|
|
200
|
+
let removedRight = false;
|
|
201
|
+
for (let i = 0; i < text.length && i < newText.length; i++) {
|
|
202
|
+
if (text[i] !== newText[i]) {
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
removeLeft++;
|
|
206
|
+
}
|
|
207
|
+
for (let i = 0; i < text.length && i < newText.length; i++) {
|
|
208
|
+
if (text[text.length - 1 - i] !== newText[newText.length - 1 - i]) {
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
removeRight++;
|
|
212
|
+
}
|
|
213
|
+
if (removeLeft > removeRight) {
|
|
214
|
+
removedLeft = true;
|
|
215
|
+
text = text.slice(removeLeft);
|
|
216
|
+
newText = newText.slice(removeLeft);
|
|
217
|
+
if (text.length + newText.length > 50) {
|
|
218
|
+
removedRight = true;
|
|
219
|
+
text = text.slice(0, -removeRight);
|
|
220
|
+
newText = newText.slice(0, -removeRight);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
removedRight = true;
|
|
225
|
+
text = text.slice(0, -removeRight);
|
|
226
|
+
newText = newText.slice(0, -removeRight);
|
|
227
|
+
if (text.length + newText.length > 50) {
|
|
228
|
+
removedLeft = true;
|
|
229
|
+
text = text.slice(removeLeft);
|
|
230
|
+
newText = newText.slice(removeLeft);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (removedLeft) {
|
|
234
|
+
text = '…' + text;
|
|
235
|
+
newText = '…' + newText;
|
|
236
|
+
}
|
|
237
|
+
if (removedRight) {
|
|
238
|
+
text += '…';
|
|
239
|
+
newText += '…';
|
|
240
|
+
}
|
|
241
|
+
return `Replace \`${text}\` with \`${newText}\`.`;
|
|
242
|
+
}
|
|
243
|
+
function getTextChanges(fix) {
|
|
244
|
+
const fixes = fix?.({
|
|
245
|
+
insertTextAfter(nodeOrToken, text) {
|
|
246
|
+
if (!nodeOrToken.loc?.end) {
|
|
247
|
+
throw new Error('Cannot insert text after a node without a location.');
|
|
248
|
+
}
|
|
249
|
+
const start = file.getPositionOfLineAndCharacter(nodeOrToken.loc.end.line - 1, nodeOrToken.loc.end.column);
|
|
250
|
+
return this.insertTextAfterRange([start, start], text);
|
|
251
|
+
},
|
|
252
|
+
insertTextAfterRange(range, text) {
|
|
253
|
+
return {
|
|
254
|
+
text,
|
|
255
|
+
range: [range[1], range[1]],
|
|
256
|
+
};
|
|
257
|
+
},
|
|
258
|
+
insertTextBefore(nodeOrToken, text) {
|
|
259
|
+
if (!nodeOrToken.loc?.start) {
|
|
260
|
+
throw new Error('Cannot insert text before a node without a location.');
|
|
261
|
+
}
|
|
262
|
+
const start = file.getPositionOfLineAndCharacter(nodeOrToken.loc.start.line - 1, nodeOrToken.loc.start.column);
|
|
263
|
+
return this.insertTextBeforeRange([start, start], text);
|
|
264
|
+
},
|
|
265
|
+
insertTextBeforeRange(range, text) {
|
|
266
|
+
return {
|
|
267
|
+
text,
|
|
268
|
+
range: [range[0], range[0]],
|
|
269
|
+
};
|
|
270
|
+
},
|
|
271
|
+
remove(nodeOrToken) {
|
|
272
|
+
if (!nodeOrToken.loc) {
|
|
273
|
+
throw new Error('Cannot remove a node without a location.');
|
|
274
|
+
}
|
|
275
|
+
const start = file.getPositionOfLineAndCharacter(nodeOrToken.loc.start.line - 1, nodeOrToken.loc.start.column);
|
|
276
|
+
const end = file.getPositionOfLineAndCharacter(nodeOrToken.loc.end.line - 1, nodeOrToken.loc.end.column);
|
|
277
|
+
return this.removeRange([start, end]);
|
|
278
|
+
},
|
|
279
|
+
removeRange(range) {
|
|
280
|
+
return {
|
|
281
|
+
text: '',
|
|
282
|
+
range,
|
|
283
|
+
};
|
|
284
|
+
},
|
|
285
|
+
replaceText(nodeOrToken, text) {
|
|
286
|
+
if (!nodeOrToken.loc) {
|
|
287
|
+
throw new Error('Cannot replace text of a node without a location.');
|
|
288
|
+
}
|
|
289
|
+
const start = file.getPositionOfLineAndCharacter(nodeOrToken.loc.start.line - 1, nodeOrToken.loc.start.column);
|
|
290
|
+
const end = file.getPositionOfLineAndCharacter(nodeOrToken.loc.end.line - 1, nodeOrToken.loc.end.column);
|
|
291
|
+
return this.replaceTextRange([start, end], text);
|
|
292
|
+
},
|
|
293
|
+
replaceTextRange(range, text) {
|
|
294
|
+
return {
|
|
295
|
+
text,
|
|
296
|
+
range,
|
|
297
|
+
};
|
|
298
|
+
},
|
|
299
|
+
});
|
|
300
|
+
const textChanges = [];
|
|
301
|
+
if (fixes && 'text' in fixes) {
|
|
302
|
+
textChanges.push({
|
|
303
|
+
newText: fixes.text,
|
|
304
|
+
span: {
|
|
305
|
+
start: fixes.range[0],
|
|
306
|
+
length: fixes.range[1] - fixes.range[0],
|
|
307
|
+
},
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
else if (fixes) {
|
|
311
|
+
for (const fix of fixes) {
|
|
312
|
+
textChanges.push({
|
|
313
|
+
newText: fix.text,
|
|
314
|
+
span: {
|
|
315
|
+
start: fix.range[0],
|
|
316
|
+
length: fix.range[1] - fix.range[0],
|
|
317
|
+
},
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return textChanges;
|
|
322
|
+
}
|
|
323
|
+
function getMessage(messageId) {
|
|
324
|
+
return eslintRule.meta?.messages?.[messageId] ?? '';
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
tsslintRule.meta = eslintRule.meta;
|
|
328
|
+
return tsslintRule;
|
|
329
|
+
}
|
|
330
|
+
function getEstree(file, getProgram) {
|
|
331
|
+
if (!estrees.has(file)) {
|
|
332
|
+
let program;
|
|
333
|
+
let SourceCode;
|
|
334
|
+
const Parser = require('@typescript-eslint/parser');
|
|
335
|
+
try {
|
|
336
|
+
SourceCode = require('../../eslint/lib/languages/js/source-code/source-code.js');
|
|
337
|
+
}
|
|
338
|
+
catch {
|
|
339
|
+
SourceCode = require(require.resolve('./node_modules/eslint/lib/languages/js/source-code/source-code.js'));
|
|
340
|
+
}
|
|
341
|
+
const programProxy = new Proxy({}, {
|
|
342
|
+
get(_target, p, receiver) {
|
|
343
|
+
program ??= getProgram();
|
|
344
|
+
return Reflect.get(program, p, receiver);
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
const { ast, scopeManager, visitorKeys, services } = Parser.parseForESLint(file, {
|
|
348
|
+
tokens: true,
|
|
349
|
+
comment: true,
|
|
350
|
+
loc: true,
|
|
351
|
+
range: true,
|
|
352
|
+
preserveNodeMaps: true,
|
|
353
|
+
filePath: file.fileName,
|
|
354
|
+
});
|
|
355
|
+
const sourceCode = new SourceCode({
|
|
356
|
+
text: file.text,
|
|
357
|
+
ast,
|
|
358
|
+
scopeManager,
|
|
359
|
+
visitorKeys,
|
|
360
|
+
parserServices: {
|
|
361
|
+
...services,
|
|
362
|
+
program: programProxy,
|
|
363
|
+
getSymbolAtLocation: (node) => programProxy.getTypeChecker().getSymbolAtLocation(services.esTreeNodeToTSNodeMap.get(node)),
|
|
364
|
+
getTypeAtLocation: (node) => programProxy.getTypeChecker().getTypeAtLocation(services.esTreeNodeToTSNodeMap.get(node)),
|
|
365
|
+
},
|
|
366
|
+
});
|
|
367
|
+
const eventQueue = sourceCode.traverse();
|
|
368
|
+
estrees.set(file, { estree: ast, sourceCode, eventQueue });
|
|
369
|
+
}
|
|
370
|
+
return estrees.get(file);
|
|
371
|
+
}
|
|
372
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsslint/compat-eslint",
|
|
3
|
+
"version": "3.0.0-alpha.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"files": [
|
|
6
|
+
"**/*.js",
|
|
7
|
+
"**/*.d.ts",
|
|
8
|
+
"bin"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/johnsoncodehk/tsslint.git",
|
|
13
|
+
"directory": "packages/compat-eslint"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/eslint": "^8.56.10",
|
|
17
|
+
"typescript": "latest"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@tsslint/types": "3.0.0-alpha.1",
|
|
21
|
+
"@typescript-eslint/parser": "^8.16.0",
|
|
22
|
+
"eslint": ">=9.0.0 <9.28.0"
|
|
23
|
+
}
|
|
24
|
+
}
|