Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not treat local resolution of require call as external module #11370

Merged
merged 4 commits into from
Oct 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2072,7 +2072,9 @@ namespace ts {
function setCommonJsModuleIndicator(node: Node) {
if (!file.commonJsModuleIndicator) {
file.commonJsModuleIndicator = node;
bindSourceFileAsExternalModule();
if (!file.externalModuleIndicator) {
bindSourceFileAsExternalModule();
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down
37 changes: 37 additions & 0 deletions tests/baselines/reference/requireAsFunctionInExternalModule.js
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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))

37 changes: 37 additions & 0 deletions tests/baselines/reference/requireAsFunctionInExternalModule.types
Original file line number Diff line number Diff line change
@@ -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

16 changes: 16 additions & 0 deletions tests/cases/compiler/requireAsFunctionInExternalModule.ts
Original file line number Diff line number Diff line change
@@ -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();