Skip to content

Commit

Permalink
Add contextual type and completions for ImportCall options argument
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Sep 20, 2021
1 parent 463138a commit 9accee9
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
140 changes: 140 additions & 0 deletions tests/baselines/reference/completionImportCallAssertion.baseline
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
}
]
13 changes: 13 additions & 0 deletions tests/cases/fourslash/completionImportCallAssertion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />

// @target: esnext
// @module: esnext

// @filename: main.ts
////import("./other.json", {/*0*/});
////import("./other.json", { asse/*1*/});

// @filename: other.json
////{}

verify.baselineCompletions();

0 comments on commit 9accee9

Please sign in to comment.