Skip to content

Commit 0dcd225

Browse files
authored
fix(compiler-sfc): handle vue-ignore on leading intersection/union type (#14950)
close #12254
1 parent 68e978e commit 0dcd225

3 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ const bar = 1
4545
props: propsModel,`)
4646
})
4747

48+
// #12254
49+
test('w/ leading intersection type ignore', () => {
50+
const { content, bindings } = compile(`<script setup lang="ts">
51+
type Foo = { foo: number }
52+
type Bar = { bar: boolean }
53+
defineProps</* @vue-ignore */ Foo & Bar>()
54+
</script>`)
55+
56+
expect(content).not.toMatch(`foo: { type`)
57+
expect(content).toMatch(`bar: { type: Boolean, required: true }`)
58+
expect(bindings).toStrictEqual({
59+
bar: BindingTypes.PROPS,
60+
})
61+
})
62+
4863
// #4764
4964
test('w/ leading code', () => {
5065
const { content } = compile(`

‎packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,30 @@ describe('resolveType', () => {
153153
})
154154
})
155155

156+
test('leading intersection type with ignore', () => {
157+
expect(
158+
resolve(`
159+
type Foo = { foo: number }
160+
type Bar = { bar: boolean }
161+
defineProps</* @vue-ignore */ Foo & Bar>()
162+
`).props,
163+
).toStrictEqual({
164+
bar: ['Boolean'],
165+
})
166+
})
167+
168+
test('leading union type with ignore', () => {
169+
expect(
170+
resolve(`
171+
type Foo = { foo: number }
172+
type Bar = { bar: boolean }
173+
defineProps</* @vue-ignore */ Foo | Bar>()
174+
`).props,
175+
).toStrictEqual({
176+
bar: ['Boolean'],
177+
})
178+
})
179+
156180
test('TSPropertySignature with ignore', () => {
157181
expect(
158182
resolve(`

‎packages/compiler-sfc/src/script/resolveType.ts‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,23 @@ function innerResolveTypeElements(
191191
scope: TypeScope,
192192
typeParameters?: Record<string, Node>,
193193
): ResolvedElements {
194-
if (
195-
node.leadingComments &&
196-
node.leadingComments.some(c => c.value.includes('@vue-ignore'))
197-
) {
194+
if (hasVueIgnore(node)) {
195+
if (
196+
(node.type === 'TSIntersectionType' || node.type === 'TSUnionType') &&
197+
node.types.length > 1
198+
) {
199+
// Babel attaches comments before the first member to the parent node.
200+
// Treat them as applying to the first member only.
201+
return mergeElements(
202+
[
203+
{ props: {} },
204+
...node.types
205+
.slice(1)
206+
.map(t => resolveTypeElements(ctx, t, scope, typeParameters)),
207+
],
208+
node.type,
209+
)
210+
}
198211
return { props: {} }
199212
}
200213
switch (node.type) {
@@ -379,6 +392,13 @@ function typeElementsToMap(
379392
return res
380393
}
381394

395+
function hasVueIgnore(node: Node): boolean {
396+
return !!(
397+
node.leadingComments &&
398+
node.leadingComments.some(c => c.value.includes('@vue-ignore'))
399+
)
400+
}
401+
382402
function mergeElements(
383403
maps: ResolvedElements[],
384404
type: 'TSUnionType' | 'TSIntersectionType',

0 commit comments

Comments
 (0)