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

View File

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

View File

@ -1,8 +1,8 @@
function* g() {
let A = babelHelpers.decorate([dec], function (_initialize, _super) {
let A = babelHelpers.decorate([dec], function (_initialize, _yield$B) {
"use strict";
class A extends _super {
class A extends _yield$B {
constructor(...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 sets = 0;
@ -38,30 +38,30 @@ expect(gets).toBe(3);
expect((_deep$obj4 = deep.obj).x && (_deep$obj4.x = 3)).toBe(0);
expect(gets).toBe(4);
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);
key = 0;
expect(obj[_ref2 = ++key] || (obj[_ref2] = 2)).toBe(1);
expect(obj[_key2 = ++key] || (obj[_key2] = 2)).toBe(1);
expect(key).toBe(1);
key = 0;
expect(obj[_ref3 = ++key] && (obj[_ref3] = 0)).toBe(0);
expect(obj[_key3 = ++key] && (obj[_key3] = 0)).toBe(0);
expect(key).toBe(1);
key = 0;
expect(obj[_ref4 = ++key] && (obj[_ref4] = 3)).toBe(0);
expect(obj[_key4 = ++key] && (obj[_key4] = 3)).toBe(0);
expect(key).toBe(1);
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(key).toBe(1);
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(key).toBe(1);
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(key).toBe(1);
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(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 sets = 0;
@ -26,23 +26,23 @@ var deep = {
};
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((_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);
var key = 0;
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);
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);
obj.x = undefined;
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(key, 1);
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(key, 1);

View File

@ -6,11 +6,11 @@ var _$a = {
1: 1,
a: 1
},
_ref = key++,
_key = key++,
{
[_ref]: y
[_key]: y
} = _$a,
x = babelHelpers.objectWithoutProperties(_$a, [_ref].map(babelHelpers.toPropertyKey));
x = babelHelpers.objectWithoutProperties(_$a, [_key].map(babelHelpers.toPropertyKey));
expect(x).toEqual({
a: 1
@ -24,13 +24,13 @@ var _$ = {
2: 2,
3: 3
},
_ref2 = ++key,
_ref3 = ++key,
_key2 = ++key,
_key3 = ++key,
{
[_ref2]: y,
[_ref3]: z
[_key2]: y,
[_key3]: z
} = _$,
rest = babelHelpers.objectWithoutProperties(_$, [_ref2, _ref3].map(babelHelpers.toPropertyKey));
rest = babelHelpers.objectWithoutProperties(_$, [_key2, _key3].map(babelHelpers.toPropertyKey));
expect(y).toBe(2);
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;
var _ref = key++,
var _key = key++,
{
[_ref]: {
[_key]: {
y
}
} = {
@ -18,7 +18,7 @@ var _ref = key++,
a: 1,
y: 1
}
}[_ref], ["y"]);
}[_key], ["y"]);
expect(x).toEqual({
a: 1
@ -38,18 +38,18 @@ var _$ = {
z: 3
}
},
_ref2 = ++key,
_ref3 = ++key,
_key2 = ++key,
_key3 = ++key,
{
[_ref3]: {
[_key3]: {
y
},
[_ref2]: {
[_key2]: {
z
}
} = _$,
rest_y = babelHelpers.objectWithoutProperties(_$[_ref3], ["y"]),
rest_z = babelHelpers.objectWithoutProperties(_$[_ref2], ["z"]);
rest_y = babelHelpers.objectWithoutProperties(_$[_key3], ["y"]),
rest_z = babelHelpers.objectWithoutProperties(_$[_key2], ["z"]);
expect(y).toBe(2);
expect(rest_y).toEqual({

View File

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

View File

@ -1,4 +1,4 @@
var _ref, _ref2;
var _ref, _;
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) {
return rect.width * rect.height;
}
const result = (_ref = (_ref2 = (_ref3 = -5, Math.abs(_ref3)), {
const result = (_ref = (_ref2 = (_ = -5, Math.abs(_)), {
width: _ref2,
height: _ref2 + 3
}), 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
, Math.floor(_ref4) // -3
const result = (_ref = (_ref2 = (_ref3 = (_ = -2.2 // -2.2
, Math.floor(_) // -3
), () => Math.pow(_ref3, 5) // () => -243
), _ref2() // -243
), Math.sign(_ref)); // -1

View File

@ -1,6 +1,6 @@
function first() {
var _ref;
var _index;
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.
function gatherNodeParts(node: Object, parts: Array) {
if (t.isModuleDeclaration(node)) {
if (node.source) {
gatherNodeParts(node.source, parts);
} else if (node.specifiers && node.specifiers.length) {
for (const specifier of (node.specifiers: Array)) {
gatherNodeParts(specifier, parts);
switch (node?.type) {
default:
if (t.isModuleDeclaration(node)) {
if (node.source) {
gatherNodeParts(node.source, 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) {
gatherNodeParts(node.declaration, parts);
}
} else if (t.isModuleSpecifier(node)) {
gatherNodeParts(node.local, parts);
} else if (t.isMemberExpression(node)) {
gatherNodeParts(node.object, parts);
gatherNodeParts(node.property, parts);
} else if (t.isIdentifier(node)) {
parts.push(node.name);
} else if (t.isLiteral(node)) {
parts.push(node.value);
} else if (t.isCallExpression(node)) {
gatherNodeParts(node.callee, parts);
} else if (t.isObjectExpression(node) || t.isObjectPattern(node)) {
for (const prop of (node.properties: Array)) {
gatherNodeParts(prop.key || prop.argument, parts);
}
} else if (t.isPrivateName(node)) {
gatherNodeParts(node.id, parts);
} else if (t.isThisExpression(node)) {
parts.push("this");
} else if (t.isSuper(node)) {
parts.push("super");
break;
case "MemberExpression":
case "OptionalMemberExpression":
case "JSXMemberExpression":
gatherNodeParts(node.object, parts);
gatherNodeParts(node.property, parts);
break;
case "Identifier":
case "JSXIdentifier":
parts.push(node.name);
break;
case "CallExpression":
case "OptionalCallExpression":
case "NewExpression":
gatherNodeParts(node.callee, parts);
break;
case "ObjectExpression":
case "ObjectPattern":
for (const e of node.properties) {
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}`;
}
generateUidBasedOnNode(parent: 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;
}
generateUidBasedOnNode(node: Object, defaultName?: String) {
const parts = [];
gatherNodeParts(node, parts);
@ -291,11 +385,8 @@ export default class Scope {
* Generate a unique identifier based on a node.
*/
generateUidIdentifierBasedOnNode(
parent: Object,
defaultName?: String,
): Object {
return t.identifier(this.generateUidBasedOnNode(parent, defaultName));
generateUidIdentifierBasedOnNode(node: Object, defaultName?: String): Object {
return t.identifier(this.generateUidBasedOnNode(node, defaultName));
}
/**