/
index.js
172 lines (141 loc) 路 4.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { declare } from "@babel/helper-plugin-utils";
import { types as t } from "@babel/core";
export default declare((api, options) => {
api.assertVersion(7);
const { loose } = options;
function getSpreadLiteral(spread, scope) {
if (loose && !t.isIdentifier(spread.argument, { name: "arguments" })) {
return spread.argument;
} else {
return scope.toArray(spread.argument, true);
}
}
function hasSpread(nodes) {
for (let i = 0; i < nodes.length; i++) {
if (t.isSpreadElement(nodes[i])) {
return true;
}
}
return false;
}
function push(_props, nodes) {
if (!_props.length) return _props;
nodes.push(t.arrayExpression(_props));
return [];
}
function build(props: Array, scope) {
const nodes = [];
let _props = [];
for (const prop of props) {
if (t.isSpreadElement(prop)) {
_props = push(_props, nodes);
nodes.push(getSpreadLiteral(prop, scope));
} else {
_props.push(prop);
}
}
push(_props, nodes);
return nodes;
}
return {
name: "transform-spread",
visitor: {
ArrayExpression(path) {
const { node, scope } = path;
const elements = node.elements;
if (!hasSpread(elements)) return;
const nodes = build(elements, scope);
let first = nodes[0];
// If there is only one element in the ArrayExpression and
// the element was transformed (Array.prototype.slice.call or toConsumableArray)
// we know that the transformed code already takes care of cloning the array.
// So we can simply return that element.
if (nodes.length === 1 && first !== elements[0].argument) {
path.replaceWith(first);
return;
}
// If the first element is a ArrayExpression we can directly call
// concat on it.
// `[..].concat(..)`
// If not then we have to use `[].concat(arr)` and not `arr.concat`
// because `arr` could be extended/modified (e.g. Immutable) and we do not know exactly
// what concat would produce.
if (!t.isArrayExpression(first)) {
first = t.arrayExpression([]);
} else {
nodes.shift();
}
path.replaceWith(
t.callExpression(
t.memberExpression(first, t.identifier("concat")),
nodes,
),
);
},
CallExpression(path) {
const { node, scope } = path;
const args = node.arguments;
if (!hasSpread(args)) return;
const calleePath = path.get("callee");
if (calleePath.isSuper()) return;
let contextLiteral = scope.buildUndefinedNode();
node.arguments = [];
let nodes;
if (args.length === 1 && args[0].argument.name === "arguments") {
nodes = [args[0].argument];
} else {
nodes = build(args, scope);
}
const first = nodes.shift();
if (nodes.length) {
node.arguments.push(
t.callExpression(
t.memberExpression(first, t.identifier("concat")),
nodes,
),
);
} else {
node.arguments.push(first);
}
const callee = node.callee;
if (calleePath.isMemberExpression()) {
const temp = scope.maybeGenerateMemoised(callee.object);
if (temp) {
callee.object = t.assignmentExpression("=", temp, callee.object);
contextLiteral = temp;
} else {
contextLiteral = t.cloneNode(callee.object);
}
t.appendToMemberExpression(callee, t.identifier("apply"));
} else {
node.callee = t.memberExpression(node.callee, t.identifier("apply"));
}
if (t.isSuper(contextLiteral)) {
contextLiteral = t.thisExpression();
}
node.arguments.unshift(t.cloneNode(contextLiteral));
},
NewExpression(path) {
const { node, scope } = path;
let args = node.arguments;
if (!hasSpread(args)) return;
const nodes = build(args, scope);
const first = nodes.shift();
if (nodes.length) {
args = t.callExpression(
t.memberExpression(first, t.identifier("concat")),
nodes,
);
} else {
args = first;
}
path.replaceWith(
t.callExpression(path.hub.addHelper("construct"), [
node.callee,
args,
]),
);
},
},
};
});