Skip to content

Fix#48281 - Indentation not respected when executing various refactorings (TypeScript/JavaScript) #48340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/services/codefixes/fixAddMissingMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,16 @@ namespace ts.codefix {

const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), checker.getTypeAtLocation(param), /* requireOptionalProperties */ false, /* matchDiscriminantProperties */ false));
if (!length(properties)) return undefined;
return { kind: InfoKind.ObjectLiteral, token: param.name, properties, indentation: 0, parentDeclaration: parent };
return { kind: InfoKind.ObjectLiteral, token: param.name, properties, parentDeclaration: parent };
}

if (!isMemberName(token)) return undefined;

if (isIdentifier(token) && hasInitializer(parent) && parent.initializer && isObjectLiteralExpression(parent.initializer)) {
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent.initializer), checker.getTypeAtLocation(token), /* requireOptionalProperties */ false, /* matchDiscriminantProperties */ false));
if (!length(properties)) return undefined;
return { kind: InfoKind.ObjectLiteral, token, properties, indentation: undefined, parentDeclaration: parent.initializer };

return { kind: InfoKind.ObjectLiteral, token, properties, parentDeclaration: parent.initializer };
}

if (isIdentifier(token) && isJsxOpeningLikeElement(token.parent)) {
Expand Down Expand Up @@ -235,6 +236,7 @@ namespace ts.codefix {
if (enumDeclaration && !isPrivateIdentifier(token) && !isSourceFileFromLibrary(program, enumDeclaration.getSourceFile())) {
return { kind: InfoKind.Enum, token, parentDeclaration: enumDeclaration };
}

return undefined;
}

Expand Down
23 changes: 22 additions & 1 deletion src/services/formatting/smartIndenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,28 @@ namespace ts.formatting {
// indentation is first non-whitespace character in a previous line
// for block indentation, we should look for a line which contains something that's not
// whitespace.
if (options.indentStyle === IndentStyle.Block) {
const currentToken = getTokenAtPosition(sourceFile, position);
// for object literal, we want to the indentation work like block
// if { starts in any position (can be in the middle of line)
// the following indentation should treat { as starting of that line (including leading whitespace)
// ```
// const a: { x: undefined, y: undefined } = {} // leading 4 whitespaces and { starts in the middle of line
// ->
// const a: { x: undefined, y: undefined } = {
// x: undefined,
// y: undefined,
// }
// ---------------------
// const a: {x : undefined, y: undefined } =
// {}
// ->
// const a: { x: undefined, y: undefined } =
// { // leading 5 whitespaces and { starts at 6 column
// x: undefined,
// y: undefined,
// }
// ```
if (options.indentStyle === IndentStyle.Block || currentToken.kind === SyntaxKind.OpenBraceToken) {
return getBlockIndent(sourceFile, position, options);
}

Expand Down
8 changes: 2 additions & 6 deletions src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ namespace ts.textChanges {
* Text of inserted node will be formatted with this delta, otherwise delta will be inferred from the new node kind
*/
delta?: number;
/**
* Do not trim leading white spaces in the edit range
*/
preserveLeadingWhitespace?: boolean;
}

export interface ReplaceWithMultipleNodesOptions extends InsertNodeOptions {
Expand Down Expand Up @@ -492,7 +488,7 @@ namespace ts.textChanges {
}
const startPosition = getPrecedingNonSpaceCharacterPosition(sourceFile.text, fnStart - 1);
const indent = sourceFile.text.slice(startPosition, fnStart);
this.insertNodeAt(sourceFile, fnStart, tag, { preserveLeadingWhitespace: false, suffix: this.newLineCharacter + indent });
this.insertNodeAt(sourceFile, fnStart, tag, { suffix: this.newLineCharacter + indent });
}

private createJSDocText(sourceFile: SourceFile, node: HasJSDoc) {
Expand Down Expand Up @@ -1068,7 +1064,7 @@ namespace ts.textChanges {
? change.nodes.map(n => removeSuffix(format(n), newLineCharacter)).join(change.options?.joiner || newLineCharacter)
: format(change.node);
// strip initial indentation (spaces or tabs) if text will be inserted in the middle of the line
const noIndent = (options.preserveLeadingWhitespace || options.indentation !== undefined || getLineStartPositionForPosition(pos, sourceFile) === pos) ? text : text.replace(/^\s+/, "");
const noIndent = (options.indentation !== undefined || getLineStartPositionForPosition(pos, sourceFile) === pos) ? text : text.replace(/^\s+/, "");
return (options.prefix || "") + noIndent
+ ((!options.suffix || endsWith(noIndent, options.suffix))
? "" : options.suffix);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/// <reference path='fourslash.ts' />

////interface Test {
//// foo: string;
//// bar(a: string): void;
////}
////function f (_spec: any) {}
////function g (_spec: Test) {}
////[|f(() => {
//// g({});
//// g(
//// {});
//// g(
//// {}
//// );
////});|]

verify.codeFixAll({
fixId: "fixMissingProperties",
fixAllDescription: ts.Diagnostics.Add_all_missing_properties.message,
newFileContent: `interface Test {
foo: string;
bar(a: string): void;
}
function f (_spec: any) {}
function g (_spec: Test) {}
f(() => {
g({
foo: "",
bar: function(a: string): void {
throw new Error("Function not implemented.");
}
});
g(
{
foo: "",
bar: function(a: string): void {
throw new Error("Function not implemented.");
}
});
g(
{
foo: "",
bar: function(a: string): void {
throw new Error("Function not implemented.");
}
}
);
});`,
});