@@ -173,17 +173,16 @@ namespace ts.codefix {
173
173
const properties = arrayFrom ( checker . getUnmatchedProperties ( checker . getTypeAtLocation ( parent ) , checker . getTypeAtLocation ( param ) , /* requireOptionalProperties */ false , /* matchDiscriminantProperties */ false ) ) ;
174
174
if ( ! length ( properties ) ) return undefined ;
175
175
176
- const formatOptions = getFormatCodeSettingsForWriting ( formatContext , sourceFile ) ;
177
- const indentation = formatting . SmartIndenter . getIndentation ( getLineStartPositionForPosition ( tokenPos , sourceFile ) , sourceFile , formatOptions , getLineStartPositionForPosition ( tokenPos , sourceFile ) === tokenPos ) ;
178
- return { kind : InfoKind . ObjectLiteral , token : param . name , properties, indentation, trimLeadingWhiteSpaces : true , parentDeclaration : parent } ;
176
+ return createObjectLiteralInfo ( param . name , properties , parent ) ;
179
177
}
180
178
181
179
if ( ! isMemberName ( token ) ) return undefined ;
182
180
183
181
if ( isIdentifier ( token ) && hasInitializer ( parent ) && parent . initializer && isObjectLiteralExpression ( parent . initializer ) ) {
184
182
const properties = arrayFrom ( checker . getUnmatchedProperties ( checker . getTypeAtLocation ( parent . initializer ) , checker . getTypeAtLocation ( token ) , /* requireOptionalProperties */ false , /* matchDiscriminantProperties */ false ) ) ;
185
183
if ( ! length ( properties ) ) return undefined ;
186
- return { kind : InfoKind . ObjectLiteral , token, properties, indentation : undefined , parentDeclaration : parent . initializer } ;
184
+
185
+ return createObjectLiteralInfo ( token , properties , parent . initializer ) ;
187
186
}
188
187
189
188
if ( isIdentifier ( token ) && isJsxOpeningLikeElement ( token . parent ) ) {
@@ -239,7 +238,36 @@ namespace ts.codefix {
239
238
if ( enumDeclaration && ! isPrivateIdentifier ( token ) && ! isSourceFileFromLibrary ( program , enumDeclaration . getSourceFile ( ) ) ) {
240
239
return { kind : InfoKind . Enum , token, parentDeclaration : enumDeclaration } ;
241
240
}
241
+
242
242
return undefined ;
243
+
244
+ // for object literal, we want to the indentation work like block
245
+ // if { starts in any position (can be in the middle of line)
246
+ // the following indentation should treat { as starting of that line (including leading whitespace)
247
+ // ```
248
+ // const a: { x: undefined, y: undefined } = {} // leading 4 whitespaces and { starts in the middle of line
249
+ // ->
250
+ // const a: { x: undefined, y: undefined } = {
251
+ // x: undefined,
252
+ // y: undefined,
253
+ // }
254
+ // ---------------------
255
+ // const a: {x : undefined, y: undefined } =
256
+ // {}
257
+ // ->
258
+ // const a: { x: undefined, y: undefined } =
259
+ // { // leading 5 whitespaces and { starts at 6 column
260
+ // x: undefined,
261
+ // y: undefined,
262
+ // }
263
+ // ```
264
+ function createObjectLiteralInfo ( token : Identifier , properties : Symbol [ ] , parentDeclaration : ObjectLiteralExpression ) : Info {
265
+ const formatOptions = getFormatCodeSettingsForWriting ( formatContext , sourceFile ) ;
266
+ const lineStartPosition = getLineStartPositionForPosition ( tokenPos , sourceFile ) ;
267
+ const indentation = formatting . SmartIndenter . getIndentation ( tokenPos , sourceFile , { ...formatOptions , indentStyle : IndentStyle . Block } , lineStartPosition === tokenPos ) ;
268
+
269
+ return { kind : InfoKind . ObjectLiteral , token, properties, indentation, trimLeadingWhiteSpaces : true , parentDeclaration } ;
270
+ }
243
271
}
244
272
245
273
function isSourceFileFromLibrary ( program : Program , node : SourceFile ) {
0 commit comments