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

feat(compiler-sfc): support props name to be defined as a string #7803

Merged
merged 55 commits into from May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
508ef22
feat(compiler-sfc): Support props name to be defined as a string
baiwusanyu-c Feb 28, 2023
923afa9
feat(compiler-sfc): updated unit test
baiwusanyu-c Feb 28, 2023
75ec1a9
feat(compiler-sfc): updated unit test
baiwusanyu-c Feb 28, 2023
31b3866
feat(compiler-sfc): updated unit test
baiwusanyu-c Feb 28, 2023
fb9d9e1
fix(compiler-sfc): updated code
baiwusanyu-c Mar 1, 2023
ccb6ce6
fix(compiler-sfc): updated snap
baiwusanyu-c Mar 1, 2023
c2be9b0
fix(compiler-sfc): updated unit test
baiwusanyu-c Mar 1, 2023
9ebb189
fix(compiler-sfc): updated code
baiwusanyu-c Mar 1, 2023
1b49b5a
fix(compiler-sfc): updated code
baiwusanyu-c Mar 1, 2023
ec2468f
fix(compiler-sfc): updated code
baiwusanyu-c Mar 1, 2023
8de19c6
fix(compiler-sfc): updated code
baiwusanyu-c Mar 1, 2023
e6a4ac6
fix(compiler-sfc): updated code
baiwusanyu-c Mar 1, 2023
4d161b1
Update compileScript.spec.ts
baiwusanyu-c Mar 2, 2023
45b6e69
Update packages/compiler-sfc/src/compileScript.ts
baiwusanyu-c Mar 6, 2023
20293f6
Update packages/compiler-sfc/src/compileScript.ts
baiwusanyu-c Mar 6, 2023
c6f3142
fix(compiler-sfc): update code
baiwusanyu-c Mar 6, 2023
1bf1ebd
Update compileScript.spec.ts
baiwusanyu-c Mar 6, 2023
37a1e37
Update compileScript.spec.ts
baiwusanyu-c Mar 6, 2023
30f8611
chore: format
sxzz Mar 6, 2023
0325357
fix: escape string
sxzz Mar 6, 2023
dc30be7
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c Mar 17, 2023
bfe8a38
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c Mar 20, 2023
f876a98
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c Mar 23, 2023
1be11c5
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c Mar 24, 2023
a119029
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c Mar 27, 2023
fee10fc
Merge remote-tracking branch 'upstream/main' into bwsy/feat/propsKeyS…
baiwusanyu-c Mar 28, 2023
147c1f7
Merge remote-tracking branch 'upstream/main' into bwsy/feat/propsKeyS…
baiwusanyu-c Mar 29, 2023
86c498e
Merge remote-tracking branch 'upstream/main' into bwsy/feat/propsKeyS…
baiwusanyu-c Mar 30, 2023
803e1bd
Merge remote-tracking branch 'upstream/main' into bwsy/feat/propsKeyS…
baiwusanyu-c Mar 30, 2023
44a3e5e
Merge remote-tracking branch 'origin/main' into bwsy/feat/propsKeyString
baiwusanyu-c Apr 5, 2023
1d7372d
update code
baiwusanyu-c Apr 5, 2023
9ee8357
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c Apr 6, 2023
0c8dc97
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c Apr 6, 2023
f805d20
Merge remote-tracking branch 'upstream/main' into bwsy/feat/propsKeyS…
baiwusanyu-c Apr 10, 2023
555ccf8
update code
baiwusanyu-c Apr 10, 2023
06a96ad
Merge remote-tracking branch 'upstream/main' into bwsy/feat/propsKeyS…
baiwusanyu-c Apr 11, 2023
e94c908
Merge remote-tracking branch 'upstream/main' into bwsy/feat/propsKeyS…
baiwusanyu-c Apr 11, 2023
9c56547
update code
baiwusanyu-c Apr 11, 2023
97bccd2
Merge remote-tracking branch 'upstream/main' into bwsy/feat/propsKeyS…
baiwusanyu-c Apr 14, 2023
0ef564f
update snap and update code
baiwusanyu-c Apr 14, 2023
7404e27
Merge remote-tracking branch 'upstream/main' into bwsy/feat/propsKeyS…
baiwusanyu-c Apr 17, 2023
31afc2f
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c Apr 19, 2023
863f908
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c Apr 20, 2023
0d99c36
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c Apr 20, 2023
cd4e263
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c Apr 21, 2023
d142721
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c Apr 25, 2023
8bf16b2
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c May 4, 2023
b2cb327
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c May 6, 2023
1420088
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c May 6, 2023
4024023
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c May 8, 2023
674c6fd
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c May 8, 2023
aa409bc
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c May 9, 2023
0699b51
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c May 12, 2023
00c2fdd
Merge branch 'main' into bwsy/feat/propsKeyString
baiwusanyu-c May 12, 2023
ec0924a
Update cssVars.ts
baiwusanyu-c May 12, 2023
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 @@ -78,6 +78,24 @@ return (_ctx, _cache) => {
}"
`;

exports[`sfc props transform > default values w/ runtime declaration & key is string 1`] = `
"import { mergeDefaults as _mergeDefaults } from 'vue'

