mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
fix(44837): add graceful recovery for require completions (#45151)
This commit is contained in:
@@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
25
tests/cases/fourslash/completionsInRequire.ts
Normal file
25
tests/cases/fourslash/completionsInRequire.ts
Normal 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
|
||||
})
|
||||
Reference in New Issue
Block a user