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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Babel produces invalid TypeScript code if comment follows type annotation and is not stripped #14751

Closed
1 task
marcins opened this issue Jul 13, 2022 · 5 comments 路 Fixed by #14758 or #14762
Closed
1 task
Labels
area: typescript i: bug i: discussion outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator

Comments

@marcins
Copy link

marcins commented Jul 13, 2022

馃捇

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

export default (): void /* hi! */ => {};

Configuration file name

babel.config.json

Configuration

{}

Current and expected behavior

Using this test harness:

const babel = require('@babel/core');
const result = babel.transformSync("export default (): void /* hi! */ => {};", {
    filename: './source.ts',
    parserOpts: {
        plugins: ["typescript"],
    },
});
console.log(result.code);

The resulting code produced by Babel looks like this:

export default ((): void
/* hi! */
=> {});

This is invalid TypeScript/JavaScript as the arrow function is not allowed to have a line terminator preceding it.

Removing the comment from the source produces working output:

export default ((): void => {});

Environment

System:
  OS: macOS 11.6.7
Binaries:
  Node: 16.15.1
  Yarn: 1.22.10
  npm: 8.11.0
npmPackages:
  @babel/core: ^7.18.6 => 7.18.6 

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @marcins! 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.

@mischnic
Copy link
Contributor

Where it goes wrong:

this.print(node.returnType, node);

this._printTrailingComments(node);

if (printNewLines) this.newline(1);

// Add a newline before and after a block comment, unless explicitly
// disallowed
const printNewLines =
isBlockComment && !skipNewLines && !this._noLineTerminator;

And printNewLines is true in this case. So that "explicitly disallowed" is apparently missing a case (but AFAICT the only special case currently are pure comments)

@marcins
Copy link
Author

marcins commented Jul 14, 2022

For what it's worth, our current workaround for this issue is to add this simple Babel plugin to our config:

module.exports = () => ({
  visitor: {
    ArrowFunctionExpression({ node }) {
      if (node.returnType != null) {
        node.returnType.trailingComments = [];
      }
    },
  },
});

@liuxingbaoyu
Copy link
Member

#14758

@marcins
Copy link
Author

marcins commented Jul 14, 2022

Note that we found additional places where this is a problem, not just with arrow functions, the other particular case we had looked like this:

const foo: any /* Hi! */[] = [];

Yes, the comment is in the middle of the type definition (just to explain the use case, in our actual codebase the comment describes the expected type of any but due to issues can't be typed that way). This is valid TS, and the type of foo is any[], however as per the arrow functions above Babel will generate this invalid code:

const foo: any
/* Hi! */
[] = [];

Where the error is 'const' declarations must be initialized.

I guess broadly this issue will occur anywhere a block comment occurs after or in the middle of a type declaration.

@liuxingbaoyu liuxingbaoyu reopened this Jul 15, 2022
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 18, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript i: bug i: discussion outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator
Projects
None yet
5 participants