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

Function ArrayExpression

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

Source from the content-addressed store, hash-verified

112
113 visitor: {
114 ArrayExpression(path): void {
115 const { node, scope } = path;
116 const elements = node.elements;
117 if (!hasSpread(elements)) return;
118
119 const nodes = build(elements, scope);
120 let first = nodes[0];
121
122 // If there is only one element in the ArrayExpression and
123 // the element was transformed (Array.prototype.slice.call or toConsumableArray)
124 // we know that the transformed code already takes care of cloning the array.
125 // So we can simply return that element.
126 if (
127 nodes.length === 1 &&
128 first !== (elements[0] as t.SpreadElement).argument
129 ) {
130 path.replaceWith(first);
131 return;
132 }
133
134 // If the first element is a ArrayExpression we can directly call
135 // concat on it.
136 // `[..].concat(..)`
137 // If not then we have to use `[].concat(arr)` and not `arr.concat`
138 // because `arr` could be extended/modified (e.g. Immutable) and we do not know exactly
139 // what concat would produce.
140 if (!t.isArrayExpression(first)) {
141 first = t.arrayExpression([]);
142 } else {
143 nodes.shift();
144 }
145
146 path.replaceWith(
147 t.callExpression(
148 t.memberExpression(first, t.identifier("concat")),
149 nodes,
150 ),
151 );
152 },
153 CallExpression(path): void {
154 const { node, scope } = path;
155

Callers

nothing calls this directly

Calls 3

isArrayExpressionMethod · 0.80
hasSpreadFunction · 0.70
buildFunction · 0.70

Tested by

no test coverage detected