Skip to content

Commit bb01b3d

Browse files
committed
fix: multiline imports
1 parent db8d3fe commit bb01b3d

File tree

3 files changed

+87
-6
lines changed

3 files changed

+87
-6
lines changed

src/commands/__tests__/scan.test.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path'
2-
import { scan } from '../scan'
2+
import { resetScanGlobals, scan } from '../scan'
33
import * as sd from '../../utils/scan-directories'
44
import * as ei from '../../utils/extract-imports'
55
import exp from 'constants'
@@ -12,6 +12,7 @@ describe('scan', () => {
1212
const logSpy = jest.spyOn(console, 'log')
1313

1414
beforeEach(() => {
15+
resetScanGlobals()
1516
jest.clearAllMocks()
1617
})
1718

@@ -132,6 +133,14 @@ describe('scan', () => {
132133
filePath: '/path/to/directory/file2.ts',
133134
fileContent: 'import { useState } from "react";',
134135
})
136+
callback({
137+
filePath: '/path/to/directory/file3.ts',
138+
fileContent: 'import { useState } from "react";',
139+
})
140+
callback({
141+
filePath: '/path/to/directory/file4.ts',
142+
fileContent: 'import { useState } from "react";',
143+
})
135144
})
136145

137146
jest
@@ -169,4 +178,69 @@ describe('scan', () => {
169178
expect.stringContaining('"default": {}'),
170179
)
171180
})
181+
182+
it('should work with multiple lines of import statements', () => {
183+
const resolvedDirectory =
184+
'/Users/aminroslan/Projects/Qwerqy/scan-imports/test'
185+
jest.spyOn(path, 'resolve').mockReturnValue(resolvedDirectory)
186+
187+
const directory = 'test'
188+
const importName = './importme.ts'
189+
const extension = '.tsx,.ts'
190+
const details = false
191+
192+
jest
193+
.spyOn(sd, 'scanDirectories')
194+
.mockImplementation((dirPath, modName, exts, callback) => {
195+
callback({
196+
filePath: `${resolvedDirectory}/file1.ts`,
197+
fileContent: `import {
198+
Foo,
199+
Bar,
200+
Baz,
201+
Qux
202+
} from "./importme.ts";`,
203+
})
204+
})
205+
206+
jest
207+
.spyOn<any, any>(ei, 'extractImports')
208+
.mockImplementation((_filePath, _modName) => {
209+
return [
210+
{
211+
getText: () => `import {
212+
Foo,
213+
Bar,
214+
Baz,
215+
Qux
216+
} from "./importme.ts";`,
217+
getDefaultImport: () => null,
218+
getNamedImports: () => [
219+
{ getText: () => 'Foo' },
220+
{ getText: () => 'Bar' },
221+
{ getText: () => 'Baz' },
222+
{ getText: () => 'Qux' },
223+
],
224+
},
225+
]
226+
})
227+
228+
scan({ directory, import: importName, extension, details, alpha: false })
229+
230+
expect(sd.scanDirectories).toHaveBeenCalled()
231+
expect(logSpy.mock.calls[0][0].trim()).toMatchInlineSnapshot(
232+
`"Found 1 files with "./importme.ts" imports across directory /Users/aminroslan/Projects/Qwerqy/scan-imports/test:"`,
233+
)
234+
expect(logSpy.mock.calls[1][0].trim()).toMatchInlineSnapshot(`
235+
"{
236+
 "default": {},
237+
 "named": {
238+
 "Foo": 1,
239+
 "Bar": 1,
240+
 "Baz": 1,
241+
 "Qux": 1
242+
 }
243+
}"
244+
`)
245+
})
172246
})

src/commands/scan.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ import { scanDirectories } from '../utils/scan-directories'
55
import extractImports from '../utils/extract-imports'
66
import { appendFileSync } from 'fs'
77

8-
let importCount = 0
9-
let importResults: ImportResult[] = []
10-
let importsUsed: ImportsUsed = {
8+
export let importCount = 0
9+
export let importResults: ImportResult[] = []
10+
export let importsUsed: ImportsUsed = {
1111
default: {},
1212
named: {},
1313
}
14-
let importsCount: Import = {}
14+
export let importsCount: Import = {}
15+
16+
export function resetScanGlobals() {
17+
importCount = 0
18+
importResults = []
19+
importsUsed = { default: {}, named: {} }
20+
importsCount = {}
21+
}
1522

1623
export async function scan(options: {
1724
directory: string

src/utils/file-contains-import.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function fileContainsImport(
1616
const fileContent = fs.readFileSync(filePath, 'utf8')
1717

1818
if (
19-
new RegExp(`import.*from\\s+["']${importName}["']`, 'm').test(fileContent)
19+
new RegExp(`import.*?from\\s+["']${importName}["']`, 'ms').test(fileContent)
2020
) {
2121
return { filePath, fileContent }
2222
}

0 commit comments

Comments
 (0)