silphius/app/astride/evaluators/member.js
2024-06-22 10:47:17 -05:00

17 lines
516 B
JavaScript

export default function(node, {evaluate, scope}) {
const {computed, object, property, wrapper} = node;
const member = (O, P) => (wrapper?.optional ? O?.[P] : O[P]);
const O = evaluate(object, {scope});
const P = computed
? evaluate(property, {scope})
// Otherwise, identifier
: {value: property.name};
if (O.async || P.async) {
return {
async: true,
value: Promise.all([O.value, P.value]).then(([O, P]) => member(O, P)),
};
}
return {value: member(O.value, P.value)};
}