Skip to content

Commit 61928e1

Browse files
a-tarasyukBobobUnicorn
authored andcommitted
feat(18147): skip uncessary parenthesis (microsoft#44769)
1 parent 751462e commit 61928e1

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

src/services/refactors/extractSymbol.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ namespace ts.refactor.extractSymbol {
11411141
? undefined
11421142
: checker.typeToTypeNode(checker.getContextualType(node)!, scope, NodeBuilderFlags.NoTruncation); // TODO: GH#18217
11431143

1144-
let initializer = transformConstantInitializer(node, substitutions);
1144+
let initializer = transformConstantInitializer(skipParentheses(node), substitutions);
11451145

11461146
({ variableType, initializer } = transformFunctionInitializerAndType(variableType, initializer));
11471147

@@ -1375,7 +1375,7 @@ namespace ts.refactor.extractSymbol {
13751375
}
13761376
let returnValueProperty: string | undefined;
13771377
let ignoreReturns = false;
1378-
const statements = factory.createNodeArray(isBlock(body) ? body.statements.slice(0) : [isStatement(body) ? body : factory.createReturnStatement(body as Expression)]);
1378+
const statements = factory.createNodeArray(isBlock(body) ? body.statements.slice(0) : [isStatement(body) ? body : factory.createReturnStatement(skipParentheses(body as Expression))]);
13791379
// rewrite body if either there are writes that should be propagated back via return statements or there are substitutions
13801380
if (hasWritesOrVariableDeclarations || substitutions.size) {
13811381
const rewrittenStatements = visitNodes(statements, visitor).slice();
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @filename: foo.ts
4+
////const foo = 1 * /*a*/(2 + 2)/*b*/;
5+
6+
goTo.select("a", "b");
7+
edit.applyRefactor({
8+
refactorName: "Extract Symbol",
9+
actionName: "constant_scope_0",
10+
actionDescription: "Extract to constant in enclosing scope",
11+
newContent:
12+
`const newLocal = 2 + 2;
13+
const foo = 1 * /*RENAME*/newLocal;`
14+
});
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @filename: foo.ts
4+
////const foo = 1 * /*a*/(((((2 + 2)))))/*b*/;
5+
6+
goTo.select("a", "b");
7+
edit.applyRefactor({
8+
refactorName: "Extract Symbol",
9+
actionName: "constant_scope_0",
10+
actionDescription: "Extract to constant in enclosing scope",
11+
newContent:
12+
`const newLocal = 2 + 2;
13+
const foo = 1 * /*RENAME*/newLocal;`
14+
});
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function foo() {
4+
//// const x = 10 * /*a*/(10 + 10)/*b*/;
5+
////}
6+
7+
goTo.select("a", "b");
8+
edit.applyRefactor({
9+
refactorName: "Extract Symbol",
10+
actionName: "function_scope_1",
11+
actionDescription: "Extract to function in global scope",
12+
newContent:
13+
`function foo() {
14+
const x = 10 * /*RENAME*/newFunction();
15+
}
16+
17+
function newFunction() {
18+
return 10 + 10;
19+
}
20+
`
21+
});
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function foo() {
4+
//// const x = 10 * /*a*/((((((10 + 10))))))/*b*/;
5+
////}
6+
7+
goTo.select("a", "b");
8+
edit.applyRefactor({
9+
refactorName: "Extract Symbol",
10+
actionName: "function_scope_1",
11+
actionDescription: "Extract to function in global scope",
12+
newContent:
13+
`function foo() {
14+
const x = 10 * /*RENAME*/newFunction();
15+
}
16+
17+
function newFunction() {
18+
return 10 + 10;
19+
}
20+
`
21+
});

0 commit comments

Comments
 (0)