From 5e1baffee00e027260478d0b622b8c676bb5ff8d Mon Sep 17 00:00:00 2001 From: kingwl <805037171@163.com> Date: Wed, 6 Dec 2017 12:52:22 +0800 Subject: [PATCH] fix decorator format with first parameter --- src/services/formatting/rules.ts | 11 ++++++++++- .../fourslash/formattingParameterWithDecorator.ts | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/formattingParameterWithDecorator.ts diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index 8214ffd6f2ddb..6e62a3ef023f2 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -198,7 +198,7 @@ namespace ts.formatting { RuleAction.Delete), // decorators - rule("SpaceBeforeAt", anyToken, SyntaxKind.AtToken, [isNonJsxSameLineTokenContext], RuleAction.Space), + rule("SpaceBeforeAt", anyToken, SyntaxKind.AtToken, [isNonJsxSameLineTokenContext, isNotDecoratorWithFirstParameterContext], RuleAction.Space), rule("NoSpaceAfterAt", SyntaxKind.AtToken, anyToken, [isNonJsxSameLineTokenContext], RuleAction.Delete), // Insert space after @ in decorator rule("SpaceAfterDecorator", @@ -720,4 +720,13 @@ namespace ts.formatting { function isNonNullAssertionContext(context: FormattingContext): boolean { return context.contextNode.kind === SyntaxKind.NonNullExpression; } + + function isDecoratorWithFirstParameterContext(context: FormattingContext): boolean { + return context.currentTokenSpan.kind === SyntaxKind.OpenParenToken && context.nextTokenParent.kind === SyntaxKind.Decorator && + context.nextTokenParent.parent && context.nextTokenParent.parent.kind === SyntaxKind.Parameter; + } + + function isNotDecoratorWithFirstParameterContext(context: FormattingContext): boolean { + return !isDecoratorWithFirstParameterContext(context); + } } diff --git a/tests/cases/fourslash/formattingParameterWithDecorator.ts b/tests/cases/fourslash/formattingParameterWithDecorator.ts new file mode 100644 index 0000000000000..865df621dda79 --- /dev/null +++ b/tests/cases/fourslash/formattingParameterWithDecorator.ts @@ -0,0 +1,12 @@ +/// + +/////*1*/function (@foo a: any, @bar b: any) { } +/////*2*/class Test { constructor(@foo props: any, @bar b: any) { } } + +format.document(); + +goTo.marker('1'); +verify.currentLineContentIs('function (@foo a: any, @bar b: any) { }'); + +goTo.marker('2'); +verify.currentLineContentIs('class Test { constructor(@foo props: any, @bar b: any) { } }'); \ No newline at end of file