Mark FOO in "var { x: FOO }˝ as a binding, not as a reference (#9492)

This commit is contained in:
Nicolò Ribaudo
2019-02-27 00:17:11 +01:00
committed by GitHub
parent 43eed1ac92
commit 5c8cc0d536
6 changed files with 95 additions and 16 deletions

View File

@@ -2,6 +2,17 @@ import { declare } from "@babel/helper-plugin-utils";
import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread";
import { types as t } from "@babel/core";
// TODO: Remove in Babel 8
// @babel/types <=7.3.3 counts FOO as referenced in var { x: FOO }.
// We need to detect this bug to know if "unused" means 0 or 1 references.
const ZERO_REFS = (() => {
const node = t.identifier("a");
const property = t.objectProperty(t.identifier("key"), node);
const pattern = t.objectPattern([property]);
return t.isReferenced(node, property, pattern) ? 1 : 0;
})();
export default declare((api, opts) => {
api.assertVersion(7);
@@ -98,7 +109,7 @@ export default declare((api, opts) => {
Object.keys(bindings).forEach(bindingName => {
const bindingParentPath = bindings[bindingName].parentPath;
if (
path.scope.getBinding(bindingName).references > 1 ||
path.scope.getBinding(bindingName).references > ZERO_REFS ||
!bindingParentPath.isObjectProperty()
) {
return;