• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* @internal */
2namespace ts.codefix {
3    const fixId = "addMissingDeclareProperty";
4    const errorCodes = [
5        Diagnostics.Property_0_will_overwrite_the_base_property_in_1_If_this_is_intentional_add_an_initializer_Otherwise_add_a_declare_modifier_or_remove_the_redundant_declaration.code,
6    ];
7
8    registerCodeFix({
9        errorCodes,
10        getCodeActions: function getCodeActionsToAddMissingDeclareOnProperty(context) {
11            const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start));
12            if (changes.length > 0) {
13                return [createCodeFixAction(fixId, changes, Diagnostics.Prefix_with_declare, fixId, Diagnostics.Prefix_all_incorrect_property_declarations_with_declare)];
14            }
15        },
16        fixIds: [fixId],
17        getAllCodeActions: context => {
18            const fixedNodes = new Set<Node>();
19            return codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start, fixedNodes));
20        },
21    });
22
23    function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number, fixedNodes?: Set<Node>) {
24        const token = getTokenAtPosition(sourceFile, pos);
25        if (!isIdentifier(token)) {
26            return;
27        }
28        const declaration = token.parent;
29        if (declaration.kind === SyntaxKind.PropertyDeclaration &&
30            (!fixedNodes || tryAddToSet(fixedNodes, declaration))) {
31            changeTracker.insertModifierBefore(sourceFile, SyntaxKind.DeclareKeyword, declaration);
32        }
33    }
34}
35