@tsrx/eslint-plugin 0.7.4 → 0.7.6
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 +128 -19
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +490 -87
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +10 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,52 @@
|
|
|
1
1
|
import { createRequire } from "module";
|
|
2
2
|
import { builtinRules } from "eslint/use-at-your-own-risk";
|
|
3
|
+
//#region src/utils/core-rule.ts
|
|
4
|
+
/**
|
|
5
|
+
* An ESLint core rule, for a TSRX rule that wraps it.
|
|
6
|
+
*/
|
|
7
|
+
function get_core_rule(name) {
|
|
8
|
+
const rule = builtinRules.get(name);
|
|
9
|
+
if (!rule) throw new Error(`ESLint has no core rule named '${name}'.`);
|
|
10
|
+
return rule;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The core rule's meta, with the TSRX rule's description. No wrapper is in the
|
|
14
|
+
* `recommended` configuration: `tsrx.configs.js` swaps them in.
|
|
15
|
+
*/
|
|
16
|
+
function wrapper_meta(core, description) {
|
|
17
|
+
return {
|
|
18
|
+
...core.meta,
|
|
19
|
+
docs: {
|
|
20
|
+
...core.meta?.docs,
|
|
21
|
+
description,
|
|
22
|
+
recommended: false
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The rule context, with its `report` replaced.
|
|
28
|
+
*/
|
|
29
|
+
function with_report(context, report) {
|
|
30
|
+
return Object.create(context, { report: { value: report } });
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Both rules' listeners, the first's running first where both listen to an
|
|
34
|
+
* event.
|
|
35
|
+
*/
|
|
36
|
+
function merge_listeners(first, second) {
|
|
37
|
+
const merged = { ...first };
|
|
38
|
+
for (const [key, listener] of Object.entries(second)) {
|
|
39
|
+
const existing = first[key];
|
|
40
|
+
merged[key] = existing ? (...args) => {
|
|
41
|
+
existing(...args);
|
|
42
|
+
listener(...args);
|
|
43
|
+
} : listener;
|
|
44
|
+
}
|
|
45
|
+
return merged;
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
3
48
|
//#region src/rules/no-return-in-component.ts
|
|
4
|
-
const rule$
|
|
49
|
+
const rule$8 = {
|
|
5
50
|
meta: {
|
|
6
51
|
type: "problem",
|
|
7
52
|
docs: {
|
|
@@ -64,7 +109,7 @@ const NESTED_BOUNDARY_TYPES = /* @__PURE__ */ new Set([
|
|
|
64
109
|
"MethodDefinition",
|
|
65
110
|
"PropertyDefinition"
|
|
66
111
|
]);
|
|
67
|
-
const rule$
|
|
112
|
+
const rule$7 = {
|
|
68
113
|
meta: {
|
|
69
114
|
type: "problem",
|
|
70
115
|
docs: {
|
|
@@ -131,7 +176,7 @@ const rule$6 = {
|
|
|
131
176
|
};
|
|
132
177
|
//#endregion
|
|
133
178
|
//#region src/rules/valid-for-of-key.ts
|
|
134
|
-
const rule$
|
|
179
|
+
const rule$6 = {
|
|
135
180
|
meta: {
|
|
136
181
|
type: "problem",
|
|
137
182
|
docs: {
|
|
@@ -205,7 +250,7 @@ function get_forgotten_output_statement(node) {
|
|
|
205
250
|
for (const statement of statements.slice(target_index + 1)) if (!is_ignored_statement(statement)) return null;
|
|
206
251
|
return target;
|
|
207
252
|
}
|
|
208
|
-
const rule$
|
|
253
|
+
const rule$5 = {
|
|
209
254
|
meta: {
|
|
210
255
|
type: "problem",
|
|
211
256
|
docs: { description: "Require @{...} for TSRX component bodies with setup and template output." },
|
|
@@ -552,7 +597,7 @@ function find_element(node, start) {
|
|
|
552
597
|
}
|
|
553
598
|
return null;
|
|
554
599
|
}
|
|
555
|
-
const rule$
|
|
600
|
+
const rule$4 = {
|
|
556
601
|
meta: {
|
|
557
602
|
type: "suggestion",
|
|
558
603
|
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" },
|
|
@@ -687,61 +732,16 @@ const rule$3 = {
|
|
|
687
732
|
}
|
|
688
733
|
};
|
|
689
734
|
//#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
735
|
//#region src/rules/no-unused-vars.ts
|
|
736
|
-
const core$
|
|
736
|
+
const core$3 = get_core_rule("no-unused-vars");
|
|
737
737
|
/**
|
|
738
738
|
* Core `no-unused-vars`. Its suggestion for an unused `@for` index deleted
|
|
739
739
|
* only the name, which left `index )`: here it deletes the `; index i` clause.
|
|
740
740
|
*/
|
|
741
|
-
const rule$
|
|
742
|
-
meta: wrapper_meta(core$
|
|
741
|
+
const rule$3 = {
|
|
742
|
+
meta: wrapper_meta(core$3, "Disallow unused variables, with a suggestion that removes an unused `@for` index clause"),
|
|
743
743
|
create(context) {
|
|
744
|
-
return core$
|
|
744
|
+
return core$3.create(with_report(context, (descriptor) => {
|
|
745
745
|
const node = descriptor.node;
|
|
746
746
|
const parent = node?.parent;
|
|
747
747
|
if (!node || parent?.type !== "JSXForExpression" || parent.index !== node) {
|
|
@@ -769,8 +769,8 @@ function remove_index_clause(source_code, fixer, index) {
|
|
|
769
769
|
}
|
|
770
770
|
//#endregion
|
|
771
771
|
//#region src/rules/no-await-in-loop.ts
|
|
772
|
-
const core$
|
|
773
|
-
const FUNCTION_TYPES$
|
|
772
|
+
const core$2 = get_core_rule("no-await-in-loop");
|
|
773
|
+
const FUNCTION_TYPES$2 = /* @__PURE__ */ new Set([
|
|
774
774
|
"FunctionDeclaration",
|
|
775
775
|
"FunctionExpression",
|
|
776
776
|
"ArrowFunctionExpression"
|
|
@@ -779,11 +779,11 @@ const FUNCTION_TYPES$1 = /* @__PURE__ */ new Set([
|
|
|
779
779
|
* Core `no-await-in-loop`, which also reports an `await` in a `@for` key: the
|
|
780
780
|
* key runs once per item, as the body does.
|
|
781
781
|
*/
|
|
782
|
-
const rule$
|
|
783
|
-
meta: wrapper_meta(core$
|
|
782
|
+
const rule$2 = {
|
|
783
|
+
meta: wrapper_meta(core$2, "Disallow `await` inside of loops, including the key of a `@for` loop"),
|
|
784
784
|
create(context) {
|
|
785
785
|
const reported = /* @__PURE__ */ new Set();
|
|
786
|
-
return merge_listeners(core$
|
|
786
|
+
return merge_listeners(core$2.create(with_report(context, (descriptor) => {
|
|
787
787
|
reported.add(descriptor.node);
|
|
788
788
|
context.report(descriptor);
|
|
789
789
|
})), { AwaitExpression(node) {
|
|
@@ -795,12 +795,36 @@ const rule$1 = {
|
|
|
795
795
|
}
|
|
796
796
|
};
|
|
797
797
|
function is_in_for_key(node) {
|
|
798
|
-
for (let child = node, parent = node.parent; parent && !FUNCTION_TYPES$
|
|
798
|
+
for (let child = node, parent = node.parent; parent && !FUNCTION_TYPES$2.has(parent.type); child = parent, parent = parent.parent) if (parent.type === "JSXForExpression" && parent.key === child) return true;
|
|
799
799
|
return false;
|
|
800
800
|
}
|
|
801
801
|
//#endregion
|
|
802
|
+
//#region src/utils/try-blocks.ts
|
|
803
|
+
const FUNCTION_TYPES$1 = /* @__PURE__ */ new Set([
|
|
804
|
+
"FunctionDeclaration",
|
|
805
|
+
"FunctionExpression",
|
|
806
|
+
"ArrowFunctionExpression"
|
|
807
|
+
]);
|
|
808
|
+
/**
|
|
809
|
+
* Whether code awaits or yields outside a function, as the compiler checks a
|
|
810
|
+
* `@try` body and `@pending`: a block that does runs inline in the function
|
|
811
|
+
* that holds the `@try`, instead of in a callback of the `@try`'s boundaries.
|
|
812
|
+
*/
|
|
813
|
+
function suspension_check(context) {
|
|
814
|
+
const visitor_keys = context.sourceCode.visitorKeys;
|
|
815
|
+
const suspends = (node) => {
|
|
816
|
+
if (!node || typeof node.type !== "string" || FUNCTION_TYPES$1.has(node.type)) return false;
|
|
817
|
+
if (node.type === "AwaitExpression" || node.type === "YieldExpression" || node.type === "ForOfStatement" && node.await || node.type === "VariableDeclaration" && node.kind === "await using") return true;
|
|
818
|
+
return (visitor_keys[node.type] ?? Object.keys(node).filter((key) => key !== "parent" && key !== "leadingComments" && key !== "trailingComments" && !key.startsWith("_"))).some((key) => {
|
|
819
|
+
const value = node[key];
|
|
820
|
+
return Array.isArray(value) ? value.some(suspends) : suspends(value);
|
|
821
|
+
});
|
|
822
|
+
};
|
|
823
|
+
return suspends;
|
|
824
|
+
}
|
|
825
|
+
//#endregion
|
|
802
826
|
//#region src/rules/no-unreachable.ts
|
|
803
|
-
const core = get_core_rule("no-unreachable");
|
|
827
|
+
const core$1 = get_core_rule("no-unreachable");
|
|
804
828
|
const FUNCTION_TYPES = /* @__PURE__ */ new Set([
|
|
805
829
|
"FunctionDeclaration",
|
|
806
830
|
"FunctionExpression",
|
|
@@ -827,34 +851,41 @@ const THROWING_TYPES = [
|
|
|
827
851
|
"ImportExpression"
|
|
828
852
|
];
|
|
829
853
|
/**
|
|
830
|
-
* Core `no-unreachable`, with the arms of a `@switch` as alternatives
|
|
854
|
+
* Core `no-unreachable`, with the arms of a `@switch` as alternatives, and the
|
|
855
|
+
* blocks of a `@try` run apart from the code around it.
|
|
831
856
|
*
|
|
832
857
|
* ESLint's code path analysis knows only the standard node types, so it runs
|
|
833
858
|
* 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.
|
|
835
|
-
*
|
|
836
|
-
*
|
|
837
|
-
*
|
|
859
|
+
* marks the other arms and the code after the `@switch` unreachable. It runs
|
|
860
|
+
* the blocks of a `@try` one after another too, though `@pending` shows while
|
|
861
|
+
* the body suspends, `@catch` renders when the body throws, and all three run
|
|
862
|
+
* in callbacks of the `@try`'s boundaries, while the function goes on past the
|
|
863
|
+
* `@try`. The core rule here sees each segment through a proxy whose
|
|
864
|
+
* `reachable` revives those paths: at the start of each arm of a reached
|
|
865
|
+
* `@switch`, and after the `@switch` when an arm reaches its end or there is no
|
|
866
|
+
* `@default` arm; at the start of `@pending` and `@catch` of a reached `@try`,
|
|
867
|
+
* and after it. A body or `@pending` that awaits or yields runs in the function
|
|
868
|
+
* instead, `@pending` first, so the rest then also needs it to reach its end.
|
|
838
869
|
*
|
|
839
870
|
* Code path analysis records nothing for an exit inside code it already holds
|
|
840
871
|
* unreachable, so in a revived path this rule follows `throw`, `return`,
|
|
841
872
|
* `break`, `continue`, `try` and endless loops itself. Elsewhere, every answer
|
|
842
873
|
* is the core rule's.
|
|
843
874
|
*/
|
|
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"),
|
|
875
|
+
const rule$1 = {
|
|
876
|
+
meta: wrapper_meta(core$1, "Disallow unreachable code after `return`, `throw`, `continue`, and `break` statements, with the arms of a `@switch` as alternatives, and the blocks of a `@try` run apart from the code around it"),
|
|
846
877
|
create(context) {
|
|
847
|
-
const listeners = core.create(context);
|
|
878
|
+
const listeners = core$1.create(context);
|
|
848
879
|
const revived = /* @__PURE__ */ new WeakSet();
|
|
849
880
|
const exited = /* @__PURE__ */ new WeakSet();
|
|
850
881
|
const answers = /* @__PURE__ */ new Map();
|
|
851
882
|
const outer_segments = [];
|
|
852
883
|
let current = /* @__PURE__ */ new Set();
|
|
853
884
|
const switches = /* @__PURE__ */ new Map();
|
|
885
|
+
const try_expressions = /* @__PURE__ */ new Map();
|
|
854
886
|
const tries = /* @__PURE__ */ new Map();
|
|
855
887
|
const outer_try_stacks = [];
|
|
856
888
|
let try_stack = [];
|
|
857
|
-
const loops = /* @__PURE__ */ new WeakSet();
|
|
858
889
|
const broken = /* @__PURE__ */ new WeakSet();
|
|
859
890
|
const continued = /* @__PURE__ */ new WeakSet();
|
|
860
891
|
const proxies = /* @__PURE__ */ new WeakMap();
|
|
@@ -888,6 +919,16 @@ const rule = {
|
|
|
888
919
|
exited.add(segment);
|
|
889
920
|
}
|
|
890
921
|
};
|
|
922
|
+
const revive_try_path = (try_expression, at) => {
|
|
923
|
+
const state = try_expressions.get(try_expression);
|
|
924
|
+
const body_ends = !state.inline_body || state.body_completes;
|
|
925
|
+
const reached = at === "pending" ? state.inline_pending || body_ends : body_ends && (!state.inline_pending || state.pending_completes);
|
|
926
|
+
if (state.entry && reached && !any_reachable()) {
|
|
927
|
+
revive();
|
|
928
|
+
state.pending_first = at === "pending" && !body_ends;
|
|
929
|
+
}
|
|
930
|
+
};
|
|
931
|
+
const suspends = suspension_check(context);
|
|
891
932
|
const proxy = (segment) => {
|
|
892
933
|
let result = proxies.get(segment);
|
|
893
934
|
if (!result) {
|
|
@@ -918,6 +959,7 @@ const rule = {
|
|
|
918
959
|
const parent = node.parent;
|
|
919
960
|
const state = tries.get(parent);
|
|
920
961
|
if (parent.finalizer === node && state && (state.block || state.handler || state.throws) && !any_reachable()) revive();
|
|
962
|
+
if (parent.type === "JSXTryExpression" && parent.pending === node) revive_try_path(parent, "pending");
|
|
921
963
|
listeners.BlockStatement(node);
|
|
922
964
|
}
|
|
923
965
|
};
|
|
@@ -930,16 +972,20 @@ const rule = {
|
|
|
930
972
|
};
|
|
931
973
|
}
|
|
932
974
|
const may_throw = (throws_to_catch = true) => {
|
|
933
|
-
if (!try_stack.length || !
|
|
934
|
-
for (
|
|
975
|
+
if (!try_stack.length || !in_revived_path()) return;
|
|
976
|
+
for (let index = try_stack.length - 1; index >= 0; index--) {
|
|
977
|
+
const state = try_stack[index];
|
|
935
978
|
state.throws = true;
|
|
936
|
-
if (throws_to_catch && state.in_block)
|
|
979
|
+
if (throws_to_catch && state.in_block) {
|
|
980
|
+
state.block_throws = true;
|
|
981
|
+
if (state.has_handler) return;
|
|
982
|
+
}
|
|
937
983
|
}
|
|
938
984
|
};
|
|
939
985
|
const leave_loop = (node) => {
|
|
940
986
|
if (broken.has(node) || continued.has(node) && !is_endless(node)) {
|
|
941
987
|
if (!any_reachable()) revive();
|
|
942
|
-
} else if (
|
|
988
|
+
} else if (is_endless(node)) exit();
|
|
943
989
|
};
|
|
944
990
|
const leave_breakable = (node) => {
|
|
945
991
|
if (broken.has(node) && !any_reachable()) revive();
|
|
@@ -948,9 +994,7 @@ const rule = {
|
|
|
948
994
|
...listeners,
|
|
949
995
|
...wrapped
|
|
950
996
|
}, {
|
|
951
|
-
...Object.fromEntries(LOOP_TYPES.
|
|
952
|
-
if (in_revived_path()) loops.add(node);
|
|
953
|
-
}], [`${type}:exit`, leave_loop]])),
|
|
997
|
+
...Object.fromEntries(LOOP_TYPES.map((type) => [`${type}:exit`, leave_loop])),
|
|
954
998
|
"SwitchStatement:exit": leave_breakable,
|
|
955
999
|
"LabeledStatement:exit": leave_breakable,
|
|
956
1000
|
"BreakStatement:exit"(node) {
|
|
@@ -981,8 +1025,8 @@ const rule = {
|
|
|
981
1025
|
if (is_reference(node)) may_throw();
|
|
982
1026
|
},
|
|
983
1027
|
TryStatement(node) {
|
|
984
|
-
if (!in_revived_path()) return;
|
|
985
1028
|
const state = {
|
|
1029
|
+
has_handler: Boolean(node.handler),
|
|
986
1030
|
block_throws: false,
|
|
987
1031
|
throws: false,
|
|
988
1032
|
in_block: true
|
|
@@ -1032,6 +1076,30 @@ const rule = {
|
|
|
1032
1076
|
"JSXSwitchExpression:exit"(node) {
|
|
1033
1077
|
const state = switches.get(node);
|
|
1034
1078
|
if (state.entry && (state.completes || !state.has_default) && !any_reachable()) revive();
|
|
1079
|
+
},
|
|
1080
|
+
JSXTryExpression(node) {
|
|
1081
|
+
try_expressions.set(node, {
|
|
1082
|
+
entry: any_reachable(),
|
|
1083
|
+
inline_body: suspends(node.block),
|
|
1084
|
+
inline_pending: suspends(node.pending),
|
|
1085
|
+
body_completes: false,
|
|
1086
|
+
pending_completes: false,
|
|
1087
|
+
pending_first: false
|
|
1088
|
+
});
|
|
1089
|
+
},
|
|
1090
|
+
"JSXTryExpression > BlockStatement:exit"(node) {
|
|
1091
|
+
const state = try_expressions.get(node.parent);
|
|
1092
|
+
if (node.parent.block === node) state.body_completes = any_reachable();
|
|
1093
|
+
if (node.parent.pending === node) {
|
|
1094
|
+
state.pending_completes = any_reachable();
|
|
1095
|
+
if (state.pending_first) exit();
|
|
1096
|
+
}
|
|
1097
|
+
},
|
|
1098
|
+
"JSXTryExpression > CatchClause"(node) {
|
|
1099
|
+
revive_try_path(node.parent, "handler");
|
|
1100
|
+
},
|
|
1101
|
+
"JSXTryExpression:exit"(node) {
|
|
1102
|
+
revive_try_path(node, "after");
|
|
1035
1103
|
}
|
|
1036
1104
|
});
|
|
1037
1105
|
}
|
|
@@ -1077,6 +1145,149 @@ function is_endless(node) {
|
|
|
1077
1145
|
return (node.type === "WhileStatement" || node.type === "DoWhileStatement" || node.type === "ForStatement") && node.test.type === "Literal" && Boolean(node.test.value);
|
|
1078
1146
|
}
|
|
1079
1147
|
//#endregion
|
|
1148
|
+
//#region src/rules/no-useless-assignment.ts
|
|
1149
|
+
const core = get_core_rule("no-useless-assignment");
|
|
1150
|
+
/** Nodes whose code starts a code path of its own. */
|
|
1151
|
+
const CODE_PATH_STARTS = /* @__PURE__ */ new Set([
|
|
1152
|
+
"FunctionDeclaration",
|
|
1153
|
+
"FunctionExpression",
|
|
1154
|
+
"ArrowFunctionExpression",
|
|
1155
|
+
"StaticBlock"
|
|
1156
|
+
]);
|
|
1157
|
+
/**
|
|
1158
|
+
* The parts of each node that run in the order code path analysis follows,
|
|
1159
|
+
* and only in that order, so that the code it runs after a `@try` in one of
|
|
1160
|
+
* them can really run after it.
|
|
1161
|
+
*/
|
|
1162
|
+
const ORDERED_PARTS = {
|
|
1163
|
+
Program: ["body"],
|
|
1164
|
+
BlockStatement: ["body"],
|
|
1165
|
+
ExpressionStatement: ["expression"],
|
|
1166
|
+
ReturnStatement: ["argument"],
|
|
1167
|
+
VariableDeclaration: ["declarations"],
|
|
1168
|
+
VariableDeclarator: ["init"],
|
|
1169
|
+
ExportNamedDeclaration: ["declaration"],
|
|
1170
|
+
ExportDefaultDeclaration: ["declaration"],
|
|
1171
|
+
IfStatement: ["consequent", "alternate"],
|
|
1172
|
+
LabeledStatement: ["body"],
|
|
1173
|
+
TryStatement: [
|
|
1174
|
+
"block",
|
|
1175
|
+
"handler",
|
|
1176
|
+
"finalizer"
|
|
1177
|
+
],
|
|
1178
|
+
CatchClause: ["body"],
|
|
1179
|
+
WhileStatement: ["body"],
|
|
1180
|
+
DoWhileStatement: ["body"],
|
|
1181
|
+
ForStatement: ["init", "body"],
|
|
1182
|
+
ForInStatement: ["right", "body"],
|
|
1183
|
+
ForOfStatement: ["right", "body"],
|
|
1184
|
+
AssignmentExpression: ["right"],
|
|
1185
|
+
CallExpression: ["arguments"],
|
|
1186
|
+
NewExpression: ["arguments"],
|
|
1187
|
+
ChainExpression: ["expression"],
|
|
1188
|
+
ArrayExpression: ["elements"],
|
|
1189
|
+
ObjectExpression: ["properties"],
|
|
1190
|
+
Property: ["value"],
|
|
1191
|
+
SpreadElement: ["argument"],
|
|
1192
|
+
ConditionalExpression: ["consequent", "alternate"],
|
|
1193
|
+
LogicalExpression: ["left", "right"],
|
|
1194
|
+
SequenceExpression: ["expressions"],
|
|
1195
|
+
TemplateLiteral: ["expressions"],
|
|
1196
|
+
TaggedTemplateExpression: ["quasi"],
|
|
1197
|
+
AwaitExpression: ["argument"],
|
|
1198
|
+
YieldExpression: ["argument"],
|
|
1199
|
+
TSAsExpression: ["expression"],
|
|
1200
|
+
TSSatisfiesExpression: ["expression"],
|
|
1201
|
+
TSNonNullExpression: ["expression"],
|
|
1202
|
+
JSXElement: ["children"],
|
|
1203
|
+
JSXFragment: ["children"],
|
|
1204
|
+
JSXExpressionContainer: ["expression"],
|
|
1205
|
+
JSXAttribute: ["value"],
|
|
1206
|
+
JSXSpreadAttribute: ["argument"],
|
|
1207
|
+
JSXCodeBlock: ["body", "render"],
|
|
1208
|
+
JSXIfExpression: ["statement"],
|
|
1209
|
+
JSXTryExpression: [
|
|
1210
|
+
"block",
|
|
1211
|
+
"pending",
|
|
1212
|
+
"handler"
|
|
1213
|
+
],
|
|
1214
|
+
JSXSwitchCase: ["consequent"]
|
|
1215
|
+
};
|
|
1216
|
+
const NOT_CHILDREN = /* @__PURE__ */ new Set([
|
|
1217
|
+
"parent",
|
|
1218
|
+
"metadata",
|
|
1219
|
+
"loc",
|
|
1220
|
+
"range",
|
|
1221
|
+
"leadingComments",
|
|
1222
|
+
"trailingComments",
|
|
1223
|
+
"innerComments"
|
|
1224
|
+
]);
|
|
1225
|
+
const FUNCTIONS = /* @__PURE__ */ new Set([
|
|
1226
|
+
"FunctionDeclaration",
|
|
1227
|
+
"FunctionExpression",
|
|
1228
|
+
"ArrowFunctionExpression"
|
|
1229
|
+
]);
|
|
1230
|
+
/**
|
|
1231
|
+
* Whether a node, outside the functions in it, assigns or updates anything. A
|
|
1232
|
+
* class's static block and field values count, since they run with it or
|
|
1233
|
+
* later.
|
|
1234
|
+
*/
|
|
1235
|
+
function writes(node) {
|
|
1236
|
+
if (!node || typeof node.type !== "string" || FUNCTIONS.has(node.type)) return false;
|
|
1237
|
+
if (node.type === "AssignmentExpression" || node.type === "UpdateExpression") return true;
|
|
1238
|
+
return Object.entries(node).some(([key, value]) => !NOT_CHILDREN.has(key) && (Array.isArray(value) ? value.some(writes) : writes(value)));
|
|
1239
|
+
}
|
|
1240
|
+
/**
|
|
1241
|
+
* Whether a part of a node that code path analysis runs before the child,
|
|
1242
|
+
* though it really runs after it, assigns something: a destructuring target
|
|
1243
|
+
* runs after its value, and a `@for`'s index and key after the iterable and
|
|
1244
|
+
* around the body. Code path analysis runs a `for…in` or `for…of` target
|
|
1245
|
+
* after the iterable, but the core rule doesn't see the loop assign it, so a
|
|
1246
|
+
* target that isn't a new `let` or `const` binding counts.
|
|
1247
|
+
*/
|
|
1248
|
+
function writes_late(parent, child) {
|
|
1249
|
+
switch (parent.type) {
|
|
1250
|
+
case "VariableDeclarator": return child === parent.init && writes(parent.id);
|
|
1251
|
+
case "AssignmentExpression": return child === parent.right && writes(parent.left);
|
|
1252
|
+
case "ForInStatement":
|
|
1253
|
+
case "ForOfStatement": return child === parent.right && (parent.left.type !== "VariableDeclaration" || parent.left.kind === "var");
|
|
1254
|
+
case "JSXForExpression": return child === parent.loop && (writes(parent.index) || writes(parent.key));
|
|
1255
|
+
default: return false;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
/**
|
|
1259
|
+
* Whether the code that code path analysis runs before and after a node, in
|
|
1260
|
+
* its code path, really runs before and after it. Code path analysis runs
|
|
1261
|
+
* the `@switch` arms one after another, but only one renders, so the code
|
|
1262
|
+
* after an arm before the last doesn't follow it, and an earlier arm that
|
|
1263
|
+
* assigns doesn't come before the last. Likewise `@empty` renders only when
|
|
1264
|
+
* the loop's body doesn't, so the body doesn't follow it when there is an
|
|
1265
|
+
* `@empty`, and a loop that assigns doesn't come before `@empty`. A `@for`'s
|
|
1266
|
+
* index and key run before the loop for code path analysis, but after the
|
|
1267
|
+
* iterable, and a destructuring default before the value it defaults. Nor
|
|
1268
|
+
* does it hold when code path analysis runs an assignment before the node
|
|
1269
|
+
* that really runs after it, as in a computed key of a destructuring target.
|
|
1270
|
+
*/
|
|
1271
|
+
function runs_in_order(node) {
|
|
1272
|
+
for (let previous = void 0, child = node, parent = node.parent; parent; previous = child, child = parent, parent = parent.parent) {
|
|
1273
|
+
if (CODE_PATH_STARTS.has(parent.type) || parent.type === "PropertyDefinition" && parent.value === child) return true;
|
|
1274
|
+
if (writes_late(parent, child)) return false;
|
|
1275
|
+
if (parent.type === "JSXForExpression") {
|
|
1276
|
+
if (child === parent.loop) {
|
|
1277
|
+
if (parent.empty && previous === child.body) return false;
|
|
1278
|
+
} else if (child !== parent.empty || [
|
|
1279
|
+
parent.index,
|
|
1280
|
+
parent.key,
|
|
1281
|
+
parent.loop.left,
|
|
1282
|
+
parent.loop.body
|
|
1283
|
+
].some(writes)) return false;
|
|
1284
|
+
} else if (parent.type === "JSXSwitchExpression") {
|
|
1285
|
+
if (parent.cases.at(-1) !== child || parent.cases.slice(0, -1).some(writes)) return false;
|
|
1286
|
+
} else if (!ORDERED_PARTS[parent.type]?.some((part) => Array.isArray(parent[part]) ? parent[part].includes(child) : parent[part] === child)) return false;
|
|
1287
|
+
}
|
|
1288
|
+
return true;
|
|
1289
|
+
}
|
|
1290
|
+
//#endregion
|
|
1080
1291
|
//#region src/index.ts
|
|
1081
1292
|
const plugin = {
|
|
1082
1293
|
meta: {
|
|
@@ -1084,14 +1295,202 @@ const plugin = {
|
|
|
1084
1295
|
version: "0.1.3"
|
|
1085
1296
|
},
|
|
1086
1297
|
rules: {
|
|
1087
|
-
"no-return-in-component": rule$
|
|
1088
|
-
"control-flow-jsx": rule$
|
|
1089
|
-
"valid-for-of-key": rule$
|
|
1090
|
-
"require-statement-container-body": rule$
|
|
1091
|
-
"jsx-curly-brace-presence": rule$
|
|
1092
|
-
"no-unused-vars": rule$
|
|
1093
|
-
"no-await-in-loop": rule$
|
|
1094
|
-
"no-unreachable": rule
|
|
1298
|
+
"no-return-in-component": rule$8,
|
|
1299
|
+
"control-flow-jsx": rule$7,
|
|
1300
|
+
"valid-for-of-key": rule$6,
|
|
1301
|
+
"require-statement-container-body": rule$5,
|
|
1302
|
+
"jsx-curly-brace-presence": rule$4,
|
|
1303
|
+
"no-unused-vars": rule$3,
|
|
1304
|
+
"no-await-in-loop": rule$2,
|
|
1305
|
+
"no-unreachable": rule$1,
|
|
1306
|
+
"no-useless-assignment": {
|
|
1307
|
+
meta: wrapper_meta(core, "Disallow variable assignments when the value is not used, with the blocks of a `@try` as alternatives"),
|
|
1308
|
+
create(context) {
|
|
1309
|
+
if (!context.sourceCode.text.includes("@try")) return core.create(context);
|
|
1310
|
+
const candidates = [];
|
|
1311
|
+
const cut_reports = /* @__PURE__ */ new Set();
|
|
1312
|
+
const joined_reports = /* @__PURE__ */ new Set();
|
|
1313
|
+
const node_of = (descriptor) => descriptor.node;
|
|
1314
|
+
const plain = core.create(with_report(context, (descriptor) => candidates.push(descriptor)));
|
|
1315
|
+
const cut = core.create(with_report(context, (descriptor) => cut_reports.add(node_of(descriptor))));
|
|
1316
|
+
const joined = core.create(with_report(context, (descriptor) => joined_reports.add(node_of(descriptor))));
|
|
1317
|
+
const suspends = suspension_check(context);
|
|
1318
|
+
const pieces = /* @__PURE__ */ new Map();
|
|
1319
|
+
const alternatives = /* @__PURE__ */ new Map();
|
|
1320
|
+
const outer_segments = [];
|
|
1321
|
+
let current = /* @__PURE__ */ new Set();
|
|
1322
|
+
const outer_positions = [];
|
|
1323
|
+
let position = {
|
|
1324
|
+
end: -1,
|
|
1325
|
+
for_heads: 0
|
|
1326
|
+
};
|
|
1327
|
+
const start_loop = (node) => {
|
|
1328
|
+
if (node.parent?.type === "JSXForExpression" && node.parent.loop === node) position.for_heads--;
|
|
1329
|
+
};
|
|
1330
|
+
const tries = /* @__PURE__ */ new Map();
|
|
1331
|
+
const view = (with_edges) => {
|
|
1332
|
+
const objects = /* @__PURE__ */ new Map();
|
|
1333
|
+
const get = (piece) => {
|
|
1334
|
+
let object = objects.get(piece);
|
|
1335
|
+
if (!object) {
|
|
1336
|
+
object = Object.create(piece.segment, {
|
|
1337
|
+
id: { value: `${piece.segment.id}~${piece.index}` },
|
|
1338
|
+
nextSegments: { get() {
|
|
1339
|
+
const list = pieces.get(piece.segment);
|
|
1340
|
+
const next = piece.index < list.length - 1 ? [list[piece.index + 1]] : piece.segment.nextSegments.flatMap((segment) => pieces.get(segment)?.[0] ?? []);
|
|
1341
|
+
const edges = with_edges ? [...alternatives.get(piece) ?? []] : [];
|
|
1342
|
+
return [...next, ...edges].map(get);
|
|
1343
|
+
} }
|
|
1344
|
+
});
|
|
1345
|
+
objects.set(piece, object);
|
|
1346
|
+
}
|
|
1347
|
+
return object;
|
|
1348
|
+
};
|
|
1349
|
+
return get;
|
|
1350
|
+
};
|
|
1351
|
+
const cut_view = view(false);
|
|
1352
|
+
const joined_view = view(true);
|
|
1353
|
+
const start_piece = (segment, node) => {
|
|
1354
|
+
const list = pieces.get(segment) ?? [];
|
|
1355
|
+
pieces.set(segment, list);
|
|
1356
|
+
const piece = {
|
|
1357
|
+
segment,
|
|
1358
|
+
index: list.length
|
|
1359
|
+
};
|
|
1360
|
+
list.push(piece);
|
|
1361
|
+
cut.onCodePathSegmentStart?.(cut_view(piece), node);
|
|
1362
|
+
joined.onCodePathSegmentStart?.(joined_view(piece), node);
|
|
1363
|
+
return piece;
|
|
1364
|
+
};
|
|
1365
|
+
const end_piece = (segment, node) => {
|
|
1366
|
+
const piece = pieces.get(segment).at(-1);
|
|
1367
|
+
cut.onCodePathSegmentEnd?.(cut_view(piece), node);
|
|
1368
|
+
joined.onCodePathSegmentEnd?.(joined_view(piece), node);
|
|
1369
|
+
return piece;
|
|
1370
|
+
};
|
|
1371
|
+
const cut_here = (node) => {
|
|
1372
|
+
const ended = [];
|
|
1373
|
+
const started = [];
|
|
1374
|
+
for (const segment of current) {
|
|
1375
|
+
ended.push(end_piece(segment, node));
|
|
1376
|
+
started.push(start_piece(segment, node));
|
|
1377
|
+
}
|
|
1378
|
+
return {
|
|
1379
|
+
ended,
|
|
1380
|
+
started
|
|
1381
|
+
};
|
|
1382
|
+
};
|
|
1383
|
+
const join = (from, ...to) => {
|
|
1384
|
+
for (const piece of from) {
|
|
1385
|
+
const set = alternatives.get(piece) ?? /* @__PURE__ */ new Set();
|
|
1386
|
+
alternatives.set(piece, set);
|
|
1387
|
+
for (const list of to) for (const target of list) set.add(target);
|
|
1388
|
+
}
|
|
1389
|
+
};
|
|
1390
|
+
const listeners = {
|
|
1391
|
+
onCodePathStart(...args) {
|
|
1392
|
+
outer_segments.push(current);
|
|
1393
|
+
current = /* @__PURE__ */ new Set();
|
|
1394
|
+
outer_positions.push(position);
|
|
1395
|
+
position = {
|
|
1396
|
+
end: -1,
|
|
1397
|
+
for_heads: 0
|
|
1398
|
+
};
|
|
1399
|
+
plain.onCodePathStart?.(...args);
|
|
1400
|
+
cut.onCodePathStart?.(...args);
|
|
1401
|
+
joined.onCodePathStart?.(...args);
|
|
1402
|
+
},
|
|
1403
|
+
onCodePathEnd(...args) {
|
|
1404
|
+
current = outer_segments.pop();
|
|
1405
|
+
const inner = position;
|
|
1406
|
+
position = outer_positions.pop();
|
|
1407
|
+
if (!position.for_heads) position.end = Math.max(position.end, inner.end);
|
|
1408
|
+
plain.onCodePathEnd?.(...args);
|
|
1409
|
+
cut.onCodePathEnd?.(...args);
|
|
1410
|
+
joined.onCodePathEnd?.(...args);
|
|
1411
|
+
for (const descriptor of candidates) {
|
|
1412
|
+
const node = node_of(descriptor);
|
|
1413
|
+
if (!cut_reports.has(node) || joined_reports.has(node)) context.report(descriptor);
|
|
1414
|
+
}
|
|
1415
|
+
candidates.length = 0;
|
|
1416
|
+
cut_reports.clear();
|
|
1417
|
+
joined_reports.clear();
|
|
1418
|
+
},
|
|
1419
|
+
onCodePathSegmentStart(segment, node) {
|
|
1420
|
+
current.add(segment);
|
|
1421
|
+
plain.onCodePathSegmentStart?.(segment, node);
|
|
1422
|
+
start_piece(segment, node);
|
|
1423
|
+
},
|
|
1424
|
+
onCodePathSegmentEnd(segment, node) {
|
|
1425
|
+
current.delete(segment);
|
|
1426
|
+
plain.onCodePathSegmentEnd?.(segment, node);
|
|
1427
|
+
end_piece(segment, node);
|
|
1428
|
+
},
|
|
1429
|
+
JSXForExpression() {
|
|
1430
|
+
position.for_heads++;
|
|
1431
|
+
},
|
|
1432
|
+
ForStatement: start_loop,
|
|
1433
|
+
ForInStatement: start_loop,
|
|
1434
|
+
ForOfStatement: start_loop,
|
|
1435
|
+
JSXTryExpression(node) {
|
|
1436
|
+
if (suspends(node.block) || suspends(node.pending) || !runs_in_order(node)) return;
|
|
1437
|
+
const { ended, started } = cut_here(node);
|
|
1438
|
+
tries.set(node, {
|
|
1439
|
+
single: ended.length === 1,
|
|
1440
|
+
entry: ended,
|
|
1441
|
+
body_start: started,
|
|
1442
|
+
body_end: [],
|
|
1443
|
+
pending_start: [],
|
|
1444
|
+
pending_end: [],
|
|
1445
|
+
handler_start: []
|
|
1446
|
+
});
|
|
1447
|
+
},
|
|
1448
|
+
BlockStatement(node) {
|
|
1449
|
+
const state = tries.get(node.parent);
|
|
1450
|
+
if (!state || node.parent.pending !== node) return;
|
|
1451
|
+
const { ended, started } = cut_here(node);
|
|
1452
|
+
state.single &&= ended.length === 1;
|
|
1453
|
+
state.body_end = ended;
|
|
1454
|
+
state.pending_start = started;
|
|
1455
|
+
},
|
|
1456
|
+
CatchClause(node) {
|
|
1457
|
+
const state = tries.get(node.parent);
|
|
1458
|
+
if (!state) return;
|
|
1459
|
+
const { ended, started } = cut_here(node);
|
|
1460
|
+
state.single &&= ended.length === 1;
|
|
1461
|
+
if (node.parent.pending) state.pending_end = ended;
|
|
1462
|
+
else state.body_end = ended;
|
|
1463
|
+
state.handler_start = started;
|
|
1464
|
+
},
|
|
1465
|
+
"JSXTryExpression:exit"(node) {
|
|
1466
|
+
const state = tries.get(node);
|
|
1467
|
+
if (!state) return;
|
|
1468
|
+
const { ended, started: after } = cut_here(node);
|
|
1469
|
+
if (!node.handler) state.pending_end = ended;
|
|
1470
|
+
if (!state.single || ended.length !== 1) return;
|
|
1471
|
+
const { body_start, pending_start, handler_start } = state;
|
|
1472
|
+
join(state.entry, body_start, pending_start, handler_start, after);
|
|
1473
|
+
join(state.body_end, pending_start, handler_start, after);
|
|
1474
|
+
join(state.pending_end, handler_start, after);
|
|
1475
|
+
}
|
|
1476
|
+
};
|
|
1477
|
+
const merged = { ...listeners };
|
|
1478
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(plain), ...Object.keys(cut)])) {
|
|
1479
|
+
if (key in listeners) continue;
|
|
1480
|
+
merged[key] = (...args) => {
|
|
1481
|
+
plain[key]?.(...args);
|
|
1482
|
+
const node = args[0];
|
|
1483
|
+
if (node?.type === "Identifier" || node?.type === "JSXIdentifier") {
|
|
1484
|
+
if (position.for_heads || node.range[0] < position.end) return;
|
|
1485
|
+
position.end = node.range[1];
|
|
1486
|
+
}
|
|
1487
|
+
cut[key]?.(...args);
|
|
1488
|
+
joined[key]?.(...args);
|
|
1489
|
+
};
|
|
1490
|
+
}
|
|
1491
|
+
return merged;
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1095
1494
|
},
|
|
1096
1495
|
configs: {}
|
|
1097
1496
|
};
|
|
@@ -1172,7 +1571,11 @@ plugin.configs.js = [{
|
|
|
1172
1571
|
"no-unused-vars": "off",
|
|
1173
1572
|
"tsrx/no-unused-vars": "error",
|
|
1174
1573
|
"no-unreachable": "off",
|
|
1175
|
-
"tsrx/no-unreachable": "error"
|
|
1574
|
+
"tsrx/no-unreachable": "error",
|
|
1575
|
+
...get_core_rule("no-useless-assignment").meta?.docs?.recommended === true && {
|
|
1576
|
+
"no-useless-assignment": "off",
|
|
1577
|
+
"tsrx/no-useless-assignment": "error"
|
|
1578
|
+
}
|
|
1176
1579
|
}
|
|
1177
1580
|
}];
|
|
1178
1581
|
//#endregion
|