From e32c62039e443b19ccfa23533c4a0cbccb6ff9b0 Mon Sep 17 00:00:00 2001 From: flowmemo Date: Sat, 24 Dec 2016 00:20:46 +0800 Subject: [PATCH 1/2] fix #11676 --- src/services/formatting/rules.ts | 11 +++++++++++ .../formattingNonNullAssertionOperator.ts | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/cases/fourslash/formattingNonNullAssertionOperator.ts 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 From eb188d0e24ae3f2c720c16e333a7066942718be0 Mon Sep 17 00:00:00 2001 From: flowmemo Date: Sat, 24 Dec 2016 09:25:14 +0800 Subject: [PATCH 2/2] fix #11676 - lint test --- .../fourslash/formattingNonNullAssertionOperator.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/cases/fourslash/formattingNonNullAssertionOperator.ts b/tests/cases/fourslash/formattingNonNullAssertionOperator.ts index 3318ca7f4d6..c6e79629998 100644 --- a/tests/cases/fourslash/formattingNonNullAssertionOperator.ts +++ b/tests/cases/fourslash/formattingNonNullAssertionOperator.ts @@ -7,13 +7,13 @@ /////*5*/var foo = bar!; format.document(); -goTo.marker('1'); +goTo.marker("1"); verify.currentLineContentIs("'bar'!;"); -goTo.marker('2'); +goTo.marker("2"); verify.currentLineContentIs("('bar')!;"); -goTo.marker('3'); +goTo.marker("3"); verify.currentLineContentIs("'bar'[1]!;"); -goTo.marker('4'); +goTo.marker("4"); verify.currentLineContentIs("var bar = 'bar'.foo!;"); -goTo.marker('5'); +goTo.marker("5"); verify.currentLineContentIs("var foo = bar!;"); \ No newline at end of file