summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2020-04-24 23:08:57 +0200
committerBeth Griggs <Bethany.Griggs@uk.ibm.com>2020-04-28 23:00:41 +0100
commit4abc45a4b9d4a6e5ad682ec85ea9f5988671392e (patch)
treeac0968f751f41596a610bed57456114f8965d17c
parent4035cbe63134dd72070341b6df2af65cef9f2bdd (diff)
downloadnode-new-4abc45a4b9d4a6e5ad682ec85ea9f5988671392e.tar.gz
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: https://github.com/nodejs/node/pull/29935 Fixes: https://github.com/nodejs/node/issues/33046 PR-URL: https://github.com/nodejs/node/pull/33048 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com>
-rw-r--r--lib/internal/modules/cjs/loader.js7
-rw-r--r--test/fixtures/cycles/warning-esm-half-transpiled-a.js1
-rw-r--r--test/fixtures/cycles/warning-esm-half-transpiled-b.js2
-rw-r--r--test/parallel/test-module-circular-dependency-warning.js7
4 files changed, 15 insertions, 2 deletions
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index 96988a6a47..f2715d07cb 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -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;
diff --git a/test/fixtures/cycles/warning-esm-half-transpiled-a.js b/test/fixtures/cycles/warning-esm-half-transpiled-a.js
new file mode 100644
index 0000000000..f4c17f9e4a
--- /dev/null
+++ b/test/fixtures/cycles/warning-esm-half-transpiled-a.js
@@ -0,0 +1 @@
+require('./warning-esm-half-transpiled-b.js');
diff --git a/test/fixtures/cycles/warning-esm-half-transpiled-b.js b/test/fixtures/cycles/warning-esm-half-transpiled-b.js
new file mode 100644
index 0000000000..ab31fdbffc
--- /dev/null
+++ b/test/fixtures/cycles/warning-esm-half-transpiled-b.js
@@ -0,0 +1,2 @@
+const a = require('./warning-esm-half-transpiled-a.js');
+a.__esModule;
diff --git a/test/parallel/test-module-circular-dependency-warning.js b/test/parallel/test-module-circular-dependency-warning.js
index 1089397882..1fe82c2b02 100644
--- a/test/parallel/test-module-circular-dependency-warning.js
+++ b/test/parallel/test-module-circular-dependency-warning.js
@@ -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);