Skip to content

Commit

Permalink
module: do not warn when accessing __esModule of unfinished exports
Browse files Browse the repository at this point in the history
Since this property access is performed by generated code, and not
used for accessing the actual exports of a module (and because
transpilers generally define it as the first key of `module.exports`
when it *is* present), it should be okay to allow it.

Refs: #29935
Fixes: #33046

PR-URL: #33048
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Zeyu Yang <[email protected]>
  • Loading branch information
addaleax authored and BethGriggs committed Apr 28, 2020
1 parent 3fb3836 commit 45ba9be
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,13 +824,16 @@ function emitCircularRequireWarning(prop) {
// warns when non-existend properties are accessed.
const CircularRequirePrototypeWarningProxy = new Proxy({}, {
get(target, prop) {
if (prop in target) return target[prop];
// Allow __esModule access in any case because it is used in the output
// of transpiled code to determine whether something comes from an
// ES module, and is not used as a regular key of `module.exports`.
if (prop in target || prop === '__esModule') return target[prop];
emitCircularRequireWarning(prop);
return undefined;
},

getOwnPropertyDescriptor(target, prop) {
if (ObjectPrototypeHasOwnProperty(target, prop))
if (ObjectPrototypeHasOwnProperty(target, prop) || prop === '__esModule')
return ObjectGetOwnPropertyDescriptor(target, prop);
emitCircularRequireWarning(prop);
return undefined;
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/cycles/warning-esm-half-transpiled-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./warning-esm-half-transpiled-b.js');
2 changes: 2 additions & 0 deletions test/fixtures/cycles/warning-esm-half-transpiled-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const a = require('./warning-esm-half-transpiled-a.js');
a.__esModule;
7 changes: 7 additions & 0 deletions test/parallel/test-module-circular-dependency-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ assert.strictEqual(Object.getPrototypeOf(classExport).name, 'Parent');
const esmTranspiledExport =
require(fixtures.path('cycles', 'warning-esm-transpiled-a.js'));
assert.strictEqual(esmTranspiledExport.__esModule, true);

// If module.exports.__esModule is being accessed but is not present, e.g.
// because only the one of the files is a transpiled ES module, no warning
// should be emitted.
const halfTranspiledExport =
require(fixtures.path('cycles', 'warning-esm-half-transpiled-a.js'));
assert.strictEqual(halfTranspiledExport.__esModule, undefined);

0 comments on commit 45ba9be

Please sign in to comment.