Skip to content

Commit 3528ced

Browse files
committed
wip: warn key usage of v-if branches
1 parent ab21468 commit 3528ced

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

packages/compiler-core/src/errors.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export function defaultOnError(error: CompilerError) {
1414
}
1515

1616
export function defaultOnWarn(msg: CompilerError) {
17-
throw new Error('foo')
1817
__DEV__ && console.warn(`[Vue warn]`, msg.message)
1918
}
2019

@@ -92,13 +91,16 @@ export const enum ErrorCodes {
9291
X_CACHE_HANDLER_NOT_SUPPORTED,
9392
X_SCOPE_ID_NOT_SUPPORTED,
9493

94+
// warnings
95+
X_V_IF_KEY,
96+
9597
// Special value for higher-order compilers to pick up the last code
9698
// to avoid collision of error codes. This should always be kept as the last
9799
// item.
98100
__EXTEND_POINT__
99101
}
100102

101-
export const errorMessages: { [code: number]: string } = {
103+
export const errorMessages: Record<ErrorCodes, string> = {
102104
// parse errors
103105
[ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT]: 'Illegal comment.',
104106
[ErrorCodes.CDATA_IN_HTML_CONTENT]:
@@ -129,6 +131,7 @@ export const errorMessages: { [code: number]: string } = {
129131
"Attribute name cannot start with '='.",
130132
[ErrorCodes.UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME]:
131133
"'<?' is allowed only in XML context.",
134+
[ErrorCodes.UNEXPECTED_NULL_CHARACTER]: `Unexpected null cahracter.`,
132135
[ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG]: "Illegal '/' in tags.",
133136

134137
// Vue-specific parse errors
@@ -169,5 +172,13 @@ export const errorMessages: { [code: number]: string } = {
169172
[ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler.`,
170173
[ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED]: `ES module mode is not supported in this build of compiler.`,
171174
[ErrorCodes.X_CACHE_HANDLER_NOT_SUPPORTED]: `"cacheHandlers" option is only supported when the "prefixIdentifiers" option is enabled.`,
172-
[ErrorCodes.X_SCOPE_ID_NOT_SUPPORTED]: `"scopeId" option is only supported in module mode.`
175+
[ErrorCodes.X_SCOPE_ID_NOT_SUPPORTED]: `"scopeId" option is only supported in module mode.`,
176+
177+
// warnings
178+
[ErrorCodes.X_V_IF_KEY]:
179+
`unnecessary key usage on v-if/else branches. ` +
180+
`Vue will automatically generate unique keys for each branch.`,
181+
182+
// just to fullfill types
183+
[ErrorCodes.__EXTEND_POINT__]: ``
173184
}

packages/compiler-core/src/transforms/vIf.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function processIf(
110110
}
111111

112112
if (dir.name === 'if') {
113-
const branch = createIfBranch(node, dir)
113+
const branch = createIfBranch(node, dir, context)
114114
const ifNode: IfNode = {
115115
type: NodeTypes.IF,
116116
loc: node.loc,
@@ -145,7 +145,7 @@ export function processIf(
145145
if (sibling && sibling.type === NodeTypes.IF) {
146146
// move the node to the if node's branches
147147
context.removeNode()
148-
const branch = createIfBranch(node, dir)
148+
const branch = createIfBranch(node, dir, context)
149149
if (__DEV__ && comments.length) {
150150
branch.children = [...comments, ...branch.children]
151151
}
@@ -187,7 +187,16 @@ export function processIf(
187187
}
188188
}
189189

190-
function createIfBranch(node: ElementNode, dir: DirectiveNode): IfBranchNode {
190+
function createIfBranch(
191+
node: ElementNode,
192+
dir: DirectiveNode,
193+
context: TransformContext
194+
): IfBranchNode {
195+
const userKey = findProp(node, `key`)
196+
if (__DEV__ && userKey) {
197+
context.onWarn(createCompilerError(ErrorCodes.X_V_IF_KEY, userKey.loc))
198+
}
199+
191200
return {
192201
type: NodeTypes.IF_BRANCH,
193202
loc: node.loc,
@@ -196,7 +205,7 @@ function createIfBranch(node: ElementNode, dir: DirectiveNode): IfBranchNode {
196205
node.tagType === ElementTypes.TEMPLATE && !findDir(node, 'for')
197206
? node.children
198207
: [node],
199-
userKey: findProp(node, `key`)
208+
userKey
200209
}
201210
}
202211

0 commit comments

Comments
 (0)