feat: array destructure

This commit is contained in:
cha0s 2021-05-09 02:06:44 -05:00
parent 5059525ca3
commit 6dae625953

View File

@ -26,7 +26,29 @@ export default class Sandbox {
}
}
destructure(id, init, scope) {
destructureArray(id, init, scope) {
this.setNextScope(id, scope);
const {elements} = id;
for (let i = 0; i < elements.length; ++i) {
const element = elements[i];
if (null === element) {
// eslint-disable-next-line no-continue
continue;
}
this.setNextScope(element, scope);
if (types.isIdentifier(element)) {
scope.allocate(element.name, init[i]);
}
else {
// eslint-disable-next-line no-console
console.error("destructureArray(): Can't handle type", element.type);
return undefined;
}
}
return undefined;
}
destructureObject(id, init, scope) {
this.setNextScope(id, scope);
const {properties} = id;
const promises = [];
@ -41,7 +63,7 @@ export default class Sandbox {
scope.allocate(name, init[k]);
return undefined;
}
return this.destructure(property.value, init[k], scope);
return this.destructureObject(property.value, init[k], scope);
}));
// eslint-disable-next-line no-continue
continue;
@ -50,7 +72,7 @@ export default class Sandbox {
scope.allocate(property.value.name, init[k.value]);
}
else {
const promiseOrVoid = this.destructure(property.value, init[k.value], scope);
const promiseOrVoid = this.destructureObject(property.value, init[k.value], scope);
if (promiseOrVoid) {
promises.push(promiseOrVoid);
}
@ -667,10 +689,21 @@ export default class Sandbox {
scope.allocate(id.name, init.value);
}
}
else if (types.isArrayPattern(id)) {
const promiseOrVoid = init.async
? Promise.resolve(init.value).then((init) => this.destructureArray(id, init, scope))
: this.destructureArray(id, init.value, scope);
if (promiseOrVoid) {
yield {
async: true,
value: promiseOrVoid,
};
}
}
else if (types.isObjectPattern(id)) {
const promiseOrVoid = init.async
? Promise.resolve(init.value).then((init) => this.destructure(id, init, scope))
: this.destructure(id, init.value, scope);
? Promise.resolve(init.value).then((init) => this.destructureObject(id, init, scope))
: this.destructureObject(id, init.value, scope);
if (promiseOrVoid) {
yield {
async: true,