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