Skip to content

Commit

Permalink
module: add setter for module.parent
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
aduh95 committed Oct 16, 2020
1 parent 30fb4a0 commit aaf225a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/api/modules.md
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion lib/internal/modules/cjs/loader.js
Expand Up @@ -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) => {
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-module-parent-setter-deprecation.js
@@ -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;

0 comments on commit aaf225a

Please sign in to comment.