Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion src/commands/__tests__/scan.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { scan } from '../scan'
import { resetScanGlobals, scan } from '../scan'
import * as sd from '../../utils/scan-directories'
import * as ei from '../../utils/extract-imports'
import exp from 'constants'
Expand All @@ -12,6 +12,7 @@ describe('scan', () => {
const logSpy = jest.spyOn(console, 'log')

beforeEach(() => {
resetScanGlobals()
jest.clearAllMocks()
})

Expand Down Expand Up @@ -132,6 +133,14 @@ describe('scan', () => {
filePath: '/path/to/directory/file2.ts',
fileContent: 'import { useState } from "react";',
})
callback({
filePath: '/path/to/directory/file3.ts',
fileContent: 'import { useState } from "react";',
})
callback({
filePath: '/path/to/directory/file4.ts',
fileContent: 'import { useState } from "react";',
})
})

jest
Expand Down Expand Up @@ -169,4 +178,69 @@ describe('scan', () => {
expect.stringContaining('"default": {}'),
)
})

it('should work with multiple lines of import statements', () => {
const resolvedDirectory =
'/Users/aminroslan/Projects/Qwerqy/scan-imports/test'
jest.spyOn(path, 'resolve').mockReturnValue(resolvedDirectory)

const directory = 'test'
const importName = './importme.ts'
const extension = '.tsx,.ts'
const details = false

jest
.spyOn(sd, 'scanDirectories')
.mockImplementation((dirPath, modName, exts, callback) => {
callback({
filePath: `${resolvedDirectory}/file1.ts`,
fileContent: `import {
Foo,
Bar,
Baz,
Qux
} from "./importme.ts";`,
})
})

jest
.spyOn<any, any>(ei, 'extractImports')
.mockImplementation((_filePath, _modName) => {
return [
{
getText: () => `import {
Foo,
Bar,
Baz,
Qux
} from "./importme.ts";`,
getDefaultImport: () => null,
getNamedImports: () => [
{ getText: () => 'Foo' },
{ getText: () => 'Bar' },
{ getText: () => 'Baz' },
{ getText: () => 'Qux' },
],
},
]
})

scan({ directory, import: importName, extension, details, alpha: false })

expect(sd.scanDirectories).toHaveBeenCalled()
expect(logSpy.mock.calls[0][0].trim()).toMatchInlineSnapshot(
`"Found 1 files with "./importme.ts" imports across directory /Users/aminroslan/Projects/Qwerqy/scan-imports/test:"`,
)
expect(logSpy.mock.calls[1][0].trim()).toMatchInlineSnapshot(`
"{
 "default": {},
 "named": {
 "Foo": 1,
 "Bar": 1,
 "Baz": 1,
 "Qux": 1
 }
}"
`)
})
})
15 changes: 11 additions & 4 deletions src/commands/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import { scanDirectories } from '../utils/scan-directories'
import extractImports from '../utils/extract-imports'
import { appendFileSync } from 'fs'

let importCount = 0
let importResults: ImportResult[] = []
let importsUsed: ImportsUsed = {
export let importCount = 0
export let importResults: ImportResult[] = []
export let importsUsed: ImportsUsed = {
default: {},
named: {},
}
let importsCount: Import = {}
export let importsCount: Import = {}

export function resetScanGlobals() {
importCount = 0
importResults = []
importsUsed = { default: {}, named: {} }
importsCount = {}
}

export async function scan(options: {
directory: string
Expand Down
2 changes: 1 addition & 1 deletion src/utils/file-contains-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function fileContainsImport(
const fileContent = fs.readFileSync(filePath, 'utf8')

if (
new RegExp(`import.*from\\s+["']${importName}["']`, 'm').test(fileContent)
new RegExp(`import.*?from\\s+["']${importName}["']`, 'ms').test(fileContent)
) {
return { filePath, fileContent }
}
Expand Down
Loading