mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
feat(18147): skip uncessary parenthesis (#44769)
This commit is contained in:
parent
424464d46b
commit
dc80e6a28b
@ -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();
|
||||
|
||||
14
tests/cases/fourslash/extract-const8.ts
Normal file
14
tests/cases/fourslash/extract-const8.ts
Normal 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;`
|
||||
});
|
||||
14
tests/cases/fourslash/extract-const9.ts
Normal file
14
tests/cases/fourslash/extract-const9.ts
Normal 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;`
|
||||
});
|
||||
21
tests/cases/fourslash/extract-method42.ts
Normal file
21
tests/cases/fourslash/extract-method42.ts
Normal 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;
|
||||
}
|
||||
`
|
||||
});
|
||||
21
tests/cases/fourslash/extract-method43.ts
Normal file
21
tests/cases/fourslash/extract-method43.ts
Normal 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;
|
||||
}
|
||||
`
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user