1
1
import * as path from 'path' ;
2
2
3
- import { Project , ts , PropertyDeclaration , SyntaxKind } from 'ts-morph' ;
3
+ import { Project , ts , PropertyDeclaration , SyntaxKind , VariableDeclaration } from 'ts-morph' ;
4
+ import FileEngine from '../app/engines/file.engine' ;
4
5
5
6
const ast = new Project ( ) ;
6
7
@@ -100,10 +101,16 @@ export class ImportsUtil {
100
101
* @param {string } inputVariableName like myvar
101
102
* @return {[type] } myvar value
102
103
*/
103
- public findValueInImportOrLocalVariables ( inputVariableName : string , sourceFile : ts . SourceFile ) {
104
+ public findValueInImportOrLocalVariables (
105
+ inputVariableName : string ,
106
+ sourceFile : ts . SourceFile ,
107
+ decoratorType ?: string
108
+ ) {
104
109
let metadataVariableName = inputVariableName ,
105
110
searchedImport ,
106
111
aliasOriginalName = '' ,
112
+ foundWithNamedImport = false ,
113
+ foundWithDefaultImport = false ,
107
114
foundWithAlias = false ;
108
115
109
116
const file =
@@ -129,17 +136,37 @@ export class ImportsUtil {
129
136
importAlias = namedImports [ j ] . getAliasNode ( ) . getText ( ) ;
130
137
}
131
138
if ( importName === metadataVariableName ) {
139
+ foundWithNamedImport = true ;
132
140
searchedImport = i ;
133
141
break ;
134
142
}
135
143
if ( importAlias === metadataVariableName ) {
144
+ foundWithNamedImport = true ;
136
145
foundWithAlias = true ;
137
146
aliasOriginalName = importName ;
138
147
searchedImport = i ;
139
148
break ;
140
149
}
141
150
}
142
151
}
152
+ const namespaceImport = i . getNamespaceImport ( ) ;
153
+ if ( namespaceImport ) {
154
+ const namespaceImportLocalName = namespaceImport . getText ( ) ;
155
+ if ( namespaceImportLocalName === metadataVariableName ) {
156
+ searchedImport = i ;
157
+ }
158
+ }
159
+
160
+ if ( ! foundWithNamedImport ) {
161
+ const defaultImport = i . getDefaultImport ( ) ;
162
+ if ( defaultImport ) {
163
+ const defaultImportText = defaultImport . getText ( ) ;
164
+ if ( defaultImportText === metadataVariableName ) {
165
+ foundWithDefaultImport = true ;
166
+ searchedImport = i ;
167
+ }
168
+ }
169
+ }
143
170
} ) ;
144
171
145
172
function hasFoundValues ( variableDeclaration ) {
@@ -176,40 +203,41 @@ export class ImportsUtil {
176
203
return hasFoundValues ( variableDeclaration ) ;
177
204
} else {
178
205
// Try with exports
179
- const exportDeclarations = sourceFileImport . getExportDeclarations ( ) ;
180
- if ( exportDeclarations && exportDeclarations . length > 0 ) {
181
- let i = 0 ,
182
- len = exportDeclarations . length ;
183
- for ( i ; i < len ; i ++ ) {
184
- let exportDeclaration = exportDeclarations [ i ] ;
185
- let sourceFileExportedReference =
186
- exportDeclaration . getModuleSpecifierSourceFile ( ) ;
187
- if ( sourceFileExportedReference ) {
188
- let sourceFileExportedReferencePath =
189
- sourceFileExportedReference . getFilePath ( ) ;
190
-
191
- const sourceFileExported =
192
- typeof ast . getSourceFile (
193
- sourceFileExportedReferencePath
194
- ) !== 'undefined'
195
- ? ast . getSourceFile ( sourceFileExportedReferencePath )
196
- : ast . addSourceFileAtPathIfExists (
197
- sourceFileExportedReferencePath
198
- ) ;
199
-
200
- if ( sourceFileExported ) {
201
- variableDeclaration =
202
- sourceFileExported . getVariableDeclaration ( variableName ) ;
203
- if ( variableDeclaration ) {
204
- return hasFoundValues ( variableDeclaration ) ;
205
- }
206
+ const exportDeclarations = sourceFileImport . getExportedDeclarations ( ) ;
207
+
208
+ if ( exportDeclarations && exportDeclarations . size > 0 ) {
209
+ for ( const [
210
+ exportDeclarationKey ,
211
+ exportDeclarationValues
212
+ ] of exportDeclarations ) {
213
+ exportDeclarationValues . forEach ( exportDeclarationValue => {
214
+ if (
215
+ exportDeclarationValue instanceof VariableDeclaration &&
216
+ exportDeclarationValue . getName ( ) === variableName
217
+ ) {
218
+ return hasFoundValues ( exportDeclarationValue ) ;
206
219
}
207
- }
220
+ } ) ;
208
221
}
209
222
}
210
223
}
211
224
}
212
225
}
226
+ if (
227
+ ! importPathReference &&
228
+ decoratorType === 'template' &&
229
+ searchedImport . getModuleSpecifierValue ( ) . indexOf ( '.html' ) !== - 1
230
+ ) {
231
+ const originalSourceFilePath = sourceFile . path ;
232
+ const originalSourceFilePathFolder = originalSourceFilePath . substring (
233
+ 0 ,
234
+ originalSourceFilePath . lastIndexOf ( '/' )
235
+ ) ;
236
+ const finalImportedPath =
237
+ originalSourceFilePathFolder + '/' + searchedImport . getModuleSpecifierValue ( ) ;
238
+ const finalImportedPathData = FileEngine . getSync ( finalImportedPath ) ;
239
+ return finalImportedPathData ;
240
+ }
213
241
} else {
214
242
// Find in local variables of the file
215
243
const variableDeclaration = file . getVariableDeclaration ( metadataVariableName ) ;
@@ -227,6 +255,16 @@ export class ImportsUtil {
227
255
let compilerNode =
228
256
initializer . compilerNode as ts . ObjectLiteralExpression ;
229
257
return compilerNode . properties ;
258
+ } else if (
259
+ initializerKind &&
260
+ ( initializerKind === SyntaxKind . StringLiteral ||
261
+ initializerKind === SyntaxKind . NoSubstitutionTemplateLiteral )
262
+ ) {
263
+ if ( decoratorType === 'template' ) {
264
+ return initializer . getText ( ) ;
265
+ } else {
266
+ return variableDeclaration . compilerNode ;
267
+ }
230
268
} else if ( initializerKind ) {
231
269
return variableDeclaration . compilerNode ;
232
270
}
0 commit comments