Skip to content

Ensure that emitter calls callbacks for empty blocks #18547

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 15 additions & 36 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1440,29 +1440,18 @@ namespace ts {
//

function emitBlock(node: Block) {
if (isSingleLineEmptyBlock(node)) {
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
write(" ");
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
}
else {
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
emitBlockStatements(node);
// We have to call emitLeadingComments explicitly here because otherwise leading comments of the close brace token will not be emitted
increaseIndent();
emitLeadingCommentsOfPosition(node.statements.end);
decreaseIndent();
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
}
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
emitBlockStatements(node, /*forceSingleLine*/ !node.multiLine && isEmptyBlock(node));
// We have to call emitLeadingComments explicitly here because otherwise leading comments of the close brace token will not be emitted
increaseIndent();
emitLeadingCommentsOfPosition(node.statements.end);
decreaseIndent();
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
}

function emitBlockStatements(node: BlockLike) {
if (getEmitFlags(node) & EmitFlags.SingleLine) {
emitList(node, node.statements, ListFormat.SingleLineBlockStatements);
}
else {
emitList(node, node.statements, ListFormat.MultiLineBlockStatements);
}
function emitBlockStatements(node: BlockLike, forceSingleLine: boolean) {
const format = forceSingleLine || getEmitFlags(node) & EmitFlags.SingleLine ? ListFormat.SingleLineBlockStatements : ListFormat.MultiLineBlockStatements;
emitList(node, node.statements, format);
}

function emitVariableStatement(node: VariableStatement) {
Expand Down Expand Up @@ -1889,16 +1878,11 @@ namespace ts {
}

function emitModuleBlock(node: ModuleBlock) {
if (isEmptyBlock(node)) {
write("{ }");
}
else {
pushNameGenerationScope();
write("{");
emitBlockStatements(node);
write("}");
popNameGenerationScope();
}
pushNameGenerationScope();
write("{");
emitBlockStatements(node, /*forceSingleLine*/ isEmptyBlock(node));
write("}");
popNameGenerationScope();
}

function emitCaseBlock(node: CaseBlock) {
Expand Down Expand Up @@ -2762,11 +2746,6 @@ namespace ts {
&& !rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile);
}

function isSingleLineEmptyBlock(block: Block) {
return !block.multiLine
&& isEmptyBlock(block);
}

function isEmptyBlock(block: BlockLike) {
return block.statements.length === 0
&& rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile);
Expand Down
20 changes: 20 additions & 0 deletions tests/cases/fourslash/convertToEs6Class_emptyCatchClause.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />

// @allowNonTsExtensions: true
// @Filename: /a.js
////function /**/MyClass() {}
////MyClass.prototype.foo = function() {
//// try {} catch() {}
////}

verify.applicableRefactorAvailableAtMarker("");
verify.fileAfterApplyingRefactorAtMarker("",
`class MyClass {
constructor() { }
foo() {
try { }
catch () { }
}
}
`,
'Convert to ES2015 class', 'convert');
22 changes: 22 additions & 0 deletions tests/cases/fourslash/extract-method-empty-namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />

// TODO: GH#18546
// For now this tests that at least we don't crash.

////function f() {
//// /*start*/namespace N {}/*end*/
////}

goTo.select('start', 'end')
edit.applyRefactor({
refactorName: "Extract Method",
actionName: "scope_1",
actionDescription: "Extract to function in global scope",
newContent: `function f() {
/*RENAME*/newFunction(N);
}
function newFunction(N: any) {
namespace N { }
}
`
});