Skip to content

fix(35179): formatter incorrectly remove spaces #35979

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
merged 1 commit into from
Jan 3, 2020
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
23 changes: 21 additions & 2 deletions src/services/formatting/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ namespace ts.formatting {
rule("NoSpaceAfterUnaryPrefixOperator", unaryPrefixOperators, unaryPrefixExpressions, [isNonJsxSameLineTokenContext, isNotBinaryOpContext], RuleAction.DeleteSpace),
rule("NoSpaceAfterUnaryPreincrementOperator", SyntaxKind.PlusPlusToken, unaryPreincrementExpressions, [isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
rule("NoSpaceAfterUnaryPredecrementOperator", SyntaxKind.MinusMinusToken, unaryPredecrementExpressions, [isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
rule("NoSpaceBeforeUnaryPostincrementOperator", unaryPostincrementExpressions, SyntaxKind.PlusPlusToken, [isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
rule("NoSpaceBeforeUnaryPostdecrementOperator", unaryPostdecrementExpressions, SyntaxKind.MinusMinusToken, [isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
rule("NoSpaceBeforeUnaryPostincrementOperator", unaryPostincrementExpressions, SyntaxKind.PlusPlusToken, [isNonJsxSameLineTokenContext, isNotStatementConditionContext], RuleAction.DeleteSpace),
rule("NoSpaceBeforeUnaryPostdecrementOperator", unaryPostdecrementExpressions, SyntaxKind.MinusMinusToken, [isNonJsxSameLineTokenContext, isNotStatementConditionContext], RuleAction.DeleteSpace),

// More unary operator special-casing.
// DevDiv 181814: Be careful when removing leading whitespace
Expand Down Expand Up @@ -790,6 +790,25 @@ namespace ts.formatting {
return context.contextNode.kind === SyntaxKind.NonNullExpression;
}

function isNotStatementConditionContext(context: FormattingContext): boolean {
return !isStatementConditionContext(context);
}

function isStatementConditionContext(context: FormattingContext): boolean {
switch (context.contextNode.kind) {
case SyntaxKind.IfStatement:
case SyntaxKind.ForStatement:
case SyntaxKind.ForInStatement:
case SyntaxKind.ForOfStatement:
case SyntaxKind.DoStatement:
case SyntaxKind.WhileStatement:
return true;

default:
return false;
}
}

function isSemicolonDeletionContext(context: FormattingContext): boolean {
let nextTokenKind = context.nextTokenSpan.kind;
let nextTokenStart = context.nextTokenSpan.pos;
Expand Down
49 changes: 49 additions & 0 deletions tests/cases/fourslash/spaceAfterStatementConditions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// <reference path="fourslash.ts"/>

////let i = 0;
////
////if(i<0) ++i;
////if(i<0) --i;
////
////while(i<0) ++i;
////while(i<0) --i;
////
////do ++i;
////while(i<0)
////do --i;
////while(i<0)
////
////for(let prop in { foo: 1 }) ++i;
////for(let prop in { foo: 1 }) --i;
////
////for(let foo of [1, 2]) ++i;
////for(let foo of [1, 2]) --i;
////
////for(let j = 0; j < 10; j++) ++i;
////for(let j = 0; j < 10; j++) --i;
////

format.document();
verify.currentFileContentIs(
`let i = 0;

if (i < 0) ++i;
if (i < 0) --i;

while (i < 0) ++i;
while (i < 0) --i;

do ++i;
while (i < 0)
do --i;
while (i < 0)

for (let prop in { foo: 1 }) ++i;
for (let prop in { foo: 1 }) --i;

for (let foo of [1, 2]) ++i;
for (let foo of [1, 2]) --i;

for (let j = 0; j < 10; j++) ++i;
for (let j = 0; j < 10; j++) --i;
`);