Visitar URL original
fix: add missing type for default class exports by ilyaliao · Pull Request #542 · unjs/unimport · GitHub
Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions playground/composables/DefaultClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class DefaultClass {}
6 changes: 6 additions & 0 deletions src/node/scan-dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const RE_EXCLAMATION_PREFIX = /^!/
const RE_FILE_EXT = /\.\w+$/
const RE_NAME_SEPARATOR = /[-_.]/
const RE_DTS_EXT = /\.d\.[mc]?ts$/
const RE_DEFAULT_CLASS_EXPORT = /^export\s+default\s+class(?![\w$])/

function resolveGlobsExclude(glob: string, cwd: string) {
return `${glob.startsWith('!') ? '!' : ''}${resolve(cwd, glob.replace(RE_EXCLAMATION_PREFIX, ''))}`
Expand Down Expand Up @@ -195,6 +196,11 @@ export async function scanExports(filepath: string, includeTypes: boolean, seen
// see STR_SPLITTERS: https://github.com/unjs/scule/blob/main/src/index.ts
const as = RE_NAME_SEPARATOR.test(name) ? camelCase(name) : name
imports.push({ name: 'default', as, from: filepath })

// Emit a paired type entry for a default-exported class.
if (RE_DEFAULT_CLASS_EXPORT.test(code.slice(defaultExport.start))) {
imports.push({ name: 'default', as, from: filepath, type: true, declarationType: 'class' })
}
}

async function toImport(exports: ESMExport[], additional?: Partial<Import>) {
Expand Down
4 changes: 4 additions & 0 deletions test/dts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@
expect(
(await generateTypeDeclarations()).replaceAll(cwd, '<root>'),
)
.toMatchInlineSnapshot(`

Check failure on line 67 in test/dts.test.ts

View workflow job for this annotation

GitHub Actions / ci (lts/*, macos-latest)

test/dts.test.ts > dts

Error: Snapshot `dts 1` mismatched - Expected + Received @@ -1,11 +1,11 @@ "export {} declare global { const $: typeof import('jquery').$ const BAR: typeof import('<root>/playground/composables/comma-separated').BAR const CustomEnum: typeof import('<root>/playground/composables/index').CustomEnum - const DefaultClass: typeof import('<root>/playground/composables/DefaultClass').default + const DefaultClass: typeof import('<root>/playground/composables/DefaultClass').DefaultClass const FOO: typeof import('<root>/playground/composables/comma-separated').FOO const PascalCased: typeof import('<root>/playground/composables/PascalCased').PascalCased const THREE: typeof import('three') const bar: typeof import('<root>/playground/composables/nested/bar/index').bar const bump: typeof import('<root>/playground/composables/index').bump @@ -44,11 +44,11 @@ import('vue') // @ts-ignore export type { JQuery } from 'jquery' import('jquery') // @ts-ignore - export type { default as DefaultClass } from '<root>/playground/composables/DefaultClass' + export type { default as DefaultClass, DefaultClass } from '<root>/playground/composables/DefaultClass' import('<root>/playground/composables/DefaultClass') // @ts-ignore export type { PascalCased } from '<root>/playground/composables/PascalCased' import('<root>/playground/composables/PascalCased') // @ts-ignore ❯ test/dts.test.ts:67:6

Check failure on line 67 in test/dts.test.ts

View workflow job for this annotation

GitHub Actions / ci (lts/*, ubuntu-latest)

test/dts.test.ts > dts

Error: Snapshot `dts 1` mismatched - Expected + Received @@ -1,11 +1,11 @@ "export {} declare global { const $: typeof import('jquery').$ const BAR: typeof import('<root>/playground/composables/comma-separated').BAR const CustomEnum: typeof import('<root>/playground/composables/index').CustomEnum - const DefaultClass: typeof import('<root>/playground/composables/DefaultClass').default + const DefaultClass: typeof import('<root>/playground/composables/DefaultClass').DefaultClass const FOO: typeof import('<root>/playground/composables/comma-separated').FOO const PascalCased: typeof import('<root>/playground/composables/PascalCased').PascalCased const THREE: typeof import('three') const bar: typeof import('<root>/playground/composables/nested/bar/index').bar const bump: typeof import('<root>/playground/composables/index').bump @@ -44,11 +44,11 @@ import('vue') // @ts-ignore export type { JQuery } from 'jquery' import('jquery') // @ts-ignore - export type { default as DefaultClass } from '<root>/playground/composables/DefaultClass' + export type { default as DefaultClass, DefaultClass } from '<root>/playground/composables/DefaultClass' import('<root>/playground/composables/DefaultClass') // @ts-ignore export type { PascalCased } from '<root>/playground/composables/PascalCased' import('<root>/playground/composables/PascalCased') // @ts-ignore ❯ test/dts.test.ts:67:6

Check failure on line 67 in test/dts.test.ts

View workflow job for this annotation

GitHub Actions / ci (lts/*, windows-latest)

test/dts.test.ts > dts

Error: Snapshot `dts 1` mismatched - Expected + Received @@ -1,11 +1,11 @@ "export {} declare global { const $: typeof import('jquery').$ const BAR: typeof import('<root>/playground/composables/comma-separated').BAR const CustomEnum: typeof import('<root>/playground/composables/index').CustomEnum - const DefaultClass: typeof import('<root>/playground/composables/DefaultClass').default + const DefaultClass: typeof import('<root>/playground/composables/DefaultClass').DefaultClass const FOO: typeof import('<root>/playground/composables/comma-separated').FOO const PascalCased: typeof import('<root>/playground/composables/PascalCased').PascalCased const THREE: typeof import('three') const bar: typeof import('<root>/playground/composables/nested/bar/index').bar const bump: typeof import('<root>/playground/composables/index').bump @@ -44,11 +44,11 @@ import('vue') // @ts-ignore export type { JQuery } from 'jquery' import('jquery') // @ts-ignore - export type { default as DefaultClass } from '<root>/playground/composables/DefaultClass' + export type { default as DefaultClass, DefaultClass } from '<root>/playground/composables/DefaultClass' import('<root>/playground/composables/DefaultClass') // @ts-ignore export type { PascalCased } from '<root>/playground/composables/PascalCased' import('<root>/playground/composables/PascalCased') // @ts-ignore ❯ test/dts.test.ts:67:6
"export {}
declare global {
const $: typeof import('jquery').$
const BAR: typeof import('<root>/playground/composables/comma-separated').BAR
const CustomEnum: typeof import('<root>/playground/composables/index').CustomEnum
const DefaultClass: typeof import('<root>/playground/composables/DefaultClass').default
const FOO: typeof import('<root>/playground/composables/comma-separated').FOO
const PascalCased: typeof import('<root>/playground/composables/PascalCased').PascalCased
const THREE: typeof import('three')
Expand Down Expand Up @@ -112,6 +113,9 @@
export type { JQuery } from 'jquery'
import('jquery')
// @ts-ignore
export type { default as DefaultClass } from '<root>/playground/composables/DefaultClass'
import('<root>/playground/composables/DefaultClass')
// @ts-ignore
export type { PascalCased } from '<root>/playground/composables/PascalCased'
import('<root>/playground/composables/PascalCased')
// @ts-ignore
Expand Down
12 changes: 12 additions & 0 deletions test/scan-dirs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}))
.sort((a, b) => a.as!.localeCompare(b.as!)),
)
.toMatchInlineSnapshot(`

Check failure on line 16 in test/scan-dirs.test.ts

View workflow job for this annotation

GitHub Actions / ci (lts/*, macos-latest)

test/scan-dirs.test.ts > scan-dirs > scanDirExports

Error: Snapshot `scan-dirs > scanDirExports 1` mismatched - Expected + Received @@ -44,10 +44,22 @@ "from": "DefaultClass.ts", "name": "default", "type": true, }, { + "as": "DefaultClass", + "from": "DefaultClass.ts", + "name": "DefaultClass", + }, + { + "as": "DefaultClass", + "declarationType": "class", + "from": "DefaultClass.ts", + "name": "DefaultClass", + "type": true, + }, + { "as": "foo", "from": "foo.ts", "name": "default", }, { ❯ test/scan-dirs.test.ts:16:8

Check failure on line 16 in test/scan-dirs.test.ts

View workflow job for this annotation

GitHub Actions / ci (lts/*, ubuntu-latest)

test/scan-dirs.test.ts > scan-dirs > scanDirExports

Error: Snapshot `scan-dirs > scanDirExports 1` mismatched - Expected + Received @@ -44,10 +44,22 @@ "from": "DefaultClass.ts", "name": "default", "type": true, }, { + "as": "DefaultClass", + "from": "DefaultClass.ts", + "name": "DefaultClass", + }, + { + "as": "DefaultClass", + "declarationType": "class", + "from": "DefaultClass.ts", + "name": "DefaultClass", + "type": true, + }, + { "as": "foo", "from": "foo.ts", "name": "default", }, { ❯ test/scan-dirs.test.ts:16:8

Check failure on line 16 in test/scan-dirs.test.ts

View workflow job for this annotation

GitHub Actions / ci (lts/*, windows-latest)

test/scan-dirs.test.ts > scan-dirs > scanDirExports

Error: Snapshot `scan-dirs > scanDirExports 1` mismatched - Expected + Received @@ -44,10 +44,22 @@ "from": "DefaultClass.ts", "name": "default", "type": true, }, { + "as": "DefaultClass", + "from": "DefaultClass.ts", + "name": "DefaultClass", + }, + { + "as": "DefaultClass", + "declarationType": "class", + "from": "DefaultClass.ts", + "name": "DefaultClass", + "type": true, + }, + { "as": "foo", "from": "foo.ts", "name": "default", }, { ❯ test/scan-dirs.test.ts:16:8
[
{
"as": "BAR",
Expand Down Expand Up @@ -49,6 +49,18 @@
"name": "CustomType1",
"type": true,
},
{
"as": "DefaultClass",
"from": "DefaultClass.ts",
"name": "default",
},
{
"as": "DefaultClass",
"declarationType": "class",
"from": "DefaultClass.ts",
"name": "default",
"type": true,
},
{
"as": "foo",
"from": "foo.ts",
Expand Down
Loading