From dc80e6a28b693ca9d8406a4618d7fa4723a0aa95 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Tue, 17 Aug 2021 03:20:40 +0300 Subject: [PATCH] feat(18147): skip uncessary parenthesis (#44769) --- src/services/refactors/extractSymbol.ts | 4 ++-- tests/cases/fourslash/extract-const8.ts | 14 ++++++++++++++ tests/cases/fourslash/extract-const9.ts | 14 ++++++++++++++ tests/cases/fourslash/extract-method42.ts | 21 +++++++++++++++++++++ tests/cases/fourslash/extract-method43.ts | 21 +++++++++++++++++++++ 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/extract-const8.ts create mode 100644 tests/cases/fourslash/extract-const9.ts create mode 100644 tests/cases/fourslash/extract-method42.ts create mode 100644 tests/cases/fourslash/extract-method43.ts diff --git a/src/services/refactors/extractSymbol.ts b/src/services/refactors/extractSymbol.ts index 8ffc1646c47..6490fde5387 100644 --- a/src/services/refactors/extractSymbol.ts +++ b/src/services/refactors/extractSymbol.ts @@ -1141,7 +1141,7 @@ namespace ts.refactor.extractSymbol { ? undefined : checker.typeToTypeNode(checker.getContextualType(node)!, scope, NodeBuilderFlags.NoTruncation); // TODO: GH#18217 - let initializer = transformConstantInitializer(node, substitutions); + let initializer = transformConstantInitializer(skipParentheses(node), substitutions); ({ variableType, initializer } = transformFunctionInitializerAndType(variableType, initializer)); @@ -1375,7 +1375,7 @@ namespace ts.refactor.extractSymbol { } let returnValueProperty: string | undefined; let ignoreReturns = false; - const statements = factory.createNodeArray(isBlock(body) ? body.statements.slice(0) : [isStatement(body) ? body : factory.createReturnStatement(body as Expression)]); + const statements = factory.createNodeArray(isBlock(body) ? body.statements.slice(0) : [isStatement(body) ? body : factory.createReturnStatement(skipParentheses(body as Expression))]); // rewrite body if either there are writes that should be propagated back via return statements or there are substitutions if (hasWritesOrVariableDeclarations || substitutions.size) { const rewrittenStatements = visitNodes(statements, visitor).slice(); diff --git a/tests/cases/fourslash/extract-const8.ts b/tests/cases/fourslash/extract-const8.ts new file mode 100644 index 00000000000..5c72bd82e18 --- /dev/null +++ b/tests/cases/fourslash/extract-const8.ts @@ -0,0 +1,14 @@ +/// + +// @filename: foo.ts +////const foo = 1 * /*a*/(2 + 2)/*b*/; + +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Extract Symbol", + actionName: "constant_scope_0", + actionDescription: "Extract to constant in enclosing scope", + newContent: +`const newLocal = 2 + 2; +const foo = 1 * /*RENAME*/newLocal;` +}); diff --git a/tests/cases/fourslash/extract-const9.ts b/tests/cases/fourslash/extract-const9.ts new file mode 100644 index 00000000000..b575d227967 --- /dev/null +++ b/tests/cases/fourslash/extract-const9.ts @@ -0,0 +1,14 @@ +/// + +// @filename: foo.ts +////const foo = 1 * /*a*/(((((2 + 2)))))/*b*/; + +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Extract Symbol", + actionName: "constant_scope_0", + actionDescription: "Extract to constant in enclosing scope", + newContent: +`const newLocal = 2 + 2; +const foo = 1 * /*RENAME*/newLocal;` +}); diff --git a/tests/cases/fourslash/extract-method42.ts b/tests/cases/fourslash/extract-method42.ts new file mode 100644 index 00000000000..b22dc145f81 --- /dev/null +++ b/tests/cases/fourslash/extract-method42.ts @@ -0,0 +1,21 @@ +/// + +////function foo() { +//// const x = 10 * /*a*/(10 + 10)/*b*/; +////} + +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Extract Symbol", + actionName: "function_scope_1", + actionDescription: "Extract to function in global scope", + newContent: +`function foo() { + const x = 10 * /*RENAME*/newFunction(); +} + +function newFunction() { + return 10 + 10; +} +` +}); diff --git a/tests/cases/fourslash/extract-method43.ts b/tests/cases/fourslash/extract-method43.ts new file mode 100644 index 00000000000..8deb93f4765 --- /dev/null +++ b/tests/cases/fourslash/extract-method43.ts @@ -0,0 +1,21 @@ +/// + +////function foo() { +//// const x = 10 * /*a*/((((((10 + 10))))))/*b*/; +////} + +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Extract Symbol", + actionName: "function_scope_1", + actionDescription: "Extract to function in global scope", + newContent: +`function foo() { + const x = 10 * /*RENAME*/newFunction(); +} + +function newFunction() { + return 10 + 10; +} +` +});