Skip to content

Commit 71d2f00

Browse files
author
MQuy
committed
fix(48281) - Fix object literal indentation like block
1 parent 5995338 commit 71d2f00

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,16 @@ namespace ts.codefix {
173173
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), checker.getTypeAtLocation(param), /* requireOptionalProperties */ false, /* matchDiscriminantProperties */ false));
174174
if (!length(properties)) return undefined;
175175

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);
179177
}
180178

181179
if (!isMemberName(token)) return undefined;
182180

183181
if (isIdentifier(token) && hasInitializer(parent) && parent.initializer && isObjectLiteralExpression(parent.initializer)) {
184182
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent.initializer), checker.getTypeAtLocation(token), /* requireOptionalProperties */ false, /* matchDiscriminantProperties */ false));
185183
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);
187186
}
188187

189188
if (isIdentifier(token) && isJsxOpeningLikeElement(token.parent)) {
@@ -239,7 +238,36 @@ namespace ts.codefix {
239238
if (enumDeclaration && !isPrivateIdentifier(token) && !isSourceFileFromLibrary(program, enumDeclaration.getSourceFile())) {
240239
return { kind: InfoKind.Enum, token, parentDeclaration: enumDeclaration };
241240
}
241+
242242
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+
}
243271
}
244272

245273
function isSourceFileFromLibrary(program: Program, node: SourceFile) {

tests/cases/fourslash/codeFixAddMissingProperties_PreserveIndent.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,43 @@
88
////function g (_spec: Test) {}
99
////[|f(() => {
1010
//// g({});
11+
//// g(
12+
//// {});
13+
//// g(
14+
//// {}
15+
//// );
1116
////});|]
1217

13-
verify.codeFix({
14-
index: 2,
15-
description: ts.Diagnostics.Add_missing_properties.message,
16-
newRangeContent:
17-
`f(() => {
18+
verify.codeFixAll({
19+
fixId: "fixMissingProperties",
20+
fixAllDescription: ts.Diagnostics.Add_all_missing_properties.message,
21+
newFileContent: `interface Test {
22+
foo: string;
23+
bar(a: string): void;
24+
}
25+
function f (_spec: any) {}
26+
function g (_spec: Test) {}
27+
f(() => {
1828
g({
1929
foo: "",
2030
bar: function(a: string): void {
2131
throw new Error("Function not implemented.");
2232
}
2333
});
24-
});`});
34+
g(
35+
{
36+
foo: "",
37+
bar: function(a: string): void {
38+
throw new Error("Function not implemented.");
39+
}
40+
});
41+
g(
42+
{
43+
foo: "",
44+
bar: function(a: string): void {
45+
throw new Error("Function not implemented.");
46+
}
47+
}
48+
);
49+
});`,
50+
});

0 commit comments

Comments
 (0)