@@ -22,7 +22,7 @@ import {
22
22
import { parse , ParserPlugin } from '@babel/parser'
23
23
import { hasOwn , isArray , isString } from '@vue/shared'
24
24
25
- const TO_VAR_SYMBOL = '$'
25
+ const CONVERT_SYMBOL = '$'
26
26
const ESCAPE_SYMBOL = '$$'
27
27
const shorthands = [ 'ref' , 'computed' , 'shallowRef' , 'toRef' , 'customRef' ]
28
28
const transformCheckRE = / [ ^ \w ] \$ (?: \$ | r e f | c o m p u t e d | s h a l l o w R e f ) ? \s * ( \( | \< ) /
@@ -114,10 +114,44 @@ export function transformAST(
114
114
// TODO remove when out of experimental
115
115
warnExperimental ( )
116
116
117
+ let convertSymbol = CONVERT_SYMBOL
118
+ let escapeSymbol = ESCAPE_SYMBOL
119
+
120
+ // macro import handling
121
+ for ( const node of ast . body ) {
122
+ if (
123
+ node . type === 'ImportDeclaration' &&
124
+ node . source . value === 'vue/macros'
125
+ ) {
126
+ // remove macro imports
127
+ s . remove ( node . start ! + offset , node . end ! + offset )
128
+ // check aliasing
129
+ for ( const specifier of node . specifiers ) {
130
+ if ( specifier . type === 'ImportSpecifier' ) {
131
+ const imported = ( specifier . imported as Identifier ) . name
132
+ const local = specifier . local . name
133
+ if ( local !== imported ) {
134
+ if ( imported === ESCAPE_SYMBOL ) {
135
+ escapeSymbol = local
136
+ } else if ( imported === CONVERT_SYMBOL ) {
137
+ convertSymbol = local
138
+ } else {
139
+ error (
140
+ `macro imports for ref-creating methods do not support aliasing.` ,
141
+ specifier
142
+ )
143
+ }
144
+ }
145
+ }
146
+ }
147
+ }
148
+ }
149
+
117
150
const importedHelpers = new Set < string > ( )
118
151
const rootScope : Scope = { }
119
152
const scopeStack : Scope [ ] = [ rootScope ]
120
153
let currentScope : Scope = rootScope
154
+ let escapeScope : CallExpression | undefined // inside $$()
121
155
const excludedIds = new WeakSet < Identifier > ( )
122
156
const parentStack : Node [ ] = [ ]
123
157
const propsLocalToPublicMap = Object . create ( null )
@@ -135,6 +169,16 @@ export function transformAST(
135
169
}
136
170
}
137
171
172
+ function isRefCreationCall ( callee : string ) : string | false {
173
+ if ( callee === convertSymbol ) {
174
+ return convertSymbol
175
+ }
176
+ if ( callee [ 0 ] === '$' && shorthands . includes ( callee . slice ( 1 ) ) ) {
177
+ return callee
178
+ }
179
+ return false
180
+ }
181
+
138
182
function error ( msg : string , node : Node ) {
139
183
const e = new Error ( msg )
140
184
; ( e as any ) . node = node
@@ -174,20 +218,16 @@ export function transformAST(
174
218
if ( stmt . type === 'VariableDeclaration' ) {
175
219
if ( stmt . declare ) continue
176
220
for ( const decl of stmt . declarations ) {
177
- let toVarCall
221
+ let refCall
178
222
const isCall =
179
223
decl . init &&
180
224
decl . init . type === 'CallExpression' &&
181
225
decl . init . callee . type === 'Identifier'
182
226
if (
183
227
isCall &&
184
- ( toVarCall = isToVarCall ( ( decl as any ) . init . callee . name ) )
228
+ ( refCall = isRefCreationCall ( ( decl as any ) . init . callee . name ) )
185
229
) {
186
- processRefDeclaration (
187
- toVarCall ,
188
- decl . id ,
189
- decl . init as CallExpression
190
- )
230
+ processRefDeclaration ( refCall , decl . id , decl . init as CallExpression )
191
231
} else {
192
232
const isProps =
193
233
isRoot &&
@@ -220,7 +260,7 @@ export function transformAST(
220
260
call : CallExpression
221
261
) {
222
262
excludedIds . add ( call . callee as Identifier )
223
- if ( method === TO_VAR_SYMBOL ) {
263
+ if ( method === convertSymbol ) {
224
264
// $
225
265
// remove macro
226
266
s . remove ( call . callee . start ! + offset , call . callee . end ! + offset )
@@ -491,9 +531,6 @@ export function transformAST(
491
531
492
532
// check root scope first
493
533
walkScope ( ast , true )
494
-
495
- // inside $$()
496
- let escapeScope : CallExpression | undefined
497
534
; ( walk as any ) ( ast , {
498
535
enter ( node : Node , parent ?: Node ) {
499
536
parent && parentStack . push ( parent )
@@ -544,16 +581,16 @@ export function transformAST(
544
581
if ( node . type === 'CallExpression' && node . callee . type === 'Identifier' ) {
545
582
const callee = node . callee . name
546
583
547
- const toVarCall = isToVarCall ( callee )
548
- if ( toVarCall && ( ! parent || parent . type !== 'VariableDeclarator' ) ) {
584
+ const refCall = isRefCreationCall ( callee )
585
+ if ( refCall && ( ! parent || parent . type !== 'VariableDeclarator' ) ) {
549
586
return error (
550
- `${ toVarCall } can only be used as the initializer of ` +
587
+ `${ refCall } can only be used as the initializer of ` +
551
588
`a variable declaration.` ,
552
589
node
553
590
)
554
591
}
555
592
556
- if ( callee === ESCAPE_SYMBOL ) {
593
+ if ( callee === escapeSymbol ) {
557
594
s . remove ( node . callee . start ! + offset , node . callee . end ! + offset )
558
595
escapeScope = node
559
596
}
@@ -596,16 +633,6 @@ export function transformAST(
596
633
}
597
634
}
598
635
599
- function isToVarCall ( callee : string ) : string | false {
600
- if ( callee === TO_VAR_SYMBOL ) {
601
- return TO_VAR_SYMBOL
602
- }
603
- if ( callee [ 0 ] === TO_VAR_SYMBOL && shorthands . includes ( callee . slice ( 1 ) ) ) {
604
- return callee
605
- }
606
- return false
607
- }
608
-
609
636
const RFC_LINK = `https://github.com/vuejs/rfcs/discussions/369`
610
637
const hasWarned : Record < string , boolean > = { }
611
638
0 commit comments