Skip to content

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

@marcins

Description

@marcins

💻

  • 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions