From fa9fce57c5a1d2a9c86f93e5e06daa0e533750bd Mon Sep 17 00:00:00 2001 From: JSap0914 Date: Tue, 23 Jun 2026 14:31:53 +0900 Subject: [PATCH] fix: guard against undefined module in ModuleCollection.get() (fix #2164) When isRegistered() is called with a path whose intermediate segments do not exist (e.g. ['a', 'b', 'c'] where 'a' is not registered), the reduce inside get() called .getChild() on undefined, throwing a TypeError. The fix short-circuits the reduce by returning undefined when the accumulated module is falsy, matching the existing null-guard already present in isRegistered() itself. Added a regression test for the 3-level non-existent path case. --- src/module/module-collection.js | 2 +- test/unit/module/module-collection.spec.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/module/module-collection.js b/src/module/module-collection.js index 75e641fce..43cfa694a 100644 --- a/src/module/module-collection.js +++ b/src/module/module-collection.js @@ -9,7 +9,7 @@ export default class ModuleCollection { get (path) { return path.reduce((module, key) => { - return module.getChild(key) + return module && module.getChild(key) }, this.root) } diff --git a/test/unit/module/module-collection.spec.js b/test/unit/module/module-collection.spec.js index 514965cab..fd1922f7e 100644 --- a/test/unit/module/module-collection.spec.js +++ b/test/unit/module/module-collection.spec.js @@ -96,6 +96,8 @@ describe('ModuleCollection', () => { expect(collection.isRegistered(['a', 'b'])).toBe(true) expect(collection.isRegistered(['c'])).toBe(false) expect(collection.isRegistered(['c', 'd'])).toBe(false) + // 3-level deep path where intermediate module does not exist (fix #2164) + expect(collection.isRegistered(['c', 'd', 'e'])).toBe(false) }) it('does not unregister initial modules', () => {