• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* @internal */
2namespace ts.codefix {
3    const fixId = "constructorForDerivedNeedSuperCall";
4    const errorCodes = [Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call.code];
5    registerCodeFix({
6        errorCodes,
7        getCodeActions(context) {
8            const { sourceFile, span } = context;
9            const ctr = getNode(sourceFile, span.start);
10            const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, ctr));
11            return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_super_call, fixId, Diagnostics.Add_all_missing_super_calls)];
12        },
13        fixIds: [fixId],
14        getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) =>
15            doChange(changes, context.sourceFile, getNode(diag.file, diag.start))),
16    });
17
18    function getNode(sourceFile: SourceFile, pos: number): ConstructorDeclaration {
19        const token = getTokenAtPosition(sourceFile, pos);
20        Debug.assert(isConstructorDeclaration(token.parent), "token should be at the constructor declaration");
21        return token.parent;
22    }
23
24    function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, ctr: ConstructorDeclaration) {
25        const superCall = factory.createExpressionStatement(factory.createCallExpression(factory.createSuper(), /*typeArguments*/ undefined, /*argumentsArray*/ emptyArray));
26        changes.insertNodeAtConstructorStart(sourceFile, ctr, superCall);
27    }
28}
29