Merge pull request #11370 from Microsoft/requireAsFunctionInExternalModule

Do not treat local resolution of require call as external module
This commit is contained in:
Sheetal Nandi 2016-10-17 16:37:03 -07:00 committed by GitHub
commit 28452d5a7b
6 changed files with 128 additions and 2 deletions

View File

@ -2072,7 +2072,9 @@ namespace ts {
function setCommonJsModuleIndicator(node: Node) {
if (!file.commonJsModuleIndicator) {
file.commonJsModuleIndicator = node;
bindSourceFileAsExternalModule();
if (!file.externalModuleIndicator) {
bindSourceFileAsExternalModule();
}
}
}

View File

@ -12791,7 +12791,10 @@ namespace ts {
}
// In JavaScript files, calls to any identifier 'require' are treated as external module imports
if (isInJavaScriptFile(node) && isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) {
if (isInJavaScriptFile(node) &&
isRequireCall(node, /*checkArgumentIsStringLiteral*/true) &&
// Make sure require is not a local function
!resolveName(node.expression, (<Identifier>node.expression).text, SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined)) {
return resolveExternalModuleTypeByLiteral(<StringLiteral>node.arguments[0]);
}

View File

@ -0,0 +1,37 @@
//// [tests/cases/compiler/requireAsFunctionInExternalModule.ts] ////
//// [c.js]
export default function require(a) { }
export function has(a) { return true }
//// [m.js]
import require, { has } from "./c"
export function hello() { }
if (has('ember-debug')) {
require('ember-debug');
}
//// [m2.ts]
import { hello } from "./m";
hello();
//// [c.js]
"use strict";
function require(a) { }
exports.__esModule = true;
exports["default"] = require;
function has(a) { return true; }
exports.has = has;
//// [m.js]
"use strict";
var c_1 = require("./c");
function hello() { }
exports.hello = hello;
if (c_1.has('ember-debug')) {
c_1["default"]('ember-debug');
}
//// [m2.js]
"use strict";
var m_1 = require("./m");
m_1.hello();

View File

@ -0,0 +1,31 @@
=== tests/cases/compiler/c.js ===
export default function require(a) { }
>require : Symbol(require, Decl(c.js, 0, 0))
>a : Symbol(a, Decl(c.js, 0, 32))
export function has(a) { return true }
>has : Symbol(has, Decl(c.js, 0, 38))
>a : Symbol(a, Decl(c.js, 1, 20))
=== tests/cases/compiler/m.js ===
import require, { has } from "./c"
>require : Symbol(require, Decl(m.js, 0, 6))
>has : Symbol(has, Decl(m.js, 0, 17))
export function hello() { }
>hello : Symbol(hello, Decl(m.js, 0, 34))
if (has('ember-debug')) {
>has : Symbol(has, Decl(m.js, 0, 17))
require('ember-debug');
>require : Symbol(require, Decl(m.js, 0, 6))
}
=== tests/cases/compiler/m2.ts ===
import { hello } from "./m";
>hello : Symbol(hello, Decl(m2.ts, 0, 8))
hello();
>hello : Symbol(hello, Decl(m2.ts, 0, 8))

View File

@ -0,0 +1,37 @@
=== tests/cases/compiler/c.js ===
export default function require(a) { }
>require : (a: any) => void
>a : any
export function has(a) { return true }
>has : (a: any) => boolean
>a : any
>true : true
=== tests/cases/compiler/m.js ===
import require, { has } from "./c"
>require : (a: any) => void
>has : (a: any) => boolean
export function hello() { }
>hello : () => void
if (has('ember-debug')) {
>has('ember-debug') : boolean
>has : (a: any) => boolean
>'ember-debug' : "ember-debug"
require('ember-debug');
>require('ember-debug') : void
>require : (a: any) => void
>'ember-debug' : "ember-debug"
}
=== tests/cases/compiler/m2.ts ===
import { hello } from "./m";
>hello : () => void
hello();
>hello() : void
>hello : () => void

View File

@ -0,0 +1,16 @@
// @allowjs: true
// @outDir: dist
// @Filename: c.js
export default function require(a) { }
export function has(a) { return true }
// @Filename: m.js
import require, { has } from "./c"
export function hello() { }
if (has('ember-debug')) {
require('ember-debug');
}
// @Filename: m2.ts
import { hello } from "./m";
hello();