Remove unused bindings when excluding keys with rest in loose mode (#8264)
This commit is contained in:
parent
44f738bcbf
commit
b5e64cb66e
@ -92,6 +92,17 @@ export default declare((api, opts) => {
|
|||||||
return impureComputedPropertyDeclarators;
|
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
|
//expects path to an object pattern
|
||||||
function createObjectSpread(path, file, objRef) {
|
function createObjectSpread(path, file, objRef) {
|
||||||
const props = path.get("properties");
|
const props = path.get("properties");
|
||||||
@ -241,12 +252,17 @@ export default declare((api, opts) => {
|
|||||||
const objectPatternPath = path.findParent(path =>
|
const objectPatternPath = path.findParent(path =>
|
||||||
path.isObjectPattern(),
|
path.isObjectPattern(),
|
||||||
);
|
);
|
||||||
|
|
||||||
const [
|
const [
|
||||||
impureComputedPropertyDeclarators,
|
impureComputedPropertyDeclarators,
|
||||||
argument,
|
argument,
|
||||||
callExpression,
|
callExpression,
|
||||||
] = createObjectSpread(objectPatternPath, file, ref);
|
] = createObjectSpread(objectPatternPath, file, ref);
|
||||||
|
|
||||||
|
if (loose) {
|
||||||
|
removeUnusedExcludedKeys(objectPatternPath);
|
||||||
|
}
|
||||||
|
|
||||||
t.assertIdentifier(argument);
|
t.assertIdentifier(argument);
|
||||||
|
|
||||||
insertionPath.insertBefore(impureComputedPropertyDeclarators);
|
insertionPath.insertBefore(impureComputedPropertyDeclarators);
|
||||||
|
|||||||
@ -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);
|
||||||
|
}
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
"external-helpers",
|
||||||
|
["proposal-object-rest-spread", { "loose": true }]
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user