messageformat
Version:
Intl.MessageFormat / Unicode MessageFormat 2 parser, runtime and polyfill
24 lines (23 loc) • 904 B
JavaScript
import { string } from "../functions/string.js";
import { MessageFunctionContext } from "./function-context.js";
import { resolveFunctionRef } from "./resolve-function-ref.js";
import { resolveVariableRef } from "./resolve-variable.js";
export function resolveExpression(ctx, { arg, functionRef }) {
if (functionRef) {
return resolveFunctionRef(ctx, arg, functionRef);
}
switch (arg?.type) {
case 'literal': {
const source = `|${arg.value}|`;
const msgCtx = new MessageFunctionContext(ctx, source);
const msgStr = string(msgCtx, {}, arg.value);
msgStr.source = source;
return msgStr;
}
case 'variable':
return resolveVariableRef(ctx, arg);
default:
// @ts-expect-error - should never happen
throw new Error(`Unsupported expression: ${arg?.type}`);
}
}