Skip to content

Commit 0abe3ca

Browse files
committed
feature: @putout/plugin-remove-unreachable-code: backtracking parents and enhancements
Signed-off-by: echo094 <[email protected]>
1 parent c54dd62 commit 0abe3ca

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

packages/plugin-remove-unreachable-code/lib/remove-unreachable-code.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,38 @@ module.exports.fix = ({siblings}) => {
1717
}
1818
};
1919

20-
module.exports.traverse = ({push}) => ({
21-
'ReturnStatement|ThrowStatement'(path) {
22-
const siblings = path.getAllNextSiblings();
23-
24-
if (!siblings.length)
25-
return;
26-
20+
const processBlock = (push, path, leaf) => {
21+
const siblings = path.getAllNextSiblings();
22+
23+
if (!siblings.length)
24+
return;
25+
26+
if (leaf) {
2727
const [first] = siblings;
2828

2929
if (!path.node.argument && (isBlockStatement(first) || isExpressionStatement(first)))
3030
return false;
31-
32-
if (siblings.find(isFunctionDeclaration))
33-
return;
34-
31+
}
32+
33+
for (const sibling of siblings) {
34+
if (isFunctionDeclaration(sibling)) {
35+
continue;
36+
}
37+
3538
push({
36-
path: siblings[0],
37-
siblings,
39+
path: sibling,
40+
siblings: [sibling],
3841
});
42+
}
43+
}
44+
45+
module.exports.traverse = ({push}) => ({
46+
'ReturnStatement|ThrowStatement'(path) {
47+
let leaf = true
48+
while (path.parentPath?.isBlockStatement()) {
49+
processBlock(push, path, leaf)
50+
path = path.parentPath
51+
leaf = false
52+
}
3953
},
4054
});

0 commit comments

Comments
 (0)