@@ -118,62 +118,62 @@ class Transformer {
118118 return undefined ;
119119 }
120120
121- // configureLocalization(...) -> undefined
122- if (
123- this . isCallToTaggedFunction ( node , '_LIT_LOCALIZE_CONFIGURE_LOCALIZATION_' )
124- ) {
125- return ts . createIdentifier ( 'undefined' ) ;
126- }
121+ if ( ts . isCallExpression ( node ) ) {
122+ // configureLocalization(...) -> undefined
123+ if (
124+ this . typeHasProperty (
125+ node . expression ,
126+ '_LIT_LOCALIZE_CONFIGURE_LOCALIZATION_'
127+ )
128+ ) {
129+ return ts . createIdentifier ( 'undefined' ) ;
130+ }
127131
128- // getLocale() -> "es-419"
129- if ( this . isCallToTaggedFunction ( node , '_LIT_LOCALIZE_GET_LOCALE_' ) ) {
130- return ts . createStringLiteral ( this . locale ) ;
131- }
132+ // getLocale() -> "es-419"
133+ if ( this . typeHasProperty ( node . expression , '_LIT_LOCALIZE_GET_LOCALE_' ) ) {
134+ return ts . createStringLiteral ( this . locale ) ;
135+ }
132136
133- // setLocale("es-419") -> undefined
134- if ( this . isCallToTaggedFunction ( node , '_LIT_LOCALIZE_SET_LOCALE_' ) ) {
135- return ts . createIdentifier ( 'undefined' ) ;
136- }
137+ // setLocale("es-419") -> undefined
138+ if ( this . typeHasProperty ( node . expression , '_LIT_LOCALIZE_SET_LOCALE_' ) ) {
139+ return ts . createIdentifier ( 'undefined' ) ;
140+ }
137141
138- // localeReady() -> Promise.resolve(undefined)
139- if ( this . isCallToTaggedFunction ( node , '_LIT_LOCALIZE_LOCALE_READY_' ) ) {
140- return ts . createCall (
141- ts . createPropertyAccess ( ts . createIdentifier ( 'Promise' ) , 'resolve' ) ,
142- [ ] ,
143- [ ts . createIdentifier ( 'undefined' ) ]
144- ) ;
145- }
142+ // localeReady() -> Promise.resolve(undefined)
143+ if (
144+ this . typeHasProperty ( node . expression , '_LIT_LOCALIZE_LOCALE_READY_' )
145+ ) {
146+ return ts . createCall (
147+ ts . createPropertyAccess ( ts . createIdentifier ( 'Promise' ) , 'resolve' ) ,
148+ [ ] ,
149+ [ ts . createIdentifier ( 'undefined' ) ]
150+ ) ;
151+ }
146152
147- // addLocaleChangeCallback(...) -> undefined
148- if (
149- this . isCallToTaggedFunction (
150- node ,
151- '_LIT_LOCALIZE_ADD_LOCALE_CHANGE_CALLBACK_'
152- )
153- ) {
154- return ts . createIdentifier ( 'undefined' ) ;
153+ // Localized(LitElement) -> LitElement
154+ if ( this . typeHasProperty ( node . expression , '_LIT_LOCALIZE_LOCALIZED_' ) ) {
155+ if ( node . arguments . length !== 1 ) {
156+ // TODO(aomarks) Surface as diagnostic instead.
157+ throw new KnownError (
158+ `Expected Localized mixin call to have one argument, ` +
159+ `got ${ node . arguments . length } `
160+ ) ;
161+ }
162+ return node . arguments [ 0 ] ;
163+ }
155164 }
156165
157- // removeLocaleChangeCallback(...) -> undefined
166+ // LOCALE_CHANGED_EVENT -> "lit-localize-locale-changed"
167+ //
168+ // This is slightly odd, but by replacing the LOCALE_CHANGED_EVENT const
169+ // with its static string value, we don't have to be smart about deciding
170+ // when to remove the 'lit-localize' module import, since we can assume that
171+ // everything it exports will be transformed out.
158172 if (
159- this . isCallToTaggedFunction (
160- node ,
161- '_LIT_LOCALIZE_REMOVE_LOCALE_CHANGE_CALLBACK_'
162- )
173+ ts . isIdentifier ( node ) &&
174+ this . typeHasProperty ( node , '_LIT_LOCALIZE_LOCALE_CHANGED_EVENT_' )
163175 ) {
164- return ts . createIdentifier ( 'undefined' ) ;
165- }
166-
167- // Localized(LitElement) -> LitElement
168- if ( this . isCallToTaggedFunction ( node , '_LIT_LOCALIZE_LOCALIZED_' ) ) {
169- if ( node . arguments . length !== 1 ) {
170- // TODO(aomarks) Surface as diagnostic instead.
171- throw new KnownError (
172- `Expected Localized mixin call to have one argument, ` +
173- `got ${ node . arguments . length } `
174- ) ;
175- }
176- return node . arguments [ 0 ] ;
176+ return ts . createStringLiteral ( 'lit-localize-locale-changed' ) ;
177177 }
178178
179179 return ts . visitEachChild ( node , this . boundVisitNode , this . context ) ;
@@ -429,19 +429,16 @@ class Transformer {
429429 }
430430
431431 /**
432- * Return whether the given node is call to a function which is is "tagged"
433- * with the given special identifying property (e.g. "_LIT_LOCALIZE_MSG_").
432+ * Return whether the tpe of the given node is "tagged" with the given special
433+ * identifying property (e.g. "_LIT_LOCALIZE_MSG_").
434434 */
435- isCallToTaggedFunction (
435+ typeHasProperty (
436436 node : ts . Node ,
437- tagProperty : string
437+ propertyName : string
438438 ) : node is ts . CallExpression {
439- if ( ! ts . isCallExpression ( node ) ) {
440- return false ;
441- }
442- const type = this . typeChecker . getTypeAtLocation ( node . expression ) ;
439+ const type = this . typeChecker . getTypeAtLocation ( node ) ;
443440 const props = this . typeChecker . getPropertiesOfType ( type ) ;
444- return props . some ( ( prop ) => prop . escapedName === tagProperty ) ;
441+ return props . some ( ( prop ) => prop . escapedName === propertyName ) ;
445442 }
446443}
447444
0 commit comments