From 1a1c27167536e005a4d241ef84c43c0c0ca0aaf9 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 8 Sep 2022 19:08:51 -0700 Subject: [PATCH] Don't remove space before dot if in property access on numeric literal (#50695) * Add failing test * Don't remove space before dot if in property access on numeric literal --- src/services/formatting/rules.ts | 6 +++++- tests/cases/fourslash/formatDotAfterNumber.ts | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/formatDotAfterNumber.ts diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index 144f97831bb17..ce1d6b9e2cb3c 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -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], RuleAction.DeleteSpace), + rule("NoSpaceBeforeDot", anyToken, [SyntaxKind.DotToken, SyntaxKind.QuestionDotToken], [isNonJsxSameLineTokenContext, isNotPropertyAccessOnNumericLiteral], RuleAction.DeleteSpace), rule("NoSpaceAfterDot", [SyntaxKind.DotToken, SyntaxKind.QuestionDotToken], anyToken, [isNonJsxSameLineTokenContext], RuleAction.DeleteSpace), rule("NoSpaceBetweenImportParenInImportType", SyntaxKind.ImportKeyword, SyntaxKind.OpenParenToken, [isNonJsxSameLineTokenContext, isImportTypeContext], RuleAction.DeleteSpace), @@ -893,4 +893,8 @@ namespace ts.formatting { function isSemicolonInsertionContext(context: FormattingContext): boolean { return positionIsASICandidate(context.currentTokenSpan.end, context.currentTokenParent, context.sourceFile); } + + function isNotPropertyAccessOnNumericLiteral(context: FormattingContext): boolean { + return !isPropertyAccessExpression(context.contextNode) || !isNumericLiteral(context.contextNode.expression); + } } diff --git a/tests/cases/fourslash/formatDotAfterNumber.ts b/tests/cases/fourslash/formatDotAfterNumber.ts new file mode 100644 index 0000000000000..1e08cf469c64d --- /dev/null +++ b/tests/cases/fourslash/formatDotAfterNumber.ts @@ -0,0 +1,7 @@ +/// + +////1+ 2 .toString() +3/**/ + +format.document(); +goTo.marker(""); +verify.currentLineContentIs("1 + 2 .toString() + 3");