From 135862dcac6ad9f4dfa6450e5834cf34d2eda523 Mon Sep 17 00:00:00 2001 From: daiwei Date: Fri, 4 Aug 2023 11:13:33 +0800 Subject: [PATCH 1/3] fix(compiler-sfc): handle prop key escaping properly --- packages/compiler-sfc/src/script/utils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/compiler-sfc/src/script/utils.ts b/packages/compiler-sfc/src/script/utils.ts index 42c4718e3a8..1012396d219 100644 --- a/packages/compiler-sfc/src/script/utils.ts +++ b/packages/compiler-sfc/src/script/utils.ts @@ -116,5 +116,6 @@ export const joinPaths = (path.posix || path).join export const escapeSymbolsRE = /[ !"#$%&'()*+,./:;<=>?@[\\\]^`{|}~]/g export function getEscapedKey(key: string) { + escapeSymbolsRE.lastIndex = 0 return escapeSymbolsRE.test(key) ? JSON.stringify(key) : key } From e83bbf5259c3e23cf5825dc2600ba4ecbd3fce50 Mon Sep 17 00:00:00 2001 From: daiwei Date: Fri, 4 Aug 2023 11:21:49 +0800 Subject: [PATCH 2/3] test: add test case --- .../__snapshots__/definePropsDestructure.spec.ts.snap | 1 + .../__tests__/compileScript/definePropsDestructure.spec.ts | 3 +++ 2 files changed, 4 insertions(+) diff --git a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap index c31005c40c0..35f4e066826 100644 --- a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap @@ -123,6 +123,7 @@ export default /*#__PURE__*/_defineComponent({ foo: { type: Number, required: true, default: 1 }, bar: { type: Number, required: true, default: 2 }, \\"foo:bar\\": { type: String, required: true, default: 'foo-bar' }, + \\"foo:baz\\": { type: String, required: true }, \\"onUpdate:modelValue\\": { type: Function, required: true } }, setup(__props: any) { diff --git a/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts b/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts index a41f3131bef..2cb0428e543 100644 --- a/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts @@ -152,6 +152,7 @@ describe('sfc reactive props destructure', () => { "foo": number // double-quoted string 'bar': number // single-quoted string 'foo:bar': string // single-quoted string containing symbols + 'foo:baz': string "onUpdate:modelValue": (val: number) => void // double-quoted string containing symbols }>() @@ -163,6 +164,7 @@ describe('sfc reactive props destructure', () => { foo: BindingTypes.PROPS, bar: BindingTypes.PROPS, 'foo:bar': BindingTypes.PROPS, + 'foo:baz': BindingTypes.PROPS, fooBar: BindingTypes.PROPS_ALIASED, 'onUpdate:modelValue': BindingTypes.PROPS }) @@ -171,6 +173,7 @@ describe('sfc reactive props destructure', () => { foo: { type: Number, required: true, default: 1 }, bar: { type: Number, required: true, default: 2 }, "foo:bar": { type: String, required: true, default: 'foo-bar' }, + "foo:baz": { type: String, required: true }, "onUpdate:modelValue": { type: Function, required: true } },`) assertCode(content) From bf74d9fa4c2d810272664953e621d1fc0c4d0846 Mon Sep 17 00:00:00 2001 From: daiwei Date: Fri, 4 Aug 2023 12:39:17 +0800 Subject: [PATCH 3/3] chore: improve code --- packages/compiler-sfc/src/script/utils.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/compiler-sfc/src/script/utils.ts b/packages/compiler-sfc/src/script/utils.ts index 1012396d219..9bdfa6ad24e 100644 --- a/packages/compiler-sfc/src/script/utils.ts +++ b/packages/compiler-sfc/src/script/utils.ts @@ -116,6 +116,9 @@ export const joinPaths = (path.posix || path).join export const escapeSymbolsRE = /[ !"#$%&'()*+,./:;<=>?@[\\\]^`{|}~]/g export function getEscapedKey(key: string) { - escapeSymbolsRE.lastIndex = 0 - return escapeSymbolsRE.test(key) ? JSON.stringify(key) : key + if (escapeSymbolsRE.test(key)) { + escapeSymbolsRE.lastIndex = 0 + return JSON.stringify(key) + } + return key }