Convert @babel/helper-explode-assignable-expression to ts (#12417)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
parent
8316cdaa8a
commit
62c1ccbf81
@ -1,12 +1,13 @@
|
|||||||
import type { Scope } from "@babel/traverse";
|
import type { Scope } from "@babel/traverse";
|
||||||
import * as t from "@babel/types";
|
import * as t from "@babel/types";
|
||||||
|
|
||||||
function getObjRef(node, nodes, file, scope) {
|
function getObjRef(
|
||||||
|
node: t.Identifier | t.MemberExpression,
|
||||||
|
nodes: Array<t.AssignmentExpression>,
|
||||||
|
scope: Scope,
|
||||||
|
): t.Identifier | t.Super {
|
||||||
let ref;
|
let ref;
|
||||||
if (t.isSuper(node)) {
|
if (t.isIdentifier(node)) {
|
||||||
// Super cannot be directly assigned so lets return it directly
|
|
||||||
return node;
|
|
||||||
} else if (t.isIdentifier(node)) {
|
|
||||||
if (scope.hasBinding(node.name)) {
|
if (scope.hasBinding(node.name)) {
|
||||||
// this variable is declared in scope so we can be 100% sure
|
// this variable is declared in scope so we can be 100% sure
|
||||||
// that evaluating it multiple times wont trigger a getter
|
// that evaluating it multiple times wont trigger a getter
|
||||||
@ -28,7 +29,7 @@ function getObjRef(node, nodes, file, scope) {
|
|||||||
return ref;
|
return ref;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`We can't explode this node type ${node.type}`);
|
throw new Error(`We can't explode this node type ${node["type"]}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const temp = scope.generateUidIdentifierBasedOnNode(ref);
|
const temp = scope.generateUidIdentifierBasedOnNode(ref);
|
||||||
@ -37,8 +38,17 @@ function getObjRef(node, nodes, file, scope) {
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPropRef(node, nodes, file, scope) {
|
function getPropRef(
|
||||||
|
node: t.MemberExpression,
|
||||||
|
nodes: Array<t.AssignmentExpression>,
|
||||||
|
scope: Scope,
|
||||||
|
): t.Identifier | t.Literal {
|
||||||
const prop = node.property;
|
const prop = node.property;
|
||||||
|
if (t.isPrivateName(prop)) {
|
||||||
|
throw new Error(
|
||||||
|
"We can't generate property ref for private name, please install `@babel/plugin-proposal-class-properties`",
|
||||||
|
);
|
||||||
|
}
|
||||||
const key = t.toComputedKey(node, prop);
|
const key = t.toComputedKey(node, prop);
|
||||||
if (t.isLiteral(key) && t.isPureish(key)) return key;
|
if (t.isLiteral(key) && t.isPureish(key)) return key;
|
||||||
|
|
||||||
@ -48,21 +58,22 @@ function getPropRef(node, nodes, file, scope) {
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(Babel 8): Remove the "file" parameter
|
||||||
export default function (
|
export default function (
|
||||||
node: Object,
|
node: t.Identifier | t.MemberExpression,
|
||||||
nodes: Array<Object>,
|
nodes: Array<t.AssignmentExpression>,
|
||||||
file,
|
file: void,
|
||||||
scope: Scope,
|
scope: Scope,
|
||||||
allowedSingleIdent?: boolean,
|
allowedSingleIdent?: boolean,
|
||||||
): {
|
): {
|
||||||
uid: Object,
|
uid: t.Identifier | t.MemberExpression;
|
||||||
ref: Object,
|
ref: t.Identifier | t.MemberExpression;
|
||||||
} {
|
} {
|
||||||
let obj;
|
let obj;
|
||||||
if (t.isIdentifier(node) && allowedSingleIdent) {
|
if (t.isIdentifier(node) && allowedSingleIdent) {
|
||||||
obj = node;
|
obj = node;
|
||||||
} else {
|
} else {
|
||||||
obj = getObjRef(node, nodes, file, scope);
|
obj = getObjRef(node, nodes, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
let ref, uid;
|
let ref, uid;
|
||||||
@ -71,7 +82,7 @@ export default function (
|
|||||||
ref = t.cloneNode(node);
|
ref = t.cloneNode(node);
|
||||||
uid = obj;
|
uid = obj;
|
||||||
} else {
|
} else {
|
||||||
const prop = getPropRef(node, nodes, file, scope);
|
const prop = getPropRef(node, nodes, scope);
|
||||||
const computed = node.computed || t.isLiteral(prop);
|
const computed = node.computed || t.isLiteral(prop);
|
||||||
uid = t.memberExpression(t.cloneNode(obj), t.cloneNode(prop), computed);
|
uid = t.memberExpression(t.cloneNode(obj), t.cloneNode(prop), computed);
|
||||||
ref = t.memberExpression(t.cloneNode(obj), t.cloneNode(prop), computed);
|
ref = t.memberExpression(t.cloneNode(obj), t.cloneNode(prop), computed);
|
||||||
Loading…
x
Reference in New Issue
Block a user