Skip to content

Commit

Permalink
fix(compiler-sfc): properly handle shorthand property in template exp…
Browse files Browse the repository at this point in the history
…ressions

fix vuejs#12566
  • Loading branch information
yyx990803 committed Jun 21, 2022
1 parent de03f69 commit 9b9f2bf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
34 changes: 16 additions & 18 deletions packages/compiler-sfc/src/prefixIdentifiers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import MagicString from 'magic-string'
import { parseExpression, ParserOptions, ParserPlugin } from '@babel/parser'
import { makeMap } from 'shared/util'
import { walkIdentifiers } from './babelUtils'
import { isStaticProperty, walkIdentifiers } from './babelUtils'
import { BindingMetadata } from './types'

const doNotPrefix = makeMap(
Expand Down Expand Up @@ -39,18 +39,28 @@ export function prefixIdentifiers(

walkIdentifiers(
ast,
ident => {
(ident, parent) => {
const { name } = ident
if (doNotPrefix(name)) {
return
}

if (!isScriptSetup) {
s.prependRight(ident.start!, '_vm.')
return
let prefix = `_vm.`
if (isScriptSetup) {
const type = bindings[name]
if (type && type.startsWith('setup')) {
prefix = `_setup.`
}
}

s.overwrite(ident.start!, ident.end!, rewriteIdentifier(name, bindings))
if (isStaticProperty(parent) && parent.shorthand) {
// property shorthand like { foo }, we need to add the key since
// we rewrite the value
// { foo } -> { foo: _vm.foo }
s.appendLeft(ident.end!, `: ${prefix}${name}`)
} else {
s.prependRight(ident.start!, prefix)
}
},
node => {
if (node.type === 'WithStatement') {
Expand All @@ -70,15 +80,3 @@ export function prefixIdentifiers(

return s.toString()
}

export function rewriteIdentifier(
name: string,
bindings: BindingMetadata
): string {
const type = bindings[name]
if (type && type.startsWith('setup')) {
return `_setup.${name}`
} else {
return `_vm.${name}`
}
}
5 changes: 3 additions & 2 deletions packages/compiler-sfc/test/prefixIdentifiers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const toFn = (source: string) => `function render(){${source}\n}`

it('should work', () => {
const { render } = compile(`<div id="app">
<div>{{ foo }}</div>
<div :style="{ color }">{{ foo }}</div>
<p v-for="i in list">{{ i }}</p>
<foo inline-template>
<div>{{ bar }}</div>
Expand All @@ -22,6 +22,7 @@ it('should work', () => {
expect(result).not.toMatch(`_vm._c`)
expect(result).toMatch(`_vm.foo`)
expect(result).toMatch(`_vm.list`)
expect(result).toMatch(`{ color: _vm.color }`)
expect(result).not.toMatch(`_vm.i`)
expect(result).not.toMatch(`with (this)`)

Expand All @@ -33,7 +34,7 @@ it('should work', () => {
\\"div\\",
{ attrs: { id: \\"app\\" } },
[
_c(\\"div\\", [_vm._v(_vm._s(_vm.foo))]),
_c(\\"div\\", { style: { color: _vm.color } }, [_vm._v(_vm._s(_vm.foo))]),
_vm._v(\\" \\"),
_vm._l(_vm.list, function (i) {
return _c(\\"p\\", [_vm._v(_vm._s(i))])
Expand Down

0 comments on commit 9b9f2bf

Please sign in to comment.