Skip to content

Correct super() Code Fix Formatting on Single-Line Constructor #20405

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

Closed
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
17 changes: 15 additions & 2 deletions src/services/codefixes/fixConstructorForDerivedNeedSuperCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,22 @@ namespace ts.codefix {
return undefined;
}

const body = (<ConstructorDeclaration>token.parent).body;

if (!body) {
return undefined;
}

const superCall = startOnNewLine(createStatement(createCall(createSuper(), /*typeArguments*/ undefined, /*argumentsArray*/ emptyArray)));
const statements: Statement[] = [superCall];
addRange(statements, body.statements);

const fixedBody = getMutableClone(body);
fixedBody.multiLine = true;
fixedBody.statements = createNodeArray(statements);

const changeTracker = textChanges.ChangeTracker.fromContext(context);
const superCall = createStatement(createCall(createSuper(), /*typeArguments*/ undefined, /*argumentsArray*/ emptyArray));
changeTracker.insertNodeAfter(sourceFile, getOpenBrace(<ConstructorDeclaration>token.parent, sourceFile), superCall, { suffix: context.newLineCharacter });
changeTracker.replaceNode(sourceFile, body, fixedBody);

return [{
description: getLocaleSpecificMessage(Diagnostics.Add_missing_super_call),
Expand Down
8 changes: 4 additions & 4 deletions tests/cases/fourslash/codeFixSuperCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
////class Base{
////}
////class C extends Base{
//// constructor() {[|
//// |]}
//// constructor() [|{
//// }|]
////}
// TODO: GH#18445
verify.rangeAfterCodeFix(`
verify.rangeAfterCodeFix(`{\r
super();\r
`, /*includeWhitespace*/ true);
}`, /*includeWhitespace*/ true);
11 changes: 11 additions & 0 deletions tests/cases/fourslash/codeFixSuperCallSingleLine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />

////class Base{
////}
////class C extends Base{
//// constructor() [|{}|]
////}
// TODO: GH#18445
verify.rangeAfterCodeFix(`{\r
super();\r
}`, /*includeWhitespace*/ true);
12 changes: 12 additions & 0 deletions tests/cases/fourslash/codeFixSuperCallSingleLineWithBody.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path='fourslash.ts' />

////class Base{
////}
////class C extends Base{
//// constructor() [|{ const t = 0; }|]
////}
// TODO: GH#18445
verify.rangeAfterCodeFix(`{\r
super();\r
const t = 0;\r
}`, /*includeWhitespace*/ true);