Skip to content

Commit 85e0e5a

Browse files
authored
fix(44837): add graceful recovery for require completions (microsoft#45151)
1 parent 9334a15 commit 85e0e5a

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/services/stringCompletions.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,14 @@ namespace ts.Completions.StringCompletions {
184184

185185
case SyntaxKind.CallExpression:
186186
case SyntaxKind.NewExpression:
187-
if (!isRequireCall(parent, /*checkArgumentIsStringLiteralLike*/ false) && !isImportCall(parent)) {
187+
if (!isRequireCallArgument(node) && !isImportCall(parent)) {
188188
const argumentInfo = SignatureHelp.getArgumentInfoForCompletions(node, position, sourceFile);
189189
// Get string literal completions from specialized signatures of the target
190190
// i.e. declare function f(a: 'A');
191191
// f("/*completion position*/")
192192
return argumentInfo ? getStringLiteralCompletionsFromSignature(argumentInfo, typeChecker) : fromContextualType();
193193
}
194-
// falls through (is `require("")` or `import("")`)
194+
// falls through (is `require("")` or `require(""` or `import("")`)
195195

196196
case SyntaxKind.ImportDeclaration:
197197
case SyntaxKind.ExportDeclaration:
@@ -759,4 +759,14 @@ namespace ts.Completions.StringCompletions {
759759
function containsSlash(fragment: string) {
760760
return stringContains(fragment, directorySeparator);
761761
}
762+
763+
/**
764+
* Matches
765+
* require(""
766+
* require("")
767+
*/
768+
function isRequireCallArgument(node: Node) {
769+
return isCallExpression(node.parent) && firstOrUndefined(node.parent.arguments) === node
770+
&& isIdentifier(node.parent.expression) && node.parent.expression.escapedText === "require";
771+
}
762772
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowJs: true
4+
// @Filename: foo.js
5+
////var foo = require("/**/"
6+
////
7+
////foo();
8+
////
9+
/////**
10+
//// * @return {void}
11+
//// */
12+
////function foo() {
13+
////}
14+
15+
// @Filename: package.json
16+
//// { "dependencies": { "fake-module": "latest" } }
17+
18+
// @Filename: node_modules/fake-module/index.js
19+
/////* fake-module */
20+
21+
verify.completions({
22+
marker: "",
23+
exact: ["fake-module"],
24+
isNewIdentifierLocation: true
25+
})

0 commit comments

Comments
 (0)