Skip to content

Commit 686c829

Browse files
authored
fix(compiler-sfc): allow declaring variables after defineProps (#7461)
* fix(compiler-sfc): allow declaring variables after defineProps * test(compiler-sfc): test defineProps in multiple variable declaration
1 parent 1fa3d95 commit 686c829

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap

+18
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ return () => {}
136136
})"
137137
`;
138138

139+
exports[`sfc props transform multiple variable declarations 1`] = `
140+
"import { toDisplayString as _toDisplayString, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"
141+
142+
143+
export default {
144+
props: ['foo'],
145+
setup(__props) {
146+
147+
const bar = 'fish', hello = 'world'
148+
149+
return (_ctx, _cache) => {
150+
return (_openBlock(), _createElementBlock("div", null, _toDisplayString(__props.foo) + " " + _toDisplayString(hello) + " " + _toDisplayString(bar), 1 /* TEXT */))
151+
}
152+
}
153+
154+
}"
155+
`;
156+
139157
exports[`sfc props transform nested scope 1`] = `
140158
"export default {
141159
props: ['foo', 'bar'],

packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ describe('sfc props transform', () => {
2828
})
2929
})
3030

31+
test('multiple variable declarations', () => {
32+
const { content, bindings } = compile(`
33+
<script setup>
34+
const bar = 'fish', { foo } = defineProps(['foo']), hello = 'world'
35+
</script>
36+
<template><div>{{ foo }} {{ hello }} {{ bar }}</div></template>
37+
`)
38+
expect(content).not.toMatch(`const { foo } =`)
39+
expect(content).toMatch(`const bar = 'fish', hello = 'world'`)
40+
expect(content).toMatch(`_toDisplayString(hello)`)
41+
expect(content).toMatch(`_toDisplayString(bar)`)
42+
expect(content).toMatch(`_toDisplayString(__props.foo)`)
43+
assertCode(content)
44+
expect(bindings).toStrictEqual({
45+
foo: BindingTypes.PROPS,
46+
bar: BindingTypes.SETUP_CONST,
47+
hello: BindingTypes.SETUP_CONST
48+
})
49+
})
50+
3151
test('nested scope', () => {
3252
const { content, bindings } = compile(`
3353
<script setup>

packages/compiler-sfc/src/compileScript.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1758,8 +1758,7 @@ function walkDeclaration(
17581758
registerBinding(bindings, id, bindingType)
17591759
} else {
17601760
if (isCallOf(init, DEFINE_PROPS)) {
1761-
// skip walking props destructure
1762-
return
1761+
continue
17631762
}
17641763
if (id.type === 'ObjectPattern') {
17651764
walkObjectPattern(id, bindings, isConst, isDefineCall)

0 commit comments

Comments
 (0)