Skip to content

Commit 4ff63b4

Browse files
author
Maria Solano
committed
More tests
1 parent 580a173 commit 4ff63b4

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/services/refactors/inlineVariable.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
isInitializedVariable,
1818
isTypeQueryNode,
1919
isVariableDeclarationInVariableStatement,
20+
needsParentheses,
2021
Node,
2122
Program,
2223
refactor,
@@ -176,10 +177,10 @@ function getReplacementExpression(reference: Node, replacement: Expression): Exp
176177
// If the operand has higher precedence, then it does not need to be parenthesized."
177178
//
178179
// Note that binaryOperandNeedsParentheses has further logic when the precedences
179-
// are equal, but for the purposes of this refactor we keep things simple and always
180-
// parenthesize.
180+
// are equal, but for the purposes of this refactor we keep things simple and
181+
// instead just check for special cases with needsParentheses.
181182
const { parent } = reference;
182-
if (isExpression(parent) && (getExpressionPrecedence(replacement) <= getExpressionPrecedence(parent))) {
183+
if (isExpression(parent) && (getExpressionPrecedence(replacement) < getExpressionPrecedence(parent) || needsParentheses(parent))) {
183184
return factory.createParenthesizedExpression(replacement);
184185
}
185186

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////function inc(x: number) { return x + 1; }
4+
////const y/**/ = inc(1);
5+
////const z = y + 1;
6+
////const w = inc(y);
7+
8+
goTo.marker("");
9+
verify.refactorAvailable("Inline variable");
10+
edit.applyRefactor({
11+
refactorName: "Inline variable",
12+
actionName: "Inline variable",
13+
actionDescription: "Inline variable",
14+
newContent: `function inc(x: number) { return x + 1; }
15+
const z = inc(1) + 1;
16+
const w = inc(inc(1));`
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////function foo(): number | undefined { return Math.random() > 0.5 ? 1 : undefined; }
4+
////const x/**/ = foo();
5+
////const y = x?.toString();
6+
7+
goTo.marker("");
8+
verify.refactorAvailable("Inline variable");
9+
edit.applyRefactor({
10+
refactorName: "Inline variable",
11+
actionName: "Inline variable",
12+
actionDescription: "Inline variable",
13+
newContent: `function foo(): number | undefined { return Math.random() > 0.5 ? 1 : undefined; }
14+
const y = (foo())?.toString();`
15+
});

0 commit comments

Comments
 (0)