mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-14 19:16:17 -06:00
Merge pull request #11370 from Microsoft/requireAsFunctionInExternalModule
Do not treat local resolution of require call as external module
This commit is contained in:
commit
28452d5a7b
@ -2072,7 +2072,9 @@ namespace ts {
|
||||
function setCommonJsModuleIndicator(node: Node) {
|
||||
if (!file.commonJsModuleIndicator) {
|
||||
file.commonJsModuleIndicator = node;
|
||||
bindSourceFileAsExternalModule();
|
||||
if (!file.externalModuleIndicator) {
|
||||
bindSourceFileAsExternalModule();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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]);
|
||||
}
|
||||
|
||||
|
||||
@ -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();
|
||||
@ -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))
|
||||
|
||||
@ -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
tests/cases/compiler/requireAsFunctionInExternalModule.ts
Normal file
16
tests/cases/compiler/requireAsFunctionInExternalModule.ts
Normal 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();
|
||||
Loading…
x
Reference in New Issue
Block a user