fix: wrong compilation result when _Fragment is imported (#518)

* fix: optimize Fragment judgement

Co-authored-by: Amour1688 <lcz141186@gmail.com>
This commit is contained in:
Luo Zhihao 2021-10-15 10:37:19 +08:00 committed by GitHub
parent 465d62928d
commit 35780fd7d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 2 deletions

View File

@ -32,7 +32,8 @@ export const isDirective = (src: string): boolean => src.startsWith('v-')
* @param tag string
* @returns boolean
*/
export const shouldTransformedToSlots = (tag: string) => !(tag.endsWith(FRAGMENT) || tag === KEEP_ALIVE);
// if _Fragment is already imported, it will end with number
export const shouldTransformedToSlots = (tag: string) => !(tag.match(RegExp(`^_?${FRAGMENT}\\d*$`)) || tag === KEEP_ALIVE);
/**
* Check if a Node is a component

View File

@ -1,5 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`_Fragment already imported: _Fragment already imported 1`] = `
"import { createVNode as _createVNode, createTextVNode as _createTextVNode, Fragment as _Fragment2 } from \\"vue\\";
import { Fragment as _Fragment } from 'vue';
const Root1 = () => _createVNode(_Fragment2, null, [_createTextVNode(\\"root1\\")]);
const Root2 = () => _createVNode(_Fragment, null, [_createTextVNode(\\"root2\\")]);"
`;
exports[`MereProps Order: MereProps Order 1`] = `
"import { createVNode as _createVNode, mergeProps as _mergeProps, createTextVNode as _createTextVNode } from \\"vue\\";

View File

@ -317,4 +317,22 @@ isCustomElementTests.forEach(({name, from }) => {
expect(await transpile(from, { isCustomElement: tag => tag === 'foo' })).toMatchSnapshot(name);
}
)
})
})
const fragmentTests = [{
name: '_Fragment already imported',
from: `
import { Fragment as _Fragment } from 'vue'
const Root1 = () => <>root1</>
const Root2 = () => <_Fragment>root2</_Fragment>
`
}];
fragmentTests.forEach(({ name, from}) => {
test(
name,
async () => {
expect(await transpile(from)).toMatchSnapshot(name);
},
);
});