Skip to content

Commit 16e9ada

Browse files
authored
fix: parse declaration error (unplugin#450)
1 parent 5268755 commit 16e9ada

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

src/core/declaration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const multilineCommentsRE = /\/\*.*?\*\//gms
1212
const singlelineCommentsRE = /\/\/.*$/gm
1313

1414
function extractImports(code: string) {
15-
return Object.fromEntries(Array.from(code.matchAll(/['"]?([\S]+?)['"]?\s*:\s*(.+?)[,;\n]/g)).map(i => [i[1], i[2]]))
15+
return Object.fromEntries(Array.from(code.matchAll(/['"]?([^\s'"]+)['"]?\s*:\s*(.+?)[,;\n]/g)).map(i => [i[1], i[2]]))
1616
}
1717

1818
export function parseDeclaration(code: string): DeclarationImports | undefined {

test/__snapshots__/dts.test.ts.snap

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,43 @@ declare module '@vue/runtime-core' {
2222
"
2323
`;
2424

25+
exports[`dts > parseDeclaration - has icon component like <IMdi:diceD12> 1`] = `
26+
{
27+
"component": {
28+
"ComponentA": "typeof import('./src/components/ComponentA.vue')['default']",
29+
"IMdi:diceD12": "typeof import('~icons/mdi/dice-d12')['default']",
30+
"IMdiLightAlarm": "typeof import('~icons/mdi-light/alarm')['default']",
31+
},
32+
"directive": {},
33+
}
34+
`;
35+
36+
exports[`dts > parseDeclaration - with directives 1`] = `
37+
{
38+
"component": {
39+
"ComponentA": "typeof import('./src/components/ComponentA.vue')['default']",
40+
"IMdi:diceD12": "typeof import('~icons/mdi/dice-d12')['default']",
41+
"IMdiLightAlarm": "typeof import('~icons/mdi-light/alarm')['default']",
42+
},
43+
"directive": {
44+
"vDirective": "typeof import('foo')",
45+
"vLoading": "typeof import('test/directive/Loading')['default']",
46+
"vSome": "typeof import('test/directive/Some')['default']",
47+
},
48+
}
49+
`;
50+
51+
exports[`dts > parseDeclaration 1`] = `
52+
{
53+
"component": {
54+
"ComponentA": "typeof import('./src/components/ComponentA.vue')['default']",
55+
"ComponentB": "typeof import('./src/components/ComponentB.vue')['default']",
56+
"ComponentC": "typeof import('./src/components/component-c.vue')['default']",
57+
},
58+
"directive": {},
59+
}
60+
`;
61+
2562
exports[`dts > writeDeclaration - keep unused 1`] = `
2663
"// generated by unplugin-vue-components
2764
// We suggest you to commit this file into source control

test/dts.test.ts

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path'
33
import { describe, expect, test } from 'vitest'
44
import type { ComponentResolver } from '../src'
55
import { Context } from '../src/core/context'
6-
import { getDeclaration } from '../src/core/declaration'
6+
import { getDeclaration, parseDeclaration } from '../src/core/declaration'
77

88
const resolver: ComponentResolver[] = [
99
{
@@ -82,4 +82,73 @@ const _directive_loading = _resolveDirective("loading")`
8282
expect(contents).not.toContain('comment')
8383
expect(contents).toContain('vSome')
8484
})
85+
86+
test('parseDeclaration', async () => {
87+
const code = `
88+
// generated by unplugin-vue-components
89+
// We suggest you to commit this file into source control
90+
// Read more: https://github.com/vuejs/core/pull/3399
91+
import '@vue/runtime-core'
92+
93+
export {}
94+
95+
declare module '@vue/runtime-core' {
96+
export interface GlobalComponents {
97+
ComponentA: typeof import('./src/components/ComponentA.vue')['default']
98+
ComponentB: typeof import('./src/components/ComponentB.vue')['default']
99+
ComponentC: typeof import('./src/components/component-c.vue')['default']
100+
}
101+
}`
102+
103+
const imports = parseDeclaration(code)
104+
expect(imports).matchSnapshot()
105+
})
106+
107+
test('parseDeclaration - has icon component like <IMdi:diceD12>', async () => {
108+
const code = `
109+
// generated by unplugin-vue-components
110+
// We suggest you to commit this file into source control
111+
// Read more: https://github.com/vuejs/core/pull/3399
112+
import '@vue/runtime-core'
113+
114+
export {}
115+
116+
declare module '@vue/runtime-core' {
117+
export interface GlobalComponents {
118+
ComponentA: typeof import('./src/components/ComponentA.vue')['default']
119+
'IMdi:diceD12': typeof import('~icons/mdi/dice-d12')['default']
120+
IMdiLightAlarm: typeof import('~icons/mdi-light/alarm')['default']
121+
}
122+
}`
123+
124+
const imports = parseDeclaration(code)
125+
expect(imports).matchSnapshot()
126+
})
127+
128+
test('parseDeclaration - with directives', async () => {
129+
const code = `
130+
// generated by unplugin-vue-components
131+
// We suggest you to commit this file into source control
132+
// Read more: https://github.com/vuejs/core/pull/3399
133+
import '@vue/runtime-core'
134+
135+
export {}
136+
137+
declare module '@vue/runtime-core' {
138+
export interface GlobalComponents {
139+
ComponentA: typeof import('./src/components/ComponentA.vue')['default']
140+
'IMdi:diceD12': typeof import('~icons/mdi/dice-d12')['default']
141+
IMdiLightAlarm: typeof import('~icons/mdi-light/alarm')['default']
142+
}
143+
144+
export interface ComponentCustomProperties {
145+
vDirective: typeof import('foo')
146+
vLoading: typeof import('test/directive/Loading')['default']
147+
vSome: typeof import('test/directive/Some')['default']
148+
}
149+
}`
150+
151+
const imports = parseDeclaration(code)
152+
expect(imports).matchSnapshot()
153+
})
85154
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy