From 131555fdcad6a5a4a6f3725887bce4470ec0cf0a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 3 Apr 2017 08:53:49 -0700 Subject: [PATCH 1/2] Support backticks in require calls --- src/compiler/checker.ts | 2 +- src/compiler/utilities.ts | 25 ++++++++++++------- .../javaScriptModulesWithBackticks.ts | 12 +++++++++ 3 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 tests/cases/fourslash/javaScriptModulesWithBackticks.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 014c95eca88..5cb07f2d736 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1564,7 +1564,7 @@ namespace ts { } function resolveExternalModuleNameWorker(location: Node, moduleReferenceExpression: Expression, moduleNotFoundError: DiagnosticMessage, isForAugmentation = false): Symbol { - if (moduleReferenceExpression.kind !== SyntaxKind.StringLiteral) { + if (moduleReferenceExpression.kind !== SyntaxKind.StringLiteral && moduleReferenceExpression.kind !== SyntaxKind.NoSubstitutionTemplateLiteral) { return; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index f0916855986..fd54b64a08a 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1404,17 +1404,24 @@ namespace ts { /** * Returns true if the node is a CallExpression to the identifier 'require' with - * exactly one argument. + * exactly one argument. (of the form 'require("name")') * This function does not test if the node is in a JavaScript file or not. - */ - export function isRequireCall(expression: Node, checkArgumentIsStringLiteral: boolean): expression is CallExpression { - // of the form 'require("name")' - const isRequire = expression.kind === SyntaxKind.CallExpression && - (expression).expression.kind === SyntaxKind.Identifier && - ((expression).expression).text === "require" && - (expression).arguments.length === 1; + */ + export function isRequireCall(callExpression: Node, checkArgumentIsStringLiteral: boolean): callExpression is CallExpression { + if (callExpression.kind !== SyntaxKind.CallExpression) { + return false; + } + const { expression, arguments } = callExpression as CallExpression; - return isRequire && (!checkArgumentIsStringLiteral || (expression).arguments[0].kind === SyntaxKind.StringLiteral); + if (expression.kind !== SyntaxKind.Identifier || (expression as Identifier).text !== "require") { + return false; + } + + if (arguments.length !== 1) { + return false; + } + const arg = arguments[0]; + return !checkArgumentIsStringLiteral || arg.kind === SyntaxKind.StringLiteral || arg.kind === SyntaxKind.NoSubstitutionTemplateLiteral; } export function isSingleOrDoubleQuote(charCode: number) { diff --git a/tests/cases/fourslash/javaScriptModulesWithBackticks.ts b/tests/cases/fourslash/javaScriptModulesWithBackticks.ts new file mode 100644 index 00000000000..b2ce64d3647 --- /dev/null +++ b/tests/cases/fourslash/javaScriptModulesWithBackticks.ts @@ -0,0 +1,12 @@ +/// + +// @allowJs: true +// @Filename: a.js +//// exports.x = 0; + +// @Filename: consumer.js +//// var a = require(`./a`); +//// a./**/; + +goTo.marker(); +verify.completionListContains("x"); From b81c18314dc4cf78498415dce8b1ae1a544b519b Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Mon, 3 Apr 2017 10:17:36 -0700 Subject: [PATCH 2/2] Update utilities.ts --- src/compiler/utilities.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index fd54b64a08a..42d5b76744d 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1404,7 +1404,7 @@ namespace ts { /** * Returns true if the node is a CallExpression to the identifier 'require' with - * exactly one argument. (of the form 'require("name")') + * exactly one argument (of the form 'require("name")'). * This function does not test if the node is in a JavaScript file or not. */ export function isRequireCall(callExpression: Node, checkArgumentIsStringLiteral: boolean): callExpression is CallExpression {