Skip to content

Commit

Permalink
Convert @babel/helper-explode-assignable-expression to ts (#12417)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
JLHwung and nicolo-ribaudo committed Feb 20, 2021
1 parent 8316cda commit 62c1ccb
Showing 1 changed file with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 62c1ccb

Please sign in to comment.