Skip to content

Commit

Permalink
refactor(compiler-sfc): move prop key escape logic to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 12, 2023
1 parent 690ef29 commit 574c83b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
14 changes: 2 additions & 12 deletions packages/compiler-sfc/src/script/defineProps.ts
Expand Up @@ -16,7 +16,8 @@ import {
isLiteralNode,
isCallOf,
unwrapTSNode,
toRuntimeTypeString
toRuntimeTypeString,
getEscapedKey
} from './utils'
import { genModelProps } from './defineModel'
import { getObjectOrArrayExpressionKeys } from './analyzeScriptBindings'
Expand Down Expand Up @@ -364,14 +365,3 @@ function inferValueType(node: Node): string | undefined {
return 'Function'
}
}

/**
* key may contain symbols
* e.g. onUpdate:modelValue -> "onUpdate:modelValue"
*/
export const escapeSymbolsRE = /[ !"#$%&'()*+,./:;<=>?@[\\\]^`{|}~]/g
function getEscapedKey(key: string) {
return escapeSymbolsRE.test(key)
? JSON.stringify(key)
: key
}
10 changes: 10 additions & 0 deletions packages/compiler-sfc/src/script/utils.ts
Expand Up @@ -108,3 +108,13 @@ export function normalizePath(p: string) {
}

export const joinPaths = (path.posix || path).join

/**
* key may contain symbols
* e.g. onUpdate:modelValue -> "onUpdate:modelValue"
*/
export const escapeSymbolsRE = /[ !"#$%&'()*+,./:;<=>?@[\\\]^`{|}~]/g

export function getEscapedKey(key: string) {
return escapeSymbolsRE.test(key) ? JSON.stringify(key) : key
}
7 changes: 2 additions & 5 deletions packages/compiler-sfc/src/style/cssVars.ts
Expand Up @@ -8,7 +8,7 @@ import {
BindingMetadata
} from '@vue/compiler-dom'
import { SFCDescriptor } from '../parse'
import { escapeSymbolsRE } from '../script/defineProps'
import { escapeSymbolsRE } from '../script/utils'
import { PluginCreator } from 'postcss'
import hash from 'hash-sum'

Expand All @@ -32,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(
escapeSymbolsRE,
s => `\\${s}`
)}`
return `${id}-${raw.replace(escapeSymbolsRE, s => `\\${s}`)}`
}
}

Expand Down

0 comments on commit 574c83b

Please sign in to comment.