@@ -112,9 +112,67 @@ describe('extractImportPaths', () => {
112112 )
113113
114114 const imports = extractImportPaths ( mainFile )
115- // The import extractor resolves './utils' to the directory path
115+ // The import extractor resolves './utils' to the index file
116116 expect ( imports ) . toHaveLength ( 1 )
117- expect ( imports [ 0 ] ) . toBe ( utilsDir )
117+ expect ( imports [ 0 ] ) . toBe ( utilsIndex )
118+ } )
119+ } )
120+
121+ test ( 'resolves directory imports to various index file types' , async ( ) => {
122+ await inTemporaryDirectory ( async ( tmpDir ) => {
123+ // Test with index.jsx
124+ const jsxDir = joinPath ( tmpDir , 'jsx-component' )
125+ const jsxIndex = joinPath ( jsxDir , 'index.jsx' )
126+ await mkdir ( jsxDir )
127+ await writeFile ( jsxIndex , 'export default () => <div />' )
128+
129+ // Test with index.tsx
130+ const tsxDir = joinPath ( tmpDir , 'tsx-component' )
131+ const tsxIndex = joinPath ( tsxDir , 'index.tsx' )
132+ await mkdir ( tsxDir )
133+ await writeFile ( tsxIndex , 'export default () => <div />' )
134+
135+ // Test with index.js (when importing from TypeScript)
136+ const jsDir = joinPath ( tmpDir , 'js-module' )
137+ const jsIndex = joinPath ( jsDir , 'index.js' )
138+ await mkdir ( jsDir )
139+ await writeFile ( jsIndex , 'module.exports = {}' )
140+ const mainFile = joinPath ( tmpDir , 'main.ts' )
141+ await writeFile (
142+ mainFile ,
143+ `
144+ import jsxComponent from './jsx-component'
145+ import tsxComponent from './tsx-component'
146+ import jsModule from './js-module'
147+ ` ,
148+ )
149+
150+ const imports = extractImportPaths ( mainFile )
151+ expect ( imports ) . toHaveLength ( 3 )
152+ expect ( imports ) . toContain ( jsxIndex )
153+ expect ( imports ) . toContain ( tsxIndex )
154+ expect ( imports ) . toContain ( jsIndex )
155+ } )
156+ } )
157+
158+ test ( 'returns empty array when directory has no index file' , async ( ) => {
159+ await inTemporaryDirectory ( async ( tmpDir ) => {
160+ const mainFile = joinPath ( tmpDir , 'main.js' )
161+ const emptyDir = joinPath ( tmpDir , 'empty-dir' )
162+
163+ await mkdir ( emptyDir )
164+ // Don't create any index file in the directory
165+
166+ await writeFile (
167+ mainFile ,
168+ `
169+ import something from './empty-dir'
170+ ` ,
171+ )
172+
173+ const imports = extractImportPaths ( mainFile )
174+ // Should return empty array since no index file exists
175+ expect ( imports ) . toHaveLength ( 0 )
118176 } )
119177 } )
120178 } )
@@ -501,12 +559,12 @@ describe('extractImportPathsRecursively', () => {
501559
502560 const imports = extractImportPathsRecursively ( mainFile )
503561
504- // When importing from './components', the resolveJSImport function returns the directory path
505- // The recursive function doesn't currently handle following imports from directories
506- // This is a known limitation of the current implementation
562+ // When importing from './components', it should resolve to index.js and recursively extract all imports
507563 expect ( imports ) . toContain ( mainFile )
508- expect ( imports ) . toContain ( componentsDir )
509- expect ( imports ) . toHaveLength ( 2 )
564+ expect ( imports ) . toContain ( componentsIndex )
565+ expect ( imports ) . toContain ( buttonFile )
566+ expect ( imports ) . toContain ( inputFile )
567+ expect ( imports ) . toHaveLength ( 4 )
510568 } )
511569 } )
512570
0 commit comments