Skip to content

Commit 25ff0df

Browse files
committed
fix: error with v-show
1 parent af855de commit 25ff0df

File tree

3 files changed

+73
-32
lines changed

3 files changed

+73
-32
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/transformSlotOutlet.spec.ts.snap

+32-19
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ exports[`compiler: transform <slot> outlets > default slot outlet with props 1`]
2626
"import { createSlot as _createSlot } from 'vue/vapor';
2727
2828
export function render(_ctx) {
29-
const n0 = _createSlot("default", [{
30-
foo: () => ("bar"),
31-
baz: () => (_ctx.qux),
32-
fooBar: () => (_ctx.foo-_ctx.bar)
33-
}])
29+
const n0 = _createSlot("default", [
30+
{
31+
foo: () => ("bar"),
32+
baz: () => (_ctx.qux),
33+
fooBar: () => (_ctx.foo-_ctx.bar)
34+
}
35+
])
3436
return n0
3537
}"
3638
`;
@@ -62,6 +64,15 @@ export function render(_ctx) {
6264
}"
6365
`;
6466
67+
exports[`compiler: transform <slot> outlets > error on unexpected custom directive with v-show on <slot> 1`] = `
68+
"import { createSlot as _createSlot } from 'vue/vapor';
69+
70+
export function render(_ctx) {
71+
const n0 = _createSlot("default", null)
72+
return n0
73+
}"
74+
`;
75+
6576
exports[`compiler: transform <slot> outlets > named slot outlet with fallback 1`] = `
6677
"import { createSlot as _createSlot, template as _template } from 'vue/vapor';
6778
const t0 = _template("<div></div>")
@@ -88,10 +99,12 @@ exports[`compiler: transform <slot> outlets > statically named slot outlet with
8899
"import { createSlot as _createSlot } from 'vue/vapor';
89100
90101
export function render(_ctx) {
91-
const n0 = _createSlot("foo", [{
92-
foo: () => ("bar"),
93-
baz: () => (_ctx.qux)
94-
}])
102+
const n0 = _createSlot("foo", [
103+
{
104+
foo: () => ("bar"),
105+
baz: () => (_ctx.qux)
106+
}
107+
])
95108
return n0
96109
}"
97110
`;
@@ -100,11 +113,11 @@ exports[`compiler: transform <slot> outlets > statically named slot outlet with
100113
"import { createSlot as _createSlot } from 'vue/vapor';
101114
102115
export function render(_ctx) {
103-
const n0 = _createSlot("foo", [{
104-
foo: () => ("bar")
105-
}, () => (_ctx.obj), {
106-
baz: () => (_ctx.qux)
107-
}])
116+
const n0 = _createSlot("foo", [
117+
{ foo: () => ("bar") },
118+
() => (_ctx.obj),
119+
{ baz: () => (_ctx.qux) }
120+
])
108121
return n0
109122
}"
110123
`;
@@ -114,11 +127,11 @@ exports[`compiler: transform <slot> outlets > statically named slot outlet with
114127
import { createSlot as _createSlot } from 'vue/vapor';
115128
116129
export function render(_ctx) {
117-
const n0 = _createSlot("default", [{
118-
onClick: () => _ctx.foo
119-
}, () => (_toHandlers(_ctx.bar)), {
120-
baz: () => (_ctx.qux)
121-
}])
130+
const n0 = _createSlot("default", [
131+
{ onClick: () => _ctx.foo },
132+
() => (_toHandlers(_ctx.bar)),
133+
{ baz: () => (_ctx.qux) }
134+
])
122135
return n0
123136
}"
124137
`;

packages/compiler-vapor/__tests__/transforms/transformSlotOutlet.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
transformText,
88
transformVBind,
99
transformVOn,
10+
transformVShow,
1011
} from '../../src'
1112
import { makeCompile } from './_utils'
1213

@@ -20,6 +21,7 @@ const compileWithSlotsOutlet = makeCompile({
2021
directiveTransforms: {
2122
bind: transformVBind,
2223
on: transformVOn,
24+
show: transformVShow,
2325
},
2426
})
2527

@@ -230,4 +232,27 @@ describe('compiler: transform <slot> outlets', () => {
230232
},
231233
})
232234
})
235+
236+
test('error on unexpected custom directive with v-show on <slot>', () => {
237+
const onError = vi.fn()
238+
const source = `<slot v-show="ok" />`
239+
const index = source.indexOf('v-show="ok"')
240+
const { code } = compileWithSlotsOutlet(source, { onError })
241+
expect(code).toMatchSnapshot()
242+
expect(onError.mock.calls[0][0]).toMatchObject({
243+
code: ErrorCodes.X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET,
244+
loc: {
245+
start: {
246+
offset: index,
247+
line: 1,
248+
column: index + 1,
249+
},
250+
end: {
251+
offset: index + 11,
252+
line: 1,
253+
column: index + 12,
254+
},
255+
},
256+
})
257+
})
233258
})

packages/compiler-vapor/src/transforms/transformSlotOutlet.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import {
1818
IRNodeTypes,
1919
type IRProps,
2020
type VaporDirectiveNode,
21+
type WithDirectiveIRNode,
2122
} from '../ir'
22-
import { camelize, extend, isBuiltInDirective } from '@vue/shared'
23+
import { camelize, extend } from '@vue/shared'
2324
import { newBlock } from './utils'
2425
import { buildProps } from './transformElement'
2526

@@ -37,7 +38,6 @@ export const transformSlotOutlet: NodeTransform = (node, context) => {
3738

3839
let name: SimpleExpressionNode | undefined
3940
const nonNameProps: (AttributeNode | DirectiveNode)[] = []
40-
const directives: DirectiveNode[] = []
4141
for (const prop of props) {
4242
if (prop.type === NodeTypes.ATTRIBUTE) {
4343
if (prop.value) {
@@ -59,8 +59,6 @@ export const transformSlotOutlet: NodeTransform = (node, context) => {
5959
)
6060
name.ast = null
6161
}
62-
} else if (!isBuiltInDirective(prop.name)) {
63-
directives.push(prop)
6462
} else {
6563
const nonProp = extend({}, prop)
6664
if (nonProp.name === 'bind' && nonProp.arg && isStaticExp(nonProp.arg)) {
@@ -72,15 +70,6 @@ export const transformSlotOutlet: NodeTransform = (node, context) => {
7270
}
7371
}
7472

75-
if (directives.length) {
76-
context.options.onError(
77-
createCompilerError(
78-
ErrorCodes.X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET,
79-
directives[0].loc,
80-
),
81-
)
82-
}
83-
8473
name ||= createSimpleExpression('default', true)
8574
let irProps: IRProps[] = []
8675
if (nonNameProps.length > 0) {
@@ -90,6 +79,20 @@ export const transformSlotOutlet: NodeTransform = (node, context) => {
9079
true,
9180
)
9281
irProps = isDynamic ? props : [props]
82+
83+
const { operation } = context.block
84+
const directives = operation.filter(
85+
oper => oper.type === IRNodeTypes.WITH_DIRECTIVE,
86+
) as WithDirectiveIRNode[]
87+
88+
if (directives.length) {
89+
context.options.onError(
90+
createCompilerError(
91+
ErrorCodes.X_V_SLOT_UNEXPECTED_DIRECTIVE_ON_SLOT_OUTLET,
92+
directives[0].dir.loc,
93+
),
94+
)
95+
}
9396
}
9497

9598
return () => {

0 commit comments

Comments
 (0)