@@ -4,6 +4,7 @@ import path from 'path';
44import ts from 'typescript' ;
55import {
66 convertInterface ,
7+ extractImportsFromImportTag ,
78 findCustomType ,
89 findInterfacesFromExtends ,
910 findPathAndTypesFromImports ,
@@ -192,26 +193,28 @@ describe('CEM', function () {
192193 describe ( 'findPath()' , function ( ) {
193194 const importsNode = classNode . jsDoc [ 0 ] . tags ;
194195 const importLength = importsNode . length ;
196+ const moduleFilePath = path . resolve ( ROOT_DIR , MODULE_DIR , 'cc-test-component.js' ) ;
195197
196198 it ( 'should retrieve the @typedef filePath from the first one in the test file.' , function ( ) {
197- const filePath = findTypePath ( importsNode [ 0 ] , ROOT_DIR , MODULE_DIR ) ;
199+ const filePath = findTypePath ( importsNode [ 0 ] , ts , moduleFilePath ) ;
198200 expect ( filePath ) . to . equal ( `${ ROOT_DIR } /${ MODULE_DIR } /cc-test-component.types.d.ts` ) ;
199201 } ) ;
200202
201203 it ( 'should retrieve the common @typedef filePath located at the end of the test file.' , function ( ) {
202- const filePath = findTypePath ( importsNode [ importLength - 1 ] , ROOT_DIR , MODULE_DIR ) ;
204+ const filePath = findTypePath ( importsNode [ importLength - 1 ] , ts , moduleFilePath ) ;
203205 expect ( filePath ) . to . equal ( `${ ROOT_DIR } /${ MODULE_DIR } /common.types.d.ts` ) ;
204206 } ) ;
205207
206208 it ( 'should return null if the filePath is incorrect.' , function ( ) {
207- const filePath = findTypePath ( importsNode [ importLength - 2 ] , ROOT_DIR , MODULE_DIR ) ;
209+ const filePath = findTypePath ( importsNode [ importLength - 2 ] , ts , moduleFilePath ) ;
208210 expect ( filePath ) . to . equal ( null ) ;
209211 } ) ;
210212 } ) ;
211213
212214 describe ( 'findSubtypes()' , function ( ) {
213215 const importsNode = classNode . jsDoc [ 0 ] . tags ;
214- const filePath = findTypePath ( importsNode [ 0 ] , ROOT_DIR , MODULE_DIR ) ;
216+ const moduleFilePath = path . resolve ( ROOT_DIR , MODULE_DIR , 'cc-test-component.js' ) ;
217+ const filePath = findTypePath ( importsNode [ 0 ] , ts , moduleFilePath ) ;
215218 const sourceCode = readFileSync ( filePath ) . toString ( ) ;
216219 const sourceAst = ts . createSourceFile ( filePath , sourceCode , ts . ScriptTarget . ES2015 , true ) ;
217220
@@ -277,7 +280,8 @@ describe('CEM', function () {
277280 describe ( 'convertInterface()' , function ( ) {
278281 it ( 'should return the needed interface in the type file for a given interface name.' , function ( ) {
279282 const importsNode = classNode . jsDoc [ 0 ] . tags ;
280- const filePath = findTypePath ( importsNode [ 0 ] , ROOT_DIR , MODULE_DIR ) ;
283+ const moduleFilePath = path . resolve ( ROOT_DIR , MODULE_DIR , 'cc-test-component.js' ) ;
284+ const filePath = findTypePath ( importsNode [ 0 ] , ts , moduleFilePath ) ;
281285 const sourceCode = readFileSync ( filePath ) . toString ( ) ;
282286 const sourceAst = ts . createSourceFile ( filePath , sourceCode , ts . ScriptTarget . ES2015 , true ) ;
283287 const interfaceStr = convertInterface ( ts , sourceAst , sourceCode , 'TheInterface' , filePath ) ;
@@ -316,7 +320,7 @@ describe('CEM', function () {
316320
317321 describe ( 'findPathAndTypesFromImports()' , function ( ) {
318322 it ( 'should return an array with all the imports filePath' , function ( ) {
319- const file = 'cc-test-component.types.js ' ;
323+ const file = 'cc-test-component.types.d.ts ' ;
320324 const pathFile = path . resolve ( ROOT_DIR , MODULE_DIR , file ) ;
321325 const rootPath = path . resolve ( ROOT_DIR , MODULE_DIR ) ;
322326
@@ -332,4 +336,54 @@ describe('CEM', function () {
332336 ] ) ;
333337 } ) ;
334338 } ) ;
339+
340+ describe ( 'extractImportsFromImportTag()' , function ( ) {
341+ const filenameWithImport = 'test-mocha/cem/fixtures/cc-test-component-with-import.js' ;
342+ const sourceCodeWithImport = fs . readFileSync ( filenameWithImport , { encoding : 'utf-8' } ) ;
343+ const sourceAstWithImport = ts . createSourceFile (
344+ filenameWithImport ,
345+ sourceCodeWithImport ,
346+ ts . ScriptTarget . ES2015 ,
347+ true ,
348+ ) ;
349+ const classNodeWithImport = sourceAstWithImport . statements . find (
350+ ( node ) => node . kind === ts . SyntaxKind . ClassDeclaration ,
351+ ) ;
352+ const moduleFilePath = path . resolve ( ROOT_DIR , MODULE_DIR , 'cc-test-component-with-import.js' ) ;
353+
354+ it ( 'should extract module path and types from @import tag' , function ( ) {
355+ const importTags =
356+ classNodeWithImport ?. jsDoc ?. flatMap (
357+ ( doc ) => doc . tags ?. filter ( ( tag ) => tag . kind === ts . SyntaxKind . JSDocImportTag ) || [ ] ,
358+ ) || [ ] ;
359+
360+ const firstImportTag = importTags [ 0 ] ;
361+ const result = extractImportsFromImportTag ( firstImportTag , ts , moduleFilePath ) ;
362+
363+ expect ( result ) . to . not . be . null ;
364+ expect ( result . path ) . to . equal ( `${ ROOT_DIR } /${ MODULE_DIR } /cc-test-component.types.d.ts` ) ;
365+ expect ( result . types ) . to . have . members ( [ 'Foo' , 'Bar' ] ) ;
366+ } ) ;
367+
368+ it ( 'should handle multiple types from same module' , function ( ) {
369+ const importTags =
370+ classNodeWithImport ?. jsDoc ?. flatMap (
371+ ( doc ) => doc . tags ?. filter ( ( tag ) => tag . kind === ts . SyntaxKind . JSDocImportTag ) || [ ] ,
372+ ) || [ ] ;
373+
374+ const secondImportTag = importTags [ 1 ] ;
375+ const result = extractImportsFromImportTag ( secondImportTag , ts , moduleFilePath ) ;
376+
377+ expect ( result ) . to . not . be . null ;
378+ expect ( result . path ) . to . equal ( `${ ROOT_DIR } /${ MODULE_DIR } /cc-test-component.types.d.ts` ) ;
379+ expect ( result . types ) . to . have . members ( [ 'TheInterface' ] ) ;
380+ } ) ;
381+
382+ it ( 'should return null for invalid import tag' , function ( ) {
383+ const invalidTag = { moduleSpecifier : null , importClause : null } ;
384+ const result = extractImportsFromImportTag ( invalidTag , ts , moduleFilePath ) ;
385+
386+ expect ( result ) . to . be . null ;
387+ } ) ;
388+ } ) ;
335389} ) ;
0 commit comments