Skip to content

Commit 2409657

Browse files
committed
fix: @putout/plugin-remove-unreachable-code: report count
1 parent fbd75fe commit 2409657

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,39 @@ const not = (fn) => (...a) => !fn(...a);
1313

1414
module.exports.report = () => `Unreachable code`;
1515

16-
module.exports.fix = (path) => {
17-
remove(path);
16+
module.exports.fix = ({siblings}) => {
17+
siblings.map(remove);
1818
};
1919

2020
module.exports.traverse = ({push}) => ({
2121
'ReturnStatement|ThrowStatement'(path) {
22-
while (path.parentPath?.isBlockStatement()) {
23-
const siblings = path.getAllNextSiblings();
24-
const {argument} = path.node;
22+
let nextPath = path;
23+
24+
while (nextPath.parentPath?.isBlockStatement()) {
25+
const siblings = nextPath
26+
.getAllNextSiblings()
27+
.filter(not(isFunctionDeclaration));
28+
29+
const prevPath = nextPath;
2530

26-
path = path.parentPath;
31+
nextPath = nextPath.parentPath;
32+
33+
if (!siblings.length)
34+
continue;
35+
36+
const {argument} = path.node;
2737

2838
if (checkFirstSibling({argument, siblings}))
2939
continue;
3040

31-
processSiblings({
32-
push,
41+
push({
42+
path: prevPath,
3343
siblings,
3444
});
3545
}
3646
},
3747
});
3848

39-
const processSiblings = ({push, siblings}) => {
40-
if (!siblings.length)
41-
return;
42-
43-
siblings
44-
.filter(not(isFunctionDeclaration))
45-
.map(push);
46-
};
47-
4849
function checkFirstSibling({argument, siblings}) {
4950
if (argument)
5051
return false;

packages/plugin-remove-unreachable-code/test/fixture/backtrack-normal.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ function fun3(f) {
66
return x3 * 2;
77
}
88
return 3;
9+
return 5;
10+
return 7;
911
}
1012

11-
fun3(x3);
13+
fun3(x3);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ test('plugin-remove-unreachable-code: transform: backtrack-normal', (t) => {
4444
t.end();
4545
});
4646

47+
test('plugin-remove-unreachable-code: report: backtrack-normal', (t) => {
48+
t.report('backtrack-normal', ['Unreachable code', 'Unreachable code', 'Unreachable code']);
49+
t.end();
50+
});
51+
4752
test('plugin-remove-unreachable-code: no report: backtrack-func', (t) => {
4853
t.noReport('backtrack-func');
4954
t.end();

0 commit comments

Comments
 (0)