From 3e0a467164ad6821a8bd135f3f64f328d1ed35a8 Mon Sep 17 00:00:00 2001 From: Joshua Koudys Date: Fri, 23 Mar 2018 08:50:21 +0530 Subject: [PATCH] feat: Avoid infinite loop scenario for self-linked module parents --- index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 3710319..5110066 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,11 @@ var find_package_json = function(location) { return location; } +// Get the parent module, or null if parent links itself +var get_parent_module = function(module) { + return (module !== module.parent) ? module.parent : null; +} + // Find the package.json object of the module closest up the module call tree that contains name in that module's peerOptionalDependencies var find_package_json_with_name = function(name) { // Walk up the module call tree until we find a module containing name in its peerOptionalDependencies @@ -33,7 +38,7 @@ var find_package_json_with_name = function(name) { location = currentModule.filename; var location = find_package_json(location) if (!location) { - currentModule = currentModule.parent; + currentModule = get_parent_module(currentModule); continue; } @@ -44,7 +49,7 @@ var find_package_json_with_name = function(name) { // Check whether this package.json contains peerOptionalDependencies containing the name we're searching for if (!object.peerOptionalDependencies || (object.peerOptionalDependencies && !object.peerOptionalDependencies[parts[0]])) { - currentModule = currentModule.parent; + currentModule = get_parent_module(currentModule); continue; } found = true;