Skip to content

Prevent JSX pragma detection in transform-typescript from leaking between files - #9798

Merged
nicolo-ribaudo merged 4 commits into
babel:masterfrom
SamyPesse:fix/ts-jsx-pragma
Apr 15, 2019
Merged

nicolo-ribaudo merged 4 commits into
babel:masterfrom
SamyPesse:fix/ts-jsx-pragma

Conversation

@SamyPesse

@SamyPesse SamyPesse commented Mar 30, 2019 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues?
Patch: Bug Fix? Bug Fix
Major: Breaking Change? No
Minor: New Feature? No
Tests Added + Pass? No test added
Documentation PR Link
Any Dependency Changes? No
License MIT

This PR fixes a bug in support of JSX pragma in Typescript (introduced in #9095).

We've noticed this bug while building a large codebase with webpack+babel+typescript. Some files from our projects are using /** @jsx h*/ to use a custom function for JSX syntax; and some others are using the React syntax.

We've noticed that our react imports were being removed by babel because the plugin instance was being reused by babel-loader and webpack; and it was causing an error :

ReferenceError: React is not defined

This PR resets the jsxPragma being used to the initial value for each new file being processed.

I can't add a unit test for this bug because it requires processing 2 files (one with /** @jsx something */ then one with the react syntax).

I've tested a build of this PR and it correctly fixes the issue for us.

@babel-bot

babel-bot commented Mar 30, 2019 •

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/10640/

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the currentJsxPragma variable is file-specific, I think that it sould be better to store it in the file object:

const PRAGMA_KEY = "@babel/plugin-transform-typescript/jsxPragma";
file.set(PRAGMA_KEY, jsxMatches[1]);

// ...

file.get(PRAGMA_KEY);

@SamyPesse

Copy link
Copy Markdown
Contributor Author

@nicolo-ribaudo Thank you for the review, I'm quite new to the babel internals, I'll make the changes right now :)

@SamyPesse

Copy link
Copy Markdown
Contributor Author

I've made the changes, and tested it on our application (it works fine) 🚀

I'm just wondering what is the signature of file.get, I did file.get(key) || defaultValue since the JSX pragma should never be an empty string.

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering what is the signature of file.get

It's the same as Map#get


Since with out current testing infrastructure it's not really possible to test transpilation of multiple files, I'm ok with this PR even if it is without tests.

@Retsam

Retsam commented Apr 12, 2019

Copy link
Copy Markdown
Contributor

Looks like this would fix #9549.

@nicolo-ribaudo
nicolo-ribaudo merged commit 66be4aa into babel:master Apr 15, 2019
@lock lock Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 4, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Oct 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants