diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index 5b1543b79f0..b9cf85b5b60 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -245,6 +245,9 @@ namespace ts.formatting { public NoSpaceAfterTypeAssertion: Rule; public SpaceAfterTypeAssertion: Rule; + // No space before non-null assertion operator + public NoSpaceBeforeNonNullAssertionOperator: Rule; + constructor() { /// /// Common Rules @@ -410,6 +413,9 @@ namespace ts.formatting { this.NoSpaceBeforeEqualInJsxAttribute = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.EqualsToken), RuleOperation.create2(new RuleOperationContext(Rules.IsJsxAttributeContext, Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete)); this.NoSpaceAfterEqualInJsxAttribute = new Rule(RuleDescriptor.create3(SyntaxKind.EqualsToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsJsxAttributeContext, Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete)); + // No space before non-null assertion operator + this.NoSpaceBeforeNonNullAssertionOperator = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.ExclamationToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsNonNullAssertionContext), RuleAction.Delete)); + // These rules are higher in priority than user-configurable rules. this.HighPriorityCommonRules = [ this.IgnoreBeforeComment, this.IgnoreAfterLineComment, @@ -456,6 +462,7 @@ namespace ts.formatting { this.SpaceBeforeAt, this.NoSpaceAfterAt, this.SpaceAfterDecorator, + this.NoSpaceBeforeNonNullAssertionOperator ]; // These rules are lower in priority than user-configurable rules. @@ -882,5 +889,9 @@ namespace ts.formatting { static IsYieldOrYieldStarWithOperand(context: FormattingContext): boolean { return context.contextNode.kind === SyntaxKind.YieldExpression && (context.contextNode).expression !== undefined; } + + static IsNonNullAssertionContext(context: FormattingContext): boolean { + return context.contextNode.kind === SyntaxKind.NonNullExpression; + } } } \ No newline at end of file diff --git a/tests/cases/fourslash/formattingNonNullAssertionOperator.ts b/tests/cases/fourslash/formattingNonNullAssertionOperator.ts new file mode 100644 index 00000000000..3318ca7f4d6 --- /dev/null +++ b/tests/cases/fourslash/formattingNonNullAssertionOperator.ts @@ -0,0 +1,19 @@ +/// + +/////*1*/'bar'!; +/////*2*/('bar')!; +/////*3*/'bar'[1]!; +/////*4*/var bar = 'bar'.foo!; +/////*5*/var foo = bar!; + +format.document(); +goTo.marker('1'); +verify.currentLineContentIs("'bar'!;"); +goTo.marker('2'); +verify.currentLineContentIs("('bar')!;"); +goTo.marker('3'); +verify.currentLineContentIs("'bar'[1]!;"); +goTo.marker('4'); +verify.currentLineContentIs("var bar = 'bar'.foo!;"); +goTo.marker('5'); +verify.currentLineContentIs("var foo = bar!;"); \ No newline at end of file