Skip to content

Commit ee36b9a

Browse files
author
MQuy
committed
fix(48281) - Indentation for object literal started with curly brace similar to block
1 parent 71d2f00 commit ee36b9a

File tree

3 files changed

+29
-43
lines changed

3 files changed

+29
-43
lines changed

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace ts.codefix {
1919
errorCodes,
2020
getCodeActions(context) {
2121
const typeChecker = context.program.getTypeChecker();
22-
const info = getInfo(context.sourceFile, context.span.start, context.errorCode, typeChecker, context.program, context.formatContext);
22+
const info = getInfo(context.sourceFile, context.span.start, context.errorCode, typeChecker, context.program);
2323
if (!info) {
2424
return undefined;
2525
}
@@ -50,7 +50,7 @@ namespace ts.codefix {
5050

5151
return createCombinedCodeActions(textChanges.ChangeTracker.with(context, changes => {
5252
eachDiagnostic(context, errorCodes, diag => {
53-
const info = getInfo(diag.file, diag.start, diag.code, checker, context.program, context.formatContext);
53+
const info = getInfo(diag.file, diag.start, diag.code, checker, context.program);
5454
if (!info || !addToSeen(seen, getNodeId(info.parentDeclaration) + "#" + info.token.text)) {
5555
return;
5656
}
@@ -141,7 +141,6 @@ namespace ts.codefix {
141141
readonly properties: Symbol[];
142142
readonly parentDeclaration: ObjectLiteralExpression;
143143
readonly indentation?: number;
144-
readonly trimLeadingWhiteSpaces?: boolean;
145144
}
146145

147146
interface JsxAttributesInfo {
@@ -151,7 +150,7 @@ namespace ts.codefix {
151150
readonly parentDeclaration: JsxOpeningLikeElement;
152151
}
153152

154-
function getInfo(sourceFile: SourceFile, tokenPos: number, errorCode: number, checker: TypeChecker, program: Program, formatContext: formatting.FormatContext): Info | undefined {
153+
function getInfo(sourceFile: SourceFile, tokenPos: number, errorCode: number, checker: TypeChecker, program: Program): Info | undefined {
155154
// The identifier of the missing property. eg:
156155
// this.missing = 1;
157156
// ^^^^^^^
@@ -172,8 +171,7 @@ namespace ts.codefix {
172171

173172
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), checker.getTypeAtLocation(param), /* requireOptionalProperties */ false, /* matchDiscriminantProperties */ false));
174173
if (!length(properties)) return undefined;
175-
176-
return createObjectLiteralInfo(param.name, properties, parent);
174+
return { kind: InfoKind.ObjectLiteral, token: param.name, properties, parentDeclaration: parent };
177175
}
178176

179177
if (!isMemberName(token)) return undefined;
@@ -182,7 +180,7 @@ namespace ts.codefix {
182180
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent.initializer), checker.getTypeAtLocation(token), /* requireOptionalProperties */ false, /* matchDiscriminantProperties */ false));
183181
if (!length(properties)) return undefined;
184182

185-
return createObjectLiteralInfo(token, properties, parent.initializer);
183+
return { kind: InfoKind.ObjectLiteral, token, properties, parentDeclaration: parent.initializer };
186184
}
187185

188186
if (isIdentifier(token) && isJsxOpeningLikeElement(token.parent)) {
@@ -240,34 +238,6 @@ namespace ts.codefix {
240238
}
241239

242240
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-
}
271241
}
272242

273243
function isSourceFileFromLibrary(program: Program, node: SourceFile) {
@@ -522,8 +492,7 @@ namespace ts.codefix {
522492
const options = {
523493
leadingTriviaOption: textChanges.LeadingTriviaOption.Exclude,
524494
trailingTriviaOption: textChanges.TrailingTriviaOption.Exclude,
525-
indentation: info.indentation,
526-
trimLeadingWhiteSpaces: info.trimLeadingWhiteSpaces,
495+
indentation: info.indentation
527496
};
528497
changes.replaceNode(context.sourceFile, info.parentDeclaration, factory.createObjectLiteralExpression([...info.parentDeclaration.properties, ...props], /*multiLine*/ true), options);
529498
}

src/services/formatting/smartIndenter.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,28 @@ namespace ts.formatting {
5555
// indentation is first non-whitespace character in a previous line
5656
// for block indentation, we should look for a line which contains something that's not
5757
// whitespace.
58-
if (options.indentStyle === IndentStyle.Block) {
58+
const currentToken = getTokenAtPosition(sourceFile, position);
59+
// for object literal, we want to the indentation work like block
60+
// if { starts in any position (can be in the middle of line)
61+
// the following indentation should treat { as starting of that line (including leading whitespace)
62+
// ```
63+
// const a: { x: undefined, y: undefined } = {} // leading 4 whitespaces and { starts in the middle of line
64+
// ->
65+
// const a: { x: undefined, y: undefined } = {
66+
// x: undefined,
67+
// y: undefined,
68+
// }
69+
// ---------------------
70+
// const a: {x : undefined, y: undefined } =
71+
// {}
72+
// ->
73+
// const a: { x: undefined, y: undefined } =
74+
// { // leading 5 whitespaces and { starts at 6 column
75+
// x: undefined,
76+
// y: undefined,
77+
// }
78+
// ```
79+
if (options.indentStyle === IndentStyle.Block || currentToken.kind === SyntaxKind.OpenBraceToken) {
5980
return getBlockIndent(sourceFile, position, options);
6081
}
6182

src/services/textChanges.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ namespace ts.textChanges {
116116
* Text of inserted node will be formatted with this delta, otherwise delta will be inferred from the new node kind
117117
*/
118118
delta?: number;
119-
/**
120-
* Trim leading white spaces in the edit range
121-
*/
122-
trimLeadingWhiteSpaces?: boolean;
123119
}
124120

125121
export interface ReplaceWithMultipleNodesOptions extends InsertNodeOptions {
@@ -1068,7 +1064,7 @@ namespace ts.textChanges {
10681064
? change.nodes.map(n => removeSuffix(format(n), newLineCharacter)).join(change.options?.joiner || newLineCharacter)
10691065
: format(change.node);
10701066
// strip initial indentation (spaces or tabs) if text will be inserted in the middle of the line
1071-
const noIndent = (options.trimLeadingWhiteSpaces || (options.indentation === undefined && getLineStartPositionForPosition(pos, sourceFile) !== pos)) ? text.replace(/^\s+/, "") : text;
1067+
const noIndent = (options.indentation !== undefined || getLineStartPositionForPosition(pos, sourceFile) === pos) ? text : text.replace(/^\s+/, "");
10721068
return (options.prefix || "") + noIndent
10731069
+ ((!options.suffix || endsWith(noIndent, options.suffix))
10741070
? "" : options.suffix);

0 commit comments

Comments
 (0)