export default {
props: _mergeDefaults(['foo', 'foo:bar'], {
foo: 1,
\\"foo:bar\\": 'foo-bar'
}),
setup(__props) {



return () => {}
}

}"
`;

exports[`sfc props transform > default values w/ runtime declaration 1`] = `
"import { mergeDefaults as _mergeDefaults } from 'vue'

Expand All @@ -96,6 +114,26 @@ return () => {}
}"
`;

exports[`sfc props transform > default values w/ type declaration & key is string 1`] = `
"import { defineComponent as _defineComponent } from 'vue'

export default /*#__PURE__*/_defineComponent({
props: {
foo: { type: Number, required: true, default: 1 },
bar: { type: Number, required: true, default: 2 },
\\"foo:bar\\": { type: String, required: true, default: 'foo-bar' },
\\"onUpdate:modelValue\\": { type: Function, required: true }
},
setup(__props: any) {



return () => {}
}

})"
`;

exports[`sfc props transform > default values w/ type declaration 1`] = `
"import { defineComponent as _defineComponent } from 'vue'

Expand Down
Expand Up @@ -83,6 +83,28 @@ describe('sfc props transform', () => {
})`)
assertCode(content)
})
test('default values w/ runtime declaration & key is string', () => {
const { content, bindings } = compile(`
<script setup>
const { foo = 1, 'foo:bar': fooBar = 'foo-bar' } = defineProps(['foo', 'foo:bar'])
</script>
`)
expect(bindings).toStrictEqual({
__propsAliases: {
fooBar: 'foo:bar'
},
foo: BindingTypes.PROPS,
'foo:bar': BindingTypes.PROPS,
fooBar: BindingTypes.PROPS_ALIASED
})

expect(content).toMatch(`
props: _mergeDefaults(['foo', 'foo:bar'], {
foo: 1,
"foo:bar": 'foo-bar'
}),`)
assertCode(content)
})

test('default values w/ type declaration', () => {
const { content } = compile(`
Expand All @@ -99,6 +121,37 @@ describe('sfc props transform', () => {
assertCode(content)
})

test('default values w/ type declaration & key is string', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
const { foo = 1, bar = 2, 'foo:bar': fooBar = 'foo-bar' } = defineProps<{
"foo": number // double-quoted string
'bar': number // single-quoted string
'foo:bar': string // single-quoted string containing symbols
"onUpdate:modelValue": (val: number) => void // double-quoted string containing symbols
}>()
</script>
`)
expect(bindings).toStrictEqual({
__propsAliases: {
fooBar: 'foo:bar'
},
foo: BindingTypes.PROPS,
bar: BindingTypes.PROPS,
'foo:bar': BindingTypes.PROPS,
fooBar: BindingTypes.PROPS_ALIASED,
'onUpdate:modelValue': BindingTypes.PROPS
})
expect(content).toMatch(`
props: {
foo: { type: Number, required: true, default: 1 },
bar: { type: Number, required: true, default: 2 },
"foo:bar": { type: String, required: true, default: 'foo-bar' },
"onUpdate:modelValue": { type: Function, required: true }
},`)
assertCode(content)
baiwusanyu-c marked this conversation as resolved.
Show resolved Hide resolved
})

test('default values w/ type declaration, prod mode', () => {
const { content } = compile(
`
Expand Down
27 changes: 20 additions & 7 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -834,8 +834,9 @@ export function compileScript(
}

const { type, required } = props[key]
const finalKey = getEscapedKey(key)
if (!isProd) {
return `${key}: { type: ${toRuntimeTypeString(
return `${finalKey}: { type: ${toRuntimeTypeString(
type
)}, required: ${required}${
defaultString ? `, ${defaultString}` : ``
Expand All @@ -850,12 +851,14 @@ export function compileScript(
// #4783 for boolean, should keep the type
// #7111 for function, if default value exists or it's not static, should keep it
// in production
return `${key}: { type: ${toRuntimeTypeString(type)}${
return `${finalKey}: { type: ${toRuntimeTypeString(type)}${
defaultString ? `, ${defaultString}` : ``
} }`
} else {
// production: checks are useless
return `${key}: ${defaultString ? `{ ${defaultString} }` : 'null'}`
return `${finalKey}: ${
defaultString ? `{ ${defaultString} }` : 'null'
}`
}
})
.join(',\n ')}\n }`
Expand Down Expand Up @@ -1625,7 +1628,7 @@ export function compileScript(
const defaults: string[] = []
for (const key in propsDestructuredBindings) {
const d = genDestructuredDefaultValue(key)
if (d) defaults.push(`${key}: ${d}`)
if (d) defaults.push(`${getEscapedKey(key)}: ${d}`)
}
if (defaults.length) {
declCode = `${helper(
Expand Down Expand Up @@ -1890,16 +1893,17 @@ function extractRuntimeProps(
for (const m of members) {
if (
(m.type === 'TSPropertySignature' || m.type === 'TSMethodSignature') &&
m.key.type === 'Identifier'
(m.key.type === 'Identifier' || m.key.type === 'StringLiteral')
) {
let type
const keyName = m.key.type === 'StringLiteral' ? m.key.value : m.key.name
if (m.type === 'TSMethodSignature') {
type = ['Function']
} else if (m.typeAnnotation) {
type = inferRuntimeType(m.typeAnnotation.typeAnnotation, declaredTypes)
}
props[m.key.name] = {
key: m.key.name,
props[keyName] = {
key: keyName,
required: !m.optional,
type: type || [`null`]
}
baiwusanyu-c marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -2355,3 +2359,12 @@ export function resolveObjectKey(node: Node, computed: boolean) {
}
return undefined
}

/**
* key may contain symbols such
* e.g. onUpdate:modelValue -> "onUpdate:modelValue"
*/
export const escapeSymbolsRE = /[ !"#$%&'()*+,./:;<=>?@[\\\]^`{|}~]/g
function getEscapedKey(key: string) {
return escapeSymbolsRE.test(key) ? JSON.stringify(key) : key
}
6 changes: 2 additions & 4 deletions packages/compiler-sfc/src/cssVars.ts
Expand Up @@ -10,6 +10,7 @@ import {
import { SFCDescriptor } from './parse'
import { PluginCreator } from 'postcss'
import hash from 'hash-sum'
import { escapeSymbolsRE } from './compileScript'

export const CSS_VARS_HELPER = `useCssVars`

Expand All @@ -31,10 +32,7 @@ function genVarName(id: string, raw: string, isProd: boolean): string {
return hash(id + raw)
} else {
// escape ASCII Punctuation & Symbols
return `${id}-${raw.replace(
/[ !"#$%&'()*+,./:;<=>?@[\\\]^`{|}~]/g,
s => `\\${s}`
)}`
return `${id}-${raw.replace(escapeSymbolsRE, s => `\\${s}`)}`
}
}

Expand Down