Skip to content

Commit ad03615

Browse files
authored
refactor: generate valid references for declaration items (#474)
* refactor: generate valid references for declaration items * fix: inline `identifierRE` * test: update snapshot
1 parent 2768c61 commit ad03615

File tree

6 files changed

+55
-54
lines changed

6 files changed

+55
-54
lines changed

src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export function defineUnimportPreset(preset: InlinePreset): InlinePreset {
99
return preset
1010
}
1111

12+
const identifierRE = /^[A-Z_$][\w$]*$/i
1213
const safePropertyName = /^[a-z$_][\w$]*$/i
1314

1415
function stringifyWith(withValues: Record<string, string>) {
@@ -190,7 +191,7 @@ export function toTypeDeclarationItems(imports: Import[], options?: TypeDeclarat
190191
typeDef += `import('${from}')`
191192

192193
if (i.name !== '*' && i.name !== '=')
193-
typeDef += `['${i.name}']`
194+
typeDef += identifierRE.test(i.name) ? `.${i.name}` : `['${i.name}']`
194195

195196
return `const ${i.as}: typeof ${typeDef}`
196197
})

test/addon-extend-imports.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ describe('addon extend imports', () => {
5454
.toMatchInlineSnapshot(`
5555
"export {}
5656
declare global {
57-
const computed1: typeof import('vue')['computed']
58-
const ref1: typeof import('vue')['ref']
59-
const useEffect: typeof import('react')['useEffect']
60-
const useState: typeof import('react')['useState']
61-
const watch1: typeof import('vue')['watch']
57+
const computed1: typeof import('vue').computed
58+
const ref1: typeof import('vue').ref
59+
const useEffect: typeof import('react').useEffect
60+
const useState: typeof import('react').useState
61+
const watch1: typeof import('vue').watch
6262
}"
6363
`)
6464
})
@@ -84,12 +84,12 @@ describe('addon extend imports', () => {
8484
.toMatchInlineSnapshot(`
8585
"export {}
8686
declare global {
87-
const computed1: typeof import('vue')['computed']
88-
const foo1: typeof import('vue')['foo']
89-
const ref1: typeof import('vue')['ref']
90-
const useEffect: typeof import('react')['useEffect']
91-
const useState: typeof import('react')['useState']
92-
const watch1: typeof import('vue')['watch']
87+
const computed1: typeof import('vue').computed
88+
const foo1: typeof import('vue').foo
89+
const ref1: typeof import('vue').ref
90+
const useEffect: typeof import('react').useEffect
91+
const useState: typeof import('react').useState
92+
const watch1: typeof import('vue').watch
9393
}"
9494
`)
9595
})

test/addon-inject-hooks.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ describe('addon inject hooks', () => {
4343
.toMatchInlineSnapshot(`
4444
"export {}
4545
declare global {
46-
const computed: typeof import('vue')['computed']
47-
const ref: typeof import('vue')['ref']
48-
const useEffect: typeof import('react')['useEffect']
49-
const useState: typeof import('react')['useState']
50-
const watch: typeof import('vue')['watch']
46+
const computed: typeof import('vue').computed
47+
const ref: typeof import('vue').ref
48+
const useEffect: typeof import('react').useEffect
49+
const useState: typeof import('react').useState
50+
const watch: typeof import('vue').watch
5151
}"
5252
`)
5353
})

test/dts.test.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,38 +67,38 @@ it('dts', async () => {
6767
.toMatchInlineSnapshot(`
6868
"export {}
6969
declare global {
70-
const $: typeof import('jquery')['$']
71-
const CustomEnum: typeof import('<root>/playground/composables/index')['CustomEnum']
72-
const PascalCased: typeof import('<root>/playground/composables/PascalCased')['PascalCased']
70+
const $: typeof import('jquery').$
71+
const CustomEnum: typeof import('<root>/playground/composables/index').CustomEnum
72+
const PascalCased: typeof import('<root>/playground/composables/PascalCased').PascalCased
7373
const THREE: typeof import('three')
74-
const bar: typeof import('<root>/playground/composables/nested/bar/index')['bar']
75-
const bump: typeof import('<root>/playground/composables/index')['bump']
76-
const computed: typeof import('vue')['computed']
77-
const customDefault: typeof import('default')['default']
78-
const doTheThing: typeof import('my-macro-library', { with: { type: "macro" } })['doTheThing']
79-
const foo: typeof import('<root>/playground/composables/foo')['default']
80-
const foobar: typeof import('foobar')['foobar']
81-
const localA: typeof import('<root>/playground/composables/index')['localA']
82-
const localBAlias: typeof import('<root>/playground/composables/index')['localBAlias']
83-
const multiplier: typeof import('<root>/playground/composables/index')['multiplier']
84-
const myBazFunction: typeof import('<root>/playground/composables/nested/bar/baz')['myBazFunction']
85-
const myfunc1: typeof import('<root>/playground/composables/nested/bar/named')['myfunc1']
86-
const myfunc2: typeof import('<root>/playground/composables/nested/bar/named')['myfunc2']
87-
const named: typeof import('<root>/playground/composables/nested/bar/index')['named']
88-
const nested: typeof import('<root>/playground/composables/nested/index')['default']
89-
const pkg: typeof import('pkg/dts')['pkg']
90-
const reactive: typeof import('vue')['reactive']
91-
const ref: typeof import('vue')['ref']
92-
const subFoo: typeof import('<root>/playground/composables/nested/bar/sub/index')['subFoo']
93-
const toRefs: typeof import('vue')['toRefs']
94-
const useDoubled: typeof import('<root>/playground/composables/index')['useDoubled']
95-
const useEffect: typeof import('react')['useEffect']
96-
const useRef: typeof import('react')['useRef']
97-
const useState: typeof import('react')['useState']
98-
const vanillaA: typeof import('<root>/playground/composables/vanilla')['vanillaA']
99-
const vanillaAMJS: typeof import('<root>/playground/composables/vanilla')['vanillaAMJS']
100-
const vanillaB: typeof import('<root>/playground/composables/vanilla')['vanillaB']
101-
const vanillaBMJS: typeof import('<root>/playground/composables/vanilla')['vanillaBMJS']
74+
const bar: typeof import('<root>/playground/composables/nested/bar/index').bar
75+
const bump: typeof import('<root>/playground/composables/index').bump
76+
const computed: typeof import('vue').computed
77+
const customDefault: typeof import('default').default
78+
const doTheThing: typeof import('my-macro-library', { with: { type: "macro" } }).doTheThing
79+
const foo: typeof import('<root>/playground/composables/foo').default
80+
const foobar: typeof import('foobar').foobar
81+
const localA: typeof import('<root>/playground/composables/index').localA
82+
const localBAlias: typeof import('<root>/playground/composables/index').localBAlias
83+
const multiplier: typeof import('<root>/playground/composables/index').multiplier
84+
const myBazFunction: typeof import('<root>/playground/composables/nested/bar/baz').myBazFunction
85+
const myfunc1: typeof import('<root>/playground/composables/nested/bar/named').myfunc1
86+
const myfunc2: typeof import('<root>/playground/composables/nested/bar/named').myfunc2
87+
const named: typeof import('<root>/playground/composables/nested/bar/index').named
88+
const nested: typeof import('<root>/playground/composables/nested/index').default
89+
const pkg: typeof import('pkg/dts').pkg
90+
const reactive: typeof import('vue').reactive
91+
const ref: typeof import('vue').ref
92+
const subFoo: typeof import('<root>/playground/composables/nested/bar/sub/index').subFoo
93+
const toRefs: typeof import('vue').toRefs
94+
const useDoubled: typeof import('<root>/playground/composables/index').useDoubled
95+
const useEffect: typeof import('react').useEffect
96+
const useRef: typeof import('react').useRef
97+
const useState: typeof import('react').useState
98+
const vanillaA: typeof import('<root>/playground/composables/vanilla').vanillaA
99+
const vanillaAMJS: typeof import('<root>/playground/composables/vanilla').vanillaAMJS
100+
const vanillaB: typeof import('<root>/playground/composables/vanilla').vanillaB
101+
const vanillaBMJS: typeof import('<root>/playground/composables/vanilla').vanillaBMJS
102102
}
103103
// for type re-export
104104
declare global {

test/vue-directives.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ describe('vue-directives', () => {
178178
expect(replaceRoot(await ctx.generateTypeDeclarations())).toMatchInlineSnapshot(`
179179
"export {}
180180
declare global {
181-
const FocusDirective: typeof import('<root>/directives/v-focus-directive')['default']
182-
const NamedDirective: typeof import('<root>/directives/named-directive')['NamedDirective']
183-
const default: typeof import('<root>/directives/awesome-directive')['default']
184-
const vRippleDirective: typeof import('<root>/directives/ripple-directive')['vRippleDirective']
181+
const FocusDirective: typeof import('<root>/directives/v-focus-directive').default
182+
const NamedDirective: typeof import('<root>/directives/named-directive').NamedDirective
183+
const default: typeof import('<root>/directives/awesome-directive').default
184+
const vRippleDirective: typeof import('<root>/directives/ripple-directive').vRippleDirective
185185
}
186186
// for vue directives auto import
187187
declare module 'vue' {
@@ -664,7 +664,7 @@ describe('vue-directives', () => {
664664
expect(replaceRoot(await ctx.generateTypeDeclarations())).toMatchInlineSnapshot(`
665665
"export {}
666666
declare global {
667-
const AwesomeDirective: typeof import('<root>/directives/awesome-directive')['AwesomeDirective']
667+
const AwesomeDirective: typeof import('<root>/directives/awesome-directive').AwesomeDirective
668668
}
669669
// for vue directives auto import
670670
declare module 'vue' {

test/vue-template.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('vue-template', () => {
7474
expect(await ctx.generateTypeDeclarations()).toMatchInlineSnapshot(`
7575
"export {}
7676
declare global {
77-
const foo: typeof import('foo')['foo']
77+
const foo: typeof import('foo').foo
7878
}
7979
// for vue template auto import
8080
import { UnwrapRef } from 'vue'

0 commit comments

Comments
 (0)