@tsrx/eslint-plugin 0.6.3 → 0.7.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/README.md +109 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +929 -94
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +44 -0
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createRequire } from "module";
|
|
2
|
+
import { builtinRules } from "eslint/use-at-your-own-risk";
|
|
2
3
|
//#region src/rules/no-return-in-component.ts
|
|
3
|
-
const rule$
|
|
4
|
+
const rule$7 = {
|
|
4
5
|
meta: {
|
|
5
6
|
type: "problem",
|
|
6
7
|
docs: {
|
|
@@ -63,7 +64,7 @@ const NESTED_BOUNDARY_TYPES = /* @__PURE__ */ new Set([
|
|
|
63
64
|
"MethodDefinition",
|
|
64
65
|
"PropertyDefinition"
|
|
65
66
|
]);
|
|
66
|
-
const rule$
|
|
67
|
+
const rule$6 = {
|
|
67
68
|
meta: {
|
|
68
69
|
type: "problem",
|
|
69
70
|
docs: {
|
|
@@ -108,7 +109,7 @@ const rule$2 = {
|
|
|
108
109
|
"ArrowFunctionExpression:exit": exitFunction,
|
|
109
110
|
JSXForExpression(node) {
|
|
110
111
|
if (insideComponent === 0 || nonComponentFunctionDepth > 0) return;
|
|
111
|
-
const body = node.body;
|
|
112
|
+
const body = node.loop?.body;
|
|
112
113
|
if (!body || containsTemplateOutput(body)) return;
|
|
113
114
|
context.report({
|
|
114
115
|
node,
|
|
@@ -130,7 +131,7 @@ const rule$2 = {
|
|
|
130
131
|
};
|
|
131
132
|
//#endregion
|
|
132
133
|
//#region src/rules/valid-for-of-key.ts
|
|
133
|
-
const rule$
|
|
134
|
+
const rule$5 = {
|
|
134
135
|
meta: {
|
|
135
136
|
type: "problem",
|
|
136
137
|
docs: {
|
|
@@ -142,71 +143,38 @@ const rule$1 = {
|
|
|
142
143
|
},
|
|
143
144
|
create(context) {
|
|
144
145
|
const checkForOfKey = (node) => {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
146
|
+
const key = node.key;
|
|
147
|
+
if (!key) return;
|
|
148
|
+
const [start, end] = key.range;
|
|
149
|
+
for (const reference of context.sourceCode.getScope(node).through) {
|
|
150
|
+
const identifier = reference.identifier;
|
|
151
|
+
const [identifier_start, identifier_end] = identifier.range;
|
|
152
|
+
if (reference.resolved || identifier_start < start || identifier_end > end) continue;
|
|
153
|
+
if (is_type_name(identifier, node)) continue;
|
|
154
|
+
context.report({
|
|
148
155
|
node: identifier,
|
|
149
156
|
messageId: "undefinedVariable",
|
|
150
157
|
data: { name: identifier.name }
|
|
151
158
|
});
|
|
152
|
-
}
|
|
153
|
-
const traverse = (node) => {
|
|
154
|
-
if (!node) return;
|
|
155
|
-
switch (node.type) {
|
|
156
|
-
case "Identifier":
|
|
157
|
-
checkIdentifier(node);
|
|
158
|
-
break;
|
|
159
|
-
case "MemberExpression":
|
|
160
|
-
traverse(node.object);
|
|
161
|
-
if (node.computed) traverse(node.property);
|
|
162
|
-
break;
|
|
163
|
-
case "BinaryExpression":
|
|
164
|
-
case "LogicalExpression":
|
|
165
|
-
traverse(node.left);
|
|
166
|
-
traverse(node.right);
|
|
167
|
-
break;
|
|
168
|
-
case "UnaryExpression":
|
|
169
|
-
traverse(node.argument);
|
|
170
|
-
break;
|
|
171
|
-
case "CallExpression":
|
|
172
|
-
traverse(node.callee);
|
|
173
|
-
node.arguments.forEach(traverse);
|
|
174
|
-
break;
|
|
175
|
-
case "ArrayExpression":
|
|
176
|
-
node.elements.forEach(traverse);
|
|
177
|
-
break;
|
|
178
|
-
case "ObjectExpression":
|
|
179
|
-
node.properties.forEach((prop) => {
|
|
180
|
-
if (prop.type === "Property") {
|
|
181
|
-
if (prop.computed) traverse(prop.key);
|
|
182
|
-
traverse(prop.value);
|
|
183
|
-
} else if (prop.type === "SpreadElement") traverse(prop.argument);
|
|
184
|
-
});
|
|
185
|
-
break;
|
|
186
|
-
case "ConditionalExpression":
|
|
187
|
-
traverse(node.test);
|
|
188
|
-
traverse(node.consequent);
|
|
189
|
-
traverse(node.alternate);
|
|
190
|
-
break;
|
|
191
|
-
case "TemplateLiteral": node.expressions.forEach(traverse);
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
traverse(node.key);
|
|
195
|
-
};
|
|
196
|
-
return {
|
|
197
|
-
ForOfStatement: checkForOfKey,
|
|
198
|
-
JSXForExpression: checkForOfKey
|
|
159
|
+
}
|
|
199
160
|
};
|
|
161
|
+
return { JSXForExpression: checkForOfKey };
|
|
200
162
|
}
|
|
201
163
|
};
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
164
|
+
const TS_EXPRESSIONS = /* @__PURE__ */ new Set([
|
|
165
|
+
"TSAsExpression",
|
|
166
|
+
"TSSatisfiesExpression",
|
|
167
|
+
"TSNonNullExpression",
|
|
168
|
+
"TSInstantiationExpression",
|
|
169
|
+
"TSParameterProperty"
|
|
170
|
+
]);
|
|
171
|
+
/**
|
|
172
|
+
* Whether the identifier names a type, as `Foo` does in `key item as Foo`.
|
|
173
|
+
* ESLint's scope analysis reports a type declared in the file as unresolved.
|
|
174
|
+
*/
|
|
175
|
+
function is_type_name(identifier, loop) {
|
|
176
|
+
for (let node = identifier.parent; node && node !== loop; node = node.parent) if (node.type.startsWith("TS") && !TS_EXPRESSIONS.has(node.type)) return true;
|
|
177
|
+
return false;
|
|
210
178
|
}
|
|
211
179
|
//#endregion
|
|
212
180
|
//#region src/rules/require-statement-container-body.ts
|
|
@@ -237,6 +205,877 @@ function get_forgotten_output_statement(node) {
|
|
|
237
205
|
for (const statement of statements.slice(target_index + 1)) if (!is_ignored_statement(statement)) return null;
|
|
238
206
|
return target;
|
|
239
207
|
}
|
|
208
|
+
const rule$4 = {
|
|
209
|
+
meta: {
|
|
210
|
+
type: "problem",
|
|
211
|
+
docs: { description: "Require @{...} for TSRX component bodies with setup and template output." },
|
|
212
|
+
fixable: "code",
|
|
213
|
+
messages: { requireStatementContainerBody: MESSAGE },
|
|
214
|
+
schema: []
|
|
215
|
+
},
|
|
216
|
+
create(context) {
|
|
217
|
+
function check_function(node) {
|
|
218
|
+
if (!node.returnType) return;
|
|
219
|
+
const forgotten_output = get_forgotten_output_statement(node);
|
|
220
|
+
if (!forgotten_output) return;
|
|
221
|
+
const body = node.body;
|
|
222
|
+
context.report({
|
|
223
|
+
node: forgotten_output,
|
|
224
|
+
messageId: "requireStatementContainerBody",
|
|
225
|
+
fix(fixer) {
|
|
226
|
+
return fixer.insertTextBefore(body, "@");
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
FunctionDeclaration: check_function,
|
|
232
|
+
FunctionExpression: check_function,
|
|
233
|
+
ArrowFunctionExpression: check_function
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/rules/jsx-curly-brace-presence.ts
|
|
239
|
+
/*!
|
|
240
|
+
* `tsrx/jsx-curly-brace-presence` is copied from eslint-plugin-react 7.37.5,
|
|
241
|
+
* `lib/rules/jsx-curly-brace-presence.js`:
|
|
242
|
+
*
|
|
243
|
+
* @fileoverview Enforce curly braces or disallow unnecessary curly brace in JSX
|
|
244
|
+
* @author Jacky Ho
|
|
245
|
+
* @author Simon Lydell
|
|
246
|
+
*
|
|
247
|
+
* The MIT License (MIT)
|
|
248
|
+
*
|
|
249
|
+
* Copyright (c) 2014 Yannick Croissant
|
|
250
|
+
*
|
|
251
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
252
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
253
|
+
* in the Software without restriction, including without limitation the rights
|
|
254
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
255
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
256
|
+
* furnished to do so, subject to the following conditions:
|
|
257
|
+
*
|
|
258
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
259
|
+
* copies or substantial portions of the Software.
|
|
260
|
+
*
|
|
261
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
262
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
263
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
264
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
265
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
266
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
267
|
+
* SOFTWARE.
|
|
268
|
+
*/
|
|
269
|
+
const OPTION_ALWAYS = "always";
|
|
270
|
+
const OPTION_NEVER = "never";
|
|
271
|
+
const OPTION_IGNORE = "ignore";
|
|
272
|
+
const OPTION_VALUES = [
|
|
273
|
+
OPTION_ALWAYS,
|
|
274
|
+
OPTION_NEVER,
|
|
275
|
+
OPTION_IGNORE
|
|
276
|
+
];
|
|
277
|
+
const DEFAULT_CONFIG = {
|
|
278
|
+
props: OPTION_NEVER,
|
|
279
|
+
children: OPTION_NEVER,
|
|
280
|
+
propElementValues: OPTION_IGNORE
|
|
281
|
+
};
|
|
282
|
+
const HTML_ENTITY_REGEX = () => /&[A-Za-z\d#]+;/g;
|
|
283
|
+
function containsLineTerminators(rawStringValue) {
|
|
284
|
+
return /[\n\r\u2028\u2029]/.test(rawStringValue);
|
|
285
|
+
}
|
|
286
|
+
function containsBackslash(rawStringValue) {
|
|
287
|
+
return rawStringValue.includes("\\");
|
|
288
|
+
}
|
|
289
|
+
function containsHTMLEntity(rawStringValue) {
|
|
290
|
+
return HTML_ENTITY_REGEX().test(rawStringValue);
|
|
291
|
+
}
|
|
292
|
+
function containsOnlyHtmlEntities(rawStringValue) {
|
|
293
|
+
return rawStringValue.replace(HTML_ENTITY_REGEX(), "").trim() === "";
|
|
294
|
+
}
|
|
295
|
+
function containsDisallowedJSXTextChars(rawStringValue) {
|
|
296
|
+
return /[{<>}]/.test(rawStringValue);
|
|
297
|
+
}
|
|
298
|
+
function containsQuoteCharacters(value) {
|
|
299
|
+
return /['"]/.test(value);
|
|
300
|
+
}
|
|
301
|
+
function containsMultilineComment(value) {
|
|
302
|
+
return /\/\*/.test(value);
|
|
303
|
+
}
|
|
304
|
+
function escapeDoubleQuotes(rawStringValue) {
|
|
305
|
+
return rawStringValue.replace(/\\"/g, "\"").replace(/"/g, "\\\"");
|
|
306
|
+
}
|
|
307
|
+
function escapeBackslashes(rawStringValue) {
|
|
308
|
+
return rawStringValue.replace(/\\/g, "\\\\");
|
|
309
|
+
}
|
|
310
|
+
function needToEscapeCharacterForJSX(raw, node) {
|
|
311
|
+
return containsBackslash(raw) || containsHTMLEntity(raw) || node.parent.type !== "JSXAttribute" && containsDisallowedJSXTextChars(raw);
|
|
312
|
+
}
|
|
313
|
+
function isJSX(node) {
|
|
314
|
+
return !!node && (node.type === "JSXElement" || node.type === "JSXFragment");
|
|
315
|
+
}
|
|
316
|
+
function isWhiteSpaces(value) {
|
|
317
|
+
return typeof value === "string" ? /^\s*$/.test(value) : false;
|
|
318
|
+
}
|
|
319
|
+
function containsWhitespaceExpression(child) {
|
|
320
|
+
if (child.type === "JSXExpressionContainer") {
|
|
321
|
+
const value = child.expression.value;
|
|
322
|
+
return value ? isWhiteSpaces(value) : false;
|
|
323
|
+
}
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
function isLineBreak(text) {
|
|
327
|
+
return containsLineTerminators(text) && text.trim() === "";
|
|
328
|
+
}
|
|
329
|
+
function wrapNonHTMLEntities(text) {
|
|
330
|
+
const HTML_ENTITY = "<HTML_ENTITY>";
|
|
331
|
+
const withCurlyBraces = text.split(HTML_ENTITY_REGEX()).map((word) => word === "" ? "" : `{${JSON.stringify(word)}}`).join(HTML_ENTITY);
|
|
332
|
+
return text.match(HTML_ENTITY_REGEX()).reduce((acc, htmlEntity) => acc.replace(HTML_ENTITY, htmlEntity), withCurlyBraces);
|
|
333
|
+
}
|
|
334
|
+
function wrapWithCurlyBraces(rawText) {
|
|
335
|
+
if (!containsLineTerminators(rawText)) return `{${JSON.stringify(rawText)}}`;
|
|
336
|
+
return rawText.split("\n").map((line) => {
|
|
337
|
+
if (line.trim() === "") return line;
|
|
338
|
+
const firstCharIndex = line.search(/[^\s]/);
|
|
339
|
+
const leftWhitespace = line.slice(0, firstCharIndex);
|
|
340
|
+
const text = line.slice(firstCharIndex);
|
|
341
|
+
if (containsHTMLEntity(line)) return `${leftWhitespace}${wrapNonHTMLEntities(text)}`;
|
|
342
|
+
return `${leftWhitespace}{${JSON.stringify(text)}}`;
|
|
343
|
+
}).join("\n");
|
|
344
|
+
}
|
|
345
|
+
function isWhiteSpaceLiteral(node) {
|
|
346
|
+
return node.type && node.type === "Literal" && node.value && isWhiteSpaces(node.value);
|
|
347
|
+
}
|
|
348
|
+
function isStringWithTrailingWhiteSpaces(value) {
|
|
349
|
+
return /^\s|\s$/.test(value);
|
|
350
|
+
}
|
|
351
|
+
function isLiteralWithTrailingWhiteSpaces(node) {
|
|
352
|
+
return node.type && node.type === "Literal" && node.value && isStringWithTrailingWhiteSpaces(node.value);
|
|
353
|
+
}
|
|
354
|
+
/** TSRX: the directives, whose clauses (`@else`, `catch (e)`) text could continue. */
|
|
355
|
+
const DIRECTIVE_TYPES = /* @__PURE__ */ new Set([
|
|
356
|
+
"JSXIfExpression",
|
|
357
|
+
"JSXForExpression",
|
|
358
|
+
"JSXSwitchExpression",
|
|
359
|
+
"JSXTryExpression"
|
|
360
|
+
]);
|
|
361
|
+
/**
|
|
362
|
+
* TSRX: whether `<style>` or `<script>` holds `node`. The first child of a
|
|
363
|
+
* `<style>` decides what it is: text makes it a scoped style block, while
|
|
364
|
+
* `{css}` keeps it an ordinary element. A `<script>` body is raw text.
|
|
365
|
+
*/
|
|
366
|
+
function is_in_raw_text_element(node) {
|
|
367
|
+
const name = node.parent?.openingElement?.name;
|
|
368
|
+
return name?.type === "JSXIdentifier" && (name.name === "style" || name.name === "script");
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* TSRX: whether an expression container or an attribute holds `element`, at any
|
|
372
|
+
* depth. A cut parsed on its own doesn't reproduce that context.
|
|
373
|
+
*/
|
|
374
|
+
function is_in_expression(element) {
|
|
375
|
+
for (let node = element.parent; node; node = node.parent) if (node.type === "JSXExpressionContainer" || node.type === "JSXAttribute") return true;
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
/** TSRX: the nearest child before (`step` -1) or after (1) `index` that isn't text. */
|
|
379
|
+
function non_text_sibling(children, index, step) {
|
|
380
|
+
let at = index + step;
|
|
381
|
+
while (children[at]?.type === "JSXText") at += step;
|
|
382
|
+
return at;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* TSRX: whether unwrapping the child `node` into `replacement` could read as
|
|
386
|
+
* TSRX syntax:
|
|
387
|
+
*
|
|
388
|
+
* - its text, joined with the text beside it, has `@`, `/`, `*` or `&`;
|
|
389
|
+
* - a comment follows the braces directly;
|
|
390
|
+
* - the nearest sibling before it that isn't text is a directive, whose clause
|
|
391
|
+
* the text could continue;
|
|
392
|
+
* - a `<style>` or `<script>` holds it;
|
|
393
|
+
* - inside an expression container, the element holds a directive.
|
|
394
|
+
*
|
|
395
|
+
* Anything else reads as the same text in TSRX as in JSX.
|
|
396
|
+
*/
|
|
397
|
+
function may_read_as_tsrx_syntax(node, replacement, sourceCode) {
|
|
398
|
+
if (is_in_raw_text_element(node)) return true;
|
|
399
|
+
const children = node.parent.children;
|
|
400
|
+
const index = children.indexOf(node);
|
|
401
|
+
const text_of = (child) => child?.type === "JSXText" ? sourceCode.getText(child) : "";
|
|
402
|
+
const run = text_of(children[index - 1]) + replacement + text_of(children[index + 1]);
|
|
403
|
+
return /[@/*&]/.test(run) || sourceCode.text[node.range[1]] === "/" || DIRECTIVE_TYPES.has(children[non_text_sibling(children, index, -1)]?.type) || is_in_expression(node.parent) && children.some((child) => DIRECTIVE_TYPES.has(child.type));
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* TSRX: a parse function from the file's parser, or `null`. A parser with only
|
|
407
|
+
* `parse` gives no diagnostics.
|
|
408
|
+
*/
|
|
409
|
+
function parser_of(context) {
|
|
410
|
+
const parser = context.languageOptions?.parser;
|
|
411
|
+
const options = {
|
|
412
|
+
...context.languageOptions.parserOptions,
|
|
413
|
+
filePath: context.filename
|
|
414
|
+
};
|
|
415
|
+
if (typeof parser?.parseForESLint === "function") return (code) => parser.parseForESLint(code, options);
|
|
416
|
+
if (typeof parser?.parse === "function") return (code) => ({ ast: parser.parse(code, options) });
|
|
417
|
+
return null;
|
|
418
|
+
}
|
|
419
|
+
/** TSRX: each child's type, with adjacent text joined. */
|
|
420
|
+
function children_shape(children) {
|
|
421
|
+
const out = [];
|
|
422
|
+
for (const child of children) {
|
|
423
|
+
const previous = out.at(-1);
|
|
424
|
+
if (child.type === "JSXText" && previous?.type === "JSXText") previous.value += child.value;
|
|
425
|
+
else out.push({
|
|
426
|
+
type: child.type,
|
|
427
|
+
value: child.type === "JSXText" ? child.value : void 0
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
return JSON.stringify(out);
|
|
431
|
+
}
|
|
432
|
+
/** TSRX: the comments inside `range`. */
|
|
433
|
+
function count_comments(comments, range) {
|
|
434
|
+
return (comments ?? []).filter((comment) => comment.range[0] >= range[0] && comment.range[1] <= range[1]).length;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* TSRX: whether unwrapping the child `node` into `replacement`, which parses as
|
|
438
|
+
* a `type` child, keeps the element around it as it was: its comments, its
|
|
439
|
+
* diagnostics and its children, with the new text joined to the text beside
|
|
440
|
+
* it. The parser of the file reads the element before and after the edit:
|
|
441
|
+
*
|
|
442
|
+
* - usually only a cut of it, from the nearest child before `node` that isn't
|
|
443
|
+
* text to the nearest one after, in a bare tag of its own;
|
|
444
|
+
* - the top-level statement around the element when it sits in an expression
|
|
445
|
+
* container or an attribute, a context the cut can't reproduce. Parsed on
|
|
446
|
+
* its own, a top-level statement reads as it does in the file. A file gets
|
|
447
|
+
* `STATEMENT_PARSE_LIMIT` of these, and past them the braces stay.
|
|
448
|
+
*
|
|
449
|
+
* Without a parser, and for the first child of a `<style>`, the braces stay.
|
|
450
|
+
*/
|
|
451
|
+
function unwraps_to_the_same_children(context, state, node, replacement, type) {
|
|
452
|
+
const parse = parser_of(context);
|
|
453
|
+
if (!parse) return false;
|
|
454
|
+
const text = context.sourceCode.text;
|
|
455
|
+
const element = node.parent;
|
|
456
|
+
const children = element.children;
|
|
457
|
+
const index = children.indexOf(node);
|
|
458
|
+
if (is_in_raw_text_element(node) && children.slice(0, index).every((child) => child.type === "JSXText" && !child.value.trim())) return false;
|
|
459
|
+
const expected = (nodes, at) => children_shape(nodes.map((child) => child.range[0] === at ? {
|
|
460
|
+
type,
|
|
461
|
+
value: type === "JSXText" ? replacement : void 0
|
|
462
|
+
} : child));
|
|
463
|
+
const read = (code) => {
|
|
464
|
+
try {
|
|
465
|
+
const result = parse(code);
|
|
466
|
+
return {
|
|
467
|
+
ast: result.ast,
|
|
468
|
+
diagnostics: result.services?.tsrxDiagnostics?.length ?? 0
|
|
469
|
+
};
|
|
470
|
+
} catch {
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
if (is_in_expression(element)) {
|
|
475
|
+
if (state.statement_parses >= 50) return false;
|
|
476
|
+
state.statement_parses++;
|
|
477
|
+
let statement = element;
|
|
478
|
+
while (statement.parent && statement.parent.type !== "Program") statement = statement.parent;
|
|
479
|
+
const [statement_start, statement_end] = statement.range;
|
|
480
|
+
let before = state.statements.get(statement);
|
|
481
|
+
if (before === void 0) {
|
|
482
|
+
before = read(text.slice(statement_start, statement_end));
|
|
483
|
+
state.statements.set(statement, before);
|
|
484
|
+
}
|
|
485
|
+
const after = read(text.slice(statement_start, node.range[0]) + replacement + text.slice(node.range[1], statement_end));
|
|
486
|
+
const element_start = element.range[0] - statement_start;
|
|
487
|
+
const before_element = before && find_element(before.ast.body, element_start);
|
|
488
|
+
const after_element = after && find_element(after.ast.body, element_start);
|
|
489
|
+
if (!before_element || !after_element) return false;
|
|
490
|
+
return after_element.type === before_element.type && count_comments(after.ast.comments, after_element.range) === count_comments(before.ast.comments, before_element.range) && after.diagnostics === before.diagnostics && children_shape(after_element.children) === expected(before_element.children, node.range[0] - statement_start);
|
|
491
|
+
}
|
|
492
|
+
const [start, end] = neighborhood(node);
|
|
493
|
+
const name = element.type === "JSXFragment" ? "" : text.slice(...element.openingElement.name.range);
|
|
494
|
+
const open = `<${name}>` + (is_in_raw_text_element(node) ? "{0}" : "");
|
|
495
|
+
const close = `</${name}>`;
|
|
496
|
+
const prefix = "export const tsrx = " + open;
|
|
497
|
+
const suffix = close + ";\n";
|
|
498
|
+
const root = (result) => result?.ast.body[0]?.declaration?.declarations?.[0]?.init;
|
|
499
|
+
const before = read(prefix + text.slice(start, end) + suffix);
|
|
500
|
+
const after = read(prefix + text.slice(start, node.range[0]) + replacement + text.slice(node.range[1], end) + suffix);
|
|
501
|
+
const before_root = root(before);
|
|
502
|
+
const after_root = root(after);
|
|
503
|
+
if (!before_root?.children || !after_root?.children) return false;
|
|
504
|
+
return after_root.type === before_root.type && after.ast.comments?.length === before.ast.comments?.length && after.diagnostics === before.diagnostics && children_shape(after_root.children) === expected(before_root.children, node.range[0] - start + prefix.length);
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* TSRX: the source range from the nearest child before `node` that isn't text
|
|
508
|
+
* to the nearest one after, or to the start or end of the element's children.
|
|
509
|
+
*/
|
|
510
|
+
function neighborhood(node) {
|
|
511
|
+
const element = node.parent;
|
|
512
|
+
const children = element.children;
|
|
513
|
+
const index = children.indexOf(node);
|
|
514
|
+
const first = non_text_sibling(children, index, -1);
|
|
515
|
+
const last = non_text_sibling(children, index, 1);
|
|
516
|
+
return [first >= 0 ? children[first].range[0] : (element.openingElement ?? element.openingFragment).range[1], last < children.length ? children[last].range[1] : (element.closingElement ?? element.closingFragment).range[0]];
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* TSRX: the source range from the end of the nearest child before `node` that
|
|
520
|
+
* isn't text to the start of the nearest one after, or to the start or end of
|
|
521
|
+
* the element's children.
|
|
522
|
+
*/
|
|
523
|
+
function between_neighbors(node) {
|
|
524
|
+
const element = node.parent;
|
|
525
|
+
const children = element.children;
|
|
526
|
+
const index = children.indexOf(node);
|
|
527
|
+
const first = non_text_sibling(children, index, -1);
|
|
528
|
+
const last = non_text_sibling(children, index, 1);
|
|
529
|
+
return [first >= 0 ? children[first].range[1] : (element.openingElement ?? element.openingFragment).range[1], last < children.length ? children[last].range[0] : (element.closingElement ?? element.closingFragment).range[0]];
|
|
530
|
+
}
|
|
531
|
+
const ELEMENT_TYPES = /* @__PURE__ */ new Set([
|
|
532
|
+
"JSXElement",
|
|
533
|
+
"JSXFragment",
|
|
534
|
+
"JSXStyleElement"
|
|
535
|
+
]);
|
|
536
|
+
/** TSRX: the first element or fragment in `node` that starts at `start`. */
|
|
537
|
+
function find_element(node, start) {
|
|
538
|
+
if (!node || typeof node !== "object") return null;
|
|
539
|
+
if (Array.isArray(node)) {
|
|
540
|
+
for (const child of node) {
|
|
541
|
+
const found = find_element(child, start);
|
|
542
|
+
if (found) return found;
|
|
543
|
+
}
|
|
544
|
+
return null;
|
|
545
|
+
}
|
|
546
|
+
if (node.range && (node.range[1] < start || node.range[0] > start)) return null;
|
|
547
|
+
if (ELEMENT_TYPES.has(node.type) && node.range?.[0] === start) return node;
|
|
548
|
+
for (const key of Object.keys(node)) {
|
|
549
|
+
if (key === "parent" || key === "loc" || key === "range" || key === "metadata") continue;
|
|
550
|
+
const found = find_element(node[key], start);
|
|
551
|
+
if (found) return found;
|
|
552
|
+
}
|
|
553
|
+
return null;
|
|
554
|
+
}
|
|
555
|
+
const rule$3 = {
|
|
556
|
+
meta: {
|
|
557
|
+
type: "suggestion",
|
|
558
|
+
docs: { description: "Disallow unnecessary JSX expressions when literals alone are sufficient or enforce JSX expressions on literals in JSX children or attributes, without unwrapping text that TSRX reads as syntax" },
|
|
559
|
+
fixable: "code",
|
|
560
|
+
messages: {
|
|
561
|
+
unnecessaryCurly: "Curly braces are unnecessary here.",
|
|
562
|
+
missingCurly: "Need to wrap this literal in a JSX expression."
|
|
563
|
+
},
|
|
564
|
+
schema: [{ anyOf: [{
|
|
565
|
+
type: "object",
|
|
566
|
+
properties: {
|
|
567
|
+
props: { enum: OPTION_VALUES },
|
|
568
|
+
children: { enum: OPTION_VALUES },
|
|
569
|
+
propElementValues: { enum: OPTION_VALUES }
|
|
570
|
+
},
|
|
571
|
+
additionalProperties: false
|
|
572
|
+
}, { enum: OPTION_VALUES }] }]
|
|
573
|
+
},
|
|
574
|
+
create(context) {
|
|
575
|
+
const sourceCode = context.sourceCode;
|
|
576
|
+
const getText = (node) => sourceCode.getText(node);
|
|
577
|
+
const state = {
|
|
578
|
+
statement_parses: 0,
|
|
579
|
+
statements: /* @__PURE__ */ new Map()
|
|
580
|
+
};
|
|
581
|
+
const ruleOptions = context.options[0];
|
|
582
|
+
const userConfig = typeof ruleOptions === "string" ? {
|
|
583
|
+
props: ruleOptions,
|
|
584
|
+
children: ruleOptions,
|
|
585
|
+
propElementValues: OPTION_IGNORE
|
|
586
|
+
} : Object.assign({}, DEFAULT_CONFIG, ruleOptions);
|
|
587
|
+
/**
|
|
588
|
+
* Report and fix an unnecessary curly brace violation on a node
|
|
589
|
+
*/
|
|
590
|
+
function reportUnnecessaryCurly(JSXExpressionNode) {
|
|
591
|
+
context.report({
|
|
592
|
+
node: JSXExpressionNode,
|
|
593
|
+
messageId: "unnecessaryCurly",
|
|
594
|
+
fix(fixer) {
|
|
595
|
+
const expression = JSXExpressionNode.expression;
|
|
596
|
+
let textToReplace;
|
|
597
|
+
if (isJSX(expression)) textToReplace = getText(expression);
|
|
598
|
+
else {
|
|
599
|
+
const expressionType = expression && expression.type;
|
|
600
|
+
if (JSXExpressionNode.parent.type === "JSXAttribute") {
|
|
601
|
+
if (expressionType !== "TemplateLiteral" && /["]/.test(expression.raw.slice(1, -1))) textToReplace = expression.raw;
|
|
602
|
+
else textToReplace = `"${expressionType === "TemplateLiteral" ? expression.quasis[0].value.raw : expression.raw.slice(1, -1)}"`;
|
|
603
|
+
} else if (isJSX(expression)) textToReplace = getText(expression);
|
|
604
|
+
else textToReplace = expressionType === "TemplateLiteral" ? expression.quasis[0].value.cooked : expression.value;
|
|
605
|
+
}
|
|
606
|
+
if (JSXExpressionNode.parent.type !== "JSXAttribute") {
|
|
607
|
+
const [start, end] = between_neighbors(JSXExpressionNode);
|
|
608
|
+
const [node_start, node_end] = JSXExpressionNode.range;
|
|
609
|
+
return fixer.replaceTextRange([start, end], sourceCode.text.slice(start, node_start) + textToReplace + sourceCode.text.slice(node_end, end));
|
|
610
|
+
}
|
|
611
|
+
return fixer.replaceText(JSXExpressionNode, textToReplace);
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
function reportMissingCurly(literalNode) {
|
|
616
|
+
context.report({
|
|
617
|
+
node: literalNode,
|
|
618
|
+
messageId: "missingCurly",
|
|
619
|
+
fix(fixer) {
|
|
620
|
+
if (isJSX(literalNode)) return fixer.replaceText(literalNode, `{${getText(literalNode)}}`);
|
|
621
|
+
if (containsOnlyHtmlEntities(literalNode.raw) || literalNode.parent.type === "JSXAttribute" && containsLineTerminators(literalNode.raw) || isLineBreak(literalNode.raw)) return null;
|
|
622
|
+
const expression = literalNode.parent.type === "JSXAttribute" ? `{"${escapeDoubleQuotes(escapeBackslashes(literalNode.raw.slice(1, -1)))}"}` : wrapWithCurlyBraces(literalNode.raw);
|
|
623
|
+
return fixer.replaceText(literalNode, expression);
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
function lintUnnecessaryCurly(JSXExpressionNode) {
|
|
628
|
+
const expression = JSXExpressionNode.expression;
|
|
629
|
+
const expressionType = expression.type;
|
|
630
|
+
if (sourceCode.getCommentsInside(JSXExpressionNode).length > 0) return;
|
|
631
|
+
const keepsTsrxBraces = (replacement, type) => JSXExpressionNode.parent.type !== "JSXAttribute" && may_read_as_tsrx_syntax(JSXExpressionNode, replacement, sourceCode) && !unwraps_to_the_same_children(context, state, JSXExpressionNode, replacement, type);
|
|
632
|
+
if ((expressionType === "Literal" || expressionType === "JSXText") && typeof expression.value === "string" && (JSXExpressionNode.parent.type === "JSXAttribute" && !isWhiteSpaceLiteral(expression) || !isLiteralWithTrailingWhiteSpaces(expression)) && !containsMultilineComment(expression.value) && !needToEscapeCharacterForJSX(expression.raw, JSXExpressionNode) && (isJSX(JSXExpressionNode.parent) || !containsQuoteCharacters(expression.value) || typeof expression.value === "string")) {
|
|
633
|
+
if (keepsTsrxBraces(expression.value, "JSXText")) return;
|
|
634
|
+
reportUnnecessaryCurly(JSXExpressionNode);
|
|
635
|
+
} else if (expressionType === "TemplateLiteral" && expression.expressions.length === 0 && expression.quasis[0].value.raw.indexOf("\n") === -1 && !isStringWithTrailingWhiteSpaces(expression.quasis[0].value.raw) && !needToEscapeCharacterForJSX(expression.quasis[0].value.raw, JSXExpressionNode) && !containsQuoteCharacters(expression.quasis[0].value.cooked)) {
|
|
636
|
+
if (keepsTsrxBraces(expression.quasis[0].value.cooked, "JSXText")) return;
|
|
637
|
+
reportUnnecessaryCurly(JSXExpressionNode);
|
|
638
|
+
} else if (isJSX(expression)) {
|
|
639
|
+
if (keepsTsrxBraces(getText(expression), expression.type)) return;
|
|
640
|
+
reportUnnecessaryCurly(JSXExpressionNode);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
function areRuleConditionsSatisfied(parent, config, ruleCondition) {
|
|
644
|
+
return parent.type === "JSXAttribute" && typeof config.props === "string" && config.props === ruleCondition || isJSX(parent) && typeof config.children === "string" && config.children === ruleCondition;
|
|
645
|
+
}
|
|
646
|
+
function getAdjacentSiblings(node, children) {
|
|
647
|
+
for (let i = 1; i < children.length - 1; i++) if (node === children[i]) return [children[i - 1], children[i + 1]];
|
|
648
|
+
if (node === children[0] && children[1]) return [children[1]];
|
|
649
|
+
if (node === children[children.length - 1] && children[children.length - 2]) return [children[children.length - 2]];
|
|
650
|
+
return [];
|
|
651
|
+
}
|
|
652
|
+
function hasAdjacentJsxExpressionContainers(node, children) {
|
|
653
|
+
if (!children) return false;
|
|
654
|
+
return getAdjacentSiblings(node, children.filter((child) => !isWhiteSpaceLiteral(child))).some((x) => x.type && x.type === "JSXExpressionContainer");
|
|
655
|
+
}
|
|
656
|
+
function hasAdjacentJsx(node, children) {
|
|
657
|
+
if (!children) return false;
|
|
658
|
+
return getAdjacentSiblings(node, children.filter((child) => !isWhiteSpaceLiteral(child))).some((x) => x.type && (x.type === "JSXExpressionContainer" || x.type === "JSXElement"));
|
|
659
|
+
}
|
|
660
|
+
function shouldCheckForUnnecessaryCurly(node, config) {
|
|
661
|
+
const parent = node.parent;
|
|
662
|
+
if (parent.type && parent.type === "JSXAttribute" && node.expression && node.expression.type && node.expression.type !== "Literal" && node.expression.type !== "StringLiteral" && node.expression.type !== "TemplateLiteral") return false;
|
|
663
|
+
if (isJSX(parent) && hasAdjacentJsxExpressionContainers(node, parent.children)) return false;
|
|
664
|
+
if (containsWhitespaceExpression(node) && hasAdjacentJsx(node, parent.children)) return false;
|
|
665
|
+
if (parent.children && parent.children.length === 1 && containsWhitespaceExpression(node)) return false;
|
|
666
|
+
return areRuleConditionsSatisfied(parent, config, OPTION_NEVER);
|
|
667
|
+
}
|
|
668
|
+
function shouldCheckForMissingCurly(node, config) {
|
|
669
|
+
if (isJSX(node)) return config.propElementValues !== OPTION_IGNORE;
|
|
670
|
+
if (isLineBreak(node.raw) || containsOnlyHtmlEntities(node.raw)) return false;
|
|
671
|
+
const parent = node.parent;
|
|
672
|
+
if (parent.children && parent.children.length === 1 && containsWhitespaceExpression(parent.children[0])) return false;
|
|
673
|
+
if (is_in_raw_text_element(node)) return false;
|
|
674
|
+
return areRuleConditionsSatisfied(parent, config, OPTION_ALWAYS);
|
|
675
|
+
}
|
|
676
|
+
return {
|
|
677
|
+
"JSXAttribute > JSXExpressionContainer > JSXElement"(node) {
|
|
678
|
+
if (userConfig.propElementValues === OPTION_NEVER) reportUnnecessaryCurly(node.parent);
|
|
679
|
+
},
|
|
680
|
+
JSXExpressionContainer(node) {
|
|
681
|
+
if (shouldCheckForUnnecessaryCurly(node, userConfig)) lintUnnecessaryCurly(node);
|
|
682
|
+
},
|
|
683
|
+
"JSXAttribute > JSXElement, Literal, JSXText"(node) {
|
|
684
|
+
if (shouldCheckForMissingCurly(node, userConfig)) reportMissingCurly(node);
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
//#endregion
|
|
690
|
+
//#region src/utils/core-rule.ts
|
|
691
|
+
/**
|
|
692
|
+
* An ESLint core rule, for a TSRX rule that wraps it.
|
|
693
|
+
*/
|
|
694
|
+
function get_core_rule(name) {
|
|
695
|
+
const rule = builtinRules.get(name);
|
|
696
|
+
if (!rule) throw new Error(`ESLint has no core rule named '${name}'.`);
|
|
697
|
+
return rule;
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* The core rule's meta, with the TSRX rule's description. No wrapper is in the
|
|
701
|
+
* `recommended` configuration: `tsrx.configs.js` swaps them in.
|
|
702
|
+
*/
|
|
703
|
+
function wrapper_meta(core, description) {
|
|
704
|
+
return {
|
|
705
|
+
...core.meta,
|
|
706
|
+
docs: {
|
|
707
|
+
...core.meta?.docs,
|
|
708
|
+
description,
|
|
709
|
+
recommended: false
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* The rule context, with its `report` replaced.
|
|
715
|
+
*/
|
|
716
|
+
function with_report(context, report) {
|
|
717
|
+
return Object.create(context, { report: { value: report } });
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Both rules' listeners, the first's running first where both listen to an
|
|
721
|
+
* event.
|
|
722
|
+
*/
|
|
723
|
+
function merge_listeners(first, second) {
|
|
724
|
+
const merged = { ...first };
|
|
725
|
+
for (const [key, listener] of Object.entries(second)) {
|
|
726
|
+
const existing = first[key];
|
|
727
|
+
merged[key] = existing ? (...args) => {
|
|
728
|
+
existing(...args);
|
|
729
|
+
listener(...args);
|
|
730
|
+
} : listener;
|
|
731
|
+
}
|
|
732
|
+
return merged;
|
|
733
|
+
}
|
|
734
|
+
//#endregion
|
|
735
|
+
//#region src/rules/no-unused-vars.ts
|
|
736
|
+
const core$2 = get_core_rule("no-unused-vars");
|
|
737
|
+
/**
|
|
738
|
+
* Core `no-unused-vars`. Its suggestion for an unused `@for` index deleted
|
|
739
|
+
* only the name, which left `index )`: here it deletes the `; index i` clause.
|
|
740
|
+
*/
|
|
741
|
+
const rule$2 = {
|
|
742
|
+
meta: wrapper_meta(core$2, "Disallow unused variables, with a suggestion that removes an unused `@for` index clause"),
|
|
743
|
+
create(context) {
|
|
744
|
+
return core$2.create(with_report(context, (descriptor) => {
|
|
745
|
+
const node = descriptor.node;
|
|
746
|
+
const parent = node?.parent;
|
|
747
|
+
if (!node || parent?.type !== "JSXForExpression" || parent.index !== node) {
|
|
748
|
+
context.report(descriptor);
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
context.report({
|
|
752
|
+
...descriptor,
|
|
753
|
+
suggest: descriptor.suggest?.map((suggestion) => ({
|
|
754
|
+
...suggestion,
|
|
755
|
+
fix: (fixer) => remove_index_clause(context.sourceCode, fixer, node)
|
|
756
|
+
}))
|
|
757
|
+
});
|
|
758
|
+
}));
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
/**
|
|
762
|
+
* Removes `; index i`, from the `;` before `index` to the end of the name.
|
|
763
|
+
*/
|
|
764
|
+
function remove_index_clause(source_code, fixer, index) {
|
|
765
|
+
const keyword = source_code.getTokenBefore(index);
|
|
766
|
+
const semicolon = keyword && source_code.getTokenBefore(keyword);
|
|
767
|
+
if (keyword?.value !== "index" || semicolon?.value !== ";") return null;
|
|
768
|
+
return fixer.removeRange([semicolon.range[0], index.range[1]]);
|
|
769
|
+
}
|
|
770
|
+
//#endregion
|
|
771
|
+
//#region src/rules/no-await-in-loop.ts
|
|
772
|
+
const core$1 = get_core_rule("no-await-in-loop");
|
|
773
|
+
const FUNCTION_TYPES$1 = /* @__PURE__ */ new Set([
|
|
774
|
+
"FunctionDeclaration",
|
|
775
|
+
"FunctionExpression",
|
|
776
|
+
"ArrowFunctionExpression"
|
|
777
|
+
]);
|
|
778
|
+
/**
|
|
779
|
+
* Core `no-await-in-loop`, which also reports an `await` in a `@for` key: the
|
|
780
|
+
* key runs once per item, as the body does.
|
|
781
|
+
*/
|
|
782
|
+
const rule$1 = {
|
|
783
|
+
meta: wrapper_meta(core$1, "Disallow `await` inside of loops, including the key of a `@for` loop"),
|
|
784
|
+
create(context) {
|
|
785
|
+
const reported = /* @__PURE__ */ new Set();
|
|
786
|
+
return merge_listeners(core$1.create(with_report(context, (descriptor) => {
|
|
787
|
+
reported.add(descriptor.node);
|
|
788
|
+
context.report(descriptor);
|
|
789
|
+
})), { AwaitExpression(node) {
|
|
790
|
+
if (!reported.has(node) && is_in_for_key(node)) context.report({
|
|
791
|
+
node,
|
|
792
|
+
messageId: "unexpectedAwait"
|
|
793
|
+
});
|
|
794
|
+
} });
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
function is_in_for_key(node) {
|
|
798
|
+
for (let child = node, parent = node.parent; parent && !FUNCTION_TYPES$1.has(parent.type); child = parent, parent = parent.parent) if (parent.type === "JSXForExpression" && parent.key === child) return true;
|
|
799
|
+
return false;
|
|
800
|
+
}
|
|
801
|
+
//#endregion
|
|
802
|
+
//#region src/rules/no-unreachable.ts
|
|
803
|
+
const core = get_core_rule("no-unreachable");
|
|
804
|
+
const FUNCTION_TYPES = /* @__PURE__ */ new Set([
|
|
805
|
+
"FunctionDeclaration",
|
|
806
|
+
"FunctionExpression",
|
|
807
|
+
"ArrowFunctionExpression"
|
|
808
|
+
]);
|
|
809
|
+
const LOOP_TYPES = [
|
|
810
|
+
"WhileStatement",
|
|
811
|
+
"DoWhileStatement",
|
|
812
|
+
"ForStatement",
|
|
813
|
+
"ForInStatement",
|
|
814
|
+
"ForOfStatement"
|
|
815
|
+
];
|
|
816
|
+
const SEGMENT_EVENTS = [
|
|
817
|
+
"onCodePathSegmentStart",
|
|
818
|
+
"onUnreachableCodePathSegmentStart",
|
|
819
|
+
"onCodePathSegmentEnd",
|
|
820
|
+
"onUnreachableCodePathSegmentEnd"
|
|
821
|
+
];
|
|
822
|
+
/** The expressions code path analysis counts as able to throw, with identifier references. */
|
|
823
|
+
const THROWING_TYPES = [
|
|
824
|
+
"CallExpression",
|
|
825
|
+
"NewExpression",
|
|
826
|
+
"MemberExpression",
|
|
827
|
+
"ImportExpression"
|
|
828
|
+
];
|
|
829
|
+
/**
|
|
830
|
+
* Core `no-unreachable`, with the arms of a `@switch` as alternatives.
|
|
831
|
+
*
|
|
832
|
+
* ESLint's code path analysis knows only the standard node types, so it runs
|
|
833
|
+
* the arms of a `@switch` one after another: after an arm that throws, it
|
|
834
|
+
* marks the other arms and the code after the `@switch` unreachable. The core
|
|
835
|
+
* rule here sees each segment through a proxy whose `reachable` revives those
|
|
836
|
+
* paths: at the start of each arm of a reached `@switch`, and after the
|
|
837
|
+
* `@switch` when an arm reaches its end or there is no `@default` arm.
|
|
838
|
+
*
|
|
839
|
+
* Code path analysis records nothing for an exit inside code it already holds
|
|
840
|
+
* unreachable, so in a revived path this rule follows `throw`, `return`,
|
|
841
|
+
* `break`, `continue`, `try` and endless loops itself. Elsewhere, every answer
|
|
842
|
+
* is the core rule's.
|
|
843
|
+
*/
|
|
844
|
+
const rule = {
|
|
845
|
+
meta: wrapper_meta(core, "Disallow unreachable code after `return`, `throw`, `continue`, and `break` statements, with the arms of a `@switch` as alternatives"),
|
|
846
|
+
create(context) {
|
|
847
|
+
const listeners = core.create(context);
|
|
848
|
+
const revived = /* @__PURE__ */ new WeakSet();
|
|
849
|
+
const exited = /* @__PURE__ */ new WeakSet();
|
|
850
|
+
const answers = /* @__PURE__ */ new Map();
|
|
851
|
+
const outer_segments = [];
|
|
852
|
+
let current = /* @__PURE__ */ new Set();
|
|
853
|
+
const switches = /* @__PURE__ */ new Map();
|
|
854
|
+
const tries = /* @__PURE__ */ new Map();
|
|
855
|
+
const outer_try_stacks = [];
|
|
856
|
+
let try_stack = [];
|
|
857
|
+
const loops = /* @__PURE__ */ new WeakSet();
|
|
858
|
+
const broken = /* @__PURE__ */ new WeakSet();
|
|
859
|
+
const continued = /* @__PURE__ */ new WeakSet();
|
|
860
|
+
const proxies = /* @__PURE__ */ new WeakMap();
|
|
861
|
+
function is_reachable(segment, seen) {
|
|
862
|
+
const known = answers.get(segment);
|
|
863
|
+
if (known !== void 0) return known;
|
|
864
|
+
if (segment.reachable || revived.has(segment)) return true;
|
|
865
|
+
if (exited.has(segment) || seen?.has(segment)) return false;
|
|
866
|
+
const top = !seen;
|
|
867
|
+
seen ??= /* @__PURE__ */ new Set();
|
|
868
|
+
seen.add(segment);
|
|
869
|
+
const previous = segment.allPrevSegments;
|
|
870
|
+
const result = !previous.some((segment) => segment.reachable) && previous.some((segment) => is_reachable(segment, seen));
|
|
871
|
+
if (result || top) answers.set(segment, result);
|
|
872
|
+
return result;
|
|
873
|
+
}
|
|
874
|
+
const any_reachable = () => [...current].some((segment) => is_reachable(segment));
|
|
875
|
+
const in_revived_path = () => [...current].some((segment) => !segment.reachable && is_reachable(segment));
|
|
876
|
+
const revive = () => {
|
|
877
|
+
for (const segment of current) {
|
|
878
|
+
answers.delete(segment);
|
|
879
|
+
exited.delete(segment);
|
|
880
|
+
revived.add(segment);
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
const exit = () => {
|
|
884
|
+
for (const segment of current) {
|
|
885
|
+
if (segment.reachable) continue;
|
|
886
|
+
answers.delete(segment);
|
|
887
|
+
revived.delete(segment);
|
|
888
|
+
exited.add(segment);
|
|
889
|
+
}
|
|
890
|
+
};
|
|
891
|
+
const proxy = (segment) => {
|
|
892
|
+
let result = proxies.get(segment);
|
|
893
|
+
if (!result) {
|
|
894
|
+
result = { get reachable() {
|
|
895
|
+
return is_reachable(segment);
|
|
896
|
+
} };
|
|
897
|
+
proxies.set(segment, result);
|
|
898
|
+
}
|
|
899
|
+
return result;
|
|
900
|
+
};
|
|
901
|
+
const wrapped = {
|
|
902
|
+
onCodePathStart(...args) {
|
|
903
|
+
outer_segments.push(current);
|
|
904
|
+
current = /* @__PURE__ */ new Set();
|
|
905
|
+
outer_try_stacks.push(try_stack);
|
|
906
|
+
try_stack = [];
|
|
907
|
+
listeners.onCodePathStart?.(...args);
|
|
908
|
+
},
|
|
909
|
+
onCodePathEnd(...args) {
|
|
910
|
+
current = outer_segments.pop();
|
|
911
|
+
try_stack = outer_try_stacks.pop();
|
|
912
|
+
listeners.onCodePathEnd?.(...args);
|
|
913
|
+
},
|
|
914
|
+
onCodePathSegmentLoop() {
|
|
915
|
+
answers.clear();
|
|
916
|
+
},
|
|
917
|
+
BlockStatement(node) {
|
|
918
|
+
const parent = node.parent;
|
|
919
|
+
const state = tries.get(parent);
|
|
920
|
+
if (parent.finalizer === node && state && (state.block || state.handler || state.throws) && !any_reachable()) revive();
|
|
921
|
+
listeners.BlockStatement(node);
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
for (const event of SEGMENT_EVENTS) {
|
|
925
|
+
const starts = event.endsWith("Start");
|
|
926
|
+
wrapped[event] = (segment, node) => {
|
|
927
|
+
if (starts) current.add(segment);
|
|
928
|
+
else current.delete(segment);
|
|
929
|
+
listeners[event]?.(proxy(segment), node);
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
const may_throw = (throws_to_catch = true) => {
|
|
933
|
+
if (!try_stack.length || !any_reachable()) return;
|
|
934
|
+
for (const state of try_stack) {
|
|
935
|
+
state.throws = true;
|
|
936
|
+
if (throws_to_catch && state.in_block) state.block_throws = true;
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
const leave_loop = (node) => {
|
|
940
|
+
if (broken.has(node) || continued.has(node) && !is_endless(node)) {
|
|
941
|
+
if (!any_reachable()) revive();
|
|
942
|
+
} else if (loops.has(node) && is_endless(node)) exit();
|
|
943
|
+
};
|
|
944
|
+
const leave_breakable = (node) => {
|
|
945
|
+
if (broken.has(node) && !any_reachable()) revive();
|
|
946
|
+
};
|
|
947
|
+
return merge_listeners({
|
|
948
|
+
...listeners,
|
|
949
|
+
...wrapped
|
|
950
|
+
}, {
|
|
951
|
+
...Object.fromEntries(LOOP_TYPES.flatMap((type) => [[type, (node) => {
|
|
952
|
+
if (in_revived_path()) loops.add(node);
|
|
953
|
+
}], [`${type}:exit`, leave_loop]])),
|
|
954
|
+
"SwitchStatement:exit": leave_breakable,
|
|
955
|
+
"LabeledStatement:exit": leave_breakable,
|
|
956
|
+
"BreakStatement:exit"(node) {
|
|
957
|
+
if (!in_revived_path()) return;
|
|
958
|
+
const target = jump_target(node, true);
|
|
959
|
+
if (target) broken.add(target);
|
|
960
|
+
exit();
|
|
961
|
+
},
|
|
962
|
+
"ContinueStatement:exit"(node) {
|
|
963
|
+
if (!in_revived_path()) return;
|
|
964
|
+
const target = jump_target(node, false);
|
|
965
|
+
if (target) continued.add(target);
|
|
966
|
+
exit();
|
|
967
|
+
},
|
|
968
|
+
"DoWhileStatement > .test"(node) {
|
|
969
|
+
if (continued.has(node.parent) && !any_reachable()) revive();
|
|
970
|
+
},
|
|
971
|
+
"ThrowStatement:exit"() {
|
|
972
|
+
may_throw();
|
|
973
|
+
exit();
|
|
974
|
+
},
|
|
975
|
+
"ReturnStatement:exit"() {
|
|
976
|
+
may_throw(false);
|
|
977
|
+
exit();
|
|
978
|
+
},
|
|
979
|
+
...Object.fromEntries(THROWING_TYPES.map((type) => [`${type}:exit`, () => may_throw()])),
|
|
980
|
+
"Identifier:exit"(node) {
|
|
981
|
+
if (is_reference(node)) may_throw();
|
|
982
|
+
},
|
|
983
|
+
TryStatement(node) {
|
|
984
|
+
if (!in_revived_path()) return;
|
|
985
|
+
const state = {
|
|
986
|
+
block_throws: false,
|
|
987
|
+
throws: false,
|
|
988
|
+
in_block: true
|
|
989
|
+
};
|
|
990
|
+
tries.set(node, state);
|
|
991
|
+
try_stack.push(state);
|
|
992
|
+
},
|
|
993
|
+
"BlockStatement:exit"(node) {
|
|
994
|
+
const state = tries.get(node.parent);
|
|
995
|
+
if (!state) return;
|
|
996
|
+
if (node.parent.block === node) {
|
|
997
|
+
state.block = any_reachable();
|
|
998
|
+
state.in_block = false;
|
|
999
|
+
}
|
|
1000
|
+
if (node.parent.finalizer === node) state.finalizer = any_reachable();
|
|
1001
|
+
},
|
|
1002
|
+
CatchClause(node) {
|
|
1003
|
+
const state = tries.get(node.parent);
|
|
1004
|
+
if (state && (state.block_throws || state.block) && !any_reachable()) revive();
|
|
1005
|
+
},
|
|
1006
|
+
"CatchClause:exit"(node) {
|
|
1007
|
+
const state = tries.get(node.parent);
|
|
1008
|
+
if (state) state.handler = any_reachable();
|
|
1009
|
+
},
|
|
1010
|
+
"TryStatement:exit"(node) {
|
|
1011
|
+
const state = tries.get(node);
|
|
1012
|
+
if (!state) return;
|
|
1013
|
+
try_stack.pop();
|
|
1014
|
+
const completes = (state.block || state.handler) && (!node.finalizer || state.finalizer);
|
|
1015
|
+
if (completes && !any_reachable()) revive();
|
|
1016
|
+
else if (!completes && any_reachable()) exit();
|
|
1017
|
+
},
|
|
1018
|
+
JSXSwitchExpression(node) {
|
|
1019
|
+
switches.set(node, {
|
|
1020
|
+
entry: any_reachable(),
|
|
1021
|
+
has_default: node.cases.some((switch_case) => !switch_case.test),
|
|
1022
|
+
completes: false
|
|
1023
|
+
});
|
|
1024
|
+
},
|
|
1025
|
+
JSXSwitchCase(node) {
|
|
1026
|
+
if (switches.get(node.parent).entry && !any_reachable()) revive();
|
|
1027
|
+
},
|
|
1028
|
+
"JSXSwitchCase:exit"(node) {
|
|
1029
|
+
const state = switches.get(node.parent);
|
|
1030
|
+
state.completes ||= any_reachable();
|
|
1031
|
+
},
|
|
1032
|
+
"JSXSwitchExpression:exit"(node) {
|
|
1033
|
+
const state = switches.get(node);
|
|
1034
|
+
if (state.entry && (state.completes || !state.has_default) && !any_reachable()) revive();
|
|
1035
|
+
}
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
};
|
|
1039
|
+
/**
|
|
1040
|
+
* The statement a `break` leaves, or the loop a `continue` repeats.
|
|
1041
|
+
*/
|
|
1042
|
+
function jump_target(node, is_break) {
|
|
1043
|
+
for (let parent = node.parent; parent && !FUNCTION_TYPES.has(parent.type); parent = parent.parent) if (node.label) {
|
|
1044
|
+
if (parent.type === "LabeledStatement" && parent.label.name === node.label.name) return is_break ? parent : parent.body;
|
|
1045
|
+
} else if (LOOP_TYPES.includes(parent.type) || is_break && parent.type === "SwitchStatement") return parent;
|
|
1046
|
+
return null;
|
|
1047
|
+
}
|
|
1048
|
+
/** Whether an identifier is a reference, as code path analysis decides. */
|
|
1049
|
+
function is_reference(node) {
|
|
1050
|
+
const parent = node.parent;
|
|
1051
|
+
switch (parent.type) {
|
|
1052
|
+
case "LabeledStatement":
|
|
1053
|
+
case "BreakStatement":
|
|
1054
|
+
case "ContinueStatement":
|
|
1055
|
+
case "ArrayPattern":
|
|
1056
|
+
case "RestElement":
|
|
1057
|
+
case "ImportSpecifier":
|
|
1058
|
+
case "ImportDefaultSpecifier":
|
|
1059
|
+
case "ImportNamespaceSpecifier":
|
|
1060
|
+
case "CatchClause": return false;
|
|
1061
|
+
case "FunctionDeclaration":
|
|
1062
|
+
case "FunctionExpression":
|
|
1063
|
+
case "ArrowFunctionExpression":
|
|
1064
|
+
case "ClassDeclaration":
|
|
1065
|
+
case "ClassExpression":
|
|
1066
|
+
case "VariableDeclarator": return parent.id !== node;
|
|
1067
|
+
case "Property":
|
|
1068
|
+
case "PropertyDefinition":
|
|
1069
|
+
case "MethodDefinition": return parent.key !== node || parent.computed || parent.shorthand;
|
|
1070
|
+
case "AssignmentPattern": return parent.key !== node;
|
|
1071
|
+
default: return true;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
/** Whether a loop's test is missing or a truthy literal, as code path analysis checks. */
|
|
1075
|
+
function is_endless(node) {
|
|
1076
|
+
if (node.type === "ForStatement" && !node.test) return true;
|
|
1077
|
+
return (node.type === "WhileStatement" || node.type === "DoWhileStatement" || node.type === "ForStatement") && node.test.type === "Literal" && Boolean(node.test.value);
|
|
1078
|
+
}
|
|
240
1079
|
//#endregion
|
|
241
1080
|
//#region src/index.ts
|
|
242
1081
|
const plugin = {
|
|
@@ -245,38 +1084,14 @@ const plugin = {
|
|
|
245
1084
|
version: "0.1.3"
|
|
246
1085
|
},
|
|
247
1086
|
rules: {
|
|
248
|
-
"no-return-in-component": rule$
|
|
249
|
-
"control-flow-jsx": rule$
|
|
250
|
-
"valid-for-of-key": rule$
|
|
251
|
-
"require-statement-container-body":
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
messages: { requireStatementContainerBody: MESSAGE },
|
|
257
|
-
schema: []
|
|
258
|
-
},
|
|
259
|
-
create(context) {
|
|
260
|
-
function check_function(node) {
|
|
261
|
-
if (!node.returnType) return;
|
|
262
|
-
const forgotten_output = get_forgotten_output_statement(node);
|
|
263
|
-
if (!forgotten_output) return;
|
|
264
|
-
const body = node.body;
|
|
265
|
-
context.report({
|
|
266
|
-
node: forgotten_output,
|
|
267
|
-
messageId: "requireStatementContainerBody",
|
|
268
|
-
fix(fixer) {
|
|
269
|
-
return fixer.insertTextBefore(body, "@");
|
|
270
|
-
}
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
return {
|
|
274
|
-
FunctionDeclaration: check_function,
|
|
275
|
-
FunctionExpression: check_function,
|
|
276
|
-
ArrowFunctionExpression: check_function
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
|
-
}
|
|
1087
|
+
"no-return-in-component": rule$7,
|
|
1088
|
+
"control-flow-jsx": rule$6,
|
|
1089
|
+
"valid-for-of-key": rule$5,
|
|
1090
|
+
"require-statement-container-body": rule$4,
|
|
1091
|
+
"jsx-curly-brace-presence": rule$3,
|
|
1092
|
+
"no-unused-vars": rule$2,
|
|
1093
|
+
"no-await-in-loop": rule$1,
|
|
1094
|
+
"no-unreachable": rule
|
|
280
1095
|
},
|
|
281
1096
|
configs: {}
|
|
282
1097
|
};
|
|
@@ -340,6 +1155,26 @@ plugin.configs.strict = [
|
|
|
340
1155
|
]
|
|
341
1156
|
}
|
|
342
1157
|
];
|
|
1158
|
+
plugin.configs.react = [{
|
|
1159
|
+
name: "tsrx/react-tsrx-files",
|
|
1160
|
+
files: ["**/*.tsrx"],
|
|
1161
|
+
plugins: { tsrx: plugin },
|
|
1162
|
+
rules: {
|
|
1163
|
+
"tsrx/jsx-curly-brace-presence": "error",
|
|
1164
|
+
"react/jsx-curly-brace-presence": "off"
|
|
1165
|
+
}
|
|
1166
|
+
}];
|
|
1167
|
+
plugin.configs.js = [{
|
|
1168
|
+
name: "tsrx/js-tsrx-files",
|
|
1169
|
+
files: ["**/*.tsrx"],
|
|
1170
|
+
plugins: { tsrx: plugin },
|
|
1171
|
+
rules: {
|
|
1172
|
+
"no-unused-vars": "off",
|
|
1173
|
+
"tsrx/no-unused-vars": "error",
|
|
1174
|
+
"no-unreachable": "off",
|
|
1175
|
+
"tsrx/no-unreachable": "error"
|
|
1176
|
+
}
|
|
1177
|
+
}];
|
|
343
1178
|
//#endregion
|
|
344
1179
|
export { plugin as default };
|
|
345
1180
|
|