• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* @internal */
2namespace ts.codefix {
3    const fixName = "invalidImportSyntax";
4
5    function getCodeFixesForImportDeclaration(context: CodeFixContext, node: ImportDeclaration): CodeFixAction[] {
6        const sourceFile = getSourceFileOfNode(node);
7        const namespace = getNamespaceDeclarationNode(node) as NamespaceImport;
8        const opts = context.program.getCompilerOptions();
9        const variations: CodeFixAction[] = [];
10
11        // import Bluebird from "bluebird";
12        variations.push(createAction(context, sourceFile, node, makeImport(namespace.name, /*namedImports*/ undefined, node.moduleSpecifier, getQuotePreference(sourceFile, context.preferences))));
13
14        if (getEmitModuleKind(opts) === ModuleKind.CommonJS) {
15            // import Bluebird = require("bluebird");
16            variations.push(createAction(context, sourceFile, node, factory.createImportEqualsDeclaration(
17                /*modifiers*/ undefined,
18                /*isTypeOnly*/ false,
19                namespace.name,
20                factory.createExternalModuleReference(node.moduleSpecifier)
21            )));
22        }
23
24        return variations;
25    }
26
27    function createAction(context: CodeFixContext, sourceFile: SourceFile, node: Node, replacement: Node): CodeFixAction {
28        const changes = textChanges.ChangeTracker.with(context, t => t.replaceNode(sourceFile, node, replacement));
29        return createCodeFixActionWithoutFixAll(fixName, changes, [Diagnostics.Replace_import_with_0, changes[0].textChanges[0].newText]);
30    }
31
32    registerCodeFix({
33        errorCodes: [
34            Diagnostics.This_expression_is_not_callable.code,
35            Diagnostics.This_expression_is_not_constructable.code,
36        ],
37        getCodeActions: getActionsForUsageOfInvalidImport
38    });
39
40    function getActionsForUsageOfInvalidImport(context: CodeFixContext): CodeFixAction[] | undefined {
41        const sourceFile = context.sourceFile;
42        const targetKind = Diagnostics.This_expression_is_not_callable.code === context.errorCode ? SyntaxKind.CallExpression : SyntaxKind.NewExpression;
43        const node = findAncestor(getTokenAtPosition(sourceFile, context.span.start), a => a.kind === targetKind) as CallExpression | NewExpression;
44        if (!node) {
45            return [];
46        }
47        const expr = node.expression;
48        return getImportCodeFixesForExpression(context, expr);
49    }
50
51    registerCodeFix({
52        errorCodes: [
53            // The following error codes cover pretty much all assignability errors that could involve an expression
54            Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code,
55            Diagnostics.Type_0_does_not_satisfy_the_constraint_1.code,
56            Diagnostics.Type_0_is_not_assignable_to_type_1.code,
57            Diagnostics.Type_0_is_not_assignable_to_type_1_Two_different_types_with_this_name_exist_but_they_are_unrelated.code,
58            Diagnostics.Type_predicate_0_is_not_assignable_to_1.code,
59            Diagnostics.Property_0_of_type_1_is_not_assignable_to_2_index_type_3.code,
60            Diagnostics._0_index_type_1_is_not_assignable_to_2_index_type_3.code,
61            Diagnostics.Property_0_in_type_1_is_not_assignable_to_the_same_property_in_base_type_2.code,
62            Diagnostics.Property_0_in_type_1_is_not_assignable_to_type_2.code,
63            Diagnostics.Property_0_of_JSX_spread_attribute_is_not_assignable_to_target_property.code,
64            Diagnostics.The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1.code,
65        ],
66        getCodeActions: getActionsForInvalidImportLocation
67    });
68
69    function getActionsForInvalidImportLocation(context: CodeFixContext): CodeFixAction[] | undefined {
70        const sourceFile = context.sourceFile;
71        const node = findAncestor(getTokenAtPosition(sourceFile, context.span.start), a => a.getStart() === context.span.start && a.getEnd() === (context.span.start + context.span.length));
72        if (!node) {
73            return [];
74        }
75        return getImportCodeFixesForExpression(context, node);
76    }
77
78    function getImportCodeFixesForExpression(context: CodeFixContext, expr: Node): CodeFixAction[] | undefined {
79        const type = context.program.getTypeChecker().getTypeAtLocation(expr);
80        if (!(type.symbol && (type.symbol as TransientSymbol).originatingImport)) {
81            return [];
82        }
83        const fixes: CodeFixAction[] = [];
84        const relatedImport = (type.symbol as TransientSymbol).originatingImport!; // TODO: GH#18217
85        if (!isImportCall(relatedImport)) {
86            addRange(fixes, getCodeFixesForImportDeclaration(context, relatedImport));
87        }
88        if (isExpression(expr) && !(isNamedDeclaration(expr.parent) && expr.parent.name === expr)) {
89            const sourceFile = context.sourceFile;
90            const changes = textChanges.ChangeTracker.with(context, t => t.replaceNode(sourceFile, expr, factory.createPropertyAccessExpression(expr, "default"), {}));
91            fixes.push(createCodeFixActionWithoutFixAll(fixName, changes, Diagnostics.Use_synthetic_default_member));
92        }
93        return fixes;
94    }
95}
96