11import * as path from 'path' ;
22
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' ;
45
56const ast = new Project ( ) ;
67
@@ -100,10 +101,16 @@ export class ImportsUtil {
100101 * @param {string } inputVariableName like myvar
101102 * @return {[type] } myvar value
102103 */
103- public findValueInImportOrLocalVariables ( inputVariableName : string , sourceFile : ts . SourceFile ) {
104+ public findValueInImportOrLocalVariables (
105+ inputVariableName : string ,
106+ sourceFile : ts . SourceFile ,
107+ decoratorType ?: string
108+ ) {
104109 let metadataVariableName = inputVariableName ,
105110 searchedImport ,
106111 aliasOriginalName = '' ,
112+ foundWithNamedImport = false ,
113+ foundWithDefaultImport = false ,
107114 foundWithAlias = false ;
108115
109116 const file =
@@ -129,17 +136,37 @@ export class ImportsUtil {
129136 importAlias = namedImports [ j ] . getAliasNode ( ) . getText ( ) ;
130137 }
131138 if ( importName === metadataVariableName ) {
139+ foundWithNamedImport = true ;
132140 searchedImport = i ;
133141 break ;
134142 }
135143 if ( importAlias === metadataVariableName ) {
144+ foundWithNamedImport = true ;
136145 foundWithAlias = true ;
137146 aliasOriginalName = importName ;
138147 searchedImport = i ;
139148 break ;
140149 }
141150 }
142151 }
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+ }
143170 } ) ;
144171
145172 function hasFoundValues ( variableDeclaration ) {
@@ -176,40 +203,41 @@ export class ImportsUtil {
176203 return hasFoundValues ( variableDeclaration ) ;
177204 } else {
178205 // 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 ) ;
206219 }
207- }
220+ } ) ;
208221 }
209222 }
210223 }
211224 }
212225 }
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+ }
213241 } else {
214242 // Find in local variables of the file
215243 const variableDeclaration = file . getVariableDeclaration ( metadataVariableName ) ;
@@ -227,6 +255,16 @@ export class ImportsUtil {
227255 let compilerNode =
228256 initializer . compilerNode as ts . ObjectLiteralExpression ;
229257 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+ }
230268 } else if ( initializerKind ) {
231269 return variableDeclaration . compilerNode ;
232270 }
0 commit comments