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

modules: add setter for module.parent #35522

Merged
merged 1 commit into from Oct 16, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving this to be on the module itself is needed to make delete work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that trigger a new deprecation warning for each Module instance?

module.parent; // triggers one warning
module.parent?.parent; // triggers another warning?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because the deprecation uses a deprecation code:

if (code !== undefined) {
if (!codesWarned.has(code)) {
process.emitWarning(msg, 'DeprecationWarning', code, deprecated);
codesWarned.add(code);
}
} else {

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make delete work:

Suggested change
) : setModuleParent,
) : setModuleParent,
configurable: true,

});

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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const common = require('../common');
const common = require('../common');
const assert = require('assert');


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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
module.parent = undefined;
module.parent = undefined;
delete module.parent;
assert.strictEqual('parent' in module, false);