Skip to content

Commit 35780fd

Browse files
Luo ZhihaoAmour1688
andauthored
fix: wrong compilation result when _Fragment is imported (#518)
* fix: optimize Fragment judgement Co-authored-by: Amour1688 <[email protected]>
1 parent 465d629 commit 35780fd

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export const isDirective = (src: string): boolean => src.startsWith('v-')
3232
* @param tag string
3333
* @returns boolean
3434
*/
35-
export const shouldTransformedToSlots = (tag: string) => !(tag.endsWith(FRAGMENT) || tag === KEEP_ALIVE);
35+
// if _Fragment is already imported, it will end with number
36+
export const shouldTransformedToSlots = (tag: string) => !(tag.match(RegExp(`^_?${FRAGMENT}\\d*$`)) || tag === KEEP_ALIVE);
3637

3738
/**
3839
* Check if a Node is a component

packages/babel-plugin-jsx/test/__snapshots__/snapshot.test.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`_Fragment already imported: _Fragment already imported 1`] = `
4+
"import { createVNode as _createVNode, createTextVNode as _createTextVNode, Fragment as _Fragment2 } from \\"vue\\";
5+
import { Fragment as _Fragment } from 'vue';
6+
7+
const Root1 = () => _createVNode(_Fragment2, null, [_createTextVNode(\\"root1\\")]);
8+
9+
const Root2 = () => _createVNode(_Fragment, null, [_createTextVNode(\\"root2\\")]);"
10+
`;
11+
312
exports[`MereProps Order: MereProps Order 1`] = `
413
"import { createVNode as _createVNode, mergeProps as _mergeProps, createTextVNode as _createTextVNode } from \\"vue\\";
514

packages/babel-plugin-jsx/test/snapshot.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,22 @@ isCustomElementTests.forEach(({name, from }) => {
317317
expect(await transpile(from, { isCustomElement: tag => tag === 'foo' })).toMatchSnapshot(name);
318318
}
319319
)
320-
})
320+
})
321+
322+
const fragmentTests = [{
323+
name: '_Fragment already imported',
324+
from: `
325+
import { Fragment as _Fragment } from 'vue'
326+
const Root1 = () => <>root1</>
327+
const Root2 = () => <_Fragment>root2</_Fragment>
328+
`
329+
}];
330+
331+
fragmentTests.forEach(({ name, from}) => {
332+
test(
333+
name,
334+
async () => {
335+
expect(await transpile(from)).toMatchSnapshot(name);
336+
},
337+
);
338+
});

0 commit comments

Comments
 (0)