Skip to content

Commit 79e12eb

Browse files
author
Andy
authored
Ensure that emitter calls callbacks for empty blocks (#18547)
1 parent 0ac8406 commit 79e12eb

File tree

3 files changed

+57
-36
lines changed

3 files changed

+57
-36
lines changed

src/compiler/emitter.ts

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,29 +1440,18 @@ namespace ts {
14401440
//
14411441

14421442
function emitBlock(node: Block) {
1443-
if (isSingleLineEmptyBlock(node)) {
1444-
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
1445-
write(" ");
1446-
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
1447-
}
1448-
else {
1449-
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
1450-
emitBlockStatements(node);
1451-
// We have to call emitLeadingComments explicitly here because otherwise leading comments of the close brace token will not be emitted
1452-
increaseIndent();
1453-
emitLeadingCommentsOfPosition(node.statements.end);
1454-
decreaseIndent();
1455-
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
1456-
}
1443+
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
1444+
emitBlockStatements(node, /*forceSingleLine*/ !node.multiLine && isEmptyBlock(node));
1445+
// We have to call emitLeadingComments explicitly here because otherwise leading comments of the close brace token will not be emitted
1446+
increaseIndent();
1447+
emitLeadingCommentsOfPosition(node.statements.end);
1448+
decreaseIndent();
1449+
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
14571450
}
14581451

1459-
function emitBlockStatements(node: BlockLike) {
1460-
if (getEmitFlags(node) & EmitFlags.SingleLine) {
1461-
emitList(node, node.statements, ListFormat.SingleLineBlockStatements);
1462-
}
1463-
else {
1464-
emitList(node, node.statements, ListFormat.MultiLineBlockStatements);
1465-
}
1452+
function emitBlockStatements(node: BlockLike, forceSingleLine: boolean) {
1453+
const format = forceSingleLine || getEmitFlags(node) & EmitFlags.SingleLine ? ListFormat.SingleLineBlockStatements : ListFormat.MultiLineBlockStatements;
1454+
emitList(node, node.statements, format);
14661455
}
14671456

14681457
function emitVariableStatement(node: VariableStatement) {
@@ -1889,16 +1878,11 @@ namespace ts {
18891878
}
18901879

18911880
function emitModuleBlock(node: ModuleBlock) {
1892-
if (isEmptyBlock(node)) {
1893-
write("{ }");
1894-
}
1895-
else {
1896-
pushNameGenerationScope();
1897-
write("{");
1898-
emitBlockStatements(node);
1899-
write("}");
1900-
popNameGenerationScope();
1901-
}
1881+
pushNameGenerationScope();
1882+
write("{");
1883+
emitBlockStatements(node, /*forceSingleLine*/ isEmptyBlock(node));
1884+
write("}");
1885+
popNameGenerationScope();
19021886
}
19031887

19041888
function emitCaseBlock(node: CaseBlock) {
@@ -2762,11 +2746,6 @@ namespace ts {
27622746
&& !rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile);
27632747
}
27642748

2765-
function isSingleLineEmptyBlock(block: Block) {
2766-
return !block.multiLine
2767-
&& isEmptyBlock(block);
2768-
}
2769-
27702749
function isEmptyBlock(block: BlockLike) {
27712750
return block.statements.length === 0
27722751
&& rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowNonTsExtensions: true
4+
// @Filename: /a.js
5+
////function /**/MyClass() {}
6+
////MyClass.prototype.foo = function() {
7+
//// try {} catch() {}
8+
////}
9+
10+
verify.applicableRefactorAvailableAtMarker("");
11+
verify.fileAfterApplyingRefactorAtMarker("",
12+
`class MyClass {
13+
constructor() { }
14+
foo() {
15+
try { }
16+
catch () { }
17+
}
18+
}
19+
`,
20+
'Convert to ES2015 class', 'convert');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// TODO: GH#18546
4+
// For now this tests that at least we don't crash.
5+
6+
////function f() {
7+
//// /*start*/namespace N {}/*end*/
8+
////}
9+
10+
goTo.select('start', 'end')
11+
edit.applyRefactor({
12+
refactorName: "Extract Method",
13+
actionName: "scope_1",
14+
actionDescription: "Extract to function in global scope",
15+
newContent: `function f() {
16+
/*RENAME*/newFunction(N);
17+
}
18+
function newFunction(N: any) {
19+
namespace N { }
20+
}
21+
`
22+
});

0 commit comments

Comments
 (0)