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

libdefs: add typings for node's built-in Module module #8540

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2399,9 +2399,11 @@ declare var module: {
id: string,
filename: string,
loaded: boolean,
parent: any,
children: Array<any>,
builtinModules: Array<string>,
parent?: typeof module,
children: typeof module[],
builtinModules: string[],
path: string;
paths: string[];
...
};
declare var require: {
Expand Down
53 changes: 53 additions & 0 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,59 @@ type net$connectOptions = {
...
};

declare module "module" {
declare class Module extends module {
constructor(id: string, parent?: typeof module): void;
globalPaths: string[];
load(fileName: string): void;
syncBuiltinESMExports(): void;

static runMain(): void;
static wrap(code: string): string;

/**
* Deprecated since: v12.2.0. Please use createRequire() instead.
*/
static createRequireFromPath(path: string): (request: string) => {|[export: string]: any|};
static createRequire(path: string | URL): (request: string) => {|[export: string]: any|};
static _cache: {|[key: string]: Module|};
static _pathCache: {|[cacheKey: string]: string|};
static _nodeModulePaths(from: string): string[];
static _initPaths(): void;
static findPnpApi(lookupSource: string): PnpApi | null;

static _findPath(
request: string,
paths?: string[],
isMain?: boolean,
): string | false;

static _extensions: {|
[extension: string]: (module: Module, fileName: string) => void,
|};

static _resolveLookupPaths(
request: string,
parent?: Module,
): string[] | null;

static _load(
request: string,
parent?: Module,
isMain?: boolean,
): {|[export: string]: any|};

static _resolveFilename: (
request: string,
parent?: Module,
isMain?: boolean,
options?: {|paths?: string[]|},
) => string | false;
}

declare module.exports: typeof Module;
}

declare module "net" {

declare class Server extends net$Server {}
Expand Down
60 changes: 30 additions & 30 deletions tests/enums/enums.exp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,8 +1240,8 @@ References:
exhaustive-check.js:3:6
3| enum E {
^ [2]
<BUILTINS>/core.js:2449:3
2449| isValid(input: ?TRepresentation): boolean,
<BUILTINS>/core.js:2451:3
2451| isValid(input: ?TRepresentation): boolean,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [3]


Expand Down Expand Up @@ -1850,14 +1850,14 @@ Cannot get `E.nonExistent` because property `nonExistent` is missing in `$EnumPr
^^^^^^^^^^^

References:
<BUILTINS>/core.js:2447:43
<BUILTINS>/core.js:2449:43
v-
2447| type $EnumProto<TEnum, TRepresentation> = {|
2448| cast(input: ?TRepresentation): void | TEnum,
2449| isValid(input: ?TRepresentation): boolean,
2450| members(): Iterable<TEnum>,
2451| __proto__: null,
2452| |}
2449| type $EnumProto<TEnum, TRepresentation> = {|
2450| cast(input: ?TRepresentation): void | TEnum,
2451| isValid(input: ?TRepresentation): boolean,
2452| members(): Iterable<TEnum>,
2453| __proto__: null,
2454| |}
-^ [1]


Expand All @@ -1870,14 +1870,14 @@ Cannot call `E.nonExistent` because property `nonExistent` is missing in `$EnumP
^^^^^^^^^^^

References:
<BUILTINS>/core.js:2447:43
<BUILTINS>/core.js:2449:43
v-
2447| type $EnumProto<TEnum, TRepresentation> = {|
2448| cast(input: ?TRepresentation): void | TEnum,
2449| isValid(input: ?TRepresentation): boolean,
2450| members(): Iterable<TEnum>,
2451| __proto__: null,
2452| |}
2449| type $EnumProto<TEnum, TRepresentation> = {|
2450| cast(input: ?TRepresentation): void | TEnum,
2451| isValid(input: ?TRepresentation): boolean,
2452| members(): Iterable<TEnum>,
2453| __proto__: null,
2454| |}
-^ [1]


Expand All @@ -1904,14 +1904,14 @@ Cannot call `E.A` because property `A` is missing in `$EnumProto` [1]. [prop-mis
^

References:
<BUILTINS>/core.js:2447:43
<BUILTINS>/core.js:2449:43
v-
2447| type $EnumProto<TEnum, TRepresentation> = {|
2448| cast(input: ?TRepresentation): void | TEnum,
2449| isValid(input: ?TRepresentation): boolean,
2450| members(): Iterable<TEnum>,
2451| __proto__: null,
2452| |}
2449| type $EnumProto<TEnum, TRepresentation> = {|
2450| cast(input: ?TRepresentation): void | TEnum,
2451| isValid(input: ?TRepresentation): boolean,
2452| members(): Iterable<TEnum>,
2453| __proto__: null,
2454| |}
-^ [1]


Expand All @@ -1924,14 +1924,14 @@ Cannot call `E.toString` because property `toString` is missing in `$EnumProto`
^^^^^^^^

References:
<BUILTINS>/core.js:2447:43
<BUILTINS>/core.js:2449:43
v-
2447| type $EnumProto<TEnum, TRepresentation> = {|
2448| cast(input: ?TRepresentation): void | TEnum,
2449| isValid(input: ?TRepresentation): boolean,
2450| members(): Iterable<TEnum>,
2451| __proto__: null,
2452| |}
2449| type $EnumProto<TEnum, TRepresentation> = {|
2450| cast(input: ?TRepresentation): void | TEnum,
2451| isValid(input: ?TRepresentation): boolean,
2452| members(): Iterable<TEnum>,
2453| __proto__: null,
2454| |}
-^ [1]


Expand Down
4 changes: 2 additions & 2 deletions tests/get_def_enums/get_def_enums.exp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ library.js:4:3,4:5

main.js:23:13
Flags:
[LIB] core.js:2448:3,2448:6
[LIB] core.js:2450:3,2450:6

main.js:26:13
Flags:
[LIB] core.js:2449:3,2449:9
[LIB] core.js:2451:3,2451:9

36 changes: 36 additions & 0 deletions tests/node_tests/module/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* @flow */

const Module = require('module');

const myModule = new Module('foo');
const invalidModule = new Module(1); // error

// Instance methods
myModule.globalPaths = ['/xyz', '/abc'];
myModule.globalPaths = [1]; // error

myModule.load('bar.js');
(myModule.load('a.js'): Module); // error

// Static methods
Module._cache = {a: myModule, b: new Module('baz')};
Module._cache = {a: 123}; // error

Module._findPath('a', ['./bcd', '../xyz']);
Module._findPath('a', '/bcd'); // error

let paths = Module._resolveLookupPaths('foo');

if (paths) {
(paths: string[]);
} else {
(paths: null);
};

(Module._load('file.js'): {});
(Module._load('file.js'): {[export: string]: () => {}});

Module._resolveFilename('foo.js', 'bar'); // error
let filename = Module._resolveFilename('foo.js', myModule);

(filename: boolean); // error
Loading