From 1523bbc78654e34dc275281b6025cf0d645fcb5e Mon Sep 17 00:00:00 2001 From: zhong666 Date: Fri, 23 Dec 2022 22:06:14 +0800 Subject: [PATCH] fix: ignore `type` imports and exports (#124) --- src/analyze.ts | 9 +++++++-- test/exports.test.ts | 11 +++++++++++ test/imports.test.ts | 5 +++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/analyze.ts b/src/analyze.ts index 715df26..3a44a9c 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -62,6 +62,7 @@ export const EXPORT_DECAL_RE = /\bexport\s+(?(async function|functi const EXPORT_NAMED_RE = /\bexport\s+{(?[^}]+?)[\s,]*}(\s*from\s*["']\s*(?(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][^\n;]*)?/g; const EXPORT_STAR_RE = /\bexport\s*(\*)(\s*as\s+(?[\w$]+)\s+)?\s*(\s*from\s*["']\s*(?(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][^\n;]*)?/g; const EXPORT_DEFAULT_RE = /\bexport\s+default\s+/g; +const TYPE_RE = /^\s*?type\s/; export function findStaticImports (code: string): StaticImport[] { return matchAll(ESM_STATIC_IMPORT_RE, code, { type: "static" }); @@ -79,7 +80,7 @@ export function parseStaticImport (matched: StaticImport): ParsedStaticImport { const namedImports = {}; for (const namedImport of cleanedImports.match(/{([^}]*)}/)?.[1]?.split(",") || []) { const [, source = namedImport.trim(), importName = source] = namedImport.match(/^\s*(\S*) as (\S*)\s*$/) || []; - if (source) { + if (source && !TYPE_RE.test(source)) { namedImports[source] = importName; } } @@ -102,7 +103,11 @@ export function findExports (code: string): ESMExport[] { // Find named exports const namedExports: NamedExport[] = matchAll(EXPORT_NAMED_RE, code, { type: "named" }); for (const namedExport of namedExports) { - namedExport.names = namedExport.exports.replace(/^\r?\n?/, "").split(/\s*,\s*/g).map(name => name.replace(/^.*?\sas\s/, "").trim()); + namedExport.names = namedExport.exports + .replace(/^\r?\n?/, "") + .split(/\s*,\s*/g) + .filter(name => !TYPE_RE.test(name)) + .map(name => name.replace(/^.*?\sas\s/, "").trim()); } // Find export default diff --git a/test/exports.test.ts b/test/exports.test.ts index 82190ea..0182843 100644 --- a/test/exports.test.ts +++ b/test/exports.test.ts @@ -146,6 +146,17 @@ export default function useMain() {} expect(matches1[0].name).toEqual("default"); expect(matches2).toEqual(matches1); }); + + it("ignore export type", () => { + const code = ` +export { type AType, type B as BType, foo } from 'foo' +`; + const matches = findExports(code); + expect(matches).toHaveLength(1); + expect(matches[0].names).toHaveLength(1); + expect(matches[0].names[0]).toEqual("foo"); + expect(matches[0].name).toEqual("foo"); + }); }); describe("fineExportNames", () => { diff --git a/test/imports.test.ts b/test/imports.test.ts index d2681b4..6812ae8 100644 --- a/test/imports.test.ts +++ b/test/imports.test.ts @@ -92,6 +92,11 @@ staticTests[`import { namedImports: { Component: "Component" } }; +staticTests["import { foo, type Foo } from \"foo\""] = { + specifier: "foo", + namedImports: { foo: "foo" } +}; + // -- Dynamic import -- const dynamicTests = { "const { test, /* here */, another, } = await import ( \"module-name\" );": {