@@ -52,7 +52,7 @@ export function transformOutput(
5252 }
5353 opts . outDir = pathLib . join ( outRoot , '/' , locale ) ;
5454 program . emit ( undefined , undefined , undefined , undefined , {
55- before : [ litLocalizeTransform ( translations , program ) ] ,
55+ before : [ litLocalizeTransform ( translations , locale , program ) ] ,
5656 } ) ;
5757 }
5858}
@@ -62,10 +62,11 @@ export function transformOutput(
6262 */
6363export function litLocalizeTransform (
6464 translations : Map < string , Message > | undefined ,
65+ locale : string ,
6566 program : ts . Program
6667) : ts . TransformerFactory < ts . SourceFile > {
6768 return ( context ) => {
68- const transformer = new Transformer ( context , translations , program ) ;
69+ const transformer = new Transformer ( context , translations , locale , program ) ;
6970 return ( file ) => ts . visitNode ( file , transformer . boundVisitNode ) ;
7071 } ;
7172}
@@ -76,26 +77,32 @@ export function litLocalizeTransform(
7677class Transformer {
7778 private context : ts . TransformationContext ;
7879 private translations : Map < string , Message > | undefined ;
80+ private locale : string ;
7981 private typeChecker : ts . TypeChecker ;
8082 boundVisitNode = this . visitNode . bind ( this ) ;
8183
8284 constructor (
8385 context : ts . TransformationContext ,
8486 translations : Map < string , Message > | undefined ,
87+ locale : string ,
8588 program : ts . Program
8689 ) {
8790 this . context = context ;
8891 this . translations = translations ;
92+ this . locale = locale ;
8993 this . typeChecker = program . getTypeChecker ( ) ;
9094 }
9195
9296 /**
9397 * Top-level delegating visitor for all nodes.
9498 */
9599 visitNode ( node : ts . Node ) : ts . VisitResult < ts . Node > {
100+ // msg('greeting', 'hello') -> 'hola'
96101 if ( isMsgCall ( node , this . typeChecker ) ) {
97102 return this . replaceMsgCall ( node ) ;
98103 }
104+
105+ // html`<b>${msg('greeting', 'hello')}</b>` -> html`<b>hola</b>`
99106 if ( isLitTemplate ( node ) ) {
100107 // If an html-tagged template literal embeds a msg call, we want to
101108 // collapse the result of that msg call into the parent template.
@@ -105,9 +112,49 @@ class Transformer {
105112 )
106113 ) ;
107114 }
115+
116+ // import ... from 'lit-localize' -> (removed)
108117 if ( this . isLitLocalizeImport ( node ) ) {
109118 return undefined ;
110119 }
120+
121+ // configureTransformLocalization(...) -> {getLocale: () => "es-419"}
122+ if (
123+ this . isCallToTaggedFunction (
124+ node ,
125+ '_LIT_LOCALIZE_CONFIGURE_TRANSFORM_LOCALIZATION_'
126+ )
127+ ) {
128+ return ts . createObjectLiteral (
129+ [
130+ ts . createPropertyAssignment (
131+ ts . createIdentifier ( 'getLocale' ) ,
132+ ts . createArrowFunction (
133+ undefined ,
134+ undefined ,
135+ [ ] ,
136+ undefined ,
137+ ts . createToken ( ts . SyntaxKind . EqualsGreaterThanToken ) ,
138+ ts . createStringLiteral ( this . locale )
139+ )
140+ ) ,
141+ ] ,
142+ false
143+ ) ;
144+ }
145+
146+ // configureLocalization(...) -> Error
147+ if (
148+ this . isCallToTaggedFunction ( node , '_LIT_LOCALIZE_CONFIGURE_LOCALIZATION_' )
149+ ) {
150+ // TODO(aomarks) This error is not surfaced earlier in the analysis phase
151+ // as a nicely formatted diagnostic, but it should be.
152+ throw new KnownError (
153+ 'Cannot use configureLocalization in transform mode. ' +
154+ 'Use configureTransformLocalization instead.'
155+ ) ;
156+ }
157+
111158 return ts . visitEachChild ( node , this . boundVisitNode , this . context ) ;
112159 }
113160
@@ -357,6 +404,22 @@ class Transformer {
357404 }
358405 return false ;
359406 }
407+
408+ /**
409+ * Return whether the given node is call to a function which is is "tagged"
410+ * with the given special identifying property (e.g. "_LIT_LOCALIZE_MSG_").
411+ */
412+ isCallToTaggedFunction (
413+ node : ts . Node ,
414+ tagProperty : string
415+ ) : node is ts . CallExpression {
416+ if ( ! ts . isCallExpression ( node ) ) {
417+ return false ;
418+ }
419+ const type = this . typeChecker . getTypeAtLocation ( node . expression ) ;
420+ const props = this . typeChecker . getPropertiesOfType ( type ) ;
421+ return props . some ( ( prop ) => prop . escapedName === tagProperty ) ;
422+ }
360423}
361424
362425/**
0 commit comments