Skip to content

Retain JSX pragma if defined as a comment - #9095

Merged
existentialism merged 1 commit into
babel:masterfrom
dstaley:retain-jsx-pragma
Jan 26, 2019
Merged

existentialism merged 1 commit into
babel:masterfrom
dstaley:retain-jsx-pragma

Conversation

@dstaley

@dstaley dstaley commented Nov 28, 2018

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

Currently, the TypeScript transform elides JSX pragma imports unless used explicitly. This PR modifies the transform to properly detect the JSX pragma declaration when defined as a comment, and appropriately retains imports if the import matches the pragma. The check was added by copying over the pragma detection from babel-plugin-transform-react-jsx.

@dstaley dstaley changed the title retain jsx pragma if defined as a comment Retain JSX pragma if defined as a comment Nov 28, 2018
@babel-bot

Copy link
Copy Markdown
Collaborator

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

@renchap

renchap commented Dec 5, 2018

Copy link
Copy Markdown

👍 This is needed to get the new Emotion 10 CSS Prop working when using Typescript with Babel.
Is it anything I can do to help get this merged?

@existentialism existentialism added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Dec 7, 2018
@maxmurdoch

maxmurdoch commented Dec 9, 2018 •

Copy link
Copy Markdown

Thanks so much for doing this @dstaley and @renchap 👏

@chibicode

Copy link
Copy Markdown

Until this is merged, I'm doing:

  /** @jsx jsx */
  import { css, jsx } from '@emotion/core'
+ export const jsxBabelFix = jsx

To prevent linters from complaining 👍

@nicolo-ribaudo

Copy link
Copy Markdown
Member

Isn't it enough to use the jsxPragma option?

@chibicode

Copy link
Copy Markdown

@nicolo-ribaudo

@jfrolich says: #8958 (comment)

This can be fixed by setting the jsxPragma option in the typescript transform. However now React is removed when Fragments are used.

@nicolo-ribaudo

Copy link
Copy Markdown
Member

With this PR parsing the comment has exactly the same behavior of using the jsxPragma option.

@dstaley

dstaley commented Dec 13, 2018

Copy link
Copy Markdown
Contributor Author

@nicolo-ribaudo The goal here is to allow the use of different pragmas for different files, which mirrors the behavior of Babel in non-TypeScript projects.

@kevinmarrec

kevinmarrec commented Dec 13, 2018 •

Copy link
Copy Markdown

@dstaley Is there a way that you update within this PR the @babel/preset-typescript to use the last version of @babel/core and @babel/plugin-transform-typescript ?

The TypeScript preset seems not being maintained (last publish 3 months ago)

(or maybe should I make a side PR, to keep things clear, the thing is that your PR should result in a newer version of @babel/plugin-transform-typescript, so idk)

@dstaley

dstaley commented Dec 13, 2018

Copy link
Copy Markdown
Contributor Author

@kevinmarrec yeah it's probably best to keep those separate since there might be a reason the presentation hasn't been updated.

@miketdonahue

Copy link
Copy Markdown

Any update on getting this merge in? @dstaley - Some of us Emotion users are pretty stuck right now.

@aaronjensen

Copy link
Copy Markdown
Contributor

@Meebix I'm not stuck anymore. I'm using https://emotion.sh/docs/@emotion/babel-preset-css-prop and it works just fine. One thing to note is that I had to make sure that @babel/preset-env was the first preset in my list.

I combined it w/ a custom plugin to handle fragments:

// With emotion 10 and @emotion/babel-preset-css-prop, the only reason to import
// React is to get Fragment syntax. This plugin will automatically add a React
// import if a fragment is found so we don't have to remember to import React.
module.exports = function importReactForFragments(babel) {
  const { types: t } = babel

  const getFragmentImport = () =>
    t.importDeclaration(
      [t.importSpecifier(t.identifier('React'), t.identifier('default'))],
      t.stringLiteral('react')
    )

  const detectReactImport = (path, state) => {
    if (path.node.local.name === 'React') {
      state.set('jsxFragmentImportDetected', true)
    }
  }

  return {
    visitor: {
      Program: {
        exit(path, state) {
          if (!state.get('jsxFragmentDetected')) return
          if (state.get('jsxFragmentImportDetected')) return
          path.unshiftContainer('body', getFragmentImport())
        },
      },
      ImportNamespaceSpecifier: detectReactImport,
      ImportDefaultSpecifier: detectReactImport,
      ImportSpecifier: detectReactImport,
      JSXFragment(path, state) {
        state.set('jsxFragmentDetected', true)
      },
    },
  }
}

@miketdonahue

Copy link
Copy Markdown

Ah, ok cool @aaronjensen - thanks for the details!

@jfrolich

jfrolich commented Jan 4, 2019

Copy link
Copy Markdown

It's still a hack/workaround. Would be great if this can be resolved.

@renchap

renchap commented Jan 26, 2019

Copy link
Copy Markdown

@nicolo-ribaudo is it anything needed to get this merged? This is very much needed for using the latest Emotion version with Typescript & Babel without using hacks.

@existentialism
existentialism merged commit 03230ea into babel:master Jan 26, 2019
nicolo-ribaudo pushed a commit that referenced this pull request Feb 15, 2019
…pescript@^7.3.2 (#9497) [skip ci]

Bumps to 7.3.2 the version of `@babel/plugin-transform-typescript` used by `@babel/preset-typescript`.

This allows us to use `@babel/preset-typescript` and pick up at least one useful bug fix (#9095).

A similar PR (#9181) was closed as it bumped to a version that didn't have changes. However reading the comments from that PR, I get the sense that bumping the version in this manner should not be necessary, and should be handled by Lerna. Is this correct?

Please let me know if I can make any improvements to this PR, the description or if there's anything else I can do to help.
nicolo-ribaudo pushed a commit that referenced this pull request Apr 15, 2019
…ween files (#9798)

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.
@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

area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Import used by @jsx pragma is elided by typescript transform