11import MagicString from 'magic-string'
22import { parseExpression , ParserOptions , ParserPlugin } from '@babel/parser'
33import { makeMap } from 'shared/util'
4- import { walkIdentifiers } from './babelUtils'
4+ import { isStaticProperty , walkIdentifiers } from './babelUtils'
55import { BindingMetadata } from './types'
66
77const doNotPrefix = makeMap (
@@ -39,18 +39,28 @@ export function prefixIdentifiers(
3939
4040 walkIdentifiers (
4141 ast ,
42- ident => {
42+ ( ident , parent ) => {
4343 const { name } = ident
4444 if ( doNotPrefix ( name ) ) {
4545 return
4646 }
4747
48- if ( ! isScriptSetup ) {
49- s . prependRight ( ident . start ! , '_vm.' )
50- return
48+ let prefix = `_vm.`
49+ if ( isScriptSetup ) {
50+ const type = bindings [ name ]
51+ if ( type && type . startsWith ( 'setup' ) ) {
52+ prefix = `_setup.`
53+ }
5154 }
5255
53- s . overwrite ( ident . start ! , ident . end ! , rewriteIdentifier ( name , bindings ) )
56+ if ( isStaticProperty ( parent ) && parent . shorthand ) {
57+ // property shorthand like { foo }, we need to add the key since
58+ // we rewrite the value
59+ // { foo } -> { foo: _vm.foo }
60+ s . appendLeft ( ident . end ! , `: ${ prefix } ${ name } ` )
61+ } else {
62+ s . prependRight ( ident . start ! , prefix )
63+ }
5464 } ,
5565 node => {
5666 if ( node . type === 'WithStatement' ) {
@@ -70,15 +80,3 @@ export function prefixIdentifiers(
7080
7181 return s . toString ( )
7282}
73-
74- export function rewriteIdentifier (
75- name : string ,
76- bindings : BindingMetadata
77- ) : string {
78- const type = bindings [ name ]
79- if ( type && type . startsWith ( 'setup' ) ) {
80- return `_setup.${ name } `
81- } else {
82- return `_vm.${ name } `
83- }
84- }
0 commit comments