-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(plugin-react): fix React is not defined when component name is lo…
…wercase add test
- Loading branch information
yulong.xiang
committed
Feb 10, 2022
1 parent
5c9c812
commit 3da9a28
Showing
2 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { restoreJSX } from './restore-jsx' | ||
import * as babel from '@babel/core' | ||
|
||
async function jsx(sourceCode: string) { | ||
const [ast] = await restoreJSX(babel, sourceCode, 'test.js') | ||
if (ast === null) { | ||
return ast | ||
} | ||
const { code } = await babel.transformFromAstAsync(ast, null, { | ||
configFile: false | ||
}) | ||
return code | ||
} | ||
// jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; | ||
// React__default.createElement(Foo)`) | ||
// Tests adapted from: https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx/blob/63137b6/test/index.js | ||
describe('restore-jsx', () => { | ||
it('should trans to ', async () => { | ||
expect( | ||
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; | ||
React__default.createElement(foo)`) | ||
).toBeNull() | ||
expect( | ||
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; | ||
React__default.createElement("h1")`) | ||
).toMatch(`<h1 />;`) | ||
expect( | ||
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; | ||
React__default.createElement(Foo)`) | ||
).toMatch(`<Foo />;`) | ||
expect( | ||
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; | ||
React__default.createElement(Foo.Bar)`) | ||
).toMatch(`<Foo.Bar />;`) | ||
expect( | ||
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; | ||
React__default.createElement(Foo.Bar.Baz)`) | ||
).toMatch(`<Foo.Bar.Baz />;`) | ||
}) | ||
|
||
it('should handle props', async () => { | ||
expect( | ||
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; | ||
React__default.createElement(foo, {hi: there})`) | ||
).toBeNull() | ||
expect( | ||
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; | ||
React__default.createElement("h1", {hi: there})`) | ||
).toMatch(`"<h1 hi={there} />;"`) | ||
expect( | ||
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; | ||
React__default.createElement(Foo, {hi: there})`) | ||
).toMatch(`<Foo hi={there} />;`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters