Skip to content

Commit 586ec51

Browse files
committed
chore: warnings for ref transform
1 parent 8f1101c commit 586ec51

File tree

3 files changed

+70
-45
lines changed

3 files changed

+70
-45
lines changed

packages/compiler-sfc/src/compileScript.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {
4848
genNormalScriptCssVarsCode
4949
} from './cssVars'
5050
import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate'
51-
import { warnExperimental, warnOnce } from './warn'
51+
import { warnOnce } from './warn'
5252
import { rewriteDefault } from './rewriteDefault'
5353
import { createCache } from './cache'
5454
import {
@@ -652,10 +652,6 @@ export function compileScript(
652652

653653
// apply ref transform
654654
if (enableRefTransform && shouldTransformRef(script.content)) {
655-
warnExperimental(
656-
`ref sugar`,
657-
`https://github.com/vuejs/rfcs/discussions/369`
658-
)
659655
const { rootVars, importedHelpers } = transformRefAST(
660656
scriptAst,
661657
s,
@@ -900,10 +896,6 @@ export function compileScript(
900896

901897
// 3. Apply ref sugar transform
902898
if (enableRefTransform && shouldTransformRef(scriptSetup.content)) {
903-
warnExperimental(
904-
`ref sugar`,
905-
`https://github.com/vuejs/rfcs/discussions/369`
906-
)
907899
const { rootVars, importedHelpers } = transformRefAST(
908900
scriptSetupAst,
909901
s,

packages/compiler-sfc/src/warn.ts

-15
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,3 @@ export function warn(msg: string) {
1414
`\x1b[1m\x1b[33m[@vue/compiler-sfc]\x1b[0m\x1b[33m ${msg}\x1b[0m\n`
1515
)
1616
}
17-
18-
export function warnExperimental(feature: string, url: string) {
19-
// eslint-disable-next-line
20-
if (typeof window !== 'undefined') {
21-
return
22-
}
23-
warnOnce(
24-
`${feature} is still an experimental proposal.\n` +
25-
`Follow its status at ${url}.`
26-
)
27-
warnOnce(
28-
`When using experimental features,\n` +
29-
`it is recommended to pin your vue dependencies to exact versions to avoid breakage.`
30-
)
31-
}

packages/ref-transform/src/refTransform.ts

+69-21
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ export function transformAST(
104104
rootVars: string[]
105105
importedHelpers: string[]
106106
} {
107+
// TODO remove when out of experimental
108+
warnExperimental()
109+
107110
const importedHelpers = new Set<string>()
108111
const rootScope: Scope = {}
109112
const scopeStack: Scope[] = [rootScope]
@@ -148,7 +151,12 @@ export function transformAST(
148151
if (stmt.declare) continue
149152
for (const decl of stmt.declarations) {
150153
let toVarCall
151-
if (decl.init && (toVarCall = isToVarCall(decl.init))) {
154+
if (
155+
decl.init &&
156+
decl.init.type === 'CallExpression' &&
157+
decl.init.callee.type === 'Identifier' &&
158+
(toVarCall = isToVarCall(decl.init.callee.name))
159+
) {
152160
processRefDeclaration(
153161
toVarCall,
154162
decl.init as CallExpression,
@@ -369,18 +377,38 @@ export function transformAST(
369377
}
370378
}
371379

372-
const toVarCall = isToVarCall(node)
373-
if (toVarCall && (!parent || parent.type !== 'VariableDeclarator')) {
374-
return error(
375-
`${toVarCall} can only be used as the initializer of ` +
376-
`a variable declaration.`,
377-
node
378-
)
379-
}
380+
if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
381+
const callee = node.callee.name
380382

381-
if (isToRefCall(node)) {
382-
s.remove(node.callee.start! + offset, node.callee.end! + offset)
383-
return this.skip()
383+
const toVarCall = isToVarCall(callee)
384+
if (toVarCall && (!parent || parent.type !== 'VariableDeclarator')) {
385+
return error(
386+
`${toVarCall} can only be used as the initializer of ` +
387+
`a variable declaration.`,
388+
node
389+
)
390+
}
391+
392+
if (callee === TO_REF_SYMBOL) {
393+
s.remove(node.callee.start! + offset, node.callee.end! + offset)
394+
return this.skip()
395+
}
396+
397+
// TODO remove when out of experimental
398+
if (callee === '$raw') {
399+
error(
400+
`$raw() has been replaced by $$(). ` +
401+
`See ${RFC_LINK} for latest updates.`,
402+
node
403+
)
404+
}
405+
if (callee === '$fromRef') {
406+
error(
407+
`$fromRef() has been replaced by $(). ` +
408+
`See ${RFC_LINK} for latest updates.`,
409+
node
410+
)
411+
}
384412
}
385413
},
386414
leave(node: Node, parent?: Node) {
@@ -401,11 +429,7 @@ export function transformAST(
401429
}
402430
}
403431

404-
function isToVarCall(node: Node): string | false {
405-
if (node.type !== 'CallExpression' || node.callee.type !== 'Identifier') {
406-
return false
407-
}
408-
const callee = node.callee.name
432+
function isToVarCall(callee: string): string | false {
409433
if (callee === TO_VAR_SYMBOL) {
410434
return TO_VAR_SYMBOL
411435
}
@@ -415,9 +439,33 @@ function isToVarCall(node: Node): string | false {
415439
return false
416440
}
417441

418-
function isToRefCall(node: Node): node is CallExpression {
419-
return (
420-
node.type === 'CallExpression' &&
421-
(node.callee as Identifier).name === TO_REF_SYMBOL
442+
const RFC_LINK = `https://github.com/vuejs/rfcs/discussions/369`
443+
const hasWarned: Record<string, boolean> = {}
444+
445+
function warnExperimental() {
446+
// eslint-disable-next-line
447+
if (typeof window !== 'undefined') {
448+
return
449+
}
450+
warnOnce(
451+
`@vue/ref-transform is an experimental feature.\n` +
452+
`Experimental features may change behavior between patch versions.\n` +
453+
`It is recommended to pin your vue dependencies to exact versions to avoid breakage.\n` +
454+
`You can follow the proposal's status at ${RFC_LINK}.`
455+
)
456+
}
457+
458+
function warnOnce(msg: string) {
459+
const isNodeProd =
460+
typeof process !== 'undefined' && process.env.NODE_ENV === 'production'
461+
if (!isNodeProd && !__TEST__ && !hasWarned[msg]) {
462+
hasWarned[msg] = true
463+
warn(msg)
464+
}
465+
}
466+
467+
function warn(msg: string) {
468+
console.warn(
469+
`\x1b[1m\x1b[33m[@vue/compiler-sfc]\x1b[0m\x1b[33m ${msg}\x1b[0m\n`
422470
)
423471
}

0 commit comments

Comments
 (0)