Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MF2 spec updates #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 47 additions & 21 deletions packages/mf2-fluent/src/fluent-to-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import * as Fluent from '@fluent/syntax';
import deepEqual from 'fast-deep-equal';
import {
Expression,
FunctionAnnotation,
FunctionRef,
InputDeclaration,
Literal,
LocalDeclaration,
PatternMessage,
SelectMessage,
VariableRef,
Expand Down Expand Up @@ -49,16 +51,21 @@ function findSelectArgs(pattern: Fluent.Pattern): SelectArg[] {
return args;
}

function asSelectExpression(
function asSelectorDeclaration(
{ selector, defaultName, keys }: SelectArg,
index: number,
detectNumberSelection: boolean = true
): Expression {
): InputDeclaration | LocalDeclaration {
switch (selector.type) {
case 'StringLiteral':
return {
type: 'expression',
arg: asValue(selector),
annotation: { type: 'function', name: 'string' }
type: 'local',
name: `_${index}`,
value: {
type: 'expression',
arg: asValue(selector),
functionRef: { type: 'function', name: 'string' }
}
};
case 'VariableReference': {
let name = detectNumberSelection ? 'number' : 'string';
Expand All @@ -74,15 +81,28 @@ function asSelectExpression(
}
}
return {
type: 'expression',
arg: asValue(selector),
annotation: { type: 'function', name }
type: 'input',
name: selector.id.name,
value: {
type: 'expression',
arg: asValue(selector),
functionRef: { type: 'function', name }
}
};
}
}
return asExpression(selector);
const exp = asExpression(selector);
return exp.arg?.type === 'variable'
? {
type: 'input',
name: exp.arg.name,
value: exp as Expression<VariableRef>
}
: { type: 'local', name: `_${index}`, value: exp };
}

function asValue(exp: Fluent.VariableReference): VariableRef;
function asValue(exp: Fluent.InlineExpression): Literal | VariableRef;
function asValue(exp: Fluent.InlineExpression): Literal | VariableRef {
switch (exp.type) {
case 'NumberLiteral':
Expand All @@ -102,14 +122,14 @@ function asExpression(exp: Fluent.Expression): Expression {
return {
type: 'expression',
arg: asValue(exp),
annotation: { type: 'function', name: 'number' }
functionRef: { type: 'function', name: 'number' }
};
case 'StringLiteral':
case 'VariableReference': {
return { type: 'expression', arg: asValue(exp) };
}
case 'FunctionReference': {
const annotation: FunctionAnnotation = {
const annotation: FunctionRef = {
type: 'function',
name: exp.id.name.toLowerCase()
};
Expand All @@ -130,8 +150,8 @@ function asExpression(exp: Fluent.Expression): Expression {
}
}
return args.length > 0
? { type: 'expression', arg: args[0], annotation }
: { type: 'expression', annotation };
? { type: 'expression', arg: args[0], functionRef: annotation }
: { type: 'expression', functionRef: annotation };
}
case 'MessageReference': {
const msgId = exp.attribute
Expand All @@ -140,13 +160,13 @@ function asExpression(exp: Fluent.Expression): Expression {
return {
type: 'expression',
arg: { type: 'literal', value: msgId },
annotation: { type: 'function', name: 'message' }
functionRef: { type: 'function', name: 'fluent:message' }
};
}
case 'TermReference': {
const annotation: FunctionAnnotation = {
const annotation: FunctionRef = {
type: 'function',
name: 'message'
name: 'fluent:message'
};
const msgId = exp.attribute
? `-${exp.id.name}.${exp.attribute.name}`
Expand All @@ -165,7 +185,7 @@ function asExpression(exp: Fluent.Expression): Expression {
return {
type: 'expression',
arg: { type: 'literal', value: msgId },
annotation
functionRef: annotation
};
}

Expand Down Expand Up @@ -248,7 +268,7 @@ export function fluentToMessage(
keys: key.map((k, i) =>
k === CATCHALL
? { type: '*', value: args[i].defaultName }
: { type: 'literal', quoted: false, value: String(k) }
: { type: 'literal', value: String(k) }
),
value: []
}));
Expand Down Expand Up @@ -299,10 +319,16 @@ export function fluentToMessage(
}
addParts(ast, []);

const declarations = args.map((arg, index) =>
asSelectorDeclaration(arg, index, detectNumberSelection)
);
return {
type: 'select',
declarations: [],
selectors: args.map(arg => asSelectExpression(arg, detectNumberSelection)),
declarations,
selectors: declarations.map(decl => ({
type: 'variable',
name: decl.name
})),
variants
};
}
26 changes: 12 additions & 14 deletions packages/mf2-fluent/src/fluent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,24 +715,24 @@ describe('fluentToResourceData', () => {
const msg = data.get('multi')?.get('') as SelectMessage;
expect(msg.variants.map(v => v.keys)).toMatchObject([
[
{ type: 'literal', quoted: false, value: '0' },
{ type: 'literal', quoted: false, value: 'feminine' }
{ type: 'literal', value: '0' },
{ type: 'literal', value: 'feminine' }
],
[
{ type: 'literal', quoted: false, value: '0' },
{ type: 'literal', quoted: false, value: 'masculine' }
{ type: 'literal', value: '0' },
{ type: 'literal', value: 'masculine' }
],
[
{ type: 'literal', quoted: false, value: '0' },
{ type: 'literal', value: '0' },
{ type: '*', value: 'neuter' }
],
[
{ type: '*', value: 'other' },
{ type: 'literal', quoted: false, value: 'feminine' }
{ type: 'literal', value: 'feminine' }
],
[
{ type: '*', value: 'other' },
{ type: 'literal', quoted: false, value: 'masculine' }
{ type: 'literal', value: 'masculine' }
],
[
{ type: '*', value: 'other' },
Expand Down Expand Up @@ -779,13 +779,11 @@ describe('messagetoFluent', () => {
value: {
type: 'expression',
arg: { type: 'variable', name: 'num' },
annotation: { type: 'function', name: 'number' }
functionRef: { type: 'function', name: 'number' }
}
}
],
selectors: [
{ type: 'expression', arg: { type: 'variable', name: 'local' } }
],
selectors: [{ type: 'variable', name: 'local' }],
variants: [
{
keys: [{ type: '*' }],
Expand Down Expand Up @@ -849,12 +847,12 @@ describe('messagetoFluent', () => {
{
type: 'expression',
arg: { type: 'literal', value: 'msg' },
annotation: { type: 'function', name: 'message' }
functionRef: { type: 'function', name: 'fluent:message' }
},
{
type: 'expression',
arg: { type: 'variable', name: 'local' },
annotation: { type: 'function', name: 'message' }
functionRef: { type: 'function', name: 'fluent:message' }
}
]
};
Expand Down Expand Up @@ -891,7 +889,7 @@ describe('messagetoFluent', () => {
{
type: 'expression',
arg: { type: 'variable', name: 'input' },
annotation: { type: 'function', name: 'message' }
functionRef: { type: 'function', name: 'fluent:message' }
}
]
};
Expand Down
2 changes: 1 addition & 1 deletion packages/mf2-fluent/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ export function getFluentFunctions(res: FluentMessageResource) {
}
Object.freeze(message);

return { message };
return { 'fluent:message': message };
}
31 changes: 14 additions & 17 deletions packages/mf2-fluent/src/message-to-fluent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
CatchallKey,
Declaration,
Expression,
FunctionAnnotation,
FunctionRef,
Literal,
Message,
Pattern,
Expand Down Expand Up @@ -34,7 +34,7 @@ export type FunctionMap = Record<string, string | symbol | null>;
*/
export const defaultFunctionMap: FunctionMap = {
datetime: 'DATETIME',
message: FluentMessageRef,
'fluent:message': FluentMessageRef,
number: 'NUMBER',
plural: 'NUMBER',
string: null
Expand Down Expand Up @@ -77,7 +77,7 @@ export function messageToFluent(
}));
const k0 = variants[0].keys;
while (k0.length > 0) {
const sel = expressionToFluent(ctx, msg.selectors[k0.length - 1]);
const sel = variableRefToFluent(ctx, msg.selectors[k0.length - 1]);
let baseKeys: (Literal | CatchallKey)[] = [];
let exp: Fluent.SelectExpression | undefined;
for (let i = 0; i < variants.length; ++i) {
Expand Down Expand Up @@ -161,7 +161,7 @@ function patternToFluent(ctx: MsgContext, pattern: Pattern) {
function functionRefToFluent(
ctx: MsgContext,
arg: Fluent.InlineExpression | null,
{ name, options }: FunctionAnnotation
{ name, options }: FunctionRef
): Fluent.InlineExpression {
const args = new Fluent.CallArguments();
if (arg) args.positional[0] = arg;
Expand Down Expand Up @@ -236,18 +236,10 @@ function literalToFluent({ value }: Literal) {

function expressionToFluent(
ctx: MsgContext,
{ arg, annotation }: Expression
{ arg, functionRef }: Expression
): Fluent.InlineExpression {
const fluentArg = arg ? valueToFluent(ctx, arg) : null;
if (annotation) {
if (annotation.type === 'function') {
return functionRefToFluent(ctx, fluentArg, annotation);
} else {
throw new Error(
`Conversion of ${annotation.type} annotation to Fluent is not supported`
);
}
}
if (functionRef) return functionRefToFluent(ctx, fluentArg, functionRef);
if (fluentArg) return fluentArg;
throw new Error('Invalid empty expression');
}
Expand All @@ -273,7 +265,12 @@ function variableRefToFluent(
{ name }: VariableRef
): Fluent.InlineExpression {
const local = ctx.declarations.find(decl => decl.name === name);
return local?.value
? expressionToFluent(ctx, local.value)
: new Fluent.VariableReference(new Fluent.Identifier(name));
if (local?.value) {
const idx = ctx.declarations.indexOf(local);
return expressionToFluent(
{ ...ctx, declarations: ctx.declarations.slice(0, idx) },
local.value
);
}
return new Fluent.VariableReference(new Fluent.Identifier(name));
}
7 changes: 6 additions & 1 deletion packages/mf2-icu-mf1/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,9 @@ function number(
*
* @beta
*/
export const getMF1Functions = () => ({ date, duration, number, time });
export const getMF1Functions = () => ({
'mf1:date': date,
'mf1:duration': duration,
'mf1:number': number,
'mf1:time': time
});
Loading
Loading