feat(18147): skip uncessary parenthesis (#44769)

This commit is contained in:
Oleksandr T 2021-08-17 03:20:40 +03:00 committed by GitHub
parent 424464d46b
commit dc80e6a28b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 2 deletions

View File

@ -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();

View File

@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />
// @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;`
});

View File

@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />
// @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;`
});

View File

@ -0,0 +1,21 @@
/// <reference path='fourslash.ts' />
////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;
}
`
});

View File

@ -0,0 +1,21 @@
/// <reference path='fourslash.ts' />
////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;
}
`
});