Remove unused bindings when excluding keys with rest in loose mode (#8264)

This commit is contained in:
Mateusz Burzyński
2018-07-07 19:54:51 +02:00
committed by GitHub
parent 44f738bcbf
commit b5e64cb66e
4 changed files with 70 additions and 0 deletions

View File

@@ -92,6 +92,17 @@ export default declare((api, opts) => {
return impureComputedPropertyDeclarators;
}
function removeUnusedExcludedKeys(path) {
const bindings = path.getOuterBindingIdentifierPaths();
Object.keys(bindings).forEach(bindingName => {
if (path.scope.getBinding(bindingName).references > 1) {
return;
}
bindings[bindingName].parentPath.remove();
});
}
//expects path to an object pattern
function createObjectSpread(path, file, objRef) {
const props = path.get("properties");
@@ -241,12 +252,17 @@ export default declare((api, opts) => {
const objectPatternPath = path.findParent(path =>
path.isObjectPattern(),
);
const [
impureComputedPropertyDeclarators,
argument,
callExpression,
] = createObjectSpread(objectPatternPath, file, ref);
if (loose) {
removeUnusedExcludedKeys(objectPatternPath);
}
t.assertIdentifier(argument);
insertionPath.insertBefore(impureComputedPropertyDeclarators);