Skip to content

Commit 8de19c6

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

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ export default /*#__PURE__*/_defineComponent({
103103
props: {
104104
foo: { type: Number, required: true, default: 1 },
105105
bar: { type: Number, required: true, default: 2 },
106-
'foo:bar': { type: String, required: true },
107-
'onUpdate:modelValue': { type: Function, required: true }
106+
\\"foo:bar\\": { type: String, required: true, default: 'foo-bar' },
107+
\\"onUpdate:modelValue\\": { type: Function, required: true }
108108
},
109109
setup(__props: any) {
110110

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('sfc props transform', () => {
102102
test('default values w/ type declaration & key is string', () => {
103103
const { content, bindings } = compile(`
104104
<script setup lang="ts">
105-
const { foo = 1, bar = 2 } = defineProps<{
105+
const { foo = 1, bar = 2, 'foo:bar': fooBar = 'foo-bar' } = defineProps<{
106106
"foo": number // double-quoted string
107107
"bar": number // single-quoted string
108108
'foo:bar': string // single-quoted string containing symbols
@@ -111,17 +111,21 @@ describe('sfc props transform', () => {
111111
</script>
112112
`)
113113
expect(bindings).toStrictEqual({
114+
__propsAliases: {
115+
fooBar: 'foo:bar'
116+
},
114117
foo: BindingTypes.PROPS,
115118
bar: BindingTypes.PROPS,
116-
"'foo:bar'": BindingTypes.PROPS,
117-
"'onUpdate:modelValue'": BindingTypes.PROPS
119+
'foo:bar': BindingTypes.PROPS,
120+
fooBar: BindingTypes.PROPS_ALIASED,
121+
'onUpdate:modelValue': BindingTypes.PROPS
118122
})
119123
expect(content).toMatch(`
120124
props: {
121125
foo: { type: Number, required: true, default: 1 },
122126
bar: { type: Number, required: true, default: 2 },
123-
'foo:bar': { type: String, required: true },
124-
'onUpdate:modelValue': { type: Function, required: true }
127+
"foo:bar": { type: String, required: true, default: 'foo-bar' },
128+
"onUpdate:modelValue": { type: Function, required: true }
125129
},`)
126130
assertCode(content)
127131
})

packages/compiler-sfc/src/compileScript.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,11 @@ 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}"`
837840
if (!isProd) {
838-
return `${key}: { type: ${toRuntimeTypeString(
841+
return `${finalKey}: { type: ${toRuntimeTypeString(
839842
type
840843
)}, required: ${required}${
841844
defaultString ? `, ${defaultString}` : ``
@@ -850,12 +853,14 @@ export function compileScript(
850853
// #4783 for boolean, should keep the type
851854
// #7111 for function, if default value exists or it's not static, should keep it
852855
// in production
853-
return `${key}: { type: ${toRuntimeTypeString(type)}${
856+
return `${finalKey}: { type: ${toRuntimeTypeString(type)}${
854857
defaultString ? `, ${defaultString}` : ``
855858
} }`
856859
} else {
857860
// production: checks are useless
858-
return `${key}: ${defaultString ? `{ ${defaultString} }` : 'null'}`
861+
return `${finalKey}: ${
862+
defaultString ? `{ ${defaultString} }` : 'null'
863+
}`
859864
}
860865
})
861866
.join(',\n ')}\n }`
@@ -1893,13 +1898,7 @@ function extractRuntimeProps(
18931898
(m.key.type === 'Identifier' || m.key.type === 'StringLiteral')
18941899
) {
18951900
let type, keyName
1896-
if (m.key.type === 'StringLiteral') {
1897-
keyName = /^[a-z0-9]+$/i.test(m.key.value)
1898-
? m.key.value
1899-
: `'${m.key.value}'`
1900-
} else {
1901-
keyName = m.key.name
1902-
}
1901+
keyName = m.key.type === 'StringLiteral' ? m.key.value : m.key.name
19031902
if (m.type === 'TSMethodSignature') {
19041903
type = ['Function']
19051904
} else if (m.typeAnnotation) {

0 commit comments

Comments
 (0)