Skip to content

Commit 9ebb189

Browse files
committed
fix(compiler-sfc): updated code
1 parent c2be9b0 commit 9ebb189

File tree

4 files changed

+29
-219
lines changed

4 files changed

+29
-219
lines changed
Lines changed: 4 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,14 @@
11
// Vitest Snapshot v1
22

3-
exports[`sfc props transform > $$() escape 1`] = `
4-
"import { toRef as _toRef } from 'vue'
5-
6-
export default {
7-
props: ['foo'],
8-
setup(__props) {
9-
const __props_bar = _toRef(__props, 'bar');
10-
const __props_foo = _toRef(__props, 'foo');
11-
12-
13-
console.log((__props_foo))
14-
console.log((__props_bar))
15-
;({ foo: __props_foo, baz: __props_bar })
16-
17-
return () => {}
18-
}
19-
20-
}"
21-
`;
22-
23-
exports[`sfc props transform > aliasing 1`] = `
24-
"import { toDisplayString as _toDisplayString } from \\"vue\\"
25-
26-
27-
export default {
28-
props: ['foo'],
29-
setup(__props) {
30-
31-
32-
let x = foo
33-
let y = __props.foo
34-
35-
return (_ctx, _cache) => {
36-
return _toDisplayString(__props.foo + __props.foo)
37-
}
38-
}
39-
40-
}"
41-
`;
42-
43-
exports[`sfc props transform > basic usage 1`] = `
44-
"import { toDisplayString as _toDisplayString } from \\"vue\\"
45-
46-
47-
export default {
48-
props: ['foo'],
49-
setup(__props) {
50-
51-
52-
console.log(__props.foo)
53-
54-
return (_ctx, _cache) => {
55-
return _toDisplayString(__props.foo)
56-
}
57-
}
58-
59-
}"
60-
`;
61-
62-
exports[`sfc props transform > computed static key 1`] = `
63-
"import { toDisplayString as _toDisplayString } from \\"vue\\"
64-
65-
66-
export default {
67-
props: ['foo'],
68-
setup(__props) {
69-
70-
71-
console.log(__props.foo)
72-
73-
return (_ctx, _cache) => {
74-
return _toDisplayString(__props.foo)
75-
}
76-
}
77-
78-
}"
79-
`;
80-
81-
exports[`sfc props transform > default values w/ runtime declaration 1`] = `
82-
"import { mergeDefaults as _mergeDefaults } from 'vue'
83-
84-
export default {
85-
props: _mergeDefaults(['foo', 'bar'], {
86-
foo: 1,
87-
bar: () => ({})
88-
}),
89-
setup(__props) {
90-
91-
92-
93-
return () => {}
94-
}
95-
96-
}"
97-
`;
98-
993
exports[`sfc props transform > default values w/ type declaration & key is string 1`] = `
1004
"import { defineComponent as _defineComponent } from 'vue'
1015
1026
export default /*#__PURE__*/_defineComponent({
1037
props: {
104-
foo: { type: Number, required: true, default: 1 }
8+
foo: { type: Number, required: true, default: 1 },
9+
bar: { type: Number, required: true, default: 2 },
10+
'foo:bar': { type: String, required: true },
11+
'onUpdate:modelValue': { type: Function, required: true }
10512
},
10613
setup(__props: any) {
10714
@@ -112,114 +19,3 @@ return () => {}
11219
11320
})"
11421
`;
115-
116-
exports[`sfc props transform > default values w/ type declaration 1`] = `
117-
"import { defineComponent as _defineComponent } from 'vue'
118-
119-
export default /*#__PURE__*/_defineComponent({
120-
props: {
121-
foo: { type: Number, required: false, default: 1 },
122-
bar: { type: Object, required: false, default: () => ({}) }
123-
},
124-
setup(__props: any) {
125-
126-
127-
128-
return () => {}
129-
}
130-
131-
})"
132-
`;
133-
134-
exports[`sfc props transform > default values w/ type declaration, prod mode 1`] = `
135-
"import { defineComponent as _defineComponent } from 'vue'
136-
137-
export default /*#__PURE__*/_defineComponent({
138-
props: {
139-
foo: { default: 1 },
140-
bar: { default: () => ({}) },
141-
baz: null,
142-
boola: { type: Boolean },
143-
boolb: { type: [Boolean, Number] },
144-
func: { type: Function, default: () => (() => {}) }
145-
},
146-
setup(__props: any) {
147-
148-
149-
150-
return () => {}
151-
}
152-
153-
})"
154-
`;
155-
156-
exports[`sfc props transform > multiple variable declarations 1`] = `
157-
"import { toDisplayString as _toDisplayString, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
158-
159-
160-
export default {
161-
props: ['foo'],
162-
setup(__props) {
163-
164-
const bar = 'fish', hello = 'world'
165-
166-
return (_ctx, _cache) => {
167-
return (_openBlock(), _createElementBlock(\\"div\\", null, _toDisplayString(__props.foo) + \\" \\" + _toDisplayString(hello) + \\" \\" + _toDisplayString(bar), 1 /* TEXT */))
168-
}
169-
}
170-
171-
}"
172-
`;
173-
174-
exports[`sfc props transform > nested scope 1`] = `
175-
"export default {
176-
props: ['foo', 'bar'],
177-
setup(__props) {
178-
179-
180-
function test(foo) {
181-
console.log(foo)
182-
console.log(__props.bar)
183-
}
184-
185-
return () => {}
186-
}
187-
188-
}"
189-
`;
190-
191-
exports[`sfc props transform > non-identifier prop names 1`] = `
192-
"import { toDisplayString as _toDisplayString } from \\"vue\\"
193-
194-
195-
export default {
196-
props: { 'foo.bar': Function },
197-
setup(__props) {
198-
199-
200-
let x = __props[\\"foo.bar\\"]
201-
202-
return (_ctx, _cache) => {
203-
return _toDisplayString(__props[\\"foo.bar\\"])
204-
}
205-
}
206-
207-
}"
208-
`;
209-
210-
exports[`sfc props transform > rest spread 1`] = `
211-
"import { createPropsRestProxy as _createPropsRestProxy } from 'vue'
212-
213-
export default {
214-
props: ['foo', 'bar', 'baz'],
215-
setup(__props) {
216-
217-
const rest = _createPropsRestProxy(__props, [\\"foo\\",\\"bar\\"]);
218-
219-
220-
221-
return () => {}
222-
}
223-
224-
}"
225-
`;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ const emit = defineEmits(['a', 'b'])
11361136
`)
11371137
assertCode(content)
11381138
})
1139-
1139+
11401140
// #7111
11411141
test('withDefaults (static) w/ production mode', () => {
11421142
const { content } = compile(
@@ -1277,7 +1277,6 @@ const emit = defineEmits(['a', 'b'])
12771277
expect(content).toMatch(`emits: ["foo", "bar"]`)
12781278
})
12791279

1280-
12811280
test('defineEmits w/ type from normal script', () => {
12821281
const { content } = compile(`
12831282
<script lang="ts">

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,28 @@ describe('sfc props transform', () => {
100100
})
101101

102102
test('default values w/ type declaration & key is string', () => {
103-
const { content , bindings} = compile(`
103+
const { content, bindings } = compile(`
104104
<script setup lang="ts">
105-
const { foo = 1 } = defineProps<{ "foo": number }>()
105+
const { foo = 1, bar = 2 } = defineProps<{
106+
"foo": number // double-quoted string
107+
"bar": number // single-quoted string
108+
'foo:bar': string // single-quoted string containing symbols
109+
"onUpdate:modelValue": (val: number)=>void } // double-quoted string containing symbols
110+
>()
106111
</script>
107112
`)
108-
expect(bindings).toMatchObject({foo:'props'})
113+
expect(bindings).toStrictEqual({
114+
foo: BindingTypes.PROPS,
115+
bar: BindingTypes.PROPS,
116+
"'foo:bar'": BindingTypes.PROPS,
117+
"'onUpdate:modelValue'": BindingTypes.PROPS
118+
})
109119
expect(content).toMatch(`
110120
props: {
111-
foo: { type: Number, required: true, default: 1 }
121+
foo: { type: Number, required: true, default: 1 },
122+
bar: { type: Number, required: true, default: 2 },
123+
'foo:bar': { type: String, required: true },
124+
'onUpdate:modelValue': { type: Function, required: true }
112125
},`)
113126
assertCode(content)
114127
})

packages/compiler-sfc/src/compileScript.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,12 +1889,14 @@ function extractRuntimeProps(
18891889
const members = node.type === 'TSTypeLiteral' ? node.members : node.body
18901890
for (const m of members) {
18911891
if (
1892-
(m.type === 'TSPropertySignature' || m.type === 'TSMethodSignature') &&
1893-
(m.key.type === 'Identifier' || m.key.type === 'StringLiteral')
1892+
(m.type === 'TSPropertySignature' || m.type === 'TSMethodSignature') &&
1893+
(m.key.type === 'Identifier' || m.key.type === 'StringLiteral')
18941894
) {
1895-
let type, keyName;
1896-
if(m.key.type === 'StringLiteral'){
1897-
keyName = m.key.value
1895+
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}'`
18981900
} else {
18991901
keyName = m.key.name
19001902
}

0 commit comments

Comments
 (0)