@1771technologies/lytenyte-pro
Version:
Blazingly fast headless React data grid with 100s of features.
59 lines (58 loc) • 1.95 kB
JavaScript
import { current, advance, expect } from "../parser/parser-context.js";
import { parseExpression } from "../parser/parse-expression.js";
import { isConstant } from "../compiler/is-constant.js";
import { valueOf } from "../compiler/value-of.js";
const TERNARY_PRECEDENCE = 1;
export const ternaryPlugin = {
name: "ternary",
infixPrecedence: (ctx) => {
const tok = current(ctx);
if (tok.type === "Punctuation" && tok.value === "?") {
return TERNARY_PRECEDENCE;
}
return undefined;
},
parseInfix: (ctx, left, minPrec) => {
const tok = current(ctx);
if (tok.type !== "Punctuation" || tok.value !== "?")
return null;
if (TERNARY_PRECEDENCE < minPrec)
return null;
advance(ctx);
const consequent = parseExpression(ctx, 0);
expect(ctx, "Punctuation", ":");
const alternate = parseExpression(ctx, 0);
return {
type: "ConditionalExpression",
test: left,
consequent,
alternate,
start: left.start,
end: alternate.end,
};
},
optimize: (node, opt) => {
if (node.type === "ConditionalExpression") {
const n = node;
const test = opt(n.test);
if (isConstant(test)) {
return valueOf(test) ? opt(n.consequent) : opt(n.alternate);
}
return {
...node,
test,
consequent: opt(n.consequent),
alternate: opt(n.alternate),
};
}
return null;
},
evaluate: (node, context, evalFn) => {
if (node.type === "ConditionalExpression") {
const n = node;
const test = evalFn(n.test, context);
return { value: test ? evalFn(n.consequent, context) : evalFn(n.alternate, context) };
}
return null;
},
};