mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 20:14:01 -06:00
* Ensure that emitter calls callbacks for empty blocks (#18547) * Fix action description
This commit is contained in:
parent
839145cd79
commit
387fc205d0
@ -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) {
|
||||
@ -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) {
|
||||
@ -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);
|
||||
|
||||
20
tests/cases/fourslash/convertToEs6Class_emptyCatchClause.ts
Normal file
20
tests/cases/fourslash/convertToEs6Class_emptyCatchClause.ts
Normal file
@ -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
tests/cases/fourslash/extract-method-empty-namespace.ts
Normal file
22
tests/cases/fourslash/extract-method-empty-namespace.ts
Normal file
@ -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 function into global scope",
|
||||
newContent: `function f() {
|
||||
/*RENAME*/newFunction(N);
|
||||
}
|
||||
function newFunction(N: any) {
|
||||
namespace N { }
|
||||
}
|
||||
`
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user