@@ -2,6 +2,7 @@ import MagicString from 'magic-string'
2
2
import { parseExpression , ParserOptions , ParserPlugin } from '@babel/parser'
3
3
import { makeMap } from 'shared/util'
4
4
import { walkIdentifiers } from './babelUtils'
5
+ import { BindingMetadata } from './types'
5
6
6
7
const doNotPrefix = makeMap (
7
8
'Infinity,undefined,NaN,isFinite,isNaN,' +
@@ -16,12 +17,13 @@ const doNotPrefix = makeMap(
16
17
* The input is expected to be the render function code directly returned from
17
18
* `compile()` calls, e.g. `with(this){return ...}`
18
19
*/
19
- export function stripWith (
20
+ export function prefixIdentifiers (
20
21
source : string ,
21
22
fnName = '' ,
22
23
isFunctional = false ,
23
24
isTS = false ,
24
- babelOptions : ParserOptions = { }
25
+ babelOptions : ParserOptions = { } ,
26
+ bindings ?: BindingMetadata
25
27
) {
26
28
source = `function ${ fnName } (${ isFunctional ? `_c,_vm` : `` } ){${ source } \n}`
27
29
@@ -40,21 +42,45 @@ export function stripWith(
40
42
walkIdentifiers (
41
43
ast ,
42
44
ident => {
43
- if ( doNotPrefix ( ident . name ) ) {
45
+ const { name } = ident
46
+ if ( doNotPrefix ( name ) ) {
44
47
return
45
48
}
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 ) )
47
56
} ,
48
57
node => {
49
58
if ( node . type === 'WithStatement' ) {
50
59
s . remove ( node . start ! , node . body . start ! + 1 )
51
60
s . remove ( node . end ! - 1 , node . end ! )
52
61
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
+ )
54
68
}
55
69
}
56
70
}
57
71
)
58
72
59
73
return s . toString ( )
60
74
}
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