@@ -23,6 +23,7 @@ import {
23
23
isNumericLiteral ,
24
24
isObjectLiteralExpression ,
25
25
isPropertyAccessExpression ,
26
+ isShorthandPropertyAssignment ,
26
27
isTypeQueryNode ,
27
28
isVariableDeclarationInVariableStatement ,
28
29
isVariableStatement ,
@@ -102,7 +103,6 @@ registerRefactor(refactorName, {
102
103
103
104
getEditsForAction ( context , actionName ) {
104
105
Debug . assert ( actionName === refactorName , "Unexpected refactor invoked" ) ;
105
-
106
106
const { file, program, startPosition } = context ;
107
107
108
108
// tryWithReferenceToken is true below since here we're already performing the refactor.
@@ -189,9 +189,10 @@ function isDeclarationExported(declaration: InitializedVariableDeclaration): boo
189
189
function getReferenceNodes ( declaration : InitializedVariableDeclaration , checker : TypeChecker , file : SourceFile ) : Identifier [ ] | undefined {
190
190
const references : Identifier [ ] = [ ] ;
191
191
const cannotInline = FindAllReferences . Core . eachSymbolReferenceInFile ( declaration . name as Identifier , checker , file , ref => {
192
- // Only inline if all references are reads. Else we might end up with an invalid scenario like:
193
- // const y = x++ + 1 -> const y = 2++ + 1
194
- if ( FindAllReferences . isWriteAccessForReference ( ref ) ) {
192
+ // Only inline if all references are reads, or if it includes a shorthand property assignment.
193
+ // Else we might end up with an invalid scenario like:
194
+ // const y = x++ + 1 -> const y = 2++ + 1,
195
+ if ( FindAllReferences . isWriteAccessForReference ( ref ) && ! isShorthandPropertyAssignment ( ref . parent ) ) {
195
196
return true ;
196
197
}
197
198
@@ -216,7 +217,7 @@ function getReferenceNodes(declaration: InitializedVariableDeclaration, checker:
216
217
return references . length === 0 || cannotInline ? undefined : references ;
217
218
}
218
219
219
- function getReplacementExpression ( reference : Node , replacement : Expression ) : Expression {
220
+ function getReplacementExpression ( reference : Node , replacement : Expression ) {
220
221
// Make sure each reference site gets its own copy of the replacement node.
221
222
replacement = getSynthesizedDeepClone ( replacement ) ;
222
223
const { parent } = reference ;
@@ -244,5 +245,13 @@ function getReplacementExpression(reference: Node, replacement: Expression): Exp
244
245
return factory . createParenthesizedExpression ( replacement ) ;
245
246
}
246
247
248
+ // Inline shorthand property assignment
249
+ // E.g.:
250
+ // const x = 1;
251
+ // const y = { x }; -> const y = { x: 1 };
252
+ if ( isIdentifier ( reference ) && isShorthandPropertyAssignment ( parent ) ) {
253
+ return factory . createPropertyAssignment ( reference , replacement ) ;
254
+ }
255
+
247
256
return replacement ;
248
257
}
0 commit comments