Skip to content

Commit

Permalink
fix: eslint warn
Browse files Browse the repository at this point in the history
  • Loading branch information
peterroe committed Apr 8, 2023
1 parent c62584c commit 06a05a3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 62 deletions.
56 changes: 28 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 29 additions & 21 deletions src/analyze.ts
Expand Up @@ -28,7 +28,7 @@ export interface DynamicImport extends ESMImport {
}

export interface TypeImport extends ESMImport {
type: "type",
type: "type";
imports: string;
specifier: string;
}
Expand Down Expand Up @@ -94,11 +94,15 @@ export function findDynamicImports(code: string): DynamicImport[] {
export function findTypeImports(code: string): TypeImport[] {
return [
...matchAll(IMPORT_NAMED_TYPE_RE, code, { type: "type" }),
...(matchAll(ESM_STATIC_IMPORT_RE, code, { type: "static" }).filter(match => /[^A-Za-z]type\s/.test(match.imports)))
]
...matchAll(ESM_STATIC_IMPORT_RE, code, { type: "static" }).filter(
(match) => /[^A-Za-z]type\s/.test(match.imports)
),
];
}

export function parseStaticImport(matched: StaticImport | TypeImport): ParsedStaticImport {
export function parseStaticImport(
matched: StaticImport | TypeImport
): ParsedStaticImport {
const cleanedImports = (matched.imports || "")
.replace(/(\/\/[^\n]*\n|\/\*.*\*\/)/g, "")
.replace(/\s+/g, " ");
Expand Down Expand Up @@ -129,9 +133,11 @@ export function parseStaticImport(matched: StaticImport | TypeImport): ParsedSta
} as ParsedStaticImport;
}

export function parseTypeImport(matched: TypeImport | StaticImport): ParsedStaticImport {
if(matched.type === 'type') {
return parseStaticImport(matched)
export function parseTypeImport(
matched: TypeImport | StaticImport
): ParsedStaticImport {
if (matched.type === "type") {
return parseStaticImport(matched);
}
const cleanedImports = (matched.imports || "")
.replace(/(\/\/[^\n]*\n|\/\*.*\*\/)/g, "")
Expand All @@ -141,29 +147,31 @@ export function parseTypeImport(matched: TypeImport | StaticImport): ParsedStati
for (const namedImport of cleanedImports
.match(/{([^}]*)}/)?.[1]
?.split(",") || []) {
const [, source = namedImport.trim(), importName = source] = (() => {
return /\s+as\s+/.test(namedImport) ? namedImport.match(/^\s*type\s+(\S*) as (\S*)\s*$/) || [] : namedImport.match(/^\s*type\s+(\S*)\s*$/) || [];
})()
const [, source = namedImport.trim(), importName = source] = (() => {
return /\s+as\s+/.test(namedImport)
? namedImport.match(/^\s*type\s+(\S*) as (\S*)\s*$/) || []
: namedImport.match(/^\s*type\s+(\S*)\s*$/) || [];
})();

if (source && TYPE_RE.test(namedImport)) {
namedImports[source] = importName;
}
if (source && TYPE_RE.test(namedImport)) {
namedImports[source] = importName;
}
}

const topLevelImports = cleanedImports.replace(/{([^}]*)}/, "");
const namespacedImport = topLevelImports.match(/\* as \s*(\S*)/)?.[1];
const defaultImport =
topLevelImports
.split(",")
.find((index) => !/[*{}]/.test(index))
?.trim() || undefined;
const topLevelImports = cleanedImports.replace(/{([^}]*)}/, "");
const namespacedImport = topLevelImports.match(/\* as \s*(\S*)/)?.[1];
const defaultImport =
topLevelImports
.split(",")
.find((index) => !/[*{}]/.test(index))
?.trim() || undefined;

return {
...matched,
defaultImport,
namespacedImport,
namedImports,
} as ParsedStaticImport
} as ParsedStaticImport;
}

export function findExports(code: string): ESMExport[] {
Expand Down
26 changes: 13 additions & 13 deletions test/imports.test.ts
Expand Up @@ -4,7 +4,7 @@ import {
findStaticImports,
parseStaticImport,
findTypeImports,
parseTypeImport
parseTypeImport,
} from "../src";

// -- Static import --
Expand Down Expand Up @@ -147,50 +147,50 @@ const TypeTests = {
namedImports: {
Foo: "Foo",
},
type: "static"
type: "static",
},
'import { member,/* hello */ type Foo as Baz, Bar } from "module-name";': {
specifier: "module-name",
namedImports: {
Foo: "Baz",
},
type: "static"
type: "static",
},
'import type { Foo, Bar } from "module-name";': {
specifier: "module-name",
namedImports: {
Foo: "Foo",
Bar: "Bar"
Bar: "Bar",
},
type: "type"
type: "type",
},
'import type Foo from "module-name";': {
specifier: "module-name",
defaultImport: "Foo",
type: "type"
type: "type",
},
'import type { Foo as Baz, Bar } from "module-name";': {
specifier: "module-name",
namedImports: {
Foo: "Baz",
Bar: "Bar"
Bar: "Bar",
},
type: "type"
type: "type",
},
'import { type member } from " module-name";': {
specifier: "module-name",
namedImports: { member: "member" },
type: "static"
type: "static",
},
'import { type member, type Foo as Bar } from " module-name";': {
specifier: "module-name",
namedImports: {
member: "member",
Foo: "Bar"
Foo: "Bar",
},
type: "static"
type: "static",
},
}
};

describe("findStaticImports", () => {
for (const [input, _results] of Object.entries(staticTests)) {
Expand Down Expand Up @@ -257,4 +257,4 @@ describe("findTypeImports", () => {
}
});
}
})
});

0 comments on commit 06a05a3

Please sign in to comment.