fix: Types containing comments generate invalid code#14762
fix: Types containing comments generate invalid code#14762nicolo-ribaudo merged 2 commits intobabel:mainfrom
Conversation
|
|
||
| if (node.returnType) { | ||
| if (node.type === "ArrowFunctionExpression") { | ||
| this._noLineTerminator = true; | ||
| this.print(node.returnType, node); | ||
| this._noLineTerminator = false; | ||
| } else { | ||
| this.print(node.returnType, node); | ||
| } | ||
| } | ||
| this.print(node.returnType, node); | ||
| } |
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/52517/ |
nicolo-ribaudo
left a comment
There was a problem hiding this comment.
I think it would be enough to disallow a line terminator in the following positions:
- After the
returnTypeof anArrowFunctionExpression - After the
typeNameof aTSTypeReferencewithparams - After the
elementTypeof aTSArrayType - After the
objectTypeof aTSIndexedAccessType
Also, could you add these generator tests with retainLines: true?
type T = U.
/* 1 */
C /* 2 */ [
/* 3 */
0];type T = U.
/* 1 */
C /* 2 */ [
/* 3 */
];type T = U.
/* 1 */
C /* 2 */ <
/* 3 */
0>;let f = (x)
/* 1 */
:
/* 2 */
void /* 3 */ => 1|
There seem to be two ways, one is like #14758 and the other is using |
|
I prefer the #14758 approach, since it lets us get a "better" output where we can print newlines. The flow rules are the same, except that the AST nodes are |
|
|
||
| type T2 = U. | ||
| /* 1 */ | ||
| C /* 2 */[]; | ||
|
|
||
|
|
There was a problem hiding this comment.
There seems to be a bug here.
update: maybe not
There was a problem hiding this comment.
Yes, the /* 3 */ comment should be printed somewhere. It's a bug that we already have regardless of this PR.
TypeAnnotation with comments generates incorrect code
This PR also contains some very minor performance optimizations.