Skip to content

No error for require, module.exports or exports in Javascript #23743

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

Merged
merged 7 commits into from
Apr 30, 2018
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
23 changes: 19 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ namespace ts {
const undefinedSymbol = createSymbol(SymbolFlags.Property, "undefined" as __String);
undefinedSymbol.declarations = [];
const argumentsSymbol = createSymbol(SymbolFlags.Property, "arguments" as __String);
const requireSymbol = createSymbol(SymbolFlags.Property, "require" as __String);
const moduleSymbol = createSymbol(SymbolFlags.Property, "module" as __String);

/** This will be set during calls to `getResolvedSignature` where services determines an apparent number of arguments greater than what is actually provided. */
let apparentArgumentCount: number | undefined;
Expand Down Expand Up @@ -1470,7 +1472,17 @@ namespace ts {
result = lookup(globals, name, meaning);
}
}

if (!result) {
if (originalLocation && isInJavaScriptFile(originalLocation) && originalLocation.parent) {
if (isRequireCall(originalLocation.parent, /*checkArgumentIsStringLiteralLike*/ false)) {
return requireSymbol;
}
if (isIdentifier(originalLocation) && isPropertyAccessExpression(originalLocation.parent) &&
originalLocation.escapedText === "module" && originalLocation.parent.name.escapedText === "exports") {
return moduleSymbol;
}
}
}
if (!result) {
if (nameNotFoundMessage) {
if (!errorLocation ||
Expand Down Expand Up @@ -4683,6 +4695,10 @@ namespace ts {
if (symbol.flags & SymbolFlags.Prototype) {
return links.type = getTypeOfPrototypeProperty(symbol);
}
// CommonsJS require/module/exports all have type any.
if (symbol === requireSymbol || symbol === moduleSymbol) {
return links.type = anyType;
}
// Handle catch clause variables
const declaration = symbol.valueDeclaration;
if (isCatchClauseVariableDeclarationOrBindingElement(declaration)) {
Expand Down Expand Up @@ -18856,11 +18872,10 @@ namespace ts {
// Make sure require is not a local function
if (!isIdentifier(node.expression)) return Debug.fail();
const resolvedRequire = resolveName(node.expression, node.expression.escapedText, SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ true);
if (!resolvedRequire) {
// project does not contain symbol named 'require' - assume commonjs require
if (resolvedRequire === requireSymbol) {
return true;
}
// project includes symbol named 'require' - make sure that it it ambient and local non-alias
// project includes symbol named 'require' - make sure that it is ambient and local non-alias
if (resolvedRequire.flags & SymbolFlags.Alias) {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/dynamicRequire.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ function foo(name) {

var s = require("t/" + name)
>s : Symbol(s, Decl(a.js, 1, 7))
>require : Symbol(require)
>name : Symbol(name, Decl(a.js, 0, 13))
}
10 changes: 2 additions & 8 deletions tests/baselines/reference/exportNestedNamespaces2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
tests/cases/conformance/salsa/first.js(1,11): error TS2304: Cannot find name 'require'.
tests/cases/conformance/salsa/first.js(2,9): error TS2339: Property 'formatters' does not exist on type 'typeof import("tests/cases/conformance/salsa/first")'.
tests/cases/conformance/salsa/second.js(1,11): error TS2304: Cannot find name 'require'.
tests/cases/conformance/salsa/second.js(2,9): error TS2339: Property 'formatters' does not exist on type 'typeof import("tests/cases/conformance/salsa/second")'.


==== tests/cases/conformance/salsa/mod.js (0 errors) ====
// Based on a pattern from adonis
exports.formatters = {}
==== tests/cases/conformance/salsa/first.js (2 errors) ====
==== tests/cases/conformance/salsa/first.js (1 errors) ====
exports = require('./mod')
~~~~~~~
!!! error TS2304: Cannot find name 'require'.
exports.formatters.j = function (v) {
~~~~~~~~~~
!!! error TS2339: Property 'formatters' does not exist on type 'typeof import("tests/cases/conformance/salsa/first")'.
return v
}
==== tests/cases/conformance/salsa/second.js (2 errors) ====
==== tests/cases/conformance/salsa/second.js (1 errors) ====
exports = require('./mod')
~~~~~~~
!!! error TS2304: Cannot find name 'require'.
exports.formatters.o = function (v) {
~~~~~~~~~~
!!! error TS2339: Property 'formatters' does not exist on type 'typeof import("tests/cases/conformance/salsa/second")'.
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/exportNestedNamespaces2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exports.formatters = {}
=== tests/cases/conformance/salsa/first.js ===
exports = require('./mod')
>exports : Symbol("tests/cases/conformance/salsa/first", Decl(first.js, 0, 0))
>require : Symbol(require)
>'./mod' : Symbol("tests/cases/conformance/salsa/mod", Decl(mod.js, 0, 0))

exports.formatters.j = function (v) {
Expand All @@ -20,6 +21,7 @@ exports.formatters.j = function (v) {
=== tests/cases/conformance/salsa/second.js ===
exports = require('./mod')
>exports : Symbol("tests/cases/conformance/salsa/second", Decl(second.js, 0, 0))
>require : Symbol(require)
>'./mod' : Symbol("tests/cases/conformance/salsa/mod", Decl(mod.js, 0, 0))

exports.formatters.o = function (v) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
tests/cases/compiler/a.js(1,15): error TS2304: Cannot find name 'require'.
tests/cases/compiler/a.js(4,1): error TS2686: 'Puppeteer' refers to a UMD global, but the current file is a module. Consider adding an import instead.


==== tests/cases/compiler/a.js (2 errors) ====
==== tests/cases/compiler/a.js (1 errors) ====
const other = require('./other');
~~~~~~~
!!! error TS2304: Cannot find name 'require'.
/** @type {Puppeteer.Keyboard} */
var ppk;
Puppeteer.connect;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
=== tests/cases/compiler/a.js ===
const other = require('./other');
>other : Symbol(other, Decl(a.js, 0, 5))
>require : Symbol(require)
>'./other' : Symbol("tests/cases/compiler/other", Decl(other.d.ts, 0, 0))

/** @type {Puppeteer.Keyboard} */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
tests/cases/conformance/jsdoc/use.js(3,7): error TS2345: Argument of type '"no"' is not assignable to parameter of type 'number'.
tests/cases/conformance/jsdoc/use.js(4,7): error TS2345: Argument of type '"also no"' is not assignable to parameter of type 'number'.
tests/cases/conformance/jsdoc/use.js(5,7): error TS2345: Argument of type '0' is not assignable to parameter of type 'string'.
tests/cases/conformance/jsdoc/use.js(6,7): error TS2345: Argument of type '1' is not assignable to parameter of type 'string'.
tests/cases/conformance/jsdoc/use.js(2,7): error TS2345: Argument of type '"no"' is not assignable to parameter of type 'number'.
tests/cases/conformance/jsdoc/use.js(3,7): error TS2345: Argument of type '"also no"' is not assignable to parameter of type 'number'.
tests/cases/conformance/jsdoc/use.js(4,7): error TS2345: Argument of type '0' is not assignable to parameter of type 'string'.
tests/cases/conformance/jsdoc/use.js(5,7): error TS2345: Argument of type '1' is not assignable to parameter of type 'string'.


==== tests/cases/conformance/jsdoc/use.js (4 errors) ====
/// <reference path='./types.d.ts'/>
var mod = require('./mod');
mod.f('no')
~~~~
Expand All @@ -20,13 +19,7 @@ tests/cases/conformance/jsdoc/use.js(6,7): error TS2345: Argument of type '1' is
~
!!! error TS2345: Argument of type '1' is not assignable to parameter of type 'string'.

==== tests/cases/conformance/jsdoc/types.d.ts (0 errors) ====
declare function require(name: string): any;
declare var exports: any;
declare var module: { exports: any };
==== tests/cases/conformance/jsdoc/mod.js (0 errors) ====
/// <reference path='./types.d.ts'/>

/** @param {number} n */
exports.f = exports.g = function fg(n) {
return n + 1
Expand Down
71 changes: 27 additions & 44 deletions tests/baselines/reference/jsdocTypeFromChainedAssignment2.symbols
Original file line number Diff line number Diff line change
@@ -1,73 +1,56 @@
=== tests/cases/conformance/jsdoc/use.js ===
/// <reference path='./types.d.ts'/>
var mod = require('./mod');
>mod : Symbol(mod, Decl(use.js, 1, 3))
>require : Symbol(require, Decl(types.d.ts, 0, 0))
>mod : Symbol(mod, Decl(use.js, 0, 3))
>require : Symbol(require)
>'./mod' : Symbol("tests/cases/conformance/jsdoc/mod", Decl(mod.js, 0, 0))

mod.f('no')
>mod.f : Symbol(f, Decl(mod.js, 0, 0))
>mod : Symbol(mod, Decl(use.js, 1, 3))
>mod : Symbol(mod, Decl(use.js, 0, 3))
>f : Symbol(f, Decl(mod.js, 0, 0))

mod.g('also no')
>mod.g : Symbol(g, Decl(mod.js, 3, 11))
>mod : Symbol(mod, Decl(use.js, 1, 3))
>g : Symbol(g, Decl(mod.js, 3, 11))
>mod.g : Symbol(g, Decl(mod.js, 1, 11))
>mod : Symbol(mod, Decl(use.js, 0, 3))
>g : Symbol(g, Decl(mod.js, 1, 11))

mod.h(0)
>mod.h : Symbol(h, Decl(mod.js, 5, 1))
>mod : Symbol(mod, Decl(use.js, 1, 3))
>h : Symbol(h, Decl(mod.js, 5, 1))
>mod.h : Symbol(h, Decl(mod.js, 3, 1))
>mod : Symbol(mod, Decl(use.js, 0, 3))
>h : Symbol(h, Decl(mod.js, 3, 1))

mod.i(1)
>mod.i : Symbol(i, Decl(mod.js, 7, 18))
>mod : Symbol(mod, Decl(use.js, 1, 3))
>i : Symbol(i, Decl(mod.js, 7, 18))

=== tests/cases/conformance/jsdoc/types.d.ts ===
declare function require(name: string): any;
>require : Symbol(require, Decl(types.d.ts, 0, 0))
>name : Symbol(name, Decl(types.d.ts, 0, 25))

declare var exports: any;
>exports : Symbol(exports, Decl(types.d.ts, 1, 11))

declare var module: { exports: any };
>module : Symbol(module, Decl(types.d.ts, 2, 11))
>exports : Symbol(exports, Decl(types.d.ts, 2, 21))
>mod.i : Symbol(i, Decl(mod.js, 5, 18))
>mod : Symbol(mod, Decl(use.js, 0, 3))
>i : Symbol(i, Decl(mod.js, 5, 18))

=== tests/cases/conformance/jsdoc/mod.js ===
/// <reference path='./types.d.ts'/>

/** @param {number} n */
exports.f = exports.g = function fg(n) {
>exports.f : Symbol(f, Decl(mod.js, 0, 0))
>exports : Symbol(f, Decl(mod.js, 0, 0))
>f : Symbol(f, Decl(mod.js, 0, 0))
>exports.g : Symbol(g, Decl(mod.js, 3, 11))
>exports : Symbol(g, Decl(mod.js, 3, 11))
>g : Symbol(g, Decl(mod.js, 3, 11))
>fg : Symbol(fg, Decl(mod.js, 3, 23))
>n : Symbol(n, Decl(mod.js, 3, 36))
>exports.g : Symbol(g, Decl(mod.js, 1, 11))
>exports : Symbol(g, Decl(mod.js, 1, 11))
>g : Symbol(g, Decl(mod.js, 1, 11))
>fg : Symbol(fg, Decl(mod.js, 1, 23))
>n : Symbol(n, Decl(mod.js, 1, 36))

return n + 1
>n : Symbol(n, Decl(mod.js, 3, 36))
>n : Symbol(n, Decl(mod.js, 1, 36))
}
/** @param {string} mom */
module.exports.h = module.exports.i = function hi(mom) {
>module.exports : Symbol(h, Decl(mod.js, 5, 1))
>module : Symbol(module, Decl(types.d.ts, 2, 11))
>exports : Symbol(exports, Decl(types.d.ts, 2, 21))
>h : Symbol(h, Decl(mod.js, 5, 1))
>module.exports : Symbol(i, Decl(mod.js, 7, 18))
>module : Symbol(module, Decl(types.d.ts, 2, 11))
>exports : Symbol(exports, Decl(types.d.ts, 2, 21))
>i : Symbol(i, Decl(mod.js, 7, 18))
>hi : Symbol(hi, Decl(mod.js, 7, 37))
>mom : Symbol(mom, Decl(mod.js, 7, 50))
>module.exports : Symbol(h, Decl(mod.js, 3, 1))
>module : Symbol(module)
>h : Symbol(h, Decl(mod.js, 3, 1))
>module.exports : Symbol(i, Decl(mod.js, 5, 18))
>module : Symbol(module)
>i : Symbol(i, Decl(mod.js, 5, 18))
>hi : Symbol(hi, Decl(mod.js, 5, 37))
>mom : Symbol(mom, Decl(mod.js, 5, 50))

return `hi, ${mom}!`;
>mom : Symbol(mom, Decl(mod.js, 7, 50))
>mom : Symbol(mom, Decl(mod.js, 5, 50))
}

21 changes: 3 additions & 18 deletions tests/baselines/reference/jsdocTypeFromChainedAssignment2.types
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
=== tests/cases/conformance/jsdoc/use.js ===
/// <reference path='./types.d.ts'/>
var mod = require('./mod');
>mod : typeof import("tests/cases/conformance/jsdoc/mod")
>require('./mod') : typeof import("tests/cases/conformance/jsdoc/mod")
>require : (name: string) => any
>require : any
>'./mod' : "./mod"

mod.f('no')
Expand Down Expand Up @@ -34,21 +33,7 @@ mod.i(1)
>i : (mom: string) => string
>1 : 1

=== tests/cases/conformance/jsdoc/types.d.ts ===
declare function require(name: string): any;
>require : (name: string) => any
>name : string

declare var exports: any;
>exports : any

declare var module: { exports: any };
>module : { exports: any; }
>exports : any

=== tests/cases/conformance/jsdoc/mod.js ===
/// <reference path='./types.d.ts'/>

/** @param {number} n */
exports.f = exports.g = function fg(n) {
>exports.f = exports.g = function fg(n) { return n + 1} : (n: number) => number
Expand All @@ -73,13 +58,13 @@ module.exports.h = module.exports.i = function hi(mom) {
>module.exports.h = module.exports.i = function hi(mom) { return `hi, ${mom}!`;} : (mom: string) => string
>module.exports.h : any
>module.exports : any
>module : { exports: any; }
>module : any
>exports : any
>h : any
>module.exports.i = function hi(mom) { return `hi, ${mom}!`;} : (mom: string) => string
>module.exports.i : any
>module.exports : any
>module : { exports: any; }
>module : any
>exports : any
>i : any
>function hi(mom) { return `hi, ${mom}!`;} : (mom: string) => string
Expand Down
Loading