diff --git a/packages/plugin-remove-unreachable-code/lib/remove-unreachable-code.js b/packages/plugin-remove-unreachable-code/lib/remove-unreachable-code.js index b8d38fedcd..ee6ccbe712 100644 --- a/packages/plugin-remove-unreachable-code/lib/remove-unreachable-code.js +++ b/packages/plugin-remove-unreachable-code/lib/remove-unreachable-code.js @@ -17,24 +17,38 @@ module.exports.fix = ({siblings}) => { } }; -module.exports.traverse = ({push}) => ({ - 'ReturnStatement|ThrowStatement'(path) { - const siblings = path.getAllNextSiblings(); - - if (!siblings.length) - return; - +const processBlock = (push, path, leaf) => { + const siblings = path.getAllNextSiblings(); + + if (!siblings.length) + return; + + if (leaf) { const [first] = siblings; if (!path.node.argument && (isBlockStatement(first) || isExpressionStatement(first))) return false; - - if (siblings.find(isFunctionDeclaration)) - return; + } + + for (const sibling of siblings) { + if (isFunctionDeclaration(sibling)) + continue; push({ - path: siblings[0], - siblings, + path: sibling, + siblings: [sibling], }); + } +}; + +module.exports.traverse = ({push}) => ({ + 'ReturnStatement|ThrowStatement'(path) { + let leaf = true; + + while (path.parentPath?.isBlockStatement()) { + processBlock(push, path, leaf); + path = path.parentPath; + leaf = false; + } }, }); diff --git a/packages/plugin-remove-unreachable-code/test/fixture/backtrack-func.js b/packages/plugin-remove-unreachable-code/test/fixture/backtrack-func.js new file mode 100644 index 0000000000..e0f33aa310 --- /dev/null +++ b/packages/plugin-remove-unreachable-code/test/fixture/backtrack-func.js @@ -0,0 +1,9 @@ +let x3 = 1; +function fun3(f) { + { + let x3 = f + 2; + return x3 * 2; + } + function m() {} +} +fun3(x3) diff --git a/packages/plugin-remove-unreachable-code/test/fixture/backtrack-normal-fix.js b/packages/plugin-remove-unreachable-code/test/fixture/backtrack-normal-fix.js new file mode 100644 index 0000000000..bfbeb6eb65 --- /dev/null +++ b/packages/plugin-remove-unreachable-code/test/fixture/backtrack-normal-fix.js @@ -0,0 +1,10 @@ +let x3 = 1; + +function fun3(f) { + { + let x3 = f + 2; + return x3 * 2; + } +} + +fun3(x3); diff --git a/packages/plugin-remove-unreachable-code/test/fixture/backtrack-normal.js b/packages/plugin-remove-unreachable-code/test/fixture/backtrack-normal.js new file mode 100644 index 0000000000..910a4288e6 --- /dev/null +++ b/packages/plugin-remove-unreachable-code/test/fixture/backtrack-normal.js @@ -0,0 +1,11 @@ +let x3 = 1; + +function fun3(f) { + { + let x3 = f + 2; + return x3 * 2; + } + return 3; +} + +fun3(x3); \ No newline at end of file diff --git a/packages/plugin-remove-unreachable-code/test/fixture/hoist-and-other-fix.js b/packages/plugin-remove-unreachable-code/test/fixture/hoist-and-other-fix.js new file mode 100644 index 0000000000..5315f12617 --- /dev/null +++ b/packages/plugin-remove-unreachable-code/test/fixture/hoist-and-other-fix.js @@ -0,0 +1,4 @@ +function f() { + return x; + function m() {} +} diff --git a/packages/plugin-remove-unreachable-code/test/fixture/hoist-and-other.js b/packages/plugin-remove-unreachable-code/test/fixture/hoist-and-other.js new file mode 100644 index 0000000000..bb78404505 --- /dev/null +++ b/packages/plugin-remove-unreachable-code/test/fixture/hoist-and-other.js @@ -0,0 +1,5 @@ +function f() { + return x; + function m() {} + return 1; +} diff --git a/packages/plugin-remove-unreachable-code/test/remove-unreachable-code.js b/packages/plugin-remove-unreachable-code/test/remove-unreachable-code.js index fa685b1957..91b9cc9367 100644 --- a/packages/plugin-remove-unreachable-code/test/remove-unreachable-code.js +++ b/packages/plugin-remove-unreachable-code/test/remove-unreachable-code.js @@ -33,3 +33,18 @@ test('plugin-remove-unreachable-code: no report: return-no-arg', (t) => { t.noReport('return-no-arg'); t.end(); }); + +test('plugin-remove-unreachable-code: transform: hoist-and-other', (t) => { + t.transform('hoist-and-other'); + t.end(); +}); + +test('plugin-remove-unreachable-code: transform: backtrack-normal', (t) => { + t.transform('backtrack-normal'); + t.end(); +}); + +test('plugin-remove-unreachable-code: no report: backtrack-func', (t) => { + t.noReport('backtrack-func'); + t.end(); +});