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

// comments in anonymous functions break React code #11304

Closed
jakub-gonet opened this issue Mar 21, 2020 · 2 comments · Fixed by #11306
Closed

// comments in anonymous functions break React code #11304

jakub-gonet opened this issue Mar 21, 2020 · 2 comments · Fixed by #11306
Assignees
Labels
Has PR i: bug i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator

Comments

@jakub-gonet
Copy link

jakub-gonet commented Mar 21, 2020

I'm submitting a bug report

Webpack Version:
4.42.0

Babel Core Version:
7.9.0

Babel Loader Version:
8.1.0

Please tell us about your environment:
OSX 10.14

Current behavior:
After running yarn build in create-react-app following code transpiles to wrong JS code:

import React from "react";

function App() {
  return (
    <div className="App">
      <A
        render={() => (
          //test
          <B />
        )}
      />
    </div>
  );
}

const A = ({ render }) => {
  return <>{render()}</>;
};

const B = () => {
  return <p>bbbbbbb</p>;
};

export default App;

Transpiled and packed:

function App(){return/*#__PURE__*/react_default.a.createElement("div",{className:"App"},/*#__PURE__*/react_default.a.createElement(App_A,{render:function render(){return/*#__PURE__*/ //test <-----------------------
react_default.a.createElement(App_B,null);}}));}var App_A=function A(_ref){var render=_ref.render;return/*#__PURE__*/react_default.a.createElement(react_default.a.Fragment,null,render());};var App_B=function B(){return/*#__PURE__*/react_default.a.createElement("p",null,"bbbbbbb");};/* harmony default export */ var src_App = (App);

(arrow added)
render() has dangling return (line break after comment), so JS adds semicolon there - function returns undefined.

When setting compact: false in loader options this behavior isn't observed

Expected/desired behavior:

babel-loader should keep parens in arrow functions, following code is valid JS

x = () => (
// TEST
5
);

Run yarn build && serve -s build (assuming serve is installed)

When changing compact to false in webpack.config.js#351 it works correctly

EDIT:

function App() {
  return (
    <div className="App">
      {(() => (
        //test
        <span>test</span>
      ))()}
    </div>
  );
}

also reproduces this issue.

@nicolo-ribaudo nicolo-ribaudo transferred this issue from babel/babel-loader Mar 21, 2020
@babel-bot
Copy link
Collaborator

Hey @jakub-gonet! 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."

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Mar 21, 2020

Simpler reproduction:

const code = `
function f() {
  return (
    /*#__PURE__*/
    //test
    0
  );
}
`;

const out = generator(parser.parse(code), {
  compact: true,
});

console.log(out.code);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Has PR i: bug i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants