@@ -48,6 +48,49 @@ function extractArgs(arg: any, warning?: (node: any) => void) {
48
48
}
49
49
}
50
50
51
+ class DummySourceMapConsumer implements sourceMap . SourceMapConsumer {
52
+ public file : string ;
53
+ public sourceRoot : string ;
54
+ public sources : string [ ] ;
55
+ public sourcesContent : string [ ] ;
56
+ public constructor ( module : wp . Module ) {
57
+ this . file = module . resource ;
58
+ this . sourceRoot = module . resource ;
59
+ this . sources = [ this . file ] ;
60
+ this . sourcesContent = [ module . _source . _value ] ;
61
+ }
62
+
63
+ computeColumnSpans ( ) { }
64
+ originalPositionFor ( generatedPosition : sourceMap . Position & { bias ?: number } ) : sourceMap . NullableMappedPosition {
65
+ return _ . assign ( {
66
+ source : this . file ,
67
+ name : null
68
+ } , generatedPosition ) ;
69
+ }
70
+ generatedPositionFor ( originalPosition : sourceMap . MappedPosition & { bias ?: number } ) : sourceMap . NullablePosition {
71
+ return _ . assign ( {
72
+ lastColumn : originalPosition . column
73
+ } , originalPosition ) ;
74
+ }
75
+ allGeneratedPositionsFor ( originalPosition : sourceMap . MappedPosition ) : sourceMap . NullablePosition [ ] {
76
+ return [ this . generatedPositionFor ( originalPosition ) ] ;
77
+ }
78
+ hasContentsOfAllSources ( ) { return true ; }
79
+ sourceContentFor ( source : string , returnNullOnMissing ?: boolean ) : string | null {
80
+ const index = this . sources . indexOf ( source ) ;
81
+ if ( index === - 1 ) {
82
+ if ( returnNullOnMissing ) {
83
+ return null ;
84
+ } else {
85
+ throw new Error ( "never" ) ;
86
+ }
87
+ }
88
+ return this . sourcesContent [ index ] ;
89
+ }
90
+ eachMapping ( ) : void {
91
+ }
92
+ }
93
+
51
94
export interface Option {
52
95
defaultLanguage : string ;
53
96
/**
@@ -305,22 +348,18 @@ export default class I18nextPlugin {
305
348
}
306
349
}
307
350
308
- protected static onTranslateFunctionCall ( this : wp . Parser , plugin : I18nextPlugin , expr : wp . Expression ) {
309
- const resource = this . state . current . resource ;
310
- if ( plugin . sourceMaps [ resource ] === undefined ) {
311
- plugin . sourceMaps [ resource ] = new SourceMapConsumer ( this . state . current . _source . _sourceMap ) ;
312
- }
313
- const sourceMap = plugin . sourceMaps [ resource ] ;
314
- const args = expr . arguments . map ( ( arg : any ) => extractArgs ( arg , ( arg ) => {
315
- const beginPos = sourceMap . originalPositionFor ( arg . loc . start ) ;
316
- const endPos = sourceMap . originalPositionFor ( arg . loc . end ) ;
317
- if ( beginPos . source !== null ) {
318
- const originalSource = sourceMap . sourceContentFor ( beginPos . source ) ;
319
- const sourceLines : string [ ] = [ ] ;
320
- if ( originalSource !== null ) {
321
- const buffer = new ReadableStreamBuffer ( ) ;
322
- buffer . put ( originalSource ) ;
323
- let lineIdx = 0 ;
351
+ protected argsToSource ( sourceMap : SourceMapConsumer , arg : wp . Expression ) : Promise < string | null > {
352
+ const beginPos = sourceMap . originalPositionFor ( arg . loc . start ) ;
353
+ const endPos = sourceMap . originalPositionFor ( arg . loc . end ) ;
354
+ if ( beginPos . source !== null ) {
355
+ const originalSource = sourceMap . sourceContentFor ( beginPos . source ) ;
356
+ const sourceLines : string [ ] = [ ] ;
357
+ if ( originalSource !== null ) {
358
+ const buffer = new ReadableStreamBuffer ( ) ;
359
+ buffer . put ( originalSource ) ;
360
+ buffer . put ( "\n" ) ;
361
+ let lineIdx = 0 ;
362
+ return new Promise < string > ( resolve => {
324
363
const lineInterface = readline . createInterface ( buffer ) . on ( "line" , ( line : string ) => {
325
364
lineIdx ++ ;
326
365
let beginCol = 0 , endCol = line . length ;
@@ -338,14 +377,31 @@ export default class I18nextPlugin {
338
377
lineInterface . close ( ) ;
339
378
}
340
379
} ) . on ( "close" , ( ) => {
341
- plugin . warningOnCompilation ( `unable to parse arg ${ sourceLines . join ( "\n" ) } at ${ resource } :( ${ beginPos . line } , ${ beginPos . column } )` ) ;
380
+ resolve ( sourceLines . join ( "\n" ) ) ;
342
381
} ) ;
343
- return ;
344
- }
382
+ } ) ;
345
383
}
346
- plugin . warningOnCompilation ( `unable to parse node at ${ resource } :(${ beginPos . line } , ${ beginPos . column } )` ) ;
347
- } ) ) ;
348
- const startPos = sourceMap . originalPositionFor ( expr . loc . start ) ;
384
+ }
385
+ return Promise . resolve ( null ) ;
386
+ }
387
+
388
+ protected static onTranslateFunctionCall ( this : wp . Parser , plugin : I18nextPlugin , expr : wp . Expression ) {
389
+ const resource = this . state . current . resource ;
390
+ if ( plugin . sourceMaps [ resource ] === undefined && this . state . current . _source . _sourceMap !== undefined ) {
391
+ plugin . sourceMaps [ resource ] = new SourceMapConsumer ( this . state . current . _source . _sourceMap ) ;
392
+ } else {
393
+ plugin . sourceMaps [ resource ] = new DummySourceMapConsumer ( this . state . current ) ;
394
+ }
395
+ const sourceMap = plugin . sourceMaps [ resource ] ;
396
+ const args = expr . arguments . map ( ( arg : any ) => extractArgs ( arg , arg => plugin . argsToSource ( sourceMap , arg ) . then ( originalSource => {
397
+ const beginPos = sourceMap . originalPositionFor ( arg . loc . start ) ;
398
+ if ( originalSource !== null ) {
399
+ plugin . warningOnCompilation ( `unable to parse arg ${ originalSource } at ${ resource } :(${ beginPos . line } , ${ beginPos . column } )` ) ;
400
+ } else {
401
+ plugin . warningOnCompilation ( `unable to parse node at ${ resource } :(${ beginPos . line } , ${ beginPos . column } )` ) ;
402
+ }
403
+ } ) ) ) ;
404
+ const startPos = sourceMap !== undefined ? sourceMap . originalPositionFor ( expr . loc . start ) : expr . loc . start ;
349
405
const pos = [ resource , startPos . line , startPos . column ] ;
350
406
351
407
for ( const lng of plugin . option . languages ) {
0 commit comments