@eslint-react/ast 5.30.0-beta.24 → 5.30.0-beta.25
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/dist/index.d.ts +4 -1
- package/dist/index.js +69 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -250,7 +250,10 @@ declare function isTerminatingStatement(node: TSESTree.Node): boolean;
|
|
|
250
250
|
* - Inspects only the last statement of a block: `{ foo(); return; }` terminates, while `{ return; foo(); }` does not; earlier `break`/`continue` paths are not modeled.
|
|
251
251
|
* - Treats an `if` as terminating only when it has an `else` and both branches terminate.
|
|
252
252
|
* - Treats a `try` as terminating when its finalizer terminates, or when its try block terminates and, if present, its catch body terminates.
|
|
253
|
-
* -
|
|
253
|
+
* - Treats a `while (true)` or `for (;;)` loop as terminating when its body terminates and contains no exiting `break`.
|
|
254
|
+
* - Treats a `do...while` loop as terminating when its body terminates and contains no exiting `break` or `continue`.
|
|
255
|
+
* - Treats a `switch` with a `default` clause as terminating when every clause's statements terminate and no clause contains an exiting `break`/`continue`; fall-through clause groups are not recognized.
|
|
256
|
+
* - Does not analyze labeled statements, and any labeled `break`/`continue` disqualifies the enclosing loop or switch conservatively.
|
|
254
257
|
* - Is purely syntactic: conditions are not evaluated and no reachability analysis is performed.
|
|
255
258
|
* @param node The node to check.
|
|
256
259
|
* @returns `true` if the statement unconditionally terminates.
|
package/dist/index.js
CHANGED
|
@@ -558,6 +558,56 @@ function isTerminatingStatement(node) {
|
|
|
558
558
|
default: return false;
|
|
559
559
|
}
|
|
560
560
|
}
|
|
561
|
+
const jumpBarrierVisitorKeys = {
|
|
562
|
+
...visitorKeys,
|
|
563
|
+
ArrowFunctionExpression: [],
|
|
564
|
+
FunctionDeclaration: [],
|
|
565
|
+
FunctionExpression: []
|
|
566
|
+
};
|
|
567
|
+
function hasExitingJump(node, checkContinue) {
|
|
568
|
+
let found = false;
|
|
569
|
+
let loopDepth = 0;
|
|
570
|
+
let loopOrSwitchDepth = 0;
|
|
571
|
+
simpleTraverse(node, {
|
|
572
|
+
enter(child) {
|
|
573
|
+
switch (child.type) {
|
|
574
|
+
case AST_NODE_TYPES.BreakStatement:
|
|
575
|
+
if (child.label != null || loopOrSwitchDepth === 0) found = true;
|
|
576
|
+
break;
|
|
577
|
+
case AST_NODE_TYPES.ContinueStatement:
|
|
578
|
+
if (child.label != null || checkContinue && loopDepth === 0) found = true;
|
|
579
|
+
break;
|
|
580
|
+
case AST_NODE_TYPES.WhileStatement:
|
|
581
|
+
case AST_NODE_TYPES.DoWhileStatement:
|
|
582
|
+
case AST_NODE_TYPES.ForStatement:
|
|
583
|
+
case AST_NODE_TYPES.ForInStatement:
|
|
584
|
+
case AST_NODE_TYPES.ForOfStatement:
|
|
585
|
+
loopDepth += 1;
|
|
586
|
+
loopOrSwitchDepth += 1;
|
|
587
|
+
break;
|
|
588
|
+
case AST_NODE_TYPES.SwitchStatement: loopOrSwitchDepth += 1;
|
|
589
|
+
}
|
|
590
|
+
},
|
|
591
|
+
leave(child) {
|
|
592
|
+
switch (child.type) {
|
|
593
|
+
case AST_NODE_TYPES.WhileStatement:
|
|
594
|
+
case AST_NODE_TYPES.DoWhileStatement:
|
|
595
|
+
case AST_NODE_TYPES.ForStatement:
|
|
596
|
+
case AST_NODE_TYPES.ForInStatement:
|
|
597
|
+
case AST_NODE_TYPES.ForOfStatement:
|
|
598
|
+
loopDepth -= 1;
|
|
599
|
+
loopOrSwitchDepth -= 1;
|
|
600
|
+
break;
|
|
601
|
+
case AST_NODE_TYPES.SwitchStatement: loopOrSwitchDepth -= 1;
|
|
602
|
+
}
|
|
603
|
+
},
|
|
604
|
+
visitorKeys: jumpBarrierVisitorKeys
|
|
605
|
+
});
|
|
606
|
+
return found;
|
|
607
|
+
}
|
|
608
|
+
function isTrueLiteral(node) {
|
|
609
|
+
return node.type === AST_NODE_TYPES.Literal && node.value === true;
|
|
610
|
+
}
|
|
561
611
|
function isUnconditionallyTerminating(node) {
|
|
562
612
|
switch (node.type) {
|
|
563
613
|
case AST_NODE_TYPES.ReturnStatement:
|
|
@@ -571,6 +621,25 @@ function isUnconditionallyTerminating(node) {
|
|
|
571
621
|
if (node.finalizer != null && isUnconditionallyTerminating(node.finalizer)) return true;
|
|
572
622
|
if (node.handler == null) return isUnconditionallyTerminating(node.block);
|
|
573
623
|
return isUnconditionallyTerminating(node.block) && isUnconditionallyTerminating(node.handler.body);
|
|
624
|
+
case AST_NODE_TYPES.SwitchStatement:
|
|
625
|
+
if (!node.cases.some((switchCase) => switchCase.test == null)) return false;
|
|
626
|
+
return node.cases.every((switchCase) => {
|
|
627
|
+
const last = switchCase.consequent.at(-1);
|
|
628
|
+
if (last == null) return false;
|
|
629
|
+
if (!isUnconditionallyTerminating(last)) return false;
|
|
630
|
+
return switchCase.consequent.every((statement) => !hasExitingJump(statement, true));
|
|
631
|
+
});
|
|
632
|
+
case AST_NODE_TYPES.WhileStatement:
|
|
633
|
+
if (!isTrueLiteral(node.test)) return false;
|
|
634
|
+
if (!isUnconditionallyTerminating(node.body)) return false;
|
|
635
|
+
return !hasExitingJump(node.body, false);
|
|
636
|
+
case AST_NODE_TYPES.DoWhileStatement:
|
|
637
|
+
if (!isUnconditionallyTerminating(node.body)) return false;
|
|
638
|
+
return !hasExitingJump(node.body, true);
|
|
639
|
+
case AST_NODE_TYPES.ForStatement:
|
|
640
|
+
if (node.test != null && !isTrueLiteral(node.test)) return false;
|
|
641
|
+
if (!isUnconditionallyTerminating(node.body)) return false;
|
|
642
|
+
return !hasExitingJump(node.body, false);
|
|
574
643
|
default: return false;
|
|
575
644
|
}
|
|
576
645
|
}
|