diff --git a/src/commands/__tests__/scan.test.ts b/src/commands/__tests__/scan.test.ts index 53e61fd..923bb82 100644 --- a/src/commands/__tests__/scan.test.ts +++ b/src/commands/__tests__/scan.test.ts @@ -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' @@ -12,6 +12,7 @@ describe('scan', () => { const logSpy = jest.spyOn(console, 'log') beforeEach(() => { + resetScanGlobals() jest.clearAllMocks() }) @@ -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 @@ -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(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 + } +}" +`) + }) }) diff --git a/src/commands/scan.ts b/src/commands/scan.ts index 2a808a3..614bd64 100644 --- a/src/commands/scan.ts +++ b/src/commands/scan.ts @@ -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 diff --git a/src/utils/file-contains-import.ts b/src/utils/file-contains-import.ts index bb271ac..14e7752 100644 --- a/src/utils/file-contains-import.ts +++ b/src/utils/file-contains-import.ts @@ -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 } }