Skip to content

Commit

Permalink
feat(sfc): support namespaced component tags when using <script setup>
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 16, 2021
1 parent a8edf2b commit e5a4412
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,60 @@ describe('compiler: element transform', () => {
expect(node.tag).toBe(`$setup["Example"]`)
})

test('resolve component from setup bindings (inline)', () => {
const { root, node } = parseWithElementTransform(`<Example/>`, {
inline: true,
bindingMetadata: {
Example: BindingTypes.SETUP_MAYBE_REF
}
})
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
expect(node.tag).toBe(`_unref(Example)`)
})

test('resolve component from setup bindings (inline const)', () => {
const { root, node } = parseWithElementTransform(`<Example/>`, {
inline: true,
bindingMetadata: {
Example: BindingTypes.SETUP_CONST
}
})
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
expect(node.tag).toBe(`Example`)
})

test('resolve namespaced component from setup bindings', () => {
const { root, node } = parseWithElementTransform(`<Foo.Example/>`, {
bindingMetadata: {
Foo: BindingTypes.SETUP_MAYBE_REF
}
})
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
expect(node.tag).toBe(`$setup["Foo"].Example`)
})

test('resolve namespaced component from setup bindings (inline)', () => {
const { root, node } = parseWithElementTransform(`<Foo.Example/>`, {
inline: true,
bindingMetadata: {
Foo: BindingTypes.SETUP_MAYBE_REF
}
})
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
expect(node.tag).toBe(`_unref(Foo).Example`)
})

test('resolve namespaced component from setup bindings (inline const)', () => {
const { root, node } = parseWithElementTransform(`<Foo.Example/>`, {
inline: true,
bindingMetadata: {
Foo: BindingTypes.SETUP_CONST
}
})
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
expect(node.tag).toBe(`Foo.Example`)
})

test('do not resolve component from non-script-setup bindings', () => {
const bindingMetadata = {
Example: BindingTypes.SETUP_MAYBE_REF
Expand Down
7 changes: 7 additions & 0 deletions packages/compiler-core/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ export function resolveComponentType(
if (fromSetup) {
return fromSetup
}
const dotIndex = tag.indexOf('.')
if (dotIndex > 0) {
const ns = resolveSetupReference(tag.slice(0, dotIndex), context)
if (ns) {
return ns + tag.slice(dotIndex)
}
}
}

// 4. Self referencing component (inferred from filename)
Expand Down

0 comments on commit e5a4412

Please sign in to comment.