@@ -47,6 +47,16 @@ const FSI = "\u2068";
4747const PDI = "\u2069" ;
4848
4949
50+ // Helper: Report an error.
51+ function report ( scope , error ) {
52+ if ( scope . errors ) {
53+ scope . errors . push ( error ) ;
54+ } else {
55+ throw error ;
56+ }
57+ }
58+
59+
5060// Helper: match a variant key to the given selector.
5161function match ( bundle , selector , key ) {
5262 if ( key === selector ) {
@@ -79,7 +89,7 @@ function getDefault(scope, variants, star) {
7989 return resolvePattern ( scope , variants [ star ] . value ) ;
8090 }
8191
82- scope . errors . push ( new RangeError ( "No default" ) ) ;
92+ report ( scope , new RangeError ( "No default" ) ) ;
8393 return new FluentNone ( ) ;
8494}
8595
@@ -127,7 +137,7 @@ function resolveExpression(scope, expr) {
127137function VariableReference ( scope , { name} ) {
128138 if ( ! scope . args || ! scope . args . hasOwnProperty ( name ) ) {
129139 if ( scope . insideTermReference === false ) {
130- scope . errors . push ( new ReferenceError ( `Unknown variable: ${ name } ` ) ) ;
140+ report ( scope , new ReferenceError ( `Unknown variable: ${ name } ` ) ) ;
131141 }
132142 return new FluentNone ( `$${ name } ` ) ;
133143 }
@@ -150,7 +160,7 @@ function VariableReference(scope, {name}) {
150160 return new FluentDateTime ( arg ) ;
151161 }
152162 default :
153- scope . errors . push (
163+ report ( scope ,
154164 new TypeError ( `Unsupported variable type: ${ name } , ${ typeof arg } ` )
155165 ) ;
156166 return new FluentNone ( `$${ name } ` ) ;
@@ -161,7 +171,7 @@ function VariableReference(scope, {name}) {
161171function MessageReference ( scope , { name, attr} ) {
162172 const message = scope . bundle . _messages . get ( name ) ;
163173 if ( ! message ) {
164- scope . errors . push ( new ReferenceError ( `Unknown message: ${ name } ` ) ) ;
174+ report ( scope , new ReferenceError ( `Unknown message: ${ name } ` ) ) ;
165175 return new FluentNone ( name ) ;
166176 }
167177
@@ -170,15 +180,15 @@ function MessageReference(scope, {name, attr}) {
170180 if ( attribute ) {
171181 return resolvePattern ( scope , attribute ) ;
172182 }
173- scope . errors . push ( new ReferenceError ( `Unknown attribute: ${ attr } ` ) ) ;
183+ report ( scope , new ReferenceError ( `Unknown attribute: ${ attr } ` ) ) ;
174184 return new FluentNone ( `${ name } .${ attr } ` ) ;
175185 }
176186
177187 if ( message . value ) {
178188 return resolvePattern ( scope , message . value ) ;
179189 }
180190
181- scope . errors . push ( new ReferenceError ( `No value: ${ name } ` ) ) ;
191+ report ( scope , new ReferenceError ( `No value: ${ name } ` ) ) ;
182192 return new FluentNone ( name ) ;
183193}
184194
@@ -187,8 +197,7 @@ function TermReference(scope, {name, attr, args}) {
187197 const id = `-${ name } ` ;
188198 const term = scope . bundle . _terms . get ( id ) ;
189199 if ( ! term ) {
190- const err = new ReferenceError ( `Unknown term: ${ id } ` ) ;
191- scope . errors . push ( err ) ;
200+ report ( scope , new ReferenceError ( `Unknown term: ${ id } ` ) ) ;
192201 return new FluentNone ( id ) ;
193202 }
194203
@@ -201,7 +210,7 @@ function TermReference(scope, {name, attr, args}) {
201210 if ( attribute ) {
202211 return resolvePattern ( local , attribute ) ;
203212 }
204- scope . errors . push ( new ReferenceError ( `Unknown attribute: ${ attr } ` ) ) ;
213+ report ( scope , new ReferenceError ( `Unknown attribute: ${ attr } ` ) ) ;
205214 return new FluentNone ( `${ id } .${ attr } ` ) ;
206215 }
207216
@@ -214,12 +223,12 @@ function FunctionReference(scope, {name, args}) {
214223 // the `FluentBundle` constructor.
215224 const func = scope . bundle . _functions [ name ] || builtins [ name ] ;
216225 if ( ! func ) {
217- scope . errors . push ( new ReferenceError ( `Unknown function: ${ name } ()` ) ) ;
226+ report ( scope , new ReferenceError ( `Unknown function: ${ name } ()` ) ) ;
218227 return new FluentNone ( `${ name } ()` ) ;
219228 }
220229
221230 if ( typeof func !== "function" ) {
222- scope . errors . push ( new TypeError ( `Function ${ name } () is not callable` ) ) ;
231+ report ( scope , new TypeError ( `Function ${ name } () is not callable` ) ) ;
223232 return new FluentNone ( `${ name } ()` ) ;
224233 }
225234
@@ -252,7 +261,7 @@ function SelectExpression(scope, {selector, variants, star}) {
252261// Resolve a pattern (a complex string with placeables).
253262export function resolveComplexPattern ( scope , ptn ) {
254263 if ( scope . dirty . has ( ptn ) ) {
255- scope . errors . push ( new RangeError ( "Cyclic reference" ) ) ;
264+ report ( scope , new RangeError ( "Cyclic reference" ) ) ;
256265 return new FluentNone ( ) ;
257266 }
258267
0 commit comments