Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: re export ts enum type #325

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 2 additions & 0 deletions playground/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export type CustomType1 = string | number
export interface CustomInterface1 {
name: string
}

export enum CustomEnum {}
11 changes: 9 additions & 2 deletions src/node/scan-dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export function dedupeDtsExports(exports: Import[]) {
if (!i.type)
return true

// import enum as both value and type
if (i.declaration === 'enum')
return true

return !exports.find(e => e.as === i.as && e.name === i.name && !e.type)
})
}
Expand Down Expand Up @@ -88,15 +92,18 @@ export async function scanExports(filepath: string, includeTypes: boolean, seen
imports.push({ name: 'default', as, from: filepath })
}

async function toImport(exports: ESMExport[], additional?: Partial<Import>) {
async function toImport(exports: (ESMExport & { declaration?: string })[], additional?: Partial<Import>) {
for (const exp of exports) {
if (exp.type === 'named') {
for (const name of exp.names)
imports.push({ name, as: name, from: filepath, ...additional })
}
else if (exp.type === 'declaration') {
if (exp.name)
if (exp.name) {
imports.push({ name: exp.name, as: exp.name, from: filepath, ...additional })
if (exp.declaration === 'enum')
imports.push({ name: exp.name, as: exp.name, from: filepath, type: true, declaration: exp.declaration, ...additional })
}
}
else if (exp.type === 'star' && exp.specifier) {
if (exp.name) {
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface ImportCommon {
disabled?: boolean
/** Won't output import in declaration file if true */
dtsDisabled?: boolean
/** Import declaration like const / var / enum */
declaration?: string
/**
* Metadata of the import
*/
Expand Down
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ export function dedupeImports(imports: Import[], warn: (msg: string) => void) {
const indexToRemove = new Set<number>()

imports.filter(i => !i.disabled).forEach((i, idx) => {
if (i.declaration === 'enum')
return

const name = i.as ?? i.name
if (!map.has(name)) {
map.set(name, idx)
return
}

const other = imports[map.get(name)!]
if (other.from === i.from) {
if (other.from === i.from && i?.declaration !== 'enum') {
indexToRemove.add(idx)
return
}
Expand Down
3 changes: 2 additions & 1 deletion test/dts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ it('dts', async () => {
"export {}
declare global {
const $: typeof import('jquery')['$']
const CustomEnum: typeof import('<root>/playground/composables/index')['CustomEnum']
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']
Expand Down Expand Up @@ -100,7 +101,7 @@ it('dts', async () => {
export type { JQuery } from 'jquery'
import('jquery')
// @ts-ignore
export type { CustomType1, CustomInterface1 } from '<root>/playground/composables/index'
export type { CustomEnum, CustomType1, CustomInterface1 } from '<root>/playground/composables/index'
import('<root>/playground/composables/index')
// @ts-ignore
export type { CustomType2 } from '<root>/playground/composables/nested/bar/sub/index'
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 @@ -19,6 +19,18 @@ describe('scan-dirs', () => {
"from": "index.ts",
"name": "bump",
},
{
"as": "CustomEnum",
"from": "index.ts",
"name": "CustomEnum",
},
{
"as": "CustomEnum",
"declaration": "enum",
"from": "index.ts",
"name": "CustomEnum",
"type": true,
},
{
"as": "CustomInterface1",
"from": "index.ts",
Expand Down
Loading