Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,16 +560,18 @@ module.exports = function (_ref) {
var paths = path.get("body");

var purge = false;
var completionPath = null;

for (var i = 0; i < paths.length; i++) {
var p = paths[i];

if (!purge && p.isCompletionStatement()) {
purge = true;
completionPath = p;
continue;
}

if (purge && !canExistAfterCompletion(p)) {
if (purge && (!canExistAfterCompletion(p) || canSafelyRemoved(p, completionPath))) {
removeOrVoid(p);
}
}
Expand Down Expand Up @@ -1387,6 +1389,35 @@ module.exports = function (_ref) {
function canExistAfterCompletion(path) {
return path.isFunctionDeclaration() || path.isVariableDeclaration({ kind: "var" });
}

function canSafelyRemoved(path, completionPath) {
if (
completionPath &&
path.isVariableDeclaration({ kind: 'var' }) &&
completionPath.isReturnStatement()
) {
var completionEnd = completionPath.node.end;

return path.node.declarations.every(function (decl) {
var name = decl.id.name || '';
var binding = path.scope.getBinding(name);

if (!binding) {
return false;
}

if (binding.referencePaths.length === 0) {
return true;
}

return binding.referencePaths.every(function (refPath) {
return refPath.node.start > completionEnd;
});
});
}

return false;
}

function getLabel(name, _path) {
var label = void 0,
Expand Down Expand Up @@ -1420,4 +1451,4 @@ module.exports = function (_ref) {
}
return t.sequenceExpression(result);
}
};
};