Skip to content

Commit 0f326ce

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

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

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

Lines changed: 26 additions & 12 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;
31+
}
32+
33+
for (const sibling of siblings) {
34+
if (isFunctionDeclaration(sibling))
35+
continue;
3436

3537
push({
36-
path: siblings[0],
37-
siblings,
38+
path: sibling,
39+
siblings: [sibling],
3840
});
41+
}
42+
};
43+
44+
module.exports.traverse = ({push}) => ({
45+
'ReturnStatement|ThrowStatement'(path) {
46+
let leaf = true;
47+
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)