fix(44837): add graceful recovery for require completions (#45151)

This commit is contained in:
Oleksandr T
2021-07-27 01:26:43 +03:00
committed by GitHub
parent 9334a15f5f
commit 85e0e5ad07
2 changed files with 37 additions and 2 deletions

View File

@@ -184,14 +184,14 @@ namespace ts.Completions.StringCompletions {
case SyntaxKind.CallExpression:
case SyntaxKind.NewExpression:
if (!isRequireCall(parent, /*checkArgumentIsStringLiteralLike*/ false) && !isImportCall(parent)) {
if (!isRequireCallArgument(node) && !isImportCall(parent)) {
const argumentInfo = SignatureHelp.getArgumentInfoForCompletions(node, position, sourceFile);
// Get string literal completions from specialized signatures of the target
// i.e. declare function f(a: 'A');
// f("/*completion position*/")
return argumentInfo ? getStringLiteralCompletionsFromSignature(argumentInfo, typeChecker) : fromContextualType();
}
// falls through (is `require("")` or `import("")`)
// falls through (is `require("")` or `require(""` or `import("")`)
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ExportDeclaration:
@@ -759,4 +759,14 @@ namespace ts.Completions.StringCompletions {
function containsSlash(fragment: string) {
return stringContains(fragment, directorySeparator);
}
/**
* Matches
* require(""
* require("")
*/
function isRequireCallArgument(node: Node) {
return isCallExpression(node.parent) && firstOrUndefined(node.parent.arguments) === node
&& isIdentifier(node.parent.expression) && node.parent.expression.escapedText === "require";
}
}

View File

@@ -0,0 +1,25 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @Filename: foo.js
////var foo = require("/**/"
////
////foo();
////
/////**
//// * @return {void}
//// */
////function foo() {
////}
// @Filename: package.json
//// { "dependencies": { "fake-module": "latest" } }
// @Filename: node_modules/fake-module/index.js
/////* fake-module */
verify.completions({
marker: "",
exact: ["fake-module"],
isNewIdentifierLocation: true
})