-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Make Babel import JSX pragma plugin aware of wp.element.createElement
#13809
Changes from all commits
5d86909
0d2c51a
14341ac
6961bb9
2f3986b
f8793f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,11 +35,18 @@ describe( 'babel-plugin-import-jsx-pragma', () => { | |
expect( string ).toBe( original ); | ||
} ); | ||
|
||
it( 'does nothing if the scope variable is already defined', () => { | ||
const original = 'const React = require("react");\n\nlet foo = <bar />;'; | ||
const string = getTransformedCode( original ); | ||
|
||
expect( string ).toBe( original ); | ||
} ); | ||
|
||
it( 'adds import for scope variable', () => { | ||
const original = 'let foo = <bar />;'; | ||
const string = getTransformedCode( original ); | ||
|
||
expect( string ).toBe( 'import React from "react";\nlet foo = <bar />;' ); | ||
expect( string ).toBe( 'import React from "react";\n' + original ); | ||
} ); | ||
|
||
it( 'allows options customization', () => { | ||
|
@@ -50,6 +57,35 @@ describe( 'babel-plugin-import-jsx-pragma', () => { | |
isDefault: false, | ||
} ); | ||
|
||
expect( string ).toBe( 'import { createElement } from "@wordpress/element";\nlet foo = <bar />;' ); | ||
expect( string ).toBe( 'import { createElement } from "@wordpress/element";\n' + original ); | ||
} ); | ||
|
||
it( 'adds import for scope variable even when defined inside the local scope', () => { | ||
const original = 'let foo = <bar />;\n\nfunction local() {\n const createElement = wp.element.createElement;\n}'; | ||
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. I had some issues with the formatting of the output when changes are applied, @aduth or @nerrad any recommendations on how to better address it? Are there any reasons we don't use babel-plugin-tester which is highlighted in Babel handbook here: https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/plugin-handbook.md#babel-plugin-tester? 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.
Sorry I don't follow, what issues were you having?
I can't give any reasons for why it's not being used, but it looks like it'd be helpful for writing babel plugin tests, so I wouldn't be opposed to it. 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.
I had to ensure that I include newline characters and spaces in the original snippet. In particular, concerning was |
||
const string = getTransformedCode( original, { | ||
scopeVariable: 'createElement', | ||
source: '@wordpress/element', | ||
isDefault: false, | ||
} ); | ||
|
||
expect( string ).toBe( 'import { createElement } from "@wordpress/element";\n' + original ); | ||
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. So does this mean that the plugin is resolving the string to something like this? import { createElement } from "@wordpress/element";
let foo = <bar />;
function local() {
const createElement = wp.element.createElement;
} Do we want that behaviour? 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. Actually nvm. I understand this is technically okay assuming this kind of script was implemented in an environment which maps the import to the global. It just sounds like its something we shouldn't automatically fix because this kind of thing would likely be found in an environment where there is no aliasing (and thus create a different kind of error (import not found) in the built file for the developer). 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. Well, I figured out that I took a very naive approach which won't work in a few cases. Your comment got me thinking what about IIFE which is a very common pattern in the WordPress world. For example: I think that the implementation I proposed checks only variable bindings in the very outer scope. |
||
} ); | ||
|
||
it( 'does nothing if the outer scope variable is already defined when using custom options', () => { | ||
const original = 'const {\n createElement\n} = wp.element;\nlet foo = <bar />;'; | ||
const string = getTransformedCode( original, { | ||
scopeVariable: 'createElement', | ||
} ); | ||
|
||
expect( string ).toBe( original ); | ||
} ); | ||
|
||
it( 'does nothing if the inner scope variable is already defined when using custom options', () => { | ||
const original = '(function () {\n const {\n createElement\n } = wp.element;\n let foo = <bar />;\n})();'; | ||
const string = getTransformedCode( original, { | ||
scopeVariable: 'createElement', | ||
} ); | ||
|
||
expect( string ).toBe( original ); | ||
} ); | ||
} ); |
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.
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.
I think you are right. In that sense, all code should work as before. It's definitely some kind of change but I probably went too far and I tried to be overcautious. I will update to
New Feature
and minor update.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.
To be honest, it's hard to decide, you could also consider it as a bug fix :)
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.
It was never updated? I think enhancement makes sense, yes.
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.
Hmm, something went wrong during rebase... I also overrode your commit. I will open a follow-up PR. Thanks for catching 👍
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.
#14106 opened to fix everything, sorry about that, I still don't know how it has happened ... :(