subscript
Version:
Modular expression parser & evaluator
24 lines (20 loc) • 1.19 kB
JavaScript
// Logical/nullish assignment operators + destructuring assignment - eval half
import { operator, compile } from '../../parse.js';
import { destructure } from '../var.js';
import { isLval, prop } from '../access.js';
const err = msg => { throw Error(msg) };
// Override = to support destructuring
operator('=', (a, b) => {
// Handle let/const/var declarations: ['=', ['let', pattern], value]
if (Array.isArray(a) && (a[0] === 'let' || a[0] === 'const' || a[0] === 'var')) {
const pattern = a[1];
b = compile(b);
if (typeof pattern === 'string') return ctx => { ctx[pattern] = b(ctx); };
return ctx => destructure(pattern, b(ctx), ctx);
}
isLval(a) || err('Invalid assignment target');
return (b = compile(b), prop(a, (obj, path, ctx) => obj[path] = b(ctx)));
});
operator('||=', (a, b) => (isLval(a) || err('Invalid assignment target'), b = compile(b), prop(a, (o, k, ctx) => o[k] ||= b(ctx))));
operator('&&=', (a, b) => (isLval(a) || err('Invalid assignment target'), b = compile(b), prop(a, (o, k, ctx) => o[k] &&= b(ctx))));
operator('??=', (a, b) => (isLval(a) || err('Invalid assignment target'), b = compile(b), prop(a, (o, k, ctx) => o[k] ??= b(ctx))));