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
Comments
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. |
Where it goes wrong:
babel/packages/babel-generator/src/printer.ts Line 561 in bcf8b22
babel/packages/babel-generator/src/printer.ts Line 811 in bcf8b22
babel/packages/babel-generator/src/printer.ts Lines 763 to 766 in bcf8b22
And |
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 = [];
}
},
},
}); |
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 const foo: any
/* Hi! */
[] = []; Where the error is I guess broadly this issue will occur anywhere a block comment occurs after or in the middle of a type declaration. |
馃捇
How are you using Babel?
Programmatic API (
babel.transform
,babel.parse
)Input code
Configuration file name
babel.config.json
Configuration
Current and expected behavior
Using this test harness:
The resulting code produced by Babel looks like this:
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:
Environment
Possible solution
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: