Skip to content

Commit

Permalink
fix: ignore type imports and exports (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
aa900031 committed Dec 23, 2022
1 parent e252ac5 commit 1523bbc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/analyze.ts
Expand Up @@ -62,6 +62,7 @@ export const EXPORT_DECAL_RE = /\bexport\s+(?<declaration>(async function|functi
const EXPORT_NAMED_RE = /\bexport\s+{(?<exports>[^}]+?)[\s,]*}(\s*from\s*["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][^\n;]*)?/g;
const EXPORT_STAR_RE = /\bexport\s*(\*)(\s*as\s+(?<name>[\w$]+)\s+)?\s*(\s*from\s*["']\s*(?<specifier>(?<="\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" });
Expand All @@ -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;
}
}
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/exports.test.ts
Expand Up @@ -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", () => {
Expand Down
5 changes: 5 additions & 0 deletions test/imports.test.ts
Expand Up @@ -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\" );": {
Expand Down

0 comments on commit 1523bbc

Please sign in to comment.