• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* @internal */
2namespace ts.codefix {
3    const fixIdAddMissingTypeof = "fixAddModuleReferTypeMissingTypeof";
4    const fixId = fixIdAddMissingTypeof;
5    const errorCodes = [Diagnostics.Module_0_does_not_refer_to_a_type_but_is_used_as_a_type_here_Did_you_mean_typeof_import_0.code];
6
7    registerCodeFix({
8        errorCodes,
9        getCodeActions: function getCodeActionsToAddMissingTypeof(context) {
10            const { sourceFile, span } = context;
11            const importType = getImportTypeNode(sourceFile, span.start);
12            const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, importType));
13            return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_typeof, fixId, Diagnostics.Add_missing_typeof)];
14        },
15        fixIds: [fixId],
16        getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) =>
17            doChange(changes, context.sourceFile, getImportTypeNode(diag.file, diag.start))),
18    });
19
20    function getImportTypeNode(sourceFile: SourceFile, pos: number): ImportTypeNode {
21        const token = getTokenAtPosition(sourceFile, pos);
22        Debug.assert(token.kind === SyntaxKind.ImportKeyword, "This token should be an ImportKeyword");
23        Debug.assert(token.parent.kind === SyntaxKind.ImportType, "Token parent should be an ImportType");
24        return token.parent as ImportTypeNode;
25    }
26
27    function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, importType: ImportTypeNode) {
28        const newTypeNode = factory.updateImportTypeNode(importType, importType.argument, importType.assertions, importType.qualifier, importType.typeArguments, /* isTypeOf */ true);
29        changes.replaceNode(sourceFile, importType, newTypeNode);
30    }
31}
32