@@ -2,6 +2,7 @@ import MagicString from 'magic-string'
22import { parseExpression , ParserOptions , ParserPlugin } from '@babel/parser'
33import { makeMap } from 'shared/util'
44import { walkIdentifiers } from './babelUtils'
5+ import { BindingMetadata } from './types'
56
67const doNotPrefix = makeMap (
78 'Infinity,undefined,NaN,isFinite,isNaN,' +
@@ -16,12 +17,13 @@ const doNotPrefix = makeMap(
1617 * The input is expected to be the render function code directly returned from
1718 * `compile()` calls, e.g. `with(this){return ...}`
1819 */
19- export function stripWith (
20+ export function prefixIdentifiers (
2021 source : string ,
2122 fnName = '' ,
2223 isFunctional = false ,
2324 isTS = false ,
24- babelOptions : ParserOptions = { }
25+ babelOptions : ParserOptions = { } ,
26+ bindings ?: BindingMetadata
2527) {
2628 source = `function ${ fnName } (${ isFunctional ? `_c,_vm` : `` } ){${ source } \n}`
2729
@@ -40,21 +42,45 @@ export function stripWith(
4042 walkIdentifiers (
4143 ast ,
4244 ident => {
43- if ( doNotPrefix ( ident . name ) ) {
45+ const { name } = ident
46+ if ( doNotPrefix ( name ) ) {
4447 return
4548 }
46- s . prependRight ( ident . start ! , '_vm.' )
49+
50+ if ( ! bindings ) {
51+ s . prependRight ( ident . start ! , '_vm.' )
52+ return
53+ }
54+
55+ s . overwrite ( ident . start ! , ident . end ! , rewriteIdentifier ( name , bindings ) )
4756 } ,
4857 node => {
4958 if ( node . type === 'WithStatement' ) {
5059 s . remove ( node . start ! , node . body . start ! + 1 )
5160 s . remove ( node . end ! - 1 , node . end ! )
5261 if ( ! isFunctional ) {
53- s . prependRight ( node . start ! , `var _vm=this;var _c=_vm._self._c;` )
62+ s . prependRight (
63+ node . start ! ,
64+ `var _vm=this,_c=_vm._self._c${
65+ bindings ? `,_setup=_vm._setupProxy;` : `;`
66+ } `
67+ )
5468 }
5569 }
5670 }
5771 )
5872
5973 return s . toString ( )
6074}
75+
76+ export function rewriteIdentifier (
77+ name : string ,
78+ bindings : BindingMetadata
79+ ) : string {
80+ const type = bindings [ name ]
81+ if ( type && type . startsWith ( 'setup' ) ) {
82+ return `_setup.${ name } `
83+ } else {
84+ return `_vm.${ name } `
85+ }
86+ }
0 commit comments