fix: Fragment

This commit is contained in:
Amour1688 2020-07-11 14:23:56 +08:00
parent 9757f3eafd
commit a4d7390273

View File

@ -125,6 +125,7 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
if (props.length === 0) { if (props.length === 0) {
return { return {
tag, tag,
isComponent,
props: t.nullLiteral(), props: t.nullLiteral(),
directives, directives,
patchFlag, patchFlag,
@ -314,6 +315,7 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
return { return {
tag, tag,
props: propsExpression, props: propsExpression,
isComponent,
directives, directives,
patchFlag, patchFlag,
dynamicPropNames, dynamicPropNames,
@ -371,11 +373,18 @@ const transformJSXElement = (
const { const {
tag, tag,
props, props,
isComponent,
directives, directives,
patchFlag, patchFlag,
dynamicPropNames, dynamicPropNames,
} = buildProps(path, state); } = buildProps(path, state);
const { scope: { bindings } } = path;
const bindingsReferenced = Object.keys(bindings).some(key => bindings[key].referenced);
const useOptimate = !(bindingsReferenced && t.isReturnStatement(path.container));
const flagNames = Object.keys(PatchFlagNames) const flagNames = Object.keys(PatchFlagNames)
.map(Number) .map(Number)
.filter((n) => n > 0 && patchFlag & n) .filter((n) => n > 0 && patchFlag & n)
@ -390,11 +399,18 @@ const transformJSXElement = (
} }
// @ts-ignore // @ts-ignore
const createVNode = t.callExpression(createIdentifier(state, 'createVNode'), [ const createVNode = t.callExpression(createIdentifier(state, useOptimate ? 'createVNode' : 'h'), [
tag, tag,
// @ts-ignore // @ts-ignore
compatibleProps ? t.callExpression(state.get('compatibleProps'), [props]) : props, compatibleProps ? t.callExpression(state.get('compatibleProps'), [props]) : props,
!!children.length ? t.arrayExpression(children) : t.nullLiteral(), !!children.length ? (
isComponent ? t.objectExpression([
t.objectProperty(
t.identifier('default'),
t.arrowFunctionExpression([], t.arrayExpression(children))
)
]) : t.arrayExpression(children)
) : t.nullLiteral(),
!!patchFlag && t.addComment(t.numericLiteral(patchFlag), 'trailing', ` ${flagNames} `, false), !!patchFlag && t.addComment(t.numericLiteral(patchFlag), 'trailing', ` ${flagNames} `, false),
!!dynamicPropNames.size !!dynamicPropNames.size
&& t.arrayExpression([...dynamicPropNames.keys()].map((name) => t.stringLiteral(name as string))), && t.arrayExpression([...dynamicPropNames.keys()].map((name) => t.stringLiteral(name as string))),