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 * 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;
|
||||
if (t.isSuper(node)) {
|
||||
// Super cannot be directly assigned so lets return it directly
|
||||
return node;
|
||||
} else if (t.isIdentifier(node)) {
|
||||
if (t.isIdentifier(node)) {
|
||||
if (scope.hasBinding(node.name)) {
|
||||
// this variable is declared in scope so we can be 100% sure
|
||||
// that evaluating it multiple times wont trigger a getter
|
||||
@ -28,7 +29,7 @@ function getObjRef(node, nodes, file, scope) {
|
||||
return ref;
|
||||
}
|
||||
} 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);
|
||||
@ -37,8 +38,17 @@ function getObjRef(node, nodes, file, scope) {
|
||||
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;
|
||||
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);
|
||||
if (t.isLiteral(key) && t.isPureish(key)) return key;
|
||||
|
||||
@ -48,21 +58,22 @@ function getPropRef(node, nodes, file, scope) {
|
||||
return temp;
|
||||
}
|
||||
|
||||
// TODO(Babel 8): Remove the "file" parameter
|
||||
export default function (
|
||||
node: Object,
|
||||
nodes: Array<Object>,
|
||||
file,
|
||||
node: t.Identifier | t.MemberExpression,
|
||||
nodes: Array<t.AssignmentExpression>,
|
||||
file: void,
|
||||
scope: Scope,
|
||||
allowedSingleIdent?: boolean,
|
||||
): {
|
||||
uid: Object,
|
||||
ref: Object,
|
||||
uid: t.Identifier | t.MemberExpression;
|
||||
ref: t.Identifier | t.MemberExpression;
|
||||
} {
|
||||
let obj;
|
||||
if (t.isIdentifier(node) && allowedSingleIdent) {
|
||||
obj = node;
|
||||
} else {
|
||||
obj = getObjRef(node, nodes, file, scope);
|
||||
obj = getObjRef(node, nodes, scope);
|
||||
}
|
||||
|
||||
let ref, uid;
|
||||
@ -71,7 +82,7 @@ export default function (
|
||||
ref = t.cloneNode(node);
|
||||
uid = obj;
|
||||
} else {
|
||||
const prop = getPropRef(node, nodes, file, scope);
|
||||
const prop = getPropRef(node, nodes, scope);
|
||||
const computed = node.computed || t.isLiteral(prop);
|
||||
uid = 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