Skip to content

Commit

Permalink
fix: distinguish enums by declarationType (#329)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <[email protected]>
Co-authored-by: Anthony Fu <[email protected]>
  • Loading branch information
3 people authored May 25, 2024
1 parent 5cf8fd3 commit a4a5e0a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 28 deletions.
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 {}
31 changes: 5 additions & 26 deletions pnpm-lock.yaml

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

9 changes: 8 additions & 1 deletion 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.declarationType === 'enum')
return true

return !exports.find(e => e.as === i.as && e.name === i.name && !e.type)
})
}
Expand Down Expand Up @@ -95,8 +99,11 @@ export async function scanExports(filepath: string, includeTypes: boolean, seen
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.declarationType === 'enum')
imports.push({ name: exp.name, as: exp.name, from: filepath, type: true, declarationType: exp.declarationType, ...additional })
}
}
else if (exp.type === 'star' && exp.specifier) {
if (exp.name) {
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type MagicString from 'magic-string'
import type { ESMExport } from 'mlly'
import type { BuiltinPresetName } from './presets'

export type ModuleId = string
Expand All @@ -16,6 +17,8 @@ export interface ImportCommon {
disabled?: boolean
/** Won't output import in declaration file if true */
dtsDisabled?: boolean
/** Import declaration type like const / var / enum */
declarationType?: ESMExport['declarationType']
/**
* Metadata of the import
*/
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +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.declarationType === '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) {
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",
"declarationType": "enum",
"from": "index.ts",
"name": "CustomEnum",
"type": true,
},
{
"as": "CustomInterface1",
"from": "index.ts",
Expand Down

0 comments on commit a4a5e0a

Please sign in to comment.