Skip to content

Commit e6a4ac6

Browse files
committed
fix(compiler-sfc): updated code
1 parent 8de19c6 commit e6a4ac6

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ return (_ctx, _cache) => {
7878
}"
7979
`;
8080

81+
exports[`sfc props transform > default values w/ runtime declaration & key is string 1`] = `
82+
"import { mergeDefaults as _mergeDefaults } from 'vue'
83+
84+
export default {
85+
props: _mergeDefaults(['foo', 'foo:bar'], {
86+
foo: 1,
87+
\\"foo:bar\\": 'foo-bar'
88+
}),
89+
setup(__props) {
90+
91+
92+
93+
return () => {}
94+
}
95+
96+
}"
97+
`;
98+
8199
exports[`sfc props transform > default values w/ runtime declaration 1`] = `
82100
"import { mergeDefaults as _mergeDefaults } from 'vue'
83101

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,28 @@ describe('sfc props transform', () => {
8383
})`)
8484
assertCode(content)
8585
})
86+
test('default values w/ runtime declaration & key is string', () => {
87+
const { content, bindings } = compile(`
88+
<script setup>
89+
const { foo = 1, 'foo:bar':fooBar = 'foo-bar' } = defineProps(['foo', 'foo:bar'])
90+
</script>
91+
`)
92+
expect(bindings).toStrictEqual({
93+
__propsAliases: {
94+
fooBar: 'foo:bar'
95+
},
96+
foo: BindingTypes.PROPS,
97+
'foo:bar': BindingTypes.PROPS,
98+
fooBar: BindingTypes.PROPS_ALIASED
99+
})
100+
101+
expect(content).toMatch(`
102+
props: _mergeDefaults(['foo', 'foo:bar'], {
103+
foo: 1,
104+
"foo:bar": 'foo-bar'
105+
}),`)
106+
assertCode(content)
107+
})
86108

87109
test('default values w/ type declaration', () => {
88110
const { content } = compile(`

packages/compiler-sfc/src/compileScript.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -834,9 +834,7 @@ export function compileScript(
834834
}
835835
836836
const { type, required } = props[key]
837-
// key may contain symbols such
838-
// e.g. onUpdate:modelValue -> "onUpdate:modelValue"
839-
const finalKey = /^[a-z0-9]+$/i.test(key) ? key : `"${key}"`
837+
const finalKey = getFinalPropsKey(key)
840838
if (!isProd) {
841839
return `${finalKey}: { type: ${toRuntimeTypeString(
842840
type
@@ -1630,7 +1628,7 @@ export function compileScript(
16301628
const defaults: string[] = []
16311629
for (const key in propsDestructuredBindings) {
16321630
const d = genDestructuredDefaultValue(key)
1633-
if (d) defaults.push(`${key}: ${d}`)
1631+
if (d) defaults.push(`${getFinalPropsKey(key)}: ${d}`)
16341632
}
16351633
if (defaults.length) {
16361634
declCode = `${helper(
@@ -2361,3 +2359,11 @@ export function resolveObjectKey(node: Node, computed: boolean) {
23612359
}
23622360
return undefined
23632361
}
2362+
2363+
/**
2364+
* key may contain symbols such
2365+
* e.g. onUpdate:modelValue -> "onUpdate:modelValue"
2366+
*/
2367+
function getFinalPropsKey(key: string) {
2368+
return /^[a-z0-9]+$/i.test(key) ? key : `"${key}"`
2369+
}

0 commit comments

Comments
 (0)