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(defineProps): defineProps starts with parenthesis #6430

Closed
wants to merge 4 commits into from
Closed
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
Expand Up @@ -632,6 +632,27 @@ return { }
}"
`;

exports[`SFC compile <script setup> defineProps starts with parenthesis 1`] = `
"export default {
props: {
foo: String
},
setup(__props, { expose }) {
expose();

const props = __props;


(() => {
console.log(props)
})();

return { props }
}

}"
`;

exports[`SFC compile <script setup> defineProps w/ external definition 1`] = `
"import { propsModel } from './props'

Expand All @@ -640,7 +661,7 @@ export default {
setup(__props, { expose }) {
expose();

const props = __props
const props = __props;



Expand All @@ -658,7 +679,7 @@ export default {
setup(__props, { expose }) {
expose();

const props = __props
const props = __props;


return { props, x }
Expand All @@ -675,7 +696,7 @@ exports[`SFC compile <script setup> defineProps() 1`] = `
setup(__props, { expose }) {
expose();

const props = __props
const props = __props;


const bar = 1
Expand All @@ -693,7 +714,7 @@ exports[`SFC compile <script setup> defineProps/defineEmits in multi-variable de
setup(__props, { expose, emit }) {
expose();

const props = __props
const props = __props;



Expand All @@ -710,7 +731,7 @@ exports[`SFC compile <script setup> defineProps/defineEmits in multi-variable de
setup(__props, { expose, emit }) {
expose();

const props = __props
const props = __props;

const a = 1;

Expand Down Expand Up @@ -1567,7 +1588,7 @@ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose, emit }) {
expose();

const props = __props
const props = __props;



Expand Down Expand Up @@ -1661,7 +1682,7 @@ const props = __props as {
foo?: string
bar?: number
baz: boolean
}
};



Expand All @@ -1684,7 +1705,7 @@ export default /*#__PURE__*/_defineComponent({
setup(__props: any, { expose }) {
expose();

const props = __props as { foo: string, bar?: number, baz: boolean, qux(): number }
const props = __props as { foo: string, bar?: number, baz: boolean, qux(): number };



Expand Down
16 changes: 16 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Expand Up @@ -96,6 +96,22 @@ const bar = 1
props: propsModel,`)
})

// #6428
test('defineProps starts with parenthesis', () => {
const { content } = compile(`
<script setup>
const props = defineProps({
foo: String
});
(() => {
console.log(props)
})();
</script>
`)

assertCode(content)
})

// #4764
test('defineProps w/ leading code', () => {
const { content } = compile(`
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -1339,7 +1339,7 @@ export function compileScript(
startOffset,
`\nconst ${propsIdentifier} = __props${
propsTypeDecl ? ` as ${genSetupPropsType(propsTypeDecl)}` : ``
}\n`
};\n`
)
}
if (propsDestructureRestId) {
Expand Down