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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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);

View File

@ -0,0 +1,22 @@
// should not remove when destructuring into existing bindings
({ a2, ...b2 } = c2);
class Comp extends React.Component {
render() {
const {
excluded,
excluded2: excludedRenamed,
used,
used2: usedRenamed,
...props
} = this.props;
console.log(used, usedRenamed);
return React.createElement("input", props);
}
}
function smth({ unused, ...rest }) {
call(rest);
}

View File

@ -0,0 +1,6 @@
{
"plugins": [
"external-helpers",
["proposal-object-rest-spread", { "loose": true }]
]
}

View File

@ -0,0 +1,26 @@
// should not remove when destructuring into existing bindings
var _c = c2;
({
a2
} = _c);
b2 = babelHelpers.objectWithoutProperties(_c, ["a2"]);
_c;
class Comp extends React.Component {
render() {
const _this$props = this.props,
{
used,
used2: usedRenamed
} = _this$props,
props = babelHelpers.objectWithoutProperties(_this$props, ["excluded", "excluded2", "used", "used2"]);
console.log(used, usedRenamed);
return React.createElement("input", props);
}
}
function smth(_ref) {
let rest = babelHelpers.objectWithoutProperties(_ref, ["unused"]);
call(rest);
}