-
Notifications
You must be signed in to change notification settings - Fork 50.8k
[Fresh] Initial Babel plugin implementation #15711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e096875
Add initial Babel plugin implementation
gaearon b08fc6e
Register exported functions
gaearon a1c02b9
Fix missing declarations
gaearon 526f91a
Remove unused code
gaearon 850362d
Don't pass filename to tests
gaearon 3a925a4
Fix bugs
gaearon dcf6559
Coalesce variable declarations
gaearon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -12,12 +12,178 @@ let freshPlugin = require('react-fresh/babel'); | |
|
|
||
| function transform(input, options = {}) { | ||
| return babel.transform(input, { | ||
| plugins: [[freshPlugin]], | ||
| babelrc: false, | ||
| plugins: ['syntax-jsx', freshPlugin], | ||
| }).code; | ||
| } | ||
|
|
||
| describe('ReactFreshBabelPlugin', () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice tests. |
||
| it('hello world', () => { | ||
| expect(transform(`hello()`)).toMatchSnapshot(); | ||
| it('registers top-level function declarations', () => { | ||
| // Hello and Bar should be registered, handleClick shouldn't. | ||
| expect( | ||
| transform(` | ||
| function Hello() { | ||
| function handleClick() {} | ||
| return <h1 onClick={handleClick}>Hi</h1>; | ||
| } | ||
|
|
||
| function Bar() { | ||
| return <Hello />; | ||
| } | ||
| `), | ||
| ).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('registers top-level exported function declarations', () => { | ||
| expect( | ||
| transform(` | ||
| export function Hello() { | ||
| function handleClick() {} | ||
| return <h1 onClick={handleClick}>Hi</h1>; | ||
| } | ||
|
|
||
| export default function Bar() { | ||
| return <Hello />; | ||
| } | ||
|
|
||
| function Baz() { | ||
| return <h1>OK</h1>; | ||
| } | ||
|
|
||
| const NotAComp = 'hi'; | ||
| export { Baz, NotAComp }; | ||
|
|
||
| export function sum() {} | ||
| export const Bad = 42; | ||
| `), | ||
| ).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('registers top-level exported named arrow functions', () => { | ||
| expect( | ||
| transform(` | ||
| export const Hello = () => { | ||
| function handleClick() {} | ||
| return <h1 onClick={handleClick}>Hi</h1>; | ||
| }; | ||
|
|
||
| export let Bar = (props) => <Hello />; | ||
|
|
||
| export default () => { | ||
| // This one should be ignored. | ||
| // You should name your components. | ||
| return <Hello />; | ||
| }; | ||
| `), | ||
| ).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('uses original function declaration if it get reassigned', () => { | ||
| // This should register the original version. | ||
| // TODO: in the future, we may *also* register the wrapped one. | ||
| expect( | ||
| transform(` | ||
| function Hello() { | ||
| return <h1>Hi</h1>; | ||
| } | ||
| Hello = connect(Hello); | ||
| `), | ||
| ).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('only registers pascal case functions', () => { | ||
| // Should not get registered. | ||
| expect( | ||
| transform(` | ||
| function hello() { | ||
| return 2 * 2; | ||
| } | ||
| `), | ||
| ).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('registers top-level variable declarations with function expressions', () => { | ||
| // Hello and Bar should be registered; handleClick, sum, Baz, and Qux shouldn't. | ||
| expect( | ||
| transform(` | ||
| let Hello = function() { | ||
| function handleClick() {} | ||
| return <h1 onClick={handleClick}>Hi</h1>; | ||
| }; | ||
| const Bar = function Baz() { | ||
| return <Hello />; | ||
| }; | ||
| function sum() {} | ||
| let Baz = 10; | ||
| var Qux; | ||
| `), | ||
| ).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('registers top-level variable declarations with arrow functions', () => { | ||
| // Hello, Bar, and Baz should be registered; handleClick and sum shouldn't. | ||
| expect( | ||
| transform(` | ||
| let Hello = () => { | ||
| const handleClick = () => {}; | ||
| return <h1 onClick={handleClick}>Hi</h1>; | ||
| } | ||
| const Bar = () => { | ||
| return <Hello />; | ||
| }; | ||
| var Baz = () => <div />; | ||
| var sum = () => {}; | ||
| `), | ||
| ).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('ignores HOC definitions', () => { | ||
| // TODO: we might want to handle HOCs at usage site, however. | ||
| // TODO: it would be nice if we could always avoid registering | ||
| // a function that is known to return a function or other non-node. | ||
| expect( | ||
| transform(` | ||
| let connect = () => { | ||
| function Comp() { | ||
| const handleClick = () => {}; | ||
| return <h1 onClick={handleClick}>Hi</h1>; | ||
| } | ||
| return Comp; | ||
| }; | ||
| function withRouter() { | ||
| return function Child() { | ||
| const handleClick = () => {}; | ||
| return <h1 onClick={handleClick}>Hi</h1>; | ||
| } | ||
| }; | ||
| `), | ||
| ).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('ignores complex definitions', () => { | ||
| expect( | ||
| transform(` | ||
| let A = foo ? () => { | ||
| return <h1>Hi</h1>; | ||
| } : null | ||
| const B = (function Foo() { | ||
| return <h1>Hi</h1>; | ||
| })(); | ||
| let C = () => () => { | ||
| return <h1>Hi</h1>; | ||
| }; | ||
| let D = bar && (() => { | ||
| return <h1>Hi</h1>; | ||
| }); | ||
| `), | ||
| ).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('ignores unnamed function declarations', () => { | ||
| expect( | ||
| transform(` | ||
| export default function() {} | ||
| `), | ||
| ).toMatchSnapshot(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