mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-12 04:17:34 -06:00
Merge pull request #6708 from Microsoft/port-6704
Ports #6704 into release-1.8
This commit is contained in:
commit
7ed7c43d28
@ -1462,7 +1462,7 @@ namespace ts {
|
||||
function bindCallExpression(node: CallExpression) {
|
||||
// We're only inspecting call expressions to detect CommonJS modules, so we can skip
|
||||
// this check if we've already seen the module indicator
|
||||
if (!file.commonJsModuleIndicator && isRequireCall(node)) {
|
||||
if (!file.commonJsModuleIndicator && isRequireCall(node, /*checkArgumentIsStringLiteral*/false)) {
|
||||
setCommonJsModuleIndicator(node);
|
||||
}
|
||||
}
|
||||
|
||||
@ -10210,7 +10210,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
// In JavaScript files, calls to any identifier 'require' are treated as external module imports
|
||||
if (isInJavaScriptFile(node) && isRequireCall(node)) {
|
||||
if (isInJavaScriptFile(node) && isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) {
|
||||
return resolveExternalModuleTypeByLiteral(<StringLiteral>node.arguments[0]);
|
||||
}
|
||||
|
||||
|
||||
@ -990,7 +990,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
function collectRequireCalls(node: Node): void {
|
||||
if (isRequireCall(node)) {
|
||||
if (isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) {
|
||||
(imports || (imports = [])).push(<StringLiteral>(<CallExpression>node).arguments[0]);
|
||||
}
|
||||
else {
|
||||
|
||||
@ -1086,12 +1086,14 @@ namespace ts {
|
||||
* exactly one argument.
|
||||
* This function does not test if the node is in a JavaScript file or not.
|
||||
*/
|
||||
export function isRequireCall(expression: Node): expression is CallExpression {
|
||||
export function isRequireCall(expression: Node, checkArgumentIsStringLiteral: boolean): expression is CallExpression {
|
||||
// of the form 'require("name")'
|
||||
return expression.kind === SyntaxKind.CallExpression &&
|
||||
(<CallExpression>expression).expression.kind === SyntaxKind.Identifier &&
|
||||
(<Identifier>(<CallExpression>expression).expression).text === "require" &&
|
||||
(<CallExpression>expression).arguments.length === 1;
|
||||
const isRequire = expression.kind === SyntaxKind.CallExpression &&
|
||||
(<CallExpression>expression).expression.kind === SyntaxKind.Identifier &&
|
||||
(<Identifier>(<CallExpression>expression).expression).text === "require" &&
|
||||
(<CallExpression>expression).arguments.length === 1;
|
||||
|
||||
return isRequire && (!checkArgumentIsStringLiteral || (<CallExpression>expression).arguments[0].kind === SyntaxKind.StringLiteral);
|
||||
}
|
||||
|
||||
/// Given a BinaryExpression, returns SpecialPropertyAssignmentKind for the various kinds of property
|
||||
|
||||
10
tests/baselines/reference/dynamicRequire.js
Normal file
10
tests/baselines/reference/dynamicRequire.js
Normal file
@ -0,0 +1,10 @@
|
||||
//// [a.js]
|
||||
|
||||
function foo(name) {
|
||||
var s = require("t/" + name)
|
||||
}
|
||||
|
||||
//// [a_out.js]
|
||||
function foo(name) {
|
||||
var s = require("t/" + name);
|
||||
}
|
||||
10
tests/baselines/reference/dynamicRequire.symbols
Normal file
10
tests/baselines/reference/dynamicRequire.symbols
Normal file
@ -0,0 +1,10 @@
|
||||
=== tests/cases/compiler/a.js ===
|
||||
|
||||
function foo(name) {
|
||||
>foo : Symbol(foo, Decl(a.js, 0, 0))
|
||||
>name : Symbol(name, Decl(a.js, 1, 13))
|
||||
|
||||
var s = require("t/" + name)
|
||||
>s : Symbol(s, Decl(a.js, 2, 7))
|
||||
>name : Symbol(name, Decl(a.js, 1, 13))
|
||||
}
|
||||
14
tests/baselines/reference/dynamicRequire.types
Normal file
14
tests/baselines/reference/dynamicRequire.types
Normal file
@ -0,0 +1,14 @@
|
||||
=== tests/cases/compiler/a.js ===
|
||||
|
||||
function foo(name) {
|
||||
>foo : (name: any) => void
|
||||
>name : any
|
||||
|
||||
var s = require("t/" + name)
|
||||
>s : any
|
||||
>require("t/" + name) : any
|
||||
>require : any
|
||||
>"t/" + name : string
|
||||
>"t/" : string
|
||||
>name : any
|
||||
}
|
||||
8
tests/cases/compiler/dynamicRequire.ts
Normal file
8
tests/cases/compiler/dynamicRequire.ts
Normal file
@ -0,0 +1,8 @@
|
||||
// @allowJs: true
|
||||
// @module: amd
|
||||
// @out: a_out.js
|
||||
|
||||
// @filename: a.js
|
||||
function foo(name) {
|
||||
var s = require("t/" + name)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user