Skip to content

Commit

Permalink
Fixes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
jupiter committed Oct 20, 2017
1 parent 5ae05a5 commit 861cf1d
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions decache.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,20 @@ require.searchCache = function (moduleName, callback) {

// Check if the module has been resolved and found within
// the cache no else so #ignore else http://git.io/vtgMI
/* istanbul ignore else */
/* istanbul ignore else */
if (mod && ((mod = require.cache[mod]) !== undefined)) {
// Recursively go over the results
(function run(mod) {
// Go over each of the module's children and
// run over it
mod.children.forEach(function (child) {
run(child);
// Prevent infinite recursion
/* istanbul ignore next */
if (child.parent === mod) {

This comment has been minimized.

Copy link
@ksuszka

ksuszka Nov 2, 2017

this change makes little sense, it will be true for direct dependences. It "solves" the infinite loop issue by not doing recursive search.
I would propose something more verbose:

function searchCache (moduleName, callback) {
    // Resolve the module identified by the specified name
    let mod = require.resolve(moduleName);
    const visited = {};

    // Check if the module has been resolved and found within
    // the cache
    if (mod && ((mod = require.cache[mod]) !== undefined)) {
        // Recursively go over the results
        (function run (module) {
            visited[module.id] = true;
            // Go over each of the module's children and
            // run over it
            module.children.forEach(function (child) {
                if (!visited[child.id]) {
                    run(child);
                }
            });

            // Call the specified callback providing the
            // found module
            callback(module);
        })(mod);
    }
};

This comment has been minimized.

Copy link
@jupiter

jupiter Nov 2, 2017

Author Contributor

Ugh, that is true. I think it was intended to be if (child === mod) { and then it won't solve the original issue. Your suggestion should fix the issue properly. Do you want to submit a PR or shall I?

return;
} else {
run(child);
}
});

// Call the specified callback providing the
Expand Down

0 comments on commit 861cf1d

Please sign in to comment.