Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't leave space for property access on non-integer literals (#50703)
  • Loading branch information
jakebailey committed Sep 9, 2022
1 parent a70bb9d commit eb40134
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/services/formatting/rules.ts
Expand Up @@ -59,7 +59,7 @@ namespace ts.formatting {
// in other cases there should be no space between '?' and next token
rule("NoSpaceAfterQuestionMark", SyntaxKind.QuestionToken, anyToken, [isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),

rule("NoSpaceBeforeDot", anyToken, [SyntaxKind.DotToken, SyntaxKind.QuestionDotToken], [isNonJsxSameLineTokenContext, isNotPropertyAccessOnNumericLiteral], RuleAction.DeleteSpace),
rule("NoSpaceBeforeDot", anyToken, [SyntaxKind.DotToken, SyntaxKind.QuestionDotToken], [isNonJsxSameLineTokenContext, isNotPropertyAccessOnIntegerLiteral], RuleAction.DeleteSpace),
rule("NoSpaceAfterDot", [SyntaxKind.DotToken, SyntaxKind.QuestionDotToken], anyToken, [isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),

rule("NoSpaceBetweenImportParenInImportType", SyntaxKind.ImportKeyword, SyntaxKind.OpenParenToken, [isNonJsxSameLineTokenContext, isImportTypeContext], RuleAction.DeleteSpace),
Expand Down Expand Up @@ -894,7 +894,9 @@ namespace ts.formatting {
return positionIsASICandidate(context.currentTokenSpan.end, context.currentTokenParent, context.sourceFile);
}

function isNotPropertyAccessOnNumericLiteral(context: FormattingContext): boolean {
return !isPropertyAccessExpression(context.contextNode) || !isNumericLiteral(context.contextNode.expression);
function isNotPropertyAccessOnIntegerLiteral(context: FormattingContext): boolean {
return !isPropertyAccessExpression(context.contextNode)
|| !isNumericLiteral(context.contextNode.expression)
|| context.contextNode.expression.getText().indexOf(".") !== -1;
}
}
21 changes: 19 additions & 2 deletions tests/cases/fourslash/formatDotAfterNumber.ts
@@ -1,7 +1,24 @@
/// <reference path='fourslash.ts' />

////1+ 2 .toString() +3/**/
////1+ 2 .toString() +3/*1*/
////1+ 2. .toString() +3/*2*/
////1+ 2.0 .toString() +3/*3*/
////1+ (2) .toString() +3/*4*/
////1+ 2_000 .toString() +3/*5*/

format.document();
goTo.marker("");

goTo.marker("1");
verify.currentLineContentIs("1 + 2 .toString() + 3");

goTo.marker("2");
verify.currentLineContentIs("1 + 2..toString() + 3");

goTo.marker("3");
verify.currentLineContentIs("1 + 2.0.toString() + 3");

goTo.marker("4");
verify.currentLineContentIs("1 + (2).toString() + 3");

goTo.marker("5");
verify.currentLineContentIs("1 + 2_000 .toString() + 3");

0 comments on commit eb40134

Please sign in to comment.