Skip to content
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

TypeScript JSX Fragment removes React import #7979

Closed
jtmthf opened this issue May 18, 2018 · 5 comments · Fixed by #7990 or #7996
Closed

TypeScript JSX Fragment removes React import #7979

jtmthf opened this issue May 18, 2018 · 5 comments · Fixed by #7990 or #7996
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@jtmthf
Copy link

jtmthf commented May 18, 2018

Bug Report

Current Behavior
Currently whenever I'm using "@babel/preset-typescript" with JSX fragments and no other JSX, the React import is removed from the compiled output. This only happens with JSX fragments. If you use React.Fragment instead, the compiled output is correct. Also worth noting that any other non-fragment jsx in the source will cause the React import to be maintained.

Input Code

import React, { SFC } from "react";

export const BreadcrumbItem: SFC = ({ children }) => <>{children}</>;

Expected code

import React from "react";
export var BreadcrumbItem = function BreadcrumbItem(_ref) {
  var children = _ref.children;
  return React.createElement(React.Fragment, null, children);
};

Actual code

export var BreadcrumbItem = function BreadcrumbItem(_ref) {
  var children = _ref.children;
  return React.createElement(React.Fragment, null, children);
};

Babel Configuration (.babelrc, package.json, cli command)

{
  "presets": [
    "@babel/preset-typescript",
    "@babel/preset-react",
    [
      "@babel/preset-env",
      {
        "modules": false,
        "targets": {
          "browsers": "ie 11"
        },
        "useBuiltIns": "entry"
      }
    ]
  ],
  "plugins": [
    ["emotion", { "sourceMap": true, "autoLabel": true }],
    "@babel/proposal-class-properties",
    "@babel/proposal-object-rest-spread"
  ]
}

Environment

  • Babel version(s): v^7.0.0-beta.47
  • Node/npm version: v8.9.4
  • OS: Windows 10
  • Monorepo no
  • How you are using Babel: cli & loader

Additional context/Screenshots

I have determined this issue to only happen when when "@babel/preset-typescript" is included. Removing any number of other plugins and presets does not fix issue. Additionally I've tried the following variations of the source and they all had the same incorrect output.

import React from "react";
import { SFC } from "react";

export const BreadcrumbItem: SFC = ({ children }) => <>{children}</>;
import * as React from "react";
import { SFC } from "react";

export const BreadcrumbItem: SFC = ({ children }) => <>{children}</>;
import React from "react";

export const BreadcrumbItem = ({ children }) => <>{children}</>;
@babel-bot
Copy link
Collaborator

Hey @jtmthf! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@strothj
Copy link

strothj commented May 18, 2018

I can confirm, though fortunately this situation is pretty rare.

@malbernaz
Copy link
Contributor

malbernaz commented May 21, 2018

#7990 solves this issue. Hopefully

@jtmthf
Copy link
Author

jtmthf commented May 21, 2018

I'm afraid #7990 isn't fixing the issue at least according to what I can see in the REPL. Here is my link

@malbernaz
Copy link
Contributor

Humm... It seems to be related to the fact that plugin-transform-typescript checks for type JSXElement but not to JSXFragment.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 20, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Aug 20, 2018
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
4 participants