TypeScript Version: 2.8.3
Search Terms: automatic semicolon insertion
Code
Imagine you have a TypeScript snippet like so:
const foo = () =>
// Print "bar"
console.log('bar');
If a transformer wants to modify the // Print "bar" comment, it must synthesize the comment in front of console.log. My understanding is that to do so, you set the text range of the CallExpr (console.log...) to -1/-1 or set NoLeadingComments on it, and then create a synthetic leading comment on the CallExpr using ts.setSyntheticLeadingComments(...).
When you do that and emit to ES5, TypeScript later on inserts a return statement, and sets the text range of the return statement to match the CallExpr's. That's fine so far, but when the synthetic comment still gets emitted between the return and the expression:
var foo = function () {
return
// Print "bar"
console.log('bar');
};
This causes automatic semicolon insertion, breaking the code above.
I believe TS should move synthetic leading comments onto the returnStatement it creates in es2015.ts in transformFunctionBody when inserting the return.
Expected behavior:
var foo = function () {
// Print "bar"
return
console.log('bar');
};
Actual behavior:
var foo = function () {
return
// Print "bar"
console.log('bar');
};
Playground Link: N/A, need a transformer that synthesizes the comment.
TypeScript Version: 2.8.3
Search Terms: automatic semicolon insertion
Code
Imagine you have a TypeScript snippet like so:
If a transformer wants to modify the
// Print "bar"comment, it must synthesize the comment in front ofconsole.log. My understanding is that to do so, you set the text range of theCallExpr(console.log...) to-1/-1or setNoLeadingCommentson it, and then create a synthetic leading comment on theCallExprusingts.setSyntheticLeadingComments(...).When you do that and emit to ES5, TypeScript later on inserts a
returnstatement, and sets the text range of the return statement to match theCallExpr's. That's fine so far, but when the synthetic comment still gets emitted between thereturnand the expression:This causes automatic semicolon insertion, breaking the code above.
I believe TS should move synthetic leading comments onto the
returnStatementit creates ines2015.tsintransformFunctionBodywhen inserting thereturn.Expected behavior:
Actual behavior:
Playground Link: N/A, need a transformer that synthesizes the comment.