1
- /* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-enum-comparison */
2
1
import {
3
- DirectiveTransform ,
4
2
createSimpleExpression ,
5
- Node ,
6
3
NodeTypes ,
4
+ isText ,
5
+ createCompoundExpression ,
6
+ TO_DISPLAY_STRING
7
+ } from '@vue/compiler-dom'
8
+ import { isNumber , isObject , isString , isSymbol , toDisplayString } from '@intlify/shared'
9
+ import { evaluateValue , parseVTExpression } from './transpiler'
10
+ import { report , ReportCodes } from './report'
11
+ import { createContentBuilder } from './builder'
12
+
13
+ import type {
14
+ DirectiveTransform ,
15
+ Node ,
7
16
SimpleExpressionNode ,
8
17
CompoundExpressionNode ,
9
- isText ,
10
18
TextNode ,
11
19
InterpolationNode ,
12
- createCompoundExpression ,
13
- TO_DISPLAY_STRING ,
14
20
TransformContext
15
21
} from '@vue/compiler-dom'
16
- import { isNumber , isObject , isString , isSymbol , toDisplayString } from '@intlify/shared'
17
- import { I18n , I18nMode , Locale } from 'vue-i18n'
18
- import { evaluateValue , parseVTExpression , TranslationParams } from './transpiler'
19
- import { report , ReportCodes } from './report'
20
- import { createContentBuilder , ContentBuilder } from './builder'
22
+ import type {
23
+ I18n ,
24
+ I18nMode ,
25
+ Locale ,
26
+ Composer ,
27
+ VueI18n ,
28
+ NamedValue ,
29
+ TranslateOptions
30
+ } from 'vue-i18n'
31
+ import type { TranslationParams , NestedValue } from './transpiler'
32
+ import type { ContentBuilder } from './builder'
21
33
22
34
// TODO: should be imported from vue-i18n
23
35
type VTDirectiveValue = {
@@ -180,12 +192,23 @@ export function transformVTDirective<
180
192
report ( parseStatus , { args : [ node . loc . source || '' ] , loc : node . loc } )
181
193
return { props : [ ] }
182
194
}
195
+ if ( parsedValue == null ) {
196
+ report ( ReportCodes . UNEXPECTED_ERROR , {
197
+ args : [ node . loc . source || '' ] ,
198
+ loc : node . loc
199
+ } )
200
+ return { props : [ ] }
201
+ }
183
202
184
- const global =
185
- i18nInstance . mode === 'composition'
186
- ? ( i18nInstance . global as any ) // eslint-disable-line @typescript-eslint/no-explicit-any
187
- : ( i18nInstance . global as any ) . __composer // eslint-disable-line @typescript-eslint/no-explicit-any
188
- const content = global . t ( ...makeParams ( parsedValue ! ) )
203
+ const global = getComposer ( i18nInstance as unknown as I18n )
204
+ if ( global == null ) {
205
+ report ( ReportCodes . NOT_RESOLVED_COMPOSER , {
206
+ args : [ node . loc . source || '' ] ,
207
+ loc : node . loc
208
+ } )
209
+ return { props : [ ] }
210
+ }
211
+ const content = global . t ( ...makeParams ( parsedValue ) )
189
212
190
213
node . children . push ( {
191
214
type : NodeTypes . TEXT ,
@@ -199,13 +222,7 @@ export function transformVTDirective<
199
222
node . children . push ( {
200
223
type : NodeTypes . INTERPOLATION ,
201
224
content : createCompoundExpression ( [
202
- createSimpleExpression (
203
- code ,
204
- false ,
205
- loc ,
206
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
207
- ConstantTypes . NOT_CONSTANT as any
208
- )
225
+ createSimpleExpression ( code , false , loc , ConstantTypes . NOT_CONSTANT as number )
209
226
] )
210
227
} as InterpolationNode )
211
228
return { props : [ ] }
@@ -217,13 +234,7 @@ export function transformVTDirective<
217
234
node . children . push ( {
218
235
type : NodeTypes . INTERPOLATION ,
219
236
content : createCompoundExpression ( [
220
- createSimpleExpression (
221
- code ,
222
- false ,
223
- loc ,
224
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
225
- ConstantTypes . NOT_CONSTANT as any
226
- )
237
+ createSimpleExpression ( code , false , loc , ConstantTypes . NOT_CONSTANT as number )
227
238
] )
228
239
} as InterpolationNode )
229
240
return { props : [ ] }
@@ -237,6 +248,34 @@ export function transformVTDirective<
237
248
}
238
249
}
239
250
251
+ function getComposer ( i18n : I18n ) : Composer | null {
252
+ return isI18nInstance ( i18n ) ? getComposerInternal ( i18n ) : null
253
+ }
254
+
255
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
256
+ function isI18nInstance ( i18n : any ) : i18n is I18n {
257
+ return i18n != null && 'global' in i18n && 'mode' in i18n
258
+ }
259
+
260
+ function getComposerInternal ( i18n : I18n ) : Composer | null {
261
+ if ( i18n . mode === 'composition' ) {
262
+ return isComposer ( i18n . global ) ? i18n . global : null
263
+ } else {
264
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
265
+ return isVueI18n ( i18n . global ) ? ( ( i18n . global as any ) . __composer as Composer ) : null
266
+ }
267
+ }
268
+
269
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
270
+ function isComposer ( target : any ) : target is Composer {
271
+ return target != null && ! ( '__composer' in target )
272
+ }
273
+
274
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
275
+ function isVueI18n ( target : any ) : target is VueI18n {
276
+ return target != null && '__composer' in target
277
+ }
278
+
240
279
function isSimpleExpressionNode ( node : Node | undefined ) : node is SimpleExpressionNode {
241
280
return node != null && node . type === NodeTypes . SIMPLE_EXPRESSION
242
281
}
@@ -248,13 +287,13 @@ function isCompoundExpressionNode(node: Node | undefined): node is CompoundExpre
248
287
function isConstant ( node : SimpleExpressionNode ) : boolean {
249
288
if ( 'isConstant' in node ) {
250
289
// for v3.0.3 earlier
251
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
252
- return ( node as any ) . isConstant
290
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
291
+ return ( node as any ) . isConstant as boolean
253
292
} else if ( 'constType' in node ) {
254
293
// for v3.0.3 or later
255
- return ( node . constType as number ) <= ConstantTypes . CAN_STRINGIFY
294
+ return ( node . constType as number ) <= ( ConstantTypes . CAN_STRINGIFY as unknown as number )
256
295
} else {
257
- throw Error ( 'unexpected error' )
296
+ throw Error ( 'unexpected error in Vue SimpleExpressionNode ' )
258
297
}
259
298
}
260
299
@@ -303,11 +342,11 @@ function parseValue(value: unknown): [VTDirectiveValue | null, ReportCodes] {
303
342
}
304
343
}
305
344
306
- function makeParams ( value : VTDirectiveValue ) : unknown [ ] {
345
+ function makeParams ( value : VTDirectiveValue ) : [ string , NamedValue , TranslateOptions ] {
307
346
const { path, locale, args, choice, plural } = value
308
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
309
- const options = { } as any
310
- const named : { [ prop : string ] : unknown } = args || { }
347
+
348
+ const options = { } as TranslateOptions
349
+ const named : NamedValue = args || { }
311
350
312
351
if ( isString ( locale ) ) {
313
352
options . locale = locale
@@ -366,12 +405,13 @@ function generateComposableCode(context: TransformContext, params: TranslationPa
366
405
return content
367
406
}
368
407
369
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
370
- function generateNamedCode ( builder : ContentBuilder , named : any ) : void {
408
+ function generateNamedCode (
409
+ builder : ContentBuilder ,
410
+ named : NestedValue < Record < string , unknown > >
411
+ ) : void {
371
412
const keys = Object . keys ( named )
372
413
keys . forEach ( k => {
373
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
374
- const v : any = named [ k ]
414
+ const v = named [ k ]
375
415
if ( isObject ( v ) ) {
376
416
builder . push ( `${ k } : { ` )
377
417
generateNamedCode ( builder , v )
@@ -419,5 +459,3 @@ function generateLegacyCode(context: TransformContext, params: TranslationParams
419
459
const content = builder . content
420
460
return content
421
461
}
422
-
423
- /* eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-enum-comparison */
0 commit comments