Skip to content

Commit f1c9629

Browse files
committed
fix: child nodes of KeepAlive should not be transformed to slots
1 parent ee3faaf commit f1c9629

File tree

4 files changed

+26
-40
lines changed

4 files changed

+26
-40
lines changed

packages/babel-plugin-jsx/src/buildProps.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const getJSXAttributeValue = (
2727
path: NodePath<t.JSXAttribute>,
2828
state: State,
2929
): (
30-
t.StringLiteral | t.Expression | null
31-
) => {
30+
t.StringLiteral | t.Expression | null
31+
) => {
3232
const valuePath = path.get('value');
3333
if (valuePath.isJSXElement()) {
3434
return transformJSXElement(valuePath, state);
@@ -181,8 +181,8 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
181181
hasStyleBinding = true;
182182
} else if (
183183
name !== 'key'
184-
&& !isDirective(name)
185-
&& name !== 'on'
184+
&& !isDirective(name)
185+
&& name !== 'on'
186186
) {
187187
dynamicPropNames.add(name);
188188
}

packages/babel-plugin-jsx/src/sugar-fragment.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,15 @@ export default ({
2020
JSXFragment: {
2121
enter(path: NodePath<t.JSXElement>, state: State) {
2222
const fragmentCallee = createIdentifier(state, FRAGMENT);
23-
path.replaceWith(
24-
t.inherits(transformFragment(
25-
path,
26-
t.isIdentifier(fragmentCallee)
27-
? t.jsxIdentifier(fragmentCallee.name)
28-
: t.jsxMemberExpression(
29-
t.jsxIdentifier((fragmentCallee.object as t.Identifier).name),
30-
t.jsxIdentifier((fragmentCallee.property as t.Identifier).name),
31-
),
32-
), path.node)
33-
,
34-
);
23+
path.replaceWith(transformFragment(
24+
path,
25+
t.isIdentifier(fragmentCallee)
26+
? t.jsxIdentifier(fragmentCallee.name)
27+
: t.jsxMemberExpression(
28+
t.jsxIdentifier((fragmentCallee.object as t.Identifier).name),
29+
t.jsxIdentifier((fragmentCallee.property as t.Identifier).name),
30+
),
31+
));
3532
},
3633
},
3734
});

packages/babel-plugin-jsx/src/transform-vue-jsx.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const transformJSXElement = (
121121
t.numericLiteral(slotFlag),
122122
) as any,
123123
].filter(Boolean));
124-
if (t.isIdentifier(child)) {
124+
if (t.isIdentifier(child) && isComponent) {
125125
VNodeChild = enableObjectSlots ? t.conditionalExpression(
126126
t.callExpression(state.get('@vue/babel-plugin-jsx/runtimeIsSlot')(), [child]),
127127
child,
@@ -212,9 +212,7 @@ export { transformJSXElement };
212212
export default ({
213213
JSXElement: {
214214
exit(path: NodePath<t.JSXElement>, state: State) {
215-
path.replaceWith(
216-
t.inherits(transformJSXElement(path, state), path.node),
217-
);
215+
path.replaceWith(transformJSXElement(path, state));
218216
},
219217
},
220218
});

packages/babel-plugin-jsx/src/utils.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import SlotFlags from './slotFlags';
77

88
const JSX_HELPER_KEY = 'JSX_HELPER_KEY';
99
const FRAGMENT = 'Fragment';
10+
const KEEP_ALIVE = 'KeepAlive';
11+
1012
/**
1113
* create Identifier
1214
* @param path NodePath
@@ -26,22 +28,11 @@ const isDirective = (src: string): boolean => src.startsWith('v-')
2628
|| (src.startsWith('v') && src.length >= 2 && src[1] >= 'A' && src[1] <= 'Z');
2729

2830
/**
29-
* Check if a Node is fragment
30-
* @param {*} path JSXIdentifier | JSXMemberExpression | JSXNamespacedName
31+
* Should transformed to slots
32+
* @param tag string
3133
* @returns boolean
3234
*/
33-
const isFragment = (
34-
path:
35-
NodePath<t.JSXIdentifier | t.JSXMemberExpression | t.JSXNamespacedName>,
36-
): boolean => {
37-
if (path.isJSXIdentifier()) {
38-
return path.node.name.endsWith(FRAGMENT);
39-
}
40-
if (path.isJSXMemberExpression()) {
41-
return path.node.property.name.endsWith(FRAGMENT);
42-
}
43-
return false;
44-
};
35+
const shouldTransformedToSlots = (tag: string) => !(tag.endsWith(FRAGMENT) || tag === KEEP_ALIVE);
4536

4637
/**
4738
* Check if a Node is a component
@@ -53,13 +44,13 @@ const isFragment = (
5344
const checkIsComponent = (path: NodePath<t.JSXOpeningElement>): boolean => {
5445
const namePath = path.get('name');
5546

56-
if (t.isJSXMemberExpression(namePath)) {
57-
return !isFragment(namePath); // For withCtx
47+
if (namePath.isJSXMemberExpression()) {
48+
return shouldTransformedToSlots(namePath.node.property.name); // For withCtx
5849
}
5950

6051
const tag = (namePath as NodePath<t.JSXIdentifier>).node.name;
6152

62-
return !tag.endsWith(FRAGMENT) && !htmlTags.includes(tag) && !svgTags.includes(tag);
53+
return shouldTransformedToSlots(tag) && !htmlTags.includes(tag) && !svgTags.includes(tag);
6354
};
6455

6556
/**
@@ -181,8 +172,8 @@ const transformJSXText = (path: NodePath<t.JSXText>): t.StringLiteral | null =>
181172
const transformJSXExpressionContainer = (
182173
path: NodePath<t.JSXExpressionContainer>,
183174
): (
184-
t.Expression
185-
) => path.get('expression').node as t.Expression;
175+
t.Expression
176+
) => path.get('expression').node as t.Expression;
186177

187178
/**
188179
* Transform JSXSpreadChild
@@ -240,7 +231,7 @@ export {
240231
transformJSXText,
241232
transformJSXSpreadChild,
242233
transformJSXExpressionContainer,
243-
isFragment,
234+
shouldTransformedToSlots,
244235
FRAGMENT,
245236
walksScope,
246237
buildIIFE,

0 commit comments

Comments
 (0)