diff --git a/src/analyze.ts b/src/analyze.ts index 87f607c..817bf52 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -63,11 +63,11 @@ export interface DefaultExport extends ESMExport { } export const ESM_STATIC_IMPORT_RE = - /(?<=\s|^|;)import\s*([\s"']*(?[\p{L}\p{M}\w\t\n\r $*,/{}@.]+)from\s*)?["']\s*(?(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gmu; + /(?<=\s|^|;|\})import\s*([\s"']*(?[\p{L}\p{M}\w\t\n\r $*,/{}@.]+)from\s*)?["']\s*(?(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gmu; export const DYNAMIC_IMPORT_RE = /import\s*\((?(?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*)\)/gm; const IMPORT_NAMED_TYPE_RE = - /(?<=\s|^|;)import\s*type\s+([\s"']*(?[\w\t\n\r $*,/{}]+)from\s*)?["']\s*(?(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gm; + /(?<=\s|^|;|})import\s*type\s+([\s"']*(?[\w\t\n\r $*,/{}]+)from\s*)?["']\s*(?(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gm; export const EXPORT_DECAL_RE = /\bexport\s+(?(async function\s*\*?|function\s*\*?|let|const enum|const|enum|var|class))\s+\*?(?[\w$]+)/g; diff --git a/test/imports.test.ts b/test/imports.test.ts index 9a1181f..3d40b4f 100644 --- a/test/imports.test.ts +++ b/test/imports.test.ts @@ -87,6 +87,13 @@ const staticTests = { specifier: "bar", }, ], + "function a(){}import baz, { x, y as z } from 'baz'": [ + { + defaultImport: "baz", + namedImports: { x: "x", y: "z" }, + specifier: "baz", + }, + ], }; staticTests[ @@ -239,7 +246,7 @@ describe("findStaticImports", () => { it(input.replace(/\n/g, "\\n"), () => { const matches = findStaticImports(input); const expected = Array.isArray(_results) ? _results : [_results]; - expect(expected.length).toEqual(matches.length); + expect(matches.length).toEqual(expected.length); for (const [index, test] of expected.entries()) { const match = matches[index]; expect(match.type).to.equal("static");