@tsrx/eslint-parser 0.6.3 → 0.7.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/README.md +88 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +453 -82
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
- package/src/index.ts +584 -53
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DIAGNOSTIC_CODES, analyzeTsrx, parseModule } from "@tsrx/core";
|
|
2
|
+
import { Definition, Referencer, ScopeManager } from "eslint-scope";
|
|
2
3
|
//#region src/index.ts
|
|
3
4
|
const visitorKeys = {
|
|
4
5
|
Program: ["body"],
|
|
@@ -7,8 +8,12 @@ const visitorKeys = {
|
|
|
7
8
|
"children",
|
|
8
9
|
"closingElement"
|
|
9
10
|
],
|
|
10
|
-
JSXOpeningElement: [
|
|
11
|
-
|
|
11
|
+
JSXOpeningElement: [
|
|
12
|
+
"name",
|
|
13
|
+
"tagExpression",
|
|
14
|
+
"attributes"
|
|
15
|
+
],
|
|
16
|
+
JSXClosingElement: ["name", "tagExpression"],
|
|
12
17
|
JSXFragment: [
|
|
13
18
|
"openingFragment",
|
|
14
19
|
"children",
|
|
@@ -28,28 +33,20 @@ const visitorKeys = {
|
|
|
28
33
|
"children",
|
|
29
34
|
"closingElement"
|
|
30
35
|
],
|
|
31
|
-
JSXIfExpression: [
|
|
32
|
-
"test",
|
|
33
|
-
"consequent",
|
|
34
|
-
"alternate"
|
|
35
|
-
],
|
|
36
|
+
JSXIfExpression: ["statement"],
|
|
36
37
|
JSXForExpression: [
|
|
37
|
-
"init",
|
|
38
|
-
"test",
|
|
39
|
-
"update",
|
|
40
|
-
"left",
|
|
41
|
-
"right",
|
|
42
|
-
"body",
|
|
43
38
|
"index",
|
|
44
39
|
"key",
|
|
40
|
+
"loop",
|
|
45
41
|
"empty"
|
|
46
42
|
],
|
|
47
43
|
JSXSwitchExpression: ["discriminant", "cases"],
|
|
44
|
+
JSXSwitchCase: ["test", "consequent"],
|
|
48
45
|
JSXTryExpression: [
|
|
49
46
|
"block",
|
|
47
|
+
"pending",
|
|
50
48
|
"handler",
|
|
51
|
-
"finalizer"
|
|
52
|
-
"pending"
|
|
49
|
+
"finalizer"
|
|
53
50
|
],
|
|
54
51
|
JSXCodeBlock: ["body", "render"],
|
|
55
52
|
StyleSheet: [],
|
|
@@ -57,18 +54,239 @@ const visitorKeys = {
|
|
|
57
54
|
"left",
|
|
58
55
|
"right",
|
|
59
56
|
"body"
|
|
57
|
+
],
|
|
58
|
+
TryStatement: [
|
|
59
|
+
"block",
|
|
60
|
+
"pending",
|
|
61
|
+
"handler",
|
|
62
|
+
"finalizer"
|
|
60
63
|
]
|
|
61
64
|
};
|
|
62
65
|
/**
|
|
63
|
-
*
|
|
66
|
+
* eslint-scope's referencer, plus the `@for` parts: the loop's `index` and
|
|
67
|
+
* `key` belong to the loop's scope, like its variable, and the `@empty` branch
|
|
68
|
+
* runs outside it. TSRX gives every iteration its own index. A `@catch` can
|
|
69
|
+
* also take a `reset` function after the error.
|
|
70
|
+
*/
|
|
71
|
+
var TSRXReferencer = class extends Referencer {
|
|
72
|
+
#tsrx_for_by_loop = /* @__PURE__ */ new Map();
|
|
73
|
+
#var_heads = /* @__PURE__ */ new Set();
|
|
74
|
+
JSXOpeningElement(node) {
|
|
75
|
+
if (!node.tagExpression) {
|
|
76
|
+
super.JSXOpeningElement(node);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
this.visit(node.tagExpression);
|
|
80
|
+
node.attributes.forEach(this.visit, this);
|
|
81
|
+
}
|
|
82
|
+
JSXClosingElement() {}
|
|
83
|
+
JSXForExpression(node) {
|
|
84
|
+
this.#tsrx_for_by_loop.set(node.loop, node);
|
|
85
|
+
const head = node.loop.type === "ForStatement" ? node.loop.init : node.loop.left;
|
|
86
|
+
if (head?.type !== "VariableDeclaration" || head.kind === "var") this.scopeManager.__nestForScope(node.loop);
|
|
87
|
+
if (head?.kind === "var") this.#var_heads.add(head);
|
|
88
|
+
this.visit(node.loop);
|
|
89
|
+
if (node.empty) this.visit(node.empty);
|
|
90
|
+
}
|
|
91
|
+
VariableDeclaration(node) {
|
|
92
|
+
if (!this.#var_heads.has(node)) {
|
|
93
|
+
super.VariableDeclaration(node);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
node.declarations.forEach((declaration, index) => {
|
|
97
|
+
this.visitVariableDeclaration(this.currentScope(), "Variable", node, index);
|
|
98
|
+
if (declaration.init) this.visit(declaration.init);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
CatchClause(node) {
|
|
102
|
+
if (!node.resetParam) {
|
|
103
|
+
super.CatchClause(node);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
this.scopeManager.__nestCatchScope(node);
|
|
107
|
+
for (const param of [node.param, node.resetParam]) this.visitPattern(param, { processRightHandNodes: true }, (pattern, info) => {
|
|
108
|
+
this.currentScope().__define(pattern, new Definition("CatchClause", pattern, node, null, null, null));
|
|
109
|
+
this.referencingDefaultValue(pattern, info.assignments, null, true);
|
|
110
|
+
});
|
|
111
|
+
this.visit(node.body);
|
|
112
|
+
this.close(node);
|
|
113
|
+
}
|
|
114
|
+
close(node) {
|
|
115
|
+
const tsrx_for = this.#tsrx_for_by_loop.get(node);
|
|
116
|
+
if (tsrx_for?.index) this.currentScope().__define(tsrx_for.index, new Definition("Variable", tsrx_for.index, tsrx_for, tsrx_for, null, null));
|
|
117
|
+
if (tsrx_for?.key) this.visit(tsrx_for.key);
|
|
118
|
+
super.close(node);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Scope analysis as ESLint runs it, with `TSRXReferencer`. A `.tsrx` file is
|
|
123
|
+
* always a module.
|
|
64
124
|
*/
|
|
65
|
-
function
|
|
125
|
+
function analyze_scope(ast, options) {
|
|
126
|
+
const ecma_features = options?.ecmaFeatures ?? {};
|
|
127
|
+
const scope_options = {
|
|
128
|
+
ignoreEval: true,
|
|
129
|
+
nodejsScope: ecma_features.globalReturn,
|
|
130
|
+
impliedStrict: ecma_features.impliedStrict,
|
|
131
|
+
ecmaVersion: typeof options?.ecmaVersion === "number" ? options.ecmaVersion : 6,
|
|
132
|
+
sourceType: "module",
|
|
133
|
+
childVisitorKeys: visitorKeys,
|
|
134
|
+
fallback: (node) => Object.keys(node).filter((key) => key !== "parent" && key !== "leadingComments" && key !== "trailingComments" && key[0] !== "_"),
|
|
135
|
+
jsx: ecma_features.jsx
|
|
136
|
+
};
|
|
137
|
+
const scope_manager = new ScopeManager(scope_options);
|
|
138
|
+
new TSRXReferencer(scope_options, scope_manager).visit(ast);
|
|
139
|
+
return scope_manager;
|
|
140
|
+
}
|
|
141
|
+
const LOOP_FIELDS = {
|
|
142
|
+
ForStatement: [
|
|
143
|
+
"init",
|
|
144
|
+
"test",
|
|
145
|
+
"update",
|
|
146
|
+
"body"
|
|
147
|
+
],
|
|
148
|
+
ForInStatement: [
|
|
149
|
+
"left",
|
|
150
|
+
"right",
|
|
151
|
+
"body"
|
|
152
|
+
],
|
|
153
|
+
ForOfStatement: [
|
|
154
|
+
"await",
|
|
155
|
+
"left",
|
|
156
|
+
"right",
|
|
157
|
+
"body"
|
|
158
|
+
]
|
|
159
|
+
};
|
|
160
|
+
const STATEMENT_FIELDS = { JSXIfExpression: {
|
|
161
|
+
keyword: "if",
|
|
162
|
+
fields: [
|
|
163
|
+
"test",
|
|
164
|
+
"consequent",
|
|
165
|
+
"alternate"
|
|
166
|
+
]
|
|
167
|
+
} };
|
|
168
|
+
const SIGNATURE_TYPES = /* @__PURE__ */ new Set([
|
|
169
|
+
"TSFunctionType",
|
|
170
|
+
"TSConstructorType",
|
|
171
|
+
"TSCallSignatureDeclaration",
|
|
172
|
+
"TSConstructSignatureDeclaration",
|
|
173
|
+
"TSMethodSignature"
|
|
174
|
+
]);
|
|
175
|
+
/** The name rules see for a dynamic tag that no JSX name can mirror. */
|
|
176
|
+
const DYNAMIC_TAG_NAME = "tsrx-dynamic-tag";
|
|
177
|
+
/**
|
|
178
|
+
* The JSX name rules see for a dynamic tag: a `JSXIdentifier` or
|
|
179
|
+
* `JSXMemberExpression` that mirrors its expression (`<{as}>` is `as`,
|
|
180
|
+
* `<{ui.Box}>` is `ui.Box`), or a synthetic `JSXIdentifier` for any other
|
|
181
|
+
* expression (`<{'div'}>`, `<{icons[key]}>`, `<{pick()}>`). The name spans the
|
|
182
|
+
* braces, so a rule that fixes the text before or after it keeps them.
|
|
183
|
+
*/
|
|
184
|
+
function dynamic_tag_name(container) {
|
|
185
|
+
return {
|
|
186
|
+
...mirror_tag_expression(container.expression) ?? {
|
|
187
|
+
type: "JSXIdentifier",
|
|
188
|
+
name: DYNAMIC_TAG_NAME
|
|
189
|
+
},
|
|
190
|
+
start: container.start,
|
|
191
|
+
end: container.end
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
/** The JSX name for an identifier or a chain of `.name` accesses, or `null`. */
|
|
195
|
+
function mirror_tag_expression(node) {
|
|
196
|
+
if (node.type === "Identifier") return {
|
|
197
|
+
type: "JSXIdentifier",
|
|
198
|
+
name: node.name,
|
|
199
|
+
start: node.start,
|
|
200
|
+
end: node.end
|
|
201
|
+
};
|
|
202
|
+
if (node.type !== "MemberExpression" || node.computed || node.property.type !== "Identifier") return null;
|
|
203
|
+
const object = node.object.type === "ThisExpression" ? {
|
|
204
|
+
type: "JSXIdentifier",
|
|
205
|
+
name: "this",
|
|
206
|
+
start: node.object.start,
|
|
207
|
+
end: node.object.end
|
|
208
|
+
} : mirror_tag_expression(node.object);
|
|
209
|
+
return object && {
|
|
210
|
+
type: "JSXMemberExpression",
|
|
211
|
+
object,
|
|
212
|
+
property: mirror_tag_expression(node.property),
|
|
213
|
+
start: node.start,
|
|
214
|
+
end: node.end
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Keep this hook for small ESLint-only AST normalizations. Returns the `JSXText`
|
|
219
|
+
* nodes, which each get a token of their own.
|
|
220
|
+
*/
|
|
221
|
+
function normalize_tsrx_ast_for_eslint(ast, code) {
|
|
222
|
+
const texts = [];
|
|
66
223
|
const seen = /* @__PURE__ */ new Set();
|
|
67
224
|
const visit = (node) => {
|
|
68
225
|
if (!node || typeof node !== "object") return;
|
|
69
226
|
if (seen.has(node)) return;
|
|
70
227
|
seen.add(node);
|
|
71
228
|
if (node.type === "JSXStyleElement" && node.metadata?.styleApplies) delete node.metadata.styleApplies;
|
|
229
|
+
if (node.type === "JSXSwitchExpression") for (const switch_case of node.cases) switch_case.type = "JSXSwitchCase";
|
|
230
|
+
if (node.type === "JSXElement" || node.type === "JSXFragment") node.children = node.children.filter((child) => child.type !== "JSXExpressionContainer" || child.expression.type !== "JSXEmptyExpression" || child.start !== child.expression.start);
|
|
231
|
+
if (node.type === "JSXForExpression") {
|
|
232
|
+
const loop = {
|
|
233
|
+
type: node.statementType,
|
|
234
|
+
start: code.indexOf("for", node.start),
|
|
235
|
+
end: node.body.end
|
|
236
|
+
};
|
|
237
|
+
for (const key of LOOP_FIELDS[node.statementType]) {
|
|
238
|
+
loop[key] = node[key];
|
|
239
|
+
delete node[key];
|
|
240
|
+
}
|
|
241
|
+
node.loop = loop;
|
|
242
|
+
}
|
|
243
|
+
if (node.type in STATEMENT_FIELDS) {
|
|
244
|
+
const { keyword, fields } = STATEMENT_FIELDS[node.type];
|
|
245
|
+
const statement = {
|
|
246
|
+
type: node.statementType,
|
|
247
|
+
start: code.indexOf(keyword, node.start),
|
|
248
|
+
end: node.end
|
|
249
|
+
};
|
|
250
|
+
for (const key of fields) {
|
|
251
|
+
statement[key] = node[key];
|
|
252
|
+
delete node[key];
|
|
253
|
+
}
|
|
254
|
+
node.statement = statement;
|
|
255
|
+
}
|
|
256
|
+
if (node.type === "TryStatement" && !node.handler && !node.finalizer) throw to_eslint_parse_error({
|
|
257
|
+
message: "Missing catch or finally clause",
|
|
258
|
+
loc: node.loc,
|
|
259
|
+
start: node.start
|
|
260
|
+
});
|
|
261
|
+
if ((node.type === "JSXOpeningElement" || node.type === "JSXClosingElement") && node.name?.type === "JSXExpressionContainer") {
|
|
262
|
+
node.tagExpression = node.name;
|
|
263
|
+
node.name = dynamic_tag_name(node.name);
|
|
264
|
+
}
|
|
265
|
+
if (SIGNATURE_TYPES.has(node.type) && node.parameters) Object.defineProperty(node, "params", {
|
|
266
|
+
value: node.parameters,
|
|
267
|
+
configurable: true
|
|
268
|
+
});
|
|
269
|
+
if (node.type === "TemplateElement") {
|
|
270
|
+
const end_offset = node.tail ? 1 : 2;
|
|
271
|
+
node.start -= 1;
|
|
272
|
+
node.end += end_offset;
|
|
273
|
+
node.range = [node.start, node.end];
|
|
274
|
+
node.loc = {
|
|
275
|
+
start: {
|
|
276
|
+
line: node.loc.start.line,
|
|
277
|
+
column: node.loc.start.column - 1
|
|
278
|
+
},
|
|
279
|
+
end: {
|
|
280
|
+
line: node.loc.end.line,
|
|
281
|
+
column: node.loc.end.column + end_offset
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
if (node.type === "JSXText" && node.end > node.start) texts.push({
|
|
286
|
+
type: "JSXText",
|
|
287
|
+
start: node.start,
|
|
288
|
+
end: node.end
|
|
289
|
+
});
|
|
72
290
|
for (const key of Object.keys(node)) {
|
|
73
291
|
if (key === "parent" || key === "loc" || key === "range") continue;
|
|
74
292
|
const value = node[key];
|
|
@@ -77,69 +295,212 @@ function normalize_tsrx_ast_for_eslint(ast) {
|
|
|
77
295
|
}
|
|
78
296
|
};
|
|
79
297
|
visit(ast);
|
|
298
|
+
return texts.sort((a, b) => a.start - b.start);
|
|
80
299
|
}
|
|
81
300
|
/**
|
|
82
301
|
* Recursively walks the AST and ensures all nodes have range and loc properties
|
|
83
302
|
* ESLint's scope analyzer requires these properties on ALL nodes
|
|
84
303
|
*/
|
|
85
|
-
function ensure_node_properties(node,
|
|
304
|
+
function ensure_node_properties(node, locate) {
|
|
86
305
|
if (!node || typeof node !== "object") return;
|
|
87
306
|
if (node.start !== void 0 && node.end !== void 0 && !node.range) node.range = [node.start, node.end];
|
|
88
|
-
if (!node.loc && node.start !== void 0 && node.end !== void 0)
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
307
|
+
if (!node.loc && node.start !== void 0 && node.end !== void 0) node.loc = locate(node.start, node.end);
|
|
308
|
+
for (const key in node) {
|
|
309
|
+
if (key === "parent" || key === "loc" || key === "range") continue;
|
|
310
|
+
const value = node[key];
|
|
311
|
+
if (Array.isArray(value)) value.forEach((child) => ensure_node_properties(child, locate));
|
|
312
|
+
else if (value && typeof value === "object" && value.type) ensure_node_properties(value, locate);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Builds `loc` objects from offsets, with the line terminators acorn and ESLint
|
|
317
|
+
* count.
|
|
318
|
+
*/
|
|
319
|
+
function create_locator(code) {
|
|
320
|
+
const line_starts = [0];
|
|
321
|
+
for (const match of code.matchAll(/\r\n?|[\n\u2028\u2029]/g)) line_starts.push(match.index + match[0].length);
|
|
322
|
+
const position = (offset) => {
|
|
323
|
+
let low = 0;
|
|
324
|
+
let high = line_starts.length - 1;
|
|
325
|
+
while (low < high) {
|
|
326
|
+
const middle = low + high + 1 >> 1;
|
|
327
|
+
if (line_starts[middle] <= offset) low = middle;
|
|
328
|
+
else high = middle - 1;
|
|
103
329
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
330
|
+
return {
|
|
331
|
+
line: low + 1,
|
|
332
|
+
column: offset - line_starts[low]
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
return (start, end) => ({
|
|
336
|
+
start: position(start),
|
|
337
|
+
end: position(end)
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Converts the parser's comments to ESLint comments, in source order. The value
|
|
342
|
+
* is read from the source: the parser strips the indentation from the value of
|
|
343
|
+
* a multi-line block comment.
|
|
344
|
+
*/
|
|
345
|
+
function to_eslint_comments(code, comments, locate) {
|
|
346
|
+
const result = [];
|
|
347
|
+
for (const { type, start, end } of [...comments].sort((a, b) => a.start - b.start)) {
|
|
348
|
+
if (result.at(-1)?.start === start) continue;
|
|
349
|
+
const value_end = type === "Block" && code.startsWith("*/", end - 2) ? end - 2 : end;
|
|
350
|
+
result.push({
|
|
351
|
+
type,
|
|
352
|
+
value: code.slice(start + 2, value_end),
|
|
353
|
+
start,
|
|
354
|
+
end,
|
|
355
|
+
range: [start, end],
|
|
356
|
+
loc: locate(start, end)
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
return result;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Converts the tokens the TSRX lexer reported into the token list ESLint
|
|
363
|
+
* expects: in source order, without overlaps, in espree's token types, and
|
|
364
|
+
* covering every character that is not whitespace or a comment.
|
|
365
|
+
*/
|
|
366
|
+
function to_eslint_tokens(code, lexer_tokens, texts, comments, locate) {
|
|
367
|
+
const lexed = [];
|
|
368
|
+
for (const token of [...lexer_tokens].sort((a, b) => a.start - b.start || a.end - b.end)) {
|
|
369
|
+
const last = lexed.at(-1);
|
|
370
|
+
if (last?.start === token.start && last.end === token.end) lexed[lexed.length - 1] = token;
|
|
371
|
+
else if (!last || token.start >= last.end) lexed.push(token);
|
|
372
|
+
}
|
|
373
|
+
const spans = [];
|
|
374
|
+
for (let i = 0; i < lexed.length; i++) {
|
|
375
|
+
const token = lexed[i];
|
|
376
|
+
const label = token.type.label;
|
|
377
|
+
if (label === "`" || label === "}" && is_template_part(lexed[i + 1])) {
|
|
378
|
+
let last = i + 1;
|
|
379
|
+
while (is_template_part(lexed[last])) last++;
|
|
380
|
+
const closer = lexed[last]?.type.label;
|
|
381
|
+
if (closer !== "${" && closer !== "`") last--;
|
|
382
|
+
spans.push({
|
|
383
|
+
type: "Template",
|
|
384
|
+
start: token.start,
|
|
385
|
+
end: lexed[last].end
|
|
386
|
+
});
|
|
387
|
+
i = last;
|
|
388
|
+
continue;
|
|
113
389
|
}
|
|
114
|
-
|
|
115
|
-
start
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
390
|
+
if (code.startsWith("</", token.start) && token.end > token.start + 1) {
|
|
391
|
+
for (const match of code.slice(token.start, token.end).matchAll(/([</>])|[^\s</>]+/g)) {
|
|
392
|
+
const start = token.start + match.index;
|
|
393
|
+
spans.push({
|
|
394
|
+
type: match[1] ? "Punctuator" : "JSXIdentifier",
|
|
395
|
+
start,
|
|
396
|
+
end: start + match[0].length
|
|
397
|
+
});
|
|
122
398
|
}
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
if (label === "eof" || token.start === token.end) continue;
|
|
402
|
+
spans.push({
|
|
403
|
+
type: token_type(token, lexed[i - 1], lexed[i - 2]),
|
|
404
|
+
start: token.start,
|
|
405
|
+
end: token.end,
|
|
406
|
+
regex: label === "regexp" ? {
|
|
407
|
+
pattern: token.value.pattern,
|
|
408
|
+
flags: token.value.flags
|
|
409
|
+
} : void 0
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
const kept = outside([...outside(spans, texts), ...texts].sort((a, b) => a.start - b.start), comments);
|
|
413
|
+
const tokens = [];
|
|
414
|
+
const add = (span) => {
|
|
415
|
+
const token = {
|
|
416
|
+
type: span.type,
|
|
417
|
+
value: code.slice(span.start, span.end),
|
|
418
|
+
start: span.start,
|
|
419
|
+
end: span.end,
|
|
420
|
+
range: [span.start, span.end],
|
|
421
|
+
loc: locate(span.start, span.end)
|
|
123
422
|
};
|
|
423
|
+
if (span.regex) token.regex = span.regex;
|
|
424
|
+
tokens.push(token);
|
|
425
|
+
};
|
|
426
|
+
const fill = (start, end) => {
|
|
427
|
+
const at = end > start && code[end - 1] === "@";
|
|
428
|
+
if (at) end--;
|
|
429
|
+
if (/\S/.test(code.slice(start, end))) add({
|
|
430
|
+
type: "JSXText",
|
|
431
|
+
start,
|
|
432
|
+
end
|
|
433
|
+
});
|
|
434
|
+
if (at) add({
|
|
435
|
+
type: "Punctuator",
|
|
436
|
+
start: end,
|
|
437
|
+
end: end + 1
|
|
438
|
+
});
|
|
439
|
+
};
|
|
440
|
+
const covered = [...kept, ...comments];
|
|
441
|
+
let position = 0;
|
|
442
|
+
for (const item of covered.sort((a, b) => a.start - b.start)) {
|
|
443
|
+
fill(position, item.start);
|
|
444
|
+
if (!("value" in item)) add(item);
|
|
445
|
+
position = item.end;
|
|
124
446
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
447
|
+
fill(position, code.length);
|
|
448
|
+
return tokens;
|
|
449
|
+
}
|
|
450
|
+
/** The spans that overlap none of the sorted, disjoint `ranges`. */
|
|
451
|
+
function outside(spans, ranges) {
|
|
452
|
+
let index = 0;
|
|
453
|
+
return spans.filter((span) => {
|
|
454
|
+
while (index < ranges.length && ranges[index].end <= span.start) index++;
|
|
455
|
+
return index === ranges.length || ranges[index].start >= span.end;
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
function is_template_part(token) {
|
|
459
|
+
return token?.type.label === "template" || token?.type.label === "invalidTemplate";
|
|
460
|
+
}
|
|
461
|
+
/** The espree token type of an acorn token. */
|
|
462
|
+
function token_type(token, previous, before_previous) {
|
|
463
|
+
const { label, keyword } = token.type;
|
|
464
|
+
switch (label) {
|
|
465
|
+
case "name": return token.value === "static" || token.value === "yield" || token.value === "let" ? "Keyword" : "Identifier";
|
|
466
|
+
case "privateId": return "PrivateIdentifier";
|
|
467
|
+
case "jsxName": return "JSXIdentifier";
|
|
468
|
+
case "jsxText": return "JSXText";
|
|
469
|
+
case "num": return "Numeric";
|
|
470
|
+
case "regexp": return "RegularExpression";
|
|
471
|
+
case "string": return previous?.type.label === "=" && before_previous?.type.label === "jsxName" ? "JSXText" : "String";
|
|
130
472
|
}
|
|
473
|
+
if (keyword === "true" || keyword === "false") return "Boolean";
|
|
474
|
+
if (keyword === "null") return "Null";
|
|
475
|
+
return keyword ? "Keyword" : "Punctuator";
|
|
131
476
|
}
|
|
477
|
+
/**
|
|
478
|
+
* The collected errors ESLint reports as a parse error: broken markup, and a
|
|
479
|
+
* directive's clause that no directive takes (TSRX1012), which the parser
|
|
480
|
+
* reads as text and an expression container, as it throws a clause written
|
|
481
|
+
* without its `@` (TSRX1009).
|
|
482
|
+
*/
|
|
132
483
|
function is_fatal_parser_diagnostic(error) {
|
|
133
|
-
return error?.code === DIAGNOSTIC_CODES.UNCLOSED_TAG || error?.code === DIAGNOSTIC_CODES.MISMATCHED_CLOSING_TAG;
|
|
484
|
+
return error?.code === DIAGNOSTIC_CODES.UNCLOSED_TAG || error?.code === DIAGNOSTIC_CODES.MISMATCHED_CLOSING_TAG || error?.code === DIAGNOSTIC_CODES.DETACHED_DIRECTIVE_CLAUSE;
|
|
134
485
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
486
|
+
/**
|
|
487
|
+
* A parse error as ESLint reads one: `lineNumber` and a 1-based `column`. A
|
|
488
|
+
* collected error's `loc` is a range, and a thrown one's is acorn's
|
|
489
|
+
* `{ line, column }`, which its message ends with too, as `(line:column)`. With
|
|
490
|
+
* `code`, an error with only `pos` gets its line and column from there.
|
|
491
|
+
*/
|
|
492
|
+
function to_eslint_parse_error(error, code) {
|
|
493
|
+
const index = error?.start ?? error?.pos;
|
|
494
|
+
let loc = error?.loc?.start ?? error?.loc;
|
|
495
|
+
if (typeof loc?.line !== "number" || typeof loc?.column !== "number") loc = code !== void 0 && typeof index === "number" ? create_locator(code)(index, index).start : null;
|
|
496
|
+
let message = error?.message || String(error);
|
|
497
|
+
if (loc && message.endsWith(` (${loc.line}:${loc.column})`)) message = message.slice(0, -` (${loc.line}:${loc.column})`.length);
|
|
498
|
+
const parse_error = new SyntaxError(message);
|
|
138
499
|
if (loc) {
|
|
139
500
|
parse_error.lineNumber = loc.line;
|
|
140
501
|
parse_error.column = loc.column + 1;
|
|
141
502
|
}
|
|
142
|
-
if (typeof
|
|
503
|
+
if (typeof index === "number") parse_error.index = index;
|
|
143
504
|
return parse_error;
|
|
144
505
|
}
|
|
145
506
|
/**
|
|
@@ -155,9 +516,13 @@ function parseForESLint(code, options) {
|
|
|
155
516
|
const ast = parseModule(code, options?.filePath || "ESLintParser.tsrx", {
|
|
156
517
|
collect: true,
|
|
157
518
|
errors,
|
|
158
|
-
comments
|
|
519
|
+
comments,
|
|
520
|
+
keywordTokens: true
|
|
159
521
|
});
|
|
160
522
|
if (!ast) throw new Error("Parser returned null or undefined AST");
|
|
523
|
+
const lexer_tokens = ast.tsrx_tokens;
|
|
524
|
+
delete ast.tsrx_tokens;
|
|
525
|
+
delete ast.tsrx_keyword_tokens;
|
|
161
526
|
analyzeTsrx(ast, options?.filePath || "ESLintParser.tsrx", {
|
|
162
527
|
collect: true,
|
|
163
528
|
errors,
|
|
@@ -165,34 +530,40 @@ function parseForESLint(code, options) {
|
|
|
165
530
|
});
|
|
166
531
|
const fatal_error = errors.find(is_fatal_parser_diagnostic);
|
|
167
532
|
if (fatal_error) throw to_eslint_parse_error(fatal_error);
|
|
168
|
-
normalize_tsrx_ast_for_eslint(ast);
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
end: {
|
|
181
|
-
line: code.split("\n").length,
|
|
182
|
-
column: 0
|
|
183
|
-
}
|
|
533
|
+
const texts = normalize_tsrx_ast_for_eslint(ast, code);
|
|
534
|
+
const locate = create_locator(code);
|
|
535
|
+
ensure_node_properties(ast, locate);
|
|
536
|
+
const eslint_comments = to_eslint_comments(code, comments, locate);
|
|
537
|
+
const result = {
|
|
538
|
+
type: ast.type || "Program",
|
|
539
|
+
start: ast.start !== void 0 ? ast.start : 0,
|
|
540
|
+
end: ast.end !== void 0 ? ast.end : code.length,
|
|
541
|
+
loc: ast.loc || {
|
|
542
|
+
start: {
|
|
543
|
+
line: 1,
|
|
544
|
+
column: 0
|
|
184
545
|
},
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
tokens: ast.tokens || []
|
|
546
|
+
end: {
|
|
547
|
+
line: code.split("\n").length,
|
|
548
|
+
column: 0
|
|
549
|
+
}
|
|
190
550
|
},
|
|
551
|
+
range: ast.range || [0, code.length],
|
|
552
|
+
body: ast.body || [],
|
|
553
|
+
sourceType: ast.sourceType || "module",
|
|
554
|
+
comments: eslint_comments,
|
|
555
|
+
tokens: to_eslint_tokens(code, lexer_tokens, texts, eslint_comments, locate)
|
|
556
|
+
};
|
|
557
|
+
return {
|
|
558
|
+
ast: result,
|
|
191
559
|
services: { tsrxDiagnostics: errors },
|
|
560
|
+
scopeManager: analyze_scope(result, options),
|
|
192
561
|
visitorKeys
|
|
193
562
|
};
|
|
194
563
|
} catch (error) {
|
|
195
564
|
if (error instanceof SyntaxError && error.lineNumber != null) throw error;
|
|
565
|
+
const parse_error = to_eslint_parse_error(error, code);
|
|
566
|
+
if (parse_error.lineNumber != null) throw parse_error;
|
|
196
567
|
throw new SyntaxError(`Failed to parse TSRX file: ${error.message || error}`);
|
|
197
568
|
}
|
|
198
569
|
}
|