refactor: use early return style in v-for

This commit is contained in:
Evan You 2019-10-09 22:31:27 -04:00
parent c7620c1056
commit 2967745e7b

View File

@ -39,7 +39,13 @@ import { PatchFlags, PatchFlagNames } from '@vue/shared'
export const transformFor = createStructuralDirectiveTransform(
'for',
(node, dir, context) => {
if (dir.exp) {
if (!dir.exp) {
context.onError(
createCompilerError(ErrorCodes.X_V_FOR_NO_EXPRESSION, dir.loc)
)
return
}
const parseResult = parseForExpression(
// can only be simple expression because vFor transform is applied
// before expression transform.
@ -47,7 +53,13 @@ export const transformFor = createStructuralDirectiveTransform(
context
)
if (parseResult) {
if (!parseResult) {
context.onError(
createCompilerError(ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION, dir.loc)
)
return
}
const { helper, addIdentifiers, removeIdentifiers, scopes } = context
const { source, value, key, index } = parseResult
@ -64,8 +76,7 @@ export const transformFor = createStructuralDirectiveTransform(
helper(FRAGMENT),
`null`,
renderExp,
fragmentFlag +
(__DEV__ ? ` /* ${PatchFlagNames[fragmentFlag]} */` : ``)
fragmentFlag + (__DEV__ ? ` /* ${PatchFlagNames[fragmentFlag]} */` : ``)
])
]) as ForCodegenNode
@ -76,8 +87,7 @@ export const transformFor = createStructuralDirectiveTransform(
valueAlias: value,
keyAlias: key,
objectIndexAlias: index,
children:
node.tagType === ElementTypes.TEMPLATE ? node.children : [node],
children: node.tagType === ElementTypes.TEMPLATE ? node.children : [node],
codegenNode
})
@ -157,16 +167,6 @@ export const transformFor = createStructuralDirectiveTransform(
)
)
}
} else {
context.onError(
createCompilerError(ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION, dir.loc)
)
}
} else {
context.onError(
createCompilerError(ErrorCodes.X_V_FOR_NO_EXPRESSION, dir.loc)
)
}
}
)