Skip to content

Commit c6f3142

Browse files
committed
fix(compiler-sfc): update code
1 parent 20293f6 commit c6f3142

File tree

4 files changed

+8
-10
lines changed

4 files changed

+8
-10
lines changed

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe('sfc props transform', () => {
126126
<script setup lang="ts">
127127
const { foo = 1, bar = 2, 'foo:bar': fooBar = 'foo-bar' } = defineProps<{
128128
"foo": number // double-quoted string
129-
"bar": number // single-quoted string
129+
'bar': number // single-quoted string
130130
'foo:bar': string // single-quoted string containing symbols
131131
"onUpdate:modelValue": (val: number)=>void } // double-quoted string containing symbols
132132
>()

packages/compiler-sfc/src/compileScript.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ export function compileScript(
834834
}
835835
836836
const { type, required } = props[key]
837-
const finalKey = getFinalPropsKey(key)
837+
const finalKey = getEscapedKey(key)
838838
if (!isProd) {
839839
return `${finalKey}: { type: ${toRuntimeTypeString(
840840
type
@@ -1628,7 +1628,7 @@ export function compileScript(
16281628
const defaults: string[] = []
16291629
for (const key in propsDestructuredBindings) {
16301630
const d = genDestructuredDefaultValue(key)
1631-
if (d) defaults.push(`${getFinalPropsKey(key)}: ${d}`)
1631+
if (d) defaults.push(`${getEscapedKey(key)}: ${d}`)
16321632
}
16331633
if (defaults.length) {
16341634
declCode = `${helper(
@@ -2364,6 +2364,7 @@ export function resolveObjectKey(node: Node, computed: boolean) {
23642364
* key may contain symbols such
23652365
* e.g. onUpdate:modelValue -> "onUpdate:modelValue"
23662366
*/
2367+
export const escapeSymbolsRE = /[ !"#$%&'()*+,./:;<=>?@[\\\]^`{|}~]/g
23672368
function getEscapedKey(key: string) {
2368-
return /^[a-z0-9]+$/i.test(key) ? key : `"${key}"`
2369+
return escapeSymbolsRE.test(key) ? `"${key}"` : key
23692370
}

packages/compiler-sfc/src/cssVars.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { SFCDescriptor } from './parse'
1111
import { PluginCreator } from 'postcss'
1212
import hash from 'hash-sum'
13+
import { escapeSymbolsRE } from './compileScript'
1314

1415
export const CSS_VARS_HELPER = `useCssVars`
1516

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

0 commit comments

Comments
 (0)