1
1
// - Parse expressions in templates into compound expressions so that each
2
2
// identifier gets more accurate source-map locations.
3
3
//
4
- // - Prefix identifiers with `_ctx.` so that they are accessed from the render
5
- // context
4
+ // - Prefix identifiers with `_ctx.` or `$xxx` (for known binding types) so that
5
+ // they are accessed from the right source
6
6
//
7
7
// - This transform is only applied in non-browser builds because it relies on
8
8
// an additional JavaScript parser. In the browser, there is no source-map
@@ -25,7 +25,8 @@ import {
25
25
import {
26
26
isGloballyWhitelisted ,
27
27
makeMap ,
28
- babelParserDefautPlugins
28
+ babelParserDefautPlugins ,
29
+ hasOwn
29
30
} from '@vue/shared'
30
31
import { createCompilerError , ErrorCodes } from '../errors'
31
32
import { Node , Function , Identifier , ObjectProperty } from '@babel/types'
@@ -99,6 +100,14 @@ export function processExpression(
99
100
return node
100
101
}
101
102
103
+ const { bindingMetadata } = context
104
+ const prefix = ( raw : string ) => {
105
+ const source = hasOwn ( bindingMetadata , raw )
106
+ ? `$` + bindingMetadata [ raw ]
107
+ : `_ctx`
108
+ return `${ source } .${ raw } `
109
+ }
110
+
102
111
// fast path if expression is a simple identifier.
103
112
const rawExp = node . content
104
113
// bail on parens to prevent any possible function invocations.
@@ -110,7 +119,7 @@ export function processExpression(
110
119
! isGloballyWhitelisted ( rawExp ) &&
111
120
! isLiteralWhitelisted ( rawExp )
112
121
) {
113
- node . content = `_ctx. ${ rawExp } `
122
+ node . content = prefix ( rawExp )
114
123
} else if ( ! context . identifiers [ rawExp ] && ! bailConstant ) {
115
124
// mark node constant for hoisting unless it's referring a scope variable
116
125
node . isConstant = true
@@ -148,7 +157,7 @@ export function processExpression(
148
157
const isDuplicate = ( node : Node & PrefixMeta ) : boolean =>
149
158
ids . some ( id => id . start === node . start )
150
159
151
- // walk the AST and look for identifiers that need to be prefixed with `_ctx.` .
160
+ // walk the AST and look for identifiers that need to be prefixed.
152
161
walkJS ( ast , {
153
162
enter ( node : Node & PrefixMeta , parent ) {
154
163
if ( node . type === 'Identifier' ) {
@@ -160,7 +169,7 @@ export function processExpression(
160
169
// rewrite the value
161
170
node . prefix = `${ node . name } : `
162
171
}
163
- node . name = `_ctx. ${ node . name } `
172
+ node . name = prefix ( node . name )
164
173
ids . push ( node )
165
174
} else if ( ! isStaticPropertyKey ( node , parent ) ) {
166
175
// The identifier is considered constant unless it's pointing to a
0 commit comments