From 5236dd0793d8ddfcd2ab1cbc87b7328f96552926 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Fri, 21 Oct 2022 09:54:22 +0300 Subject: [PATCH 1/2] fix(51245): don't rely on parent nodes in formatting rules --- src/services/formatting/rules.ts | 12 +--------- ... refactorConvertExport_namedToDefault1.ts} | 0 .../refactorConvertExport_namedToDefault2.ts | 23 +++++++++++++++++++ 3 files changed, 24 insertions(+), 11 deletions(-) rename tests/cases/fourslash/{refactorConvertExport_namedToDefault.ts => refactorConvertExport_namedToDefault1.ts} (100%) create mode 100644 tests/cases/fourslash/refactorConvertExport_namedToDefault2.ts diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index 85e67eb8cbb59..59090f56e264e 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -725,17 +725,7 @@ namespace ts.formatting { } function isEndOfDecoratorContextOnSameLine(context: FormattingContext): boolean { - return context.TokensAreOnSameLine() && - hasDecorators(context.contextNode) && - nodeIsInDecoratorContext(context.currentTokenParent) && - !nodeIsInDecoratorContext(context.nextTokenParent); - } - - function nodeIsInDecoratorContext(node: Node): boolean { - while (isExpressionNode(node)) { - node = node.parent; - } - return node.kind === SyntaxKind.Decorator; + return context.TokensAreOnSameLine() && hasDecorators(context.contextNode) && isDecorator(context.currentTokenParent); } function isStartOfVariableDeclarationList(context: FormattingContext): boolean { diff --git a/tests/cases/fourslash/refactorConvertExport_namedToDefault.ts b/tests/cases/fourslash/refactorConvertExport_namedToDefault1.ts similarity index 100% rename from tests/cases/fourslash/refactorConvertExport_namedToDefault.ts rename to tests/cases/fourslash/refactorConvertExport_namedToDefault1.ts diff --git a/tests/cases/fourslash/refactorConvertExport_namedToDefault2.ts b/tests/cases/fourslash/refactorConvertExport_namedToDefault2.ts new file mode 100644 index 0000000000000..3692e8493a5ae --- /dev/null +++ b/tests/cases/fourslash/refactorConvertExport_namedToDefault2.ts @@ -0,0 +1,23 @@ +/// + +// @Filename: /a.ts +/////*a*/export const f = () => { +//// return class C { +//// constructor(@Foo() param: any) { } +//// } +////}/*b*/ +////function Foo(...args: any[]): any {} + +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Convert export", + actionName: "Convert named export to default export", + actionDescription: "Convert named export to default export", + newContent: +`export default () => { + return class C { + constructor(@Foo() param: any) { } + } +} +function Foo(...args: any[]): any {}`, +}); From 2b1d5db5efd67f5f402ec7f05810ab68774afa4f Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Tue, 25 Oct 2022 20:15:09 +0300 Subject: [PATCH 2/2] check existing parent node --- src/services/formatting/rules.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index 59090f56e264e..07724be25ba0e 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -725,7 +725,17 @@ namespace ts.formatting { } function isEndOfDecoratorContextOnSameLine(context: FormattingContext): boolean { - return context.TokensAreOnSameLine() && hasDecorators(context.contextNode) && isDecorator(context.currentTokenParent); + return context.TokensAreOnSameLine() && + hasDecorators(context.contextNode) && + nodeIsInDecoratorContext(context.currentTokenParent) && + !nodeIsInDecoratorContext(context.nextTokenParent); + } + + function nodeIsInDecoratorContext(node: Node): boolean { + while (node && isExpression(node)) { + node = node.parent; + } + return node && node.kind === SyntaxKind.Decorator; } function isStartOfVariableDeclarationList(context: FormattingContext): boolean {