@putout/plugin-remove-unreachable-code
Version:
🐊Putout plugin adds ability to find and remove unreachable code
59 lines (41 loc) • 1.33 kB
JavaScript
import {types, operator} from 'putout';
const {remove} = operator;
const {
isExpressionStatement,
isFunctionDeclaration,
isBlockStatement,
} = types;
const not = (fn) => (...a) => !fn(...a);
export const report = () => `Avoid unreachable code`;
export const fix = ({siblings}) => {
siblings.map(remove);
};
export const traverse = ({push}) => ({
'ReturnStatement|ThrowStatement'(path) {
let nextPath = path;
while (nextPath.parentPath?.isBlockStatement()) {
const siblings = nextPath
.getAllNextSiblings()
.filter(not(isFunctionDeclaration));
const prevPath = nextPath;
nextPath = nextPath.parentPath;
if (!siblings.length)
continue;
const {argument} = path.node;
if (checkFirstSibling({argument, siblings}))
continue;
push({
path: prevPath,
siblings,
});
}
},
});
function checkFirstSibling({argument, siblings}) {
if (argument)
return false;
const [first] = siblings;
if (isBlockStatement(first))
return true;
return isExpressionStatement(first);
}