Bind the source file as external module only if not already done

Return type of require call is from external module resolution only if
it doesnt resolve to local module
Fixes #10931
This commit is contained in:
Sheetal Nandi
2016-10-04 12:25:07 -07:00
parent a70624d415
commit a830c844b7
5 changed files with 75 additions and 22 deletions

View File

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

View File

@@ -12535,7 +12535,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 | SymbolFlags.ExportValue, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined)) {
return resolveExternalModuleTypeByLiteral(<StringLiteral>node.arguments[0]);
}

View File

@@ -1,20 +0,0 @@
tests/cases/compiler/m2.ts(1,10): error TS2305: Module '"tests/cases/compiler/m"' has no exported member 'hello'.
==== tests/cases/compiler/c.js (0 errors) ====
export default function require(a) { }
export function has(a) { return true }
==== tests/cases/compiler/m.js (0 errors) ====
import require, { has } from "./c"
export function hello() { }
if (has('ember-debug')) {
require('ember-debug');
}
==== tests/cases/compiler/m2.ts (1 errors) ====
import { hello } from "./m";
~~~~~
!!! error TS2305: Module '"tests/cases/compiler/m"' has no exported member 'hello'.
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