summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/util/module-resolver.js
blob: 470a54f7c45713508ab64cefabd6788bcefd2df6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
 * @fileoverview Implements the Node.js require.resolve algorithm
 * @author Nicholas C. Zakas
 */

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const Module = require("module");

//------------------------------------------------------------------------------
// Private
//------------------------------------------------------------------------------

const DEFAULT_OPTIONS = {

    /*
     * module.paths is an array of paths to search for resolving things relative
     * to this file. Module.globalPaths contains all of the special Node.js
     * directories that can also be searched for modules.
     *
     * Need to check for existence of module.paths because Jest seems not to
     * include it. See https://github.com/eslint/eslint/issues/5791.
     */
    lookupPaths: module.paths ? module.paths.concat(Module.globalPaths) : Module.globalPaths.concat()
};

/**
 * Resolves modules based on a set of options.
 */
class ModuleResolver {

    /**
     * Resolves modules based on a set of options.
     * @param {Object} options The options for resolving modules.
     * @param {string[]} options.lookupPaths An array of paths to include in the
     *      lookup with the highest priority paths coming first.
     */
    constructor(options) {
        this.options = Object.assign({}, DEFAULT_OPTIONS, options || {});
    }

    /**
     * Resolves the file location of a given module relative to the configured
     * lookup paths.
     * @param {string} name The module name to resolve.
     * @param {string} extraLookupPath An extra path to look into for the module.
     *      This path is used with the highest priority.
     * @returns {string} The resolved file path for the module.
     * @throws {Error} If the module cannot be resolved.
     */
    resolve(name, extraLookupPath) {

        /*
         * First, clone the lookup paths so we're not messing things up for
         * subsequent calls to this function. Then, move the extraLookupPath to the
         * top of the lookup paths list so it will be searched first.
         */
        const lookupPaths = this.options.lookupPaths.concat();

        lookupPaths.unshift(extraLookupPath);

        /**
         * Module._findPath is an internal method to Node.js, then one they use to
         * lookup file paths when require() is called. So, we are hooking into the
         * exact same logic that Node.js uses.
         */
        const result = Module._findPath(name, lookupPaths); // eslint-disable-line no-underscore-dangle

        if (!result) {
            throw new Error(`Cannot find module '${name}'`);
        }

        return result;
    }
}

//------------------------------------------------------------------------------
// Public API
//------------------------------------------------------------------------------

module.exports = ModuleResolver;