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: parse declaration error #450

Merged
merged 1 commit into from Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/declaration.ts
Expand Up @@ -12,7 +12,7 @@ const multilineCommentsRE = /\/\*.*?\*\//gms
const singlelineCommentsRE = /\/\/.*$/gm

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

export function parseDeclaration(code: string): DeclarationImports | undefined {
Expand Down
37 changes: 37 additions & 0 deletions test/__snapshots__/dts.test.ts.snap
Expand Up @@ -22,6 +22,43 @@ declare module '@vue/runtime-core' {
"
`;

exports[`dts > parseDeclaration - has icon component like <IMdi:diceD12> 1`] = `
{
"component": {
"ComponentA": "typeof import('./src/components/ComponentA.vue')['default']",
"IMdi:diceD12": "typeof import('~icons/mdi/dice-d12')['default']",
"IMdiLightAlarm": "typeof import('~icons/mdi-light/alarm')['default']",
},
"directive": {},
}
`;

exports[`dts > parseDeclaration - with directives 1`] = `
{
"component": {
"ComponentA": "typeof import('./src/components/ComponentA.vue')['default']",
"IMdi:diceD12": "typeof import('~icons/mdi/dice-d12')['default']",
"IMdiLightAlarm": "typeof import('~icons/mdi-light/alarm')['default']",
},
"directive": {
"vDirective": "typeof import('foo')",
"vLoading": "typeof import('test/directive/Loading')['default']",
"vSome": "typeof import('test/directive/Some')['default']",
},
}
`;

exports[`dts > parseDeclaration 1`] = `
{
"component": {
"ComponentA": "typeof import('./src/components/ComponentA.vue')['default']",
"ComponentB": "typeof import('./src/components/ComponentB.vue')['default']",
"ComponentC": "typeof import('./src/components/component-c.vue')['default']",
},
"directive": {},
}
`;

exports[`dts > writeDeclaration - keep unused 1`] = `
"// generated by unplugin-vue-components
// We suggest you to commit this file into source control
Expand Down
71 changes: 70 additions & 1 deletion test/dts.test.ts
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import { describe, expect, test } from 'vitest'
import type { ComponentResolver } from '../src'
import { Context } from '../src/core/context'
import { getDeclaration } from '../src/core/declaration'
import { getDeclaration, parseDeclaration } from '../src/core/declaration'

const resolver: ComponentResolver[] = [
{
Expand Down Expand Up @@ -82,4 +82,73 @@ const _directive_loading = _resolveDirective("loading")`
expect(contents).not.toContain('comment')
expect(contents).toContain('vSome')
})

test('parseDeclaration', async () => {
const code = `
// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399
import '@vue/runtime-core'

export {}

declare module '@vue/runtime-core' {
export interface GlobalComponents {
ComponentA: typeof import('./src/components/ComponentA.vue')['default']
ComponentB: typeof import('./src/components/ComponentB.vue')['default']
ComponentC: typeof import('./src/components/component-c.vue')['default']
}
}`

const imports = parseDeclaration(code)
expect(imports).matchSnapshot()
})

test('parseDeclaration - has icon component like <IMdi:diceD12>', async () => {
const code = `
// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399
import '@vue/runtime-core'

export {}

declare module '@vue/runtime-core' {
export interface GlobalComponents {
ComponentA: typeof import('./src/components/ComponentA.vue')['default']
'IMdi:diceD12': typeof import('~icons/mdi/dice-d12')['default']
IMdiLightAlarm: typeof import('~icons/mdi-light/alarm')['default']
}
}`

const imports = parseDeclaration(code)
expect(imports).matchSnapshot()
})

test('parseDeclaration - with directives', async () => {
const code = `
// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399
import '@vue/runtime-core'

export {}

declare module '@vue/runtime-core' {
export interface GlobalComponents {
ComponentA: typeof import('./src/components/ComponentA.vue')['default']
'IMdi:diceD12': typeof import('~icons/mdi/dice-d12')['default']
IMdiLightAlarm: typeof import('~icons/mdi-light/alarm')['default']
}

export interface ComponentCustomProperties {
vDirective: typeof import('foo')
vLoading: typeof import('test/directive/Loading')['default']
vSome: typeof import('test/directive/Some')['default']
}
}`

const imports = parseDeclaration(code)
expect(imports).matchSnapshot()
})
})