Skip to content

Commit 8698589

Browse files
authored
feature: @putout/plugin-remove-unreachable-code: backtracking parents and enhancements (#226)
1 parent 4f64b1a commit 8698589

File tree

7 files changed

+80
-12
lines changed

7 files changed

+80
-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
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let x3 = 1;
2+
function fun3(f) {
3+
{
4+
let x3 = f + 2;
5+
return x3 * 2;
6+
}
7+
function m() {}
8+
}
9+
fun3(x3)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let x3 = 1;
2+
3+
function fun3(f) {
4+
{
5+
let x3 = f + 2;
6+
return x3 * 2;
7+
}
8+
}
9+
10+
fun3(x3);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let x3 = 1;
2+
3+
function fun3(f) {
4+
{
5+
let x3 = f + 2;
6+
return x3 * 2;
7+
}
8+
return 3;
9+
}
10+
11+
fun3(x3);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function f() {
2+
return x;
3+
function m() {}
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function f() {
2+
return x;
3+
function m() {}
4+
return 1;
5+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,18 @@ test('plugin-remove-unreachable-code: no report: return-no-arg', (t) => {
3333
t.noReport('return-no-arg');
3434
t.end();
3535
});
36+
37+
test('plugin-remove-unreachable-code: transform: hoist-and-other', (t) => {
38+
t.transform('hoist-and-other');
39+
t.end();
40+
});
41+
42+
test('plugin-remove-unreachable-code: transform: backtrack-normal', (t) => {
43+
t.transform('backtrack-normal');
44+
t.end();
45+
});
46+
47+
test('plugin-remove-unreachable-code: no report: backtrack-func', (t) => {
48+
t.noReport('backtrack-func');
49+
t.end();
50+
});

0 commit comments

Comments
 (0)