From 9accee9666472ecfd83b1f247eaae655209b2c84 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 20 Sep 2021 13:25:44 -0700 Subject: [PATCH] Add contextual type and completions for ImportCall options argument --- src/compiler/checker.ts | 11 +- .../completionImportCallAssertion.baseline | 140 ++++++++++++++++++ .../completionImportCallAssertion.ts | 13 ++ 3 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/completionImportCallAssertion.baseline create mode 100644 tests/cases/fourslash/completionImportCallAssertion.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3d3f356626b56..0d01c27b23020 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25558,6 +25558,12 @@ namespace ts { } function getContextualTypeForArgumentAtIndex(callTarget: CallLikeExpression, argIndex: number): Type { + if (isImportCall(callTarget)) { + return argIndex === 0 ? stringType : + argIndex === 1 ? getGlobalImportCallOptionsType(/*reportErrors*/ false) : + anyType; + } + // If we're already in the process of resolving the given signature, don't resolve again as // that could cause infinite recursion. Instead, return anySignature. const signature = getNodeLinks(callTarget).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(callTarget); @@ -26018,10 +26024,6 @@ namespace ts { case SyntaxKind.AwaitExpression: return getContextualTypeForAwaitOperand(parent as AwaitExpression, contextFlags); case SyntaxKind.CallExpression: - if ((parent as CallExpression).expression.kind === SyntaxKind.ImportKeyword) { - return stringType; - } - /* falls through */ case SyntaxKind.NewExpression: return getContextualTypeForArgument(parent as CallExpression | NewExpression, node); case SyntaxKind.TypeAssertionExpression: @@ -30617,6 +30619,7 @@ namespace ts { if (node.arguments.length === 0) { return createPromiseReturnType(node, anyType); } + const specifier = node.arguments[0]; const specifierType = checkExpressionCached(specifier); const optionsType = node.arguments.length > 1 ? checkExpressionCached(node.arguments[1]) : undefined; diff --git a/tests/baselines/reference/completionImportCallAssertion.baseline b/tests/baselines/reference/completionImportCallAssertion.baseline new file mode 100644 index 0000000000000..6009fb9e67f87 --- /dev/null +++ b/tests/baselines/reference/completionImportCallAssertion.baseline @@ -0,0 +1,140 @@ +[ + { + "marker": { + "fileName": "/tests/cases/fourslash/main.ts", + "position": 24, + "name": "0" + }, + "completionList": { + "isGlobalCompletion": false, + "isMemberCompletion": true, + "isNewIdentifierLocation": false, + "entries": [ + { + "name": "assert", + "kind": "property", + "kindModifiers": "declare,optional", + "sortText": "12", + "displayParts": [ + { + "text": "(", + "kind": "punctuation" + }, + { + "text": "property", + "kind": "text" + }, + { + "text": ")", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "ImportCallOptions", + "kind": "interfaceName" + }, + { + "text": ".", + "kind": "punctuation" + }, + { + "text": "assert", + "kind": "propertyName" + }, + { + "text": "?", + "kind": "punctuation" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "ImportAssertions", + "kind": "interfaceName" + } + ], + "documentation": [] + } + ] + } + }, + { + "marker": { + "fileName": "/tests/cases/fourslash/main.ts", + "position": 57, + "name": "1" + }, + "completionList": { + "isGlobalCompletion": false, + "isMemberCompletion": true, + "isNewIdentifierLocation": false, + "optionalReplacementSpan": { + "start": 53, + "length": 4 + }, + "entries": [ + { + "name": "assert", + "kind": "property", + "kindModifiers": "declare,optional", + "sortText": "12", + "displayParts": [ + { + "text": "(", + "kind": "punctuation" + }, + { + "text": "property", + "kind": "text" + }, + { + "text": ")", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "ImportCallOptions", + "kind": "interfaceName" + }, + { + "text": ".", + "kind": "punctuation" + }, + { + "text": "assert", + "kind": "propertyName" + }, + { + "text": "?", + "kind": "punctuation" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "ImportAssertions", + "kind": "interfaceName" + } + ], + "documentation": [] + } + ] + } + } +] \ No newline at end of file diff --git a/tests/cases/fourslash/completionImportCallAssertion.ts b/tests/cases/fourslash/completionImportCallAssertion.ts new file mode 100644 index 0000000000000..650629155186a --- /dev/null +++ b/tests/cases/fourslash/completionImportCallAssertion.ts @@ -0,0 +1,13 @@ +/// + +// @target: esnext +// @module: esnext + +// @filename: main.ts +////import("./other.json", {/*0*/}); +////import("./other.json", { asse/*1*/}); + +// @filename: other.json +////{} + +verify.baselineCompletions();