Support more node types in generateUidBasedOnNode (#11260)

* Support more node types in generateUidBasedOnNode

* More nodes
This commit is contained in:
Justin Ridgewell 2020-03-14 11:24:50 -04:00 committed by GitHub
parent b85d9f57f6
commit 661ffbd830
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 192 additions and 101 deletions

View File

@ -64,7 +64,7 @@ function _foo() {
while (1) { while (1) {
switch (_context3.prev = _context3.next) { switch (_context3.prev = _context3.next) {
case 0: case 0:
_bar2 = function _ref2() { _bar2 = function _bar4() {
_bar2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { _bar2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
var baz; var baz;
return regeneratorRuntime.wrap(function _callee2$(_context2) { return regeneratorRuntime.wrap(function _callee2$(_context2) {
@ -83,7 +83,7 @@ function _foo() {
return _bar2.apply(this, arguments); return _bar2.apply(this, arguments);
}; };
bar = function _ref() { bar = function _bar3() {
return _bar2.apply(this, arguments); return _bar2.apply(this, arguments);
}; };

View File

@ -1,8 +1,8 @@
async function g() { async function g() {
let A = babelHelpers.decorate([dec], function (_initialize, _super) { let A = babelHelpers.decorate([dec], function (_initialize, _await$B) {
"use strict"; "use strict";
class A extends _super { class A extends _await$B {
constructor(...args) { constructor(...args) {
super(...args); super(...args);

View File

@ -1,8 +1,8 @@
function* g() { function* g() {
let A = babelHelpers.decorate([dec], function (_initialize, _super) { let A = babelHelpers.decorate([dec], function (_initialize, _yield$B) {
"use strict"; "use strict";
class A extends _super { class A extends _yield$B {
constructor(...args) { constructor(...args) {
super(...args); super(...args);

View File

@ -1,4 +1,4 @@
var _deep$obj, _deep$obj2, _deep$obj3, _deep$obj4, _ref, _ref2, _ref3, _ref4, _deep$obj5, _ref5, _deep$obj6, _ref6, _deep$obj7, _ref7, _deep$obj8, _ref8; var _deep$obj, _deep$obj2, _deep$obj3, _deep$obj4, _key, _key2, _key3, _key4, _deep$obj5, _key5, _deep$obj6, _key6, _deep$obj7, _key7, _deep$obj8, _key8;
var x = 0; var x = 0;
var sets = 0; var sets = 0;
@ -38,30 +38,30 @@ expect(gets).toBe(3);
expect((_deep$obj4 = deep.obj).x && (_deep$obj4.x = 3)).toBe(0); expect((_deep$obj4 = deep.obj).x && (_deep$obj4.x = 3)).toBe(0);
expect(gets).toBe(4); expect(gets).toBe(4);
var key = 0; var key = 0;
expect(obj[_ref = ++key] || (obj[_ref] = 1)).toBe(1); expect(obj[_key = ++key] || (obj[_key] = 1)).toBe(1);
expect(key).toBe(1); expect(key).toBe(1);
key = 0; key = 0;
expect(obj[_ref2 = ++key] || (obj[_ref2] = 2)).toBe(1); expect(obj[_key2 = ++key] || (obj[_key2] = 2)).toBe(1);
expect(key).toBe(1); expect(key).toBe(1);
key = 0; key = 0;
expect(obj[_ref3 = ++key] && (obj[_ref3] = 0)).toBe(0); expect(obj[_key3 = ++key] && (obj[_key3] = 0)).toBe(0);
expect(key).toBe(1); expect(key).toBe(1);
key = 0; key = 0;
expect(obj[_ref4 = ++key] && (obj[_ref4] = 3)).toBe(0); expect(obj[_key4 = ++key] && (obj[_key4] = 3)).toBe(0);
expect(key).toBe(1); expect(key).toBe(1);
key = 0; key = 0;
expect((_deep$obj5 = deep.obj)[_ref5 = ++key] || (_deep$obj5[_ref5] = 1)).toBe(1); expect((_deep$obj5 = deep.obj)[_key5 = ++key] || (_deep$obj5[_key5] = 1)).toBe(1);
expect(gets).toBe(5); expect(gets).toBe(5);
expect(key).toBe(1); expect(key).toBe(1);
key = 0; key = 0;
expect((_deep$obj6 = deep.obj)[_ref6 = ++key] || (_deep$obj6[_ref6] = 2)).toBe(1); expect((_deep$obj6 = deep.obj)[_key6 = ++key] || (_deep$obj6[_key6] = 2)).toBe(1);
expect(gets).toBe(6); expect(gets).toBe(6);
expect(key).toBe(1); expect(key).toBe(1);
key = 0; key = 0;
expect((_deep$obj7 = deep.obj)[_ref7 = ++key] && (_deep$obj7[_ref7] = 0)).toBe(0); expect((_deep$obj7 = deep.obj)[_key7 = ++key] && (_deep$obj7[_key7] = 0)).toBe(0);
expect(gets).toBe(7); expect(gets).toBe(7);
expect(key).toBe(1); expect(key).toBe(1);
key = 0; key = 0;
expect((_deep$obj8 = deep.obj)[_ref8 = ++key] && (_deep$obj8[_ref8] = 3)).toBe(0); expect((_deep$obj8 = deep.obj)[_key8 = ++key] && (_deep$obj8[_key8] = 3)).toBe(0);
expect(gets).toBe(8); expect(gets).toBe(8);
expect(key).toBe(1); expect(key).toBe(1);

View File

@ -1,4 +1,4 @@
var _obj$x, _obj$x2, _deep$obj, _x, _deep$obj2, _x2, _ref, _obj, _ref2, _obj2, _deep$obj3, _ref3, _ref4, _deep$obj4, _ref5, _ref6; var _obj$x, _obj$x2, _deep$obj, _deep$obj$x, _deep$obj2, _deep$obj2$x, _key, _obj$_key, _key2, _obj$_key2, _deep$obj3, _key3, _deep$obj3$_key, _deep$obj4, _key4, _deep$obj4$_key;
var x = undefined; var x = undefined;
var sets = 0; var sets = 0;
@ -26,23 +26,23 @@ var deep = {
}; };
obj.x = undefined; obj.x = undefined;
expect((_x = (_deep$obj = deep.obj).x) !== null && _x !== void 0 ? _x : _deep$obj.x = 1).toBe(1); expect((_deep$obj$x = (_deep$obj = deep.obj).x) !== null && _deep$obj$x !== void 0 ? _deep$obj$x : _deep$obj.x = 1).toBe(1);
expect(gets, 1); expect(gets, 1);
expect((_x2 = (_deep$obj2 = deep.obj).x) !== null && _x2 !== void 0 ? _x2 : _deep$obj2.x = 2).toBe(1); expect((_deep$obj2$x = (_deep$obj2 = deep.obj).x) !== null && _deep$obj2$x !== void 0 ? _deep$obj2$x : _deep$obj2.x = 2).toBe(1);
expect(gets, 2); expect(gets, 2);
var key = 0; var key = 0;
obj.x = undefined; obj.x = undefined;
expect((_obj = obj[_ref = ++key]) !== null && _obj !== void 0 ? _obj : obj[_ref] = 1).toBe(1); expect((_obj$_key = obj[_key = ++key]) !== null && _obj$_key !== void 0 ? _obj$_key : obj[_key] = 1).toBe(1);
expect(key, 1); expect(key, 1);
key = 0; key = 0;
expect((_obj2 = obj[_ref2 = ++key]) !== null && _obj2 !== void 0 ? _obj2 : obj[_ref2] = 2).toBe(1); expect((_obj$_key2 = obj[_key2 = ++key]) !== null && _obj$_key2 !== void 0 ? _obj$_key2 : obj[_key2] = 2).toBe(1);
expect(key, 1); expect(key, 1);
obj.x = undefined; obj.x = undefined;
key = 0; key = 0;
expect((_ref4 = (_deep$obj3 = deep.obj)[_ref3 = ++key]) !== null && _ref4 !== void 0 ? _ref4 : _deep$obj3[_ref3] = 1).toBe(1); expect((_deep$obj3$_key = (_deep$obj3 = deep.obj)[_key3 = ++key]) !== null && _deep$obj3$_key !== void 0 ? _deep$obj3$_key : _deep$obj3[_key3] = 1).toBe(1);
expect(gets, 3); expect(gets, 3);
expect(key, 1); expect(key, 1);
key = 0; key = 0;
expect((_ref6 = (_deep$obj4 = deep.obj)[_ref5 = ++key]) !== null && _ref6 !== void 0 ? _ref6 : _deep$obj4[_ref5] = 2).toBe(1); expect((_deep$obj4$_key = (_deep$obj4 = deep.obj)[_key4 = ++key]) !== null && _deep$obj4$_key !== void 0 ? _deep$obj4$_key : _deep$obj4[_key4] = 2).toBe(1);
expect(gets, 4); expect(gets, 4);
expect(key, 1); expect(key, 1);

View File

@ -6,11 +6,11 @@ var _$a = {
1: 1, 1: 1,
a: 1 a: 1
}, },
_ref = key++, _key = key++,
{ {
[_ref]: y [_key]: y
} = _$a, } = _$a,
x = babelHelpers.objectWithoutProperties(_$a, [_ref].map(babelHelpers.toPropertyKey)); x = babelHelpers.objectWithoutProperties(_$a, [_key].map(babelHelpers.toPropertyKey));
expect(x).toEqual({ expect(x).toEqual({
a: 1 a: 1
@ -24,13 +24,13 @@ var _$ = {
2: 2, 2: 2,
3: 3 3: 3
}, },
_ref2 = ++key, _key2 = ++key,
_ref3 = ++key, _key3 = ++key,
{ {
[_ref2]: y, [_key2]: y,
[_ref3]: z [_key3]: z
} = _$, } = _$,
rest = babelHelpers.objectWithoutProperties(_$, [_ref2, _ref3].map(babelHelpers.toPropertyKey)); rest = babelHelpers.objectWithoutProperties(_$, [_key2, _key3].map(babelHelpers.toPropertyKey));
expect(y).toBe(2); expect(y).toBe(2);
expect(z).toBe(3); // pure, computed property should remain as-is expect(z).toBe(3); // pure, computed property should remain as-is

View File

@ -2,9 +2,9 @@ var key, x, y, z; // impure
key = 1; key = 1;
var _ref = key++, var _key = key++,
{ {
[_ref]: { [_key]: {
y y
} }
} = { } = {
@ -18,7 +18,7 @@ var _ref = key++,
a: 1, a: 1,
y: 1 y: 1
} }
}[_ref], ["y"]); }[_key], ["y"]);
expect(x).toEqual({ expect(x).toEqual({
a: 1 a: 1
@ -38,18 +38,18 @@ var _$ = {
z: 3 z: 3
} }
}, },
_ref2 = ++key, _key2 = ++key,
_ref3 = ++key, _key3 = ++key,
{ {
[_ref3]: { [_key3]: {
y y
}, },
[_ref2]: { [_key2]: {
z z
} }
} = _$, } = _$,
rest_y = babelHelpers.objectWithoutProperties(_$[_ref3], ["y"]), rest_y = babelHelpers.objectWithoutProperties(_$[_key3], ["y"]),
rest_z = babelHelpers.objectWithoutProperties(_$[_ref2], ["z"]); rest_z = babelHelpers.objectWithoutProperties(_$[_key2], ["z"]);
expect(y).toBe(2); expect(y).toBe(2);
expect(rest_y).toEqual({ expect(rest_y).toEqual({

View File

@ -1,13 +1,13 @@
"use strict"; "use strict";
var _a, _param, _foo, _x, _param2, _bar, _param3, _obj$baz, _obj; var _a, _b, _foo, _x, _y, _bar, _d, _obj$baz, _obj;
const q = (_foo = foo, _a = a, _param = [...b], function foo(_argPlaceholder) { const q = (_foo = foo, _a = a, _b = [...b], function foo(_argPlaceholder) {
return _foo(_a, ..._param, _argPlaceholder); return _foo(_a, ..._b, _argPlaceholder);
}); });
const w = (_bar = bar, _x = x, _param2 = [...y], function bar(_argPlaceholder2, _argPlaceholder3) { const w = (_bar = bar, _x = x, _y = [...y], function bar(_argPlaceholder2, _argPlaceholder3) {
return _bar(1, _x, _argPlaceholder2, ..._param2, _argPlaceholder3, 2); return _bar(1, _x, _argPlaceholder2, ..._y, _argPlaceholder3, 2);
}); });
const z = (_obj = obj, _obj$baz = _obj.baz, _param3 = [...d], function baz(_argPlaceholder4) { const z = (_obj = obj, _obj$baz = _obj.baz, _d = [...d], function baz(_argPlaceholder4) {
return _obj$baz.call(_obj, _argPlaceholder4, ..._param3); return _obj$baz.call(_obj, _argPlaceholder4, ..._d);
}); });

View File

@ -1,4 +1,4 @@
var _ref, _ref2; var _ref, _;
const abs = Math.abs; const abs = Math.abs;
const value = (_ref = (_ref2 = -5.9, abs(_ref2)), Math.floor(_ref)); const value = (_ref = (_ = -5.9, abs(_)), Math.floor(_ref));

View File

@ -1,10 +1,10 @@
var _ref, _ref2, _ref3; var _ref, _ref2, _;
function area(rect) { function area(rect) {
return rect.width * rect.height; return rect.width * rect.height;
} }
const result = (_ref = (_ref2 = (_ref3 = -5, Math.abs(_ref3)), { const result = (_ref = (_ref2 = (_ = -5, Math.abs(_)), {
width: _ref2, width: _ref2,
height: _ref2 + 3 height: _ref2 + 3
}), area(_ref)); }), area(_ref));

View File

@ -1,7 +1,7 @@
var _ref, _ref2, _ref3, _ref4; var _ref, _ref2, _ref3, _;
const result = (_ref = (_ref2 = (_ref3 = (_ref4 = -2.2 // -2.2 const result = (_ref = (_ref2 = (_ref3 = (_ = -2.2 // -2.2
, Math.floor(_ref4) // -3 , Math.floor(_) // -3
), () => Math.pow(_ref3, 5) // () => -243 ), () => Math.pow(_ref3, 5) // () => -243
), _ref2() // -243 ), _ref2() // -243
), Math.sign(_ref)); // -1 ), Math.sign(_ref)); // -1

View File

@ -1,6 +1,6 @@
function first() { function first() {
var _ref; var _index;
var index = 0; var index = 0;
return _ref = index++, _ref < 0 || arguments.length <= _ref ? undefined : arguments[_ref]; return _index = index++, _index < 0 || arguments.length <= _index ? undefined : arguments[_index];
} }

View File

@ -11,37 +11,141 @@ import { scope as scopeCache } from "../cache";
// Recursively gathers the identifying names of a node. // Recursively gathers the identifying names of a node.
function gatherNodeParts(node: Object, parts: Array) { function gatherNodeParts(node: Object, parts: Array) {
if (t.isModuleDeclaration(node)) { switch (node?.type) {
if (node.source) { default:
gatherNodeParts(node.source, parts); if (t.isModuleDeclaration(node)) {
} else if (node.specifiers && node.specifiers.length) { if (node.source) {
for (const specifier of (node.specifiers: Array)) { gatherNodeParts(node.source, parts);
gatherNodeParts(specifier, parts); } else if (node.specifiers && node.specifiers.length) {
for (const e of node.specifiers) gatherNodeParts(e, parts);
} else if (node.declaration) {
gatherNodeParts(node.declaration, parts);
}
} else if (t.isModuleSpecifier(node)) {
gatherNodeParts(node.local, parts);
} else if (t.isLiteral(node)) {
parts.push(node.value);
} }
} else if (node.declaration) { break;
gatherNodeParts(node.declaration, parts);
} case "MemberExpression":
} else if (t.isModuleSpecifier(node)) { case "OptionalMemberExpression":
gatherNodeParts(node.local, parts); case "JSXMemberExpression":
} else if (t.isMemberExpression(node)) { gatherNodeParts(node.object, parts);
gatherNodeParts(node.object, parts); gatherNodeParts(node.property, parts);
gatherNodeParts(node.property, parts); break;
} else if (t.isIdentifier(node)) {
parts.push(node.name); case "Identifier":
} else if (t.isLiteral(node)) { case "JSXIdentifier":
parts.push(node.value); parts.push(node.name);
} else if (t.isCallExpression(node)) { break;
gatherNodeParts(node.callee, parts);
} else if (t.isObjectExpression(node) || t.isObjectPattern(node)) { case "CallExpression":
for (const prop of (node.properties: Array)) { case "OptionalCallExpression":
gatherNodeParts(prop.key || prop.argument, parts); case "NewExpression":
} gatherNodeParts(node.callee, parts);
} else if (t.isPrivateName(node)) { break;
gatherNodeParts(node.id, parts);
} else if (t.isThisExpression(node)) { case "ObjectExpression":
parts.push("this"); case "ObjectPattern":
} else if (t.isSuper(node)) { for (const e of node.properties) {
parts.push("super"); gatherNodeParts(e, parts);
}
break;
case "SpreadElement":
case "RestElement":
gatherNodeParts(node.argument, parts);
break;
case "ObjectProperty":
case "ObjectMethod":
case "ClassProperty":
case "ClassMethod":
case "ClassPrivateProperty":
case "ClassPrivateMethod":
gatherNodeParts(node.key, parts);
break;
case "ThisExpression":
parts.push("this");
break;
case "Super":
parts.push("super");
break;
case "Import":
parts.push("import");
break;
case "DoExpression":
parts.push("do");
break;
case "YieldExpression":
parts.push("yield");
gatherNodeParts(node.argument, parts);
break;
case "AwaitExpression":
parts.push("await");
gatherNodeParts(node.argument, parts);
break;
case "AssignmentExpression":
gatherNodeParts(node.left, parts);
break;
case "VariableDeclarator":
gatherNodeParts(node.id, parts);
break;
case "FunctionExpression":
case "FunctionDeclaration":
case "ClassExpression":
case "ClassDeclaration":
gatherNodeParts(node.id, parts);
break;
case "PrivateName":
gatherNodeParts(node.id, parts);
break;
case "ParenthesizedExpression":
gatherNodeParts(node.expression, parts);
break;
case "UnaryExpression":
case "UpdateExpression":
gatherNodeParts(node.argument, parts);
break;
case "MetaProperty":
gatherNodeParts(node.meta, parts);
gatherNodeParts(node.property, parts);
break;
case "JSXElement":
gatherNodeParts(node.openingElement, parts);
break;
case "JSXOpeningElement":
parts.push(node.name);
break;
case "JSXFragment":
gatherNodeParts(node.openingFragment, parts);
break;
case "JSXOpeningFragment":
parts.push("Fragment");
break;
case "JSXNamespacedName":
gatherNodeParts(node.namespace, parts);
gatherNodeParts(node.name, parts);
break;
} }
} }
@ -267,17 +371,7 @@ export default class Scope {
return `_${id}`; return `_${id}`;
} }
generateUidBasedOnNode(parent: Object, defaultName?: String) { generateUidBasedOnNode(node: Object, defaultName?: String) {
let node = parent;
if (t.isAssignmentExpression(parent)) {
node = parent.left;
} else if (t.isVariableDeclarator(parent)) {
node = parent.id;
} else if (t.isObjectProperty(node) || t.isObjectMethod(node)) {
node = node.key;
}
const parts = []; const parts = [];
gatherNodeParts(node, parts); gatherNodeParts(node, parts);
@ -291,11 +385,8 @@ export default class Scope {
* Generate a unique identifier based on a node. * Generate a unique identifier based on a node.
*/ */
generateUidIdentifierBasedOnNode( generateUidIdentifierBasedOnNode(node: Object, defaultName?: String): Object {
parent: Object, return t.identifier(this.generateUidBasedOnNode(node, defaultName));
defaultName?: String,
): Object {
return t.identifier(this.generateUidBasedOnNode(parent, defaultName));
} }
/** /**