Skip to content

Commit a7292f9

Browse files
fix(mf2): Use last rather than first variable name for fallback (unicode-org/message-format-wg#903)
1 parent cda83fe commit a7292f9

File tree

3 files changed

+6
-12
lines changed

3 files changed

+6
-12
lines changed

mf2/messageformat/src/resolve/resolve-function-ref.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,9 @@ export function resolveFunctionRef(
1515
operand: Literal | VariableRef | undefined,
1616
{ name, options }: FunctionRef
1717
) {
18-
let source: string | undefined;
18+
const source = getValueSource(operand) ?? `:${name}`;
1919
try {
20-
let fnInput: [unknown] | [];
21-
if (operand) {
22-
fnInput = [resolveValue(ctx, operand)];
23-
source = getValueSource(operand);
24-
} else {
25-
fnInput = [];
26-
}
27-
source ??= `:${name}`;
28-
20+
const fnInput = operand ? [resolveValue(ctx, operand)] : [];
2921
const rf = ctx.functions[name];
3022
if (!rf) {
3123
throw new MessageError('unknown-function', `Unknown function :${name}`);
@@ -57,7 +49,6 @@ export function resolveFunctionRef(
5749
return res;
5850
} catch (error) {
5951
ctx.onError(error);
60-
source ??= getValueSource(operand) ?? `:${name}`;
6152
return fallback(source);
6253
}
6354
}

mf2/messageformat/src/resolve/resolve-value.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export function getValueSource(
2626
export function getValueSource(value: Literal | VariableRef | undefined) {
2727
switch (value?.type) {
2828
case 'literal':
29-
return '|' + value.value + '|';
29+
return (
30+
'|' + value.value.replaceAll('\\', '\\\\').replaceAll('|', '\\|') + '|'
31+
);
3032
case 'variable':
3133
return '$' + value.name;
3234
default:

mf2/messageformat/src/resolve/resolve-variable.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export function resolveVariableRef(ctx: Context, ref: VariableRef) {
8484
let type = typeof value;
8585
if (type === 'object') {
8686
const mv = value as MessageValue;
87+
if (mv.type === 'fallback') return fallback(source);
8788
if (ctx.localVars.has(mv)) return mv;
8889
if (value instanceof Number) type = 'number';
8990
else if (value instanceof String) type = 'string';

0 commit comments

Comments
 (0)