MCPcopy Create free account
hub / github.com/babel/babel / ArrayExpression

Function ArrayExpression

packages/babel-plugin-transform-spread/src/index.ts:153–191  ·  view source on GitHub ↗
(path)

Source from the content-addressed store, hash-verified

151
152 visitor: {
153 ArrayExpression(path): void {
154 const { node, scope } = path;
155 const elements = node.elements;
156 if (!hasSpread(elements)) return;
157
158 const nodes = build(elements, scope);
159 let first = nodes[0];
160
161 // If there is only one element in the ArrayExpression and
162 // the element was transformed (Array.prototype.slice.call or toConsumableArray)
163 // we know that the transformed code already takes care of cloning the array.
164 // So we can simply return that element.
165 if (
166 nodes.length === 1 &&
167 first !== (elements[0] as t.SpreadElement).argument
168 ) {
169 path.replaceWith(first);
170 return;
171 }
172
173 // If the first element is a ArrayExpression we can directly call
174 // concat on it.
175 // `[..].concat(..)`
176 // If not then we have to use `[].concat(arr)` and not `arr.concat`
177 // because `arr` could be extended/modified (e.g. Immutable) and we do not know exactly
178 // what concat would produce.
179 if (!t.isArrayExpression(first)) {
180 first = t.arrayExpression([]);
181 } else {
182 nodes.shift();
183 }
184
185 path.replaceWith(
186 t.callExpression(
187 t.memberExpression(first, t.identifier("concat")),
188 nodes,
189 ),
190 );
191 },
192 CallExpression(path): void {
193 const { node, scope } = path;
194

Callers

nothing calls this directly

Calls 3

isArrayExpressionMethod · 0.80
hasSpreadFunction · 0.70
buildFunction · 0.70

Tested by

no test coverage detected