@1771technologies/lytenyte-pro
Version:
Blazingly fast headless React data grid with 100s of features.
45 lines (44 loc) • 1.43 kB
JavaScript
import { current, advance } from "../parser/parser-context.js";
import { parseTemplateLiteral } from "../parser/parse-template-literal.js";
export const stringsPlugin = {
name: "strings",
parsePrefix: (ctx) => {
const tok = current(ctx);
if (tok.type === "String") {
advance(ctx);
return { type: "StringLiteral", value: tok.value.slice(1, -1), start: tok.start, end: tok.end };
}
if (tok.type === "TemplateLiteral") {
return parseTemplateLiteral(ctx);
}
return null;
},
optimize: (node, opt) => {
if (node.type === "TemplateLiteral") {
return {
...node,
parts: node.parts.map((p) => (p.type === "StringLiteral" ? p : opt(p))),
};
}
return null;
},
evaluate: (node, context, evalFn) => {
if (node.type === "StringLiteral") {
return { value: node.value };
}
if (node.type === "TemplateLiteral") {
const parts = node.parts;
let result = "";
for (const part of parts) {
if (part.type === "StringLiteral") {
result += part.value;
}
else {
result += String(evalFn(part, context));
}
}
return { value: result };
}
return null;
},
};