From 5f6be494fc11caf8dcf900807c5b6b646fcd8d74 Mon Sep 17 00:00:00 2001 From: Kar Rui Lau Date: Mon, 30 Sep 2024 21:59:38 +0800 Subject: [PATCH] fix: do nothing if target does not exist in getters map (#155) --- lib/register.js | 7 ++++++- test/hook/v14-double-hook.mjs | 13 +++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/register.js b/lib/register.js index 9c06cb3..2de012b 100644 --- a/lib/register.js +++ b/lib/register.js @@ -17,7 +17,12 @@ const proxyHandler = { if (name === Symbol.toStringTag) { return 'Module' } - return getters.get(target)[name]() + + const getter = getters.get(target)[name] + + if (typeof getter === 'function') { + return getter() + } }, defineProperty (target, property, descriptor) { diff --git a/test/hook/v14-double-hook.mjs b/test/hook/v14-double-hook.mjs index feb997c..2098e69 100644 --- a/test/hook/v14-double-hook.mjs +++ b/test/hook/v14-double-hook.mjs @@ -19,6 +19,15 @@ Hook([toWrap], (exports) => { } }) -const { foo } = await import('../fixtures/foo.mjs') +Hook([toWrap], (exports) => { + const shouldNotExist = exports.default + exports = function () { + return shouldNotExist() + } +}) + +const imp = await import('../fixtures/foo.mjs') -strictEqual(foo(), 'foo-first-second') +strictEqual(imp.foo(), 'foo-first-second') +// This should not throw! +strictEqual(imp.default, undefined)