@@ -2,9 +2,9 @@ import type * as AST from '@messageformat/parser';
22import type {
33 Expression ,
44 FunctionAnnotation ,
5+ InputDeclaration ,
56 Message ,
67 Options ,
7- VariableRef ,
88 Variant
99} from 'messageformat' ;
1010
@@ -54,8 +54,7 @@ function findSelectArgs(tokens: AST.Token[]): SelectArg[] {
5454
5555function tokenToPart (
5656 token : AST . Token ,
57- pluralArg : string | null ,
58- pluralOffset : number | null
57+ pluralArg : string | null
5958) : string | Expression {
6059 switch ( token . type ) {
6160 case 'content' :
@@ -84,58 +83,48 @@ function tokenToPart(
8483 annotation
8584 } ;
8685 }
87- case 'octothorpe' : {
88- if ( ! pluralArg ) return '#' ;
89- const annotation : FunctionAnnotation = {
90- type : 'function' ,
91- name : 'number'
92- } ;
93- if ( pluralOffset ) {
94- annotation . options = new Map ( [
95- [ 'pluralOffset' , { type : 'literal' , value : String ( pluralOffset ) } ]
96- ] ) ;
97- }
98- return {
99- type : 'expression' ,
100- arg : { type : 'variable' , name : pluralArg } ,
101- annotation
102- } ;
103- }
86+ case 'octothorpe' :
87+ return pluralArg
88+ ? { type : 'expression' , arg : { type : 'variable' , name : pluralArg } }
89+ : '#' ;
10490 /* istanbul ignore next - never happens */
10591 default :
10692 throw new Error ( `Unsupported token type: ${ token . type } ` ) ;
10793 }
10894}
10995
110- function argToExpression ( {
96+ function argToInputDeclaration ( {
11197 arg : selName ,
11298 pluralOffset,
11399 type
114- } : SelectArg ) : Expression {
115- const arg : VariableRef = { type : 'variable' , name : selName } ;
100+ } : SelectArg ) : InputDeclaration {
101+ let annotation : FunctionAnnotation ;
116102 if ( type === 'select' ) {
117- return {
118- type : 'expression' ,
119- arg,
120- annotation : { type : 'function' , name : 'string' }
121- } ;
122- }
103+ annotation = { type : 'function' , name : 'string' } ;
104+ } else {
105+ const options : Options = new Map ( ) ;
106+ if ( pluralOffset ) {
107+ options . set ( 'pluralOffset' , {
108+ type : 'literal' ,
109+ value : String ( pluralOffset )
110+ } ) ;
111+ }
112+ if ( type === 'selectordinal' ) {
113+ options . set ( 'type' , { type : 'literal' , value : 'ordinal' } ) ;
114+ }
123115
124- const options : Options = new Map ( ) ;
125- if ( pluralOffset ) {
126- options . set ( 'pluralOffset' , {
127- type : 'literal' ,
128- value : String ( pluralOffset )
129- } ) ;
130- }
131- if ( type === 'selectordinal' ) {
132- options . set ( 'type' , { type : 'literal' , value : 'ordinal' } ) ;
116+ annotation = { type : 'function' , name : 'number' } ;
117+ if ( options . size ) annotation . options = options ;
133118 }
134-
135- const annotation : FunctionAnnotation = { type : 'function' , name : 'number' } ;
136- if ( options . size ) annotation . options = options ;
137-
138- return { type : 'expression' , arg, annotation } ;
119+ return {
120+ type : 'input' ,
121+ name : selName ,
122+ value : {
123+ type : 'expression' ,
124+ arg : { type : 'variable' , name : selName } ,
125+ annotation
126+ }
127+ } ;
139128}
140129
141130/**
@@ -157,7 +146,7 @@ export function mf1ToMessageData(ast: AST.Token[]): Message {
157146 return {
158147 type : 'message' ,
159148 declarations : [ ] ,
160- pattern : ast . map ( token => tokenToPart ( token , null , null ) )
149+ pattern : ast . map ( token => tokenToPart ( token , null ) )
161150 } ;
162151 }
163152
@@ -221,7 +210,7 @@ export function mf1ToMessageData(ast: AST.Token[]): Message {
221210 } )
222211 ) {
223212 const i = vp . length - 1 ;
224- const part = tokenToPart ( token , pluralArg , pluralOffset ) ;
213+ const part = tokenToPart ( token , pluralArg ) ;
225214 if ( typeof vp [ i ] === 'string' && typeof part === 'string' ) {
226215 vp [ i ] += part ;
227216 } else {
@@ -236,8 +225,8 @@ export function mf1ToMessageData(ast: AST.Token[]): Message {
236225
237226 return {
238227 type : 'select' ,
239- declarations : [ ] ,
240- selectors : args . map ( argToExpression ) ,
228+ declarations : args . map ( argToInputDeclaration ) ,
229+ selectors : args . map ( arg => ( { type : 'variable' , name : arg . arg } ) ) ,
241230 variants
242231 } ;
243232}
0 commit comments