Skip to content

Commit 45e44ce

Browse files
author
Andy
authored
Merge pull request #14984 from Microsoft/backtick
Support backticks in require calls
2 parents 4186bd5 + b81c183 commit 45e44ce

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,7 @@ namespace ts {
15641564
}
15651565

15661566
function resolveExternalModuleNameWorker(location: Node, moduleReferenceExpression: Expression, moduleNotFoundError: DiagnosticMessage, isForAugmentation = false): Symbol {
1567-
if (moduleReferenceExpression.kind !== SyntaxKind.StringLiteral) {
1567+
if (moduleReferenceExpression.kind !== SyntaxKind.StringLiteral && moduleReferenceExpression.kind !== SyntaxKind.NoSubstitutionTemplateLiteral) {
15681568
return;
15691569
}
15701570

src/compiler/utilities.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,17 +1404,24 @@ namespace ts {
14041404

14051405
/**
14061406
* Returns true if the node is a CallExpression to the identifier 'require' with
1407-
* exactly one argument.
1407+
* exactly one argument (of the form 'require("name")').
14081408
* This function does not test if the node is in a JavaScript file or not.
1409-
*/
1410-
export function isRequireCall(expression: Node, checkArgumentIsStringLiteral: boolean): expression is CallExpression {
1411-
// of the form 'require("name")'
1412-
const isRequire = expression.kind === SyntaxKind.CallExpression &&
1413-
(<CallExpression>expression).expression.kind === SyntaxKind.Identifier &&
1414-
(<Identifier>(<CallExpression>expression).expression).text === "require" &&
1415-
(<CallExpression>expression).arguments.length === 1;
1416-
1417-
return isRequire && (!checkArgumentIsStringLiteral || (<CallExpression>expression).arguments[0].kind === SyntaxKind.StringLiteral);
1409+
*/
1410+
export function isRequireCall(callExpression: Node, checkArgumentIsStringLiteral: boolean): callExpression is CallExpression {
1411+
if (callExpression.kind !== SyntaxKind.CallExpression) {
1412+
return false;
1413+
}
1414+
const { expression, arguments } = callExpression as CallExpression;
1415+
1416+
if (expression.kind !== SyntaxKind.Identifier || (expression as Identifier).text !== "require") {
1417+
return false;
1418+
}
1419+
1420+
if (arguments.length !== 1) {
1421+
return false;
1422+
}
1423+
const arg = arguments[0];
1424+
return !checkArgumentIsStringLiteral || arg.kind === SyntaxKind.StringLiteral || arg.kind === SyntaxKind.NoSubstitutionTemplateLiteral;
14181425
}
14191426

14201427
export function isSingleOrDoubleQuote(charCode: number) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
///<reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
// @Filename: a.js
5+
//// exports.x = 0;
6+
7+
// @Filename: consumer.js
8+
//// var a = require(`./a`);
9+
//// a./**/;
10+
11+
goTo.marker();
12+
verify.completionListContains("x");

0 commit comments

Comments
 (0)