Skip to content

Commit 734d4fb

Browse files
committed
feat(57028): support inlining variables that are used in object literal shorthand
1 parent 7f3e34b commit 734d4fb

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/services/refactors/inlineVariable.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
isNumericLiteral,
2424
isObjectLiteralExpression,
2525
isPropertyAccessExpression,
26+
isShorthandPropertyAssignment,
2627
isTypeQueryNode,
2728
isVariableDeclarationInVariableStatement,
2829
isVariableStatement,
@@ -102,7 +103,6 @@ registerRefactor(refactorName, {
102103

103104
getEditsForAction(context, actionName) {
104105
Debug.assert(actionName === refactorName, "Unexpected refactor invoked");
105-
106106
const { file, program, startPosition } = context;
107107

108108
// tryWithReferenceToken is true below since here we're already performing the refactor.
@@ -152,7 +152,7 @@ function getInliningInfo(file: SourceFile, startPosition: number, tryWithReferen
152152
}
153153

154154
// Try finding the declaration and nodes to replace via the reference token.
155-
if (tryWithReferenceToken) {
155+
if (tryWithReferenceToken || isShorthandPropertyAssignment(parent)) {
156156
let definition = checker.resolveName(token.text, token, SymbolFlags.Value, /*excludeGlobals*/ false);
157157
definition = definition && checker.getMergedSymbol(definition);
158158

@@ -191,7 +191,7 @@ function getReferenceNodes(declaration: InitializedVariableDeclaration, checker:
191191
const cannotInline = FindAllReferences.Core.eachSymbolReferenceInFile(declaration.name as Identifier, checker, file, ref => {
192192
// Only inline if all references are reads. Else we might end up with an invalid scenario like:
193193
// const y = x++ + 1 -> const y = 2++ + 1
194-
if (FindAllReferences.isWriteAccessForReference(ref)) {
194+
if (FindAllReferences.isWriteAccessForReference(ref) && !isShorthandPropertyAssignment(ref.parent)) {
195195
return true;
196196
}
197197

@@ -216,7 +216,7 @@ function getReferenceNodes(declaration: InitializedVariableDeclaration, checker:
216216
return references.length === 0 || cannotInline ? undefined : references;
217217
}
218218

219-
function getReplacementExpression(reference: Node, replacement: Expression): Expression {
219+
function getReplacementExpression(reference: Node, replacement: Expression) {
220220
// Make sure each reference site gets its own copy of the replacement node.
221221
replacement = getSynthesizedDeepClone(replacement);
222222
const { parent } = reference;
@@ -244,5 +244,9 @@ function getReplacementExpression(reference: Node, replacement: Expression): Exp
244244
return factory.createParenthesizedExpression(replacement);
245245
}
246246

247+
if (isIdentifier(reference) && isShorthandPropertyAssignment(parent)) {
248+
return factory.createPropertyAssignment(reference, replacement);
249+
}
250+
247251
return replacement;
248252
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////const foo = 1;
4+
////const bar = { /*a*/foo/*b*/ };
5+
6+
goTo.select("a", "b");
7+
verify.refactorAvailable("Inline variable");
8+
edit.applyRefactor({
9+
refactorName: "Inline variable",
10+
actionName: "Inline variable",
11+
actionDescription: "Inline variable",
12+
newContent: "const bar = { foo: 1 };",
13+
});

0 commit comments

Comments
 (0)