Debian Codegraph @

packages/babel-helper-explode-assignable-expression/src/index.ts

raw
1 import type { Scope } from "@babel/traverse";
2 import {
3 assignmentExpression,
4 cloneNode,
5 isIdentifier,
6 isLiteral,
7 isMemberExpression,
8 isPrivateName,
9 isPureish,
10 isSuper,
11 memberExpression,
12 toComputedKey,
13 } from "@babel/types";
14 import type * as t from "@babel/types";
15
16 function getObjRef(
17 node: t.Identifier | t.MemberExpression,
18 nodes: Array<t.AssignmentExpression>,
19 scope: Scope,
20 ): t.Identifier | t.Super {
21 let ref;
22 if (isIdentifier(node)) {
23 if (scope.hasBinding(node.name)) {
24 // this variable is declared in scope so we can be 100% sure
25 // that evaluating it multiple times wont trigger a getter
26 // or something else
27 return node;
28 } else {
29 // could possibly trigger a getter so we need to only evaluate
30 // it once
31 ref = node;
32 }
33 } else if (isMemberExpression(node)) {
34 ref = node.object;
35
36 if (isSuper(ref) || (isIdentifier(ref) && scope.hasBinding(ref.name))) {
37 // the object reference that we need to save is locally declared
38 // so as per the previous comment we can be 100% sure evaluating
39 // it multiple times will be safe
40 // Super cannot be directly assigned so lets return it also
41 return ref;
42 }
43 } else {
44 throw new Error(`We can't explode this node type ${node["type"]}`);
45 }
46
47 const temp = scope.generateUidIdentifierBasedOnNode(ref);
48 scope.push({ id: temp });
49 nodes.push(assignmentExpression("=", cloneNode(temp), cloneNode(ref)));
50 return temp;
51 }
52
53 function getPropRef(
54 node: t.MemberExpression,
55 nodes: Array<t.AssignmentExpression>,
56 scope: Scope,
57 ): t.Identifier | t.Literal {
58 const prop = node.property;
59 if (isPrivateName(prop)) {
60 throw new Error(
61 "We can't generate property ref for private name, please install `@babel/plugin-proposal-class-properties`",
62 );
63 }
64 const key = toComputedKey(node, prop);
65 if (isLiteral(key) && isPureish(key)) return key;
66
67 const temp = scope.generateUidIdentifierBasedOnNode(prop);
68 scope.push({ id: temp });
69 nodes.push(assignmentExpression("=", cloneNode(temp), cloneNode(prop)));
70 return temp;
71 }
72
73 // TODO(Babel 8): Remove the "file" parameter
74 export default function (
75 node: t.Identifier | t.MemberExpression,
76 nodes: Array<t.AssignmentExpression>,
77 file: void,
78 scope: Scope,
79 allowedSingleIdent?: boolean,
80 ): {
81 uid: t.Identifier | t.MemberExpression | t.Super;
82 ref: t.Identifier | t.MemberExpression;
83 } {
84 let obj;
85 if (isIdentifier(node) && allowedSingleIdent) {
86 obj = node;
87 } else {
88 obj = getObjRef(node, nodes, scope);
89 }
90
91 let ref, uid;
92
93 if (isIdentifier(node)) {
94 ref = cloneNode(node);
95 uid = obj;
96 } else {
97 const prop = getPropRef(node, nodes, scope);
98 const computed = node.computed || isLiteral(prop);
99 uid = memberExpression(cloneNode(obj), cloneNode(prop), computed);
100 ref = memberExpression(cloneNode(obj), cloneNode(prop), computed);
101 }
102
103 return {
104 uid: uid,
105 ref: ref,
106 };
107 }