Skip to content

Commit ab21468

Browse files
committed
wip: warn v-if/v-for co-usage
1 parent 7ceb873 commit ab21468

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

packages/compiler-core/src/compat/compatConfig.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ const deprecationData: Record<CompilerDeprecationTypes, DeprecationData> = {
8181
[CompilerDeprecationTypes.COMPILER_V_IF_V_FOR_PRECEDENCE]: {
8282
message:
8383
`v-if / v-for precedence when used on the same element has changed ` +
84-
`in Vue 3. It is best to avoid the ambiguity with either <template> tags ` +
85-
`or a computed property that filters v-for data source.`,
84+
`in Vue 3: v-if now takes higher precedence and will no longer have ` +
85+
`access to v-for scope variables. It is best to avoid the ambiguity ` +
86+
`with <template> tags or use a computed property that filters v-for ` +
87+
`data source.`,
8688
link: `https://v3.vuejs.org/guide/migration/v-if-v-for.html`
8789
},
8890

packages/compiler-core/src/parse.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ import {
3333
import {
3434
checkCompatEnabled,
3535
CompilerCompatOptions,
36-
CompilerDeprecationTypes
36+
CompilerDeprecationTypes,
37+
warnDeprecation
3738
} from './compat/compatConfig'
3839

3940
type OptionalOptions =
@@ -488,6 +489,29 @@ function parseTag(
488489
props = parseAttributes(context, type).filter(p => p.name !== 'v-pre')
489490
}
490491

492+
// warn v-if/v-for usage on the same element
493+
if (__COMPAT__ && __DEV__ && !__TEST__) {
494+
let hasIf = false
495+
let hasFor = false
496+
for (let i = 0; i < props.length; i++) {
497+
const p = props[i]
498+
if (p.type === NodeTypes.DIRECTIVE) {
499+
if (p.name === 'if') {
500+
hasIf = true
501+
} else if (p.name === 'for') {
502+
hasFor = true
503+
}
504+
}
505+
if (hasIf && hasFor) {
506+
warnDeprecation(
507+
CompilerDeprecationTypes.COMPILER_V_IF_V_FOR_PRECEDENCE,
508+
context,
509+
getSelection(context, start)
510+
)
511+
}
512+
}
513+
}
514+
491515
// Tag close.
492516
let isSelfClosing = false
493517
if (context.source.length === 0) {

0 commit comments

Comments
 (0)