44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
import {isSpreadElement} from '@/astride/types.js';
|
|
|
|
export default function(node, {evaluate, scope}) {
|
|
const elements = [];
|
|
const asyncSpread = Object.create(null);
|
|
let isAsync = false;
|
|
for (const index in node.elements) {
|
|
const element = node.elements[index];
|
|
if (isSpreadElement(element)) {
|
|
const {async, value} = evaluate(element.argument, {scope});
|
|
isAsync = isAsync || async;
|
|
if (async) {
|
|
elements.push(value);
|
|
asyncSpread[elements.length - 1] = true;
|
|
}
|
|
else {
|
|
elements.push(...value);
|
|
}
|
|
}
|
|
else {
|
|
const {async, value} = evaluate(element, {scope});
|
|
isAsync = isAsync || async;
|
|
elements.push(value);
|
|
}
|
|
}
|
|
return {
|
|
async: !!isAsync,
|
|
value: !isAsync
|
|
? elements
|
|
: Promise.all(elements).then((elementsAndOrSpreads) => {
|
|
const elements = [];
|
|
for (let i = 0; i < elementsAndOrSpreads.length; ++i) {
|
|
if (asyncSpread[i]) {
|
|
elements.push(...elementsAndOrSpreads[i]);
|
|
}
|
|
else {
|
|
elements.push(elementsAndOrSpreads[i]);
|
|
}
|
|
}
|
|
return elements;
|
|
}),
|
|
};
|
|
}
|