diff --git a/src/services/stringCompletions.ts b/src/services/stringCompletions.ts index ac0c1baeb69..8105a537a76 100644 --- a/src/services/stringCompletions.ts +++ b/src/services/stringCompletions.ts @@ -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"; + } } diff --git a/tests/cases/fourslash/completionsInRequire.ts b/tests/cases/fourslash/completionsInRequire.ts new file mode 100644 index 00000000000..63d0cc10dba --- /dev/null +++ b/tests/cases/fourslash/completionsInRequire.ts @@ -0,0 +1,25 @@ +/// + +// @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 +})