Skip to content

Commit 4abc45a

Browse files
addaleaxBethGriggs
authored andcommitted
module: do not warn when accessing __esModule of unfinished exports
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: #29935Fixes: #33046 PR-URL: #33048 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Zeyu Yang <[email protected]>
1 parent 4035cbe commit 4abc45a

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

‎lib/internal/modules/cjs/loader.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,13 +824,16 @@ function emitCircularRequireWarning(prop){
824824
// warns when non-existend properties are accessed.
825825
constCircularRequirePrototypeWarningProxy=newProxy({},{
826826
get(target,prop){
827-
if(propintarget)returntarget[prop];
827+
// Allow __esModule access in any case because it is used in the output
828+
// of transpiled code to determine whether something comes from an
829+
// ES module, and is not used as a regular key of `module.exports`.
830+
if(propintarget||prop==='__esModule')returntarget[prop];
828831
emitCircularRequireWarning(prop);
829832
returnundefined;
830833
},
831834

832835
getOwnPropertyDescriptor(target,prop){
833-
if(ObjectPrototypeHasOwnProperty(target,prop))
836+
if(ObjectPrototypeHasOwnProperty(target,prop)||prop==='__esModule')
834837
returnObjectGetOwnPropertyDescriptor(target,prop);
835838
emitCircularRequireWarning(prop);
836839
returnundefined;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./warning-esm-half-transpiled-b.js');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
consta=require('./warning-esm-half-transpiled-a.js');
2+
a.__esModule;

‎test/parallel/test-module-circular-dependency-warning.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ assert.strictEqual(Object.getPrototypeOf(classExport).name, 'Parent');
3131
constesmTranspiledExport=
3232
require(fixtures.path('cycles','warning-esm-transpiled-a.js'));
3333
assert.strictEqual(esmTranspiledExport.__esModule,true);
34+
35+
// If module.exports.__esModule is being accessed but is not present, e.g.
36+
// because only the one of the files is a transpiled ES module, no warning
37+
// should be emitted.
38+
consthalfTranspiledExport=
39+
require(fixtures.path('cycles','warning-esm-half-transpiled-a.js'));
40+
assert.strictEqual(halfTranspiledExport.__esModule,undefined);

0 commit comments

Comments
(0)