module: add setter for module.parent

PR-URL: https://github.com/nodejs/node/pull/35522
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Ujjwal Sharma <ryzokuken@disroot.org>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Antoine du Hamel 2020-10-06 17:50:20 +02:00
parent 30fb4a015d
commit aaf225a2a0
3 changed files with 26 additions and 2 deletions

View File

@ -913,7 +913,7 @@ deprecated:
The module that first required this one, or `null` if the current module is the
entry point of the current process, or `undefined` if the module was loaded by
something that is not a CommonJS module (E.G.: REPL or `import`). Read only.
something that is not a CommonJS module (E.G.: REPL or `import`).
### `module.path`
<!-- YAML

View File

@ -223,13 +223,24 @@ ObjectDefineProperty(Module, 'wrapper', {
function getModuleParent() {
return moduleParentCache.get(this);
}
function setModuleParent(value) {
moduleParentCache.set(this, value);
}
ObjectDefineProperty(Module.prototype, 'parent', {
get: pendingDeprecation ? deprecate(
getModuleParent,
'module.parent is deprecated due to accuracy issues. Please use ' +
'require.main to find program entry point instead.',
'DEP0144'
) : getModuleParent
) : getModuleParent,
set: pendingDeprecation ? deprecate(
setModuleParent,
'module.parent is deprecated due to accuracy issues. Please use ' +
'require.main to find program entry point instead.',
'DEP0144'
) : setModuleParent,
});
let debug = require('internal/util/debuglog').debuglog('module', (fn) => {

View File

@ -0,0 +1,13 @@
// Flags: --pending-deprecation
'use strict';
const common = require('../common');
common.expectWarning(
'DeprecationWarning',
'module.parent is deprecated due to accuracy issues. Please use ' +
'require.main to find program entry point instead.',
'DEP0144'
);
module.parent = undefined;