1
1
import * as ts from 'typescript' ;
2
2
import * as fs from 'fs' ;
3
- import { Symbols } from '@angular/tsc-wrapped/src/symbols' ;
4
- import {
5
- isMetadataImportedSymbolReferenceExpression ,
6
- isMetadataModuleReferenceExpression
7
- } from '@angular/tsc-wrapped' ;
8
3
import { Change , InsertChange , NoopChange , MultiChange } from './change' ;
9
4
import { findNodes } from './node' ;
10
5
import { insertImport } from './route-utils' ;
@@ -105,9 +100,64 @@ export function getContentOfKeyLiteral(source: ts.SourceFile, node: ts.Node): st
105
100
}
106
101
107
102
103
+
104
+ function _angularImportsFromNode ( node : ts . ImportDeclaration ,
105
+ sourceFile : ts . SourceFile ) : { [ name : string ] : string } {
106
+ const ms = node . moduleSpecifier ;
107
+ let modulePath : string | null = null ;
108
+ switch ( ms . kind ) {
109
+ case ts . SyntaxKind . StringLiteral :
110
+ modulePath = ( ms as ts . StringLiteral ) . text ;
111
+ break ;
112
+ default :
113
+ return { } ;
114
+ }
115
+
116
+ if ( ! modulePath . startsWith ( '@angular/' ) ) {
117
+ return { } ;
118
+ }
119
+
120
+ if ( node . importClause ) {
121
+ if ( node . importClause . name ) {
122
+ // This is of the form `import Name from 'path'`. Ignore.
123
+ return { } ;
124
+ } else if ( node . importClause . namedBindings ) {
125
+ const nb = node . importClause . namedBindings ;
126
+ if ( nb . kind == ts . SyntaxKind . NamespaceImport ) {
127
+ // This is of the form `import * as name from 'path'`. Return `name.`.
128
+ return {
129
+ [ ( nb as ts . NamespaceImport ) . name . text + '.' ] : modulePath
130
+ } ;
131
+ } else {
132
+ // This is of the form `import {a,b,c} from 'path'`
133
+ const namedImports = nb as ts . NamedImports ;
134
+
135
+ return namedImports . elements
136
+ . map ( ( is : ts . ImportSpecifier ) => is . propertyName ? is . propertyName . text : is . name . text )
137
+ . reduce ( ( acc : { [ name : string ] : string } , curr : string ) => {
138
+ acc [ curr ] = modulePath ;
139
+ return acc ;
140
+ } , { } ) ;
141
+ }
142
+ }
143
+ } else {
144
+ // This is of the form `import 'path';`. Nothing to do.
145
+ return { } ;
146
+ }
147
+ }
148
+
149
+
108
150
export function getDecoratorMetadata ( source : ts . SourceFile , identifier : string ,
109
151
module : string ) : Observable < ts . Node > {
110
- const symbols = new Symbols ( source as any ) ;
152
+ const angularImports : { [ name : string ] : string }
153
+ = findNodes ( source , ts . SyntaxKind . ImportDeclaration )
154
+ . map ( ( node : ts . ImportDeclaration ) => _angularImportsFromNode ( node , source ) )
155
+ . reduce ( ( acc : { [ name : string ] : string } , current : { [ name : string ] : string } ) => {
156
+ for ( const key of Object . keys ( current ) ) {
157
+ acc [ key ] = current [ key ] ;
158
+ }
159
+ return acc ;
160
+ } , { } ) ;
111
161
112
162
return getSourceNodes ( source )
113
163
. filter ( node => {
@@ -118,10 +168,8 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string,
118
168
. filter ( expr => {
119
169
if ( expr . expression . kind == ts . SyntaxKind . Identifier ) {
120
170
const id = < ts . Identifier > expr . expression ;
121
- const metaData = symbols . resolve ( id . getFullText ( source ) ) ;
122
- if ( isMetadataImportedSymbolReferenceExpression ( metaData ) ) {
123
- return metaData . name == identifier && metaData . module == module ;
124
- }
171
+ return id . getFullText ( source ) == identifier
172
+ && angularImports [ id . getFullText ( source ) ] === module ;
125
173
} else if ( expr . expression . kind == ts . SyntaxKind . PropertyAccessExpression ) {
126
174
// This covers foo.NgModule when importing * as foo.
127
175
const paExpr = < ts . PropertyAccessExpression > expr . expression ;
@@ -130,12 +178,9 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string,
130
178
return false ;
131
179
}
132
180
133
- const id = paExpr . name ;
134
- const moduleId = < ts . Identifier > paExpr . expression ;
135
- const moduleMetaData = symbols . resolve ( moduleId . getFullText ( source ) ) ;
136
- if ( isMetadataModuleReferenceExpression ( moduleMetaData ) ) {
137
- return moduleMetaData . module == module && id . getFullText ( source ) == identifier ;
138
- }
181
+ const id = paExpr . name . text ;
182
+ const moduleId = ( < ts . Identifier > paExpr . expression ) . getText ( source ) ;
183
+ return id === identifier && ( angularImports [ moduleId + '.' ] === module ) ;
139
184
}
140
185
return false ;
141
186
} )
0 commit comments