| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 | /** * @author Toru Nagashima * See LICENSE file in root directory for full license. */"use strict"const path = require("path")const { getStringIfConstant } = require("eslint-utils")const resolve = require("resolve")const getResolvePaths = require("./get-resolve-paths")const getTryExtensions = require("./get-try-extensions")const ImportTarget = require("./import-target")const stripImportPathParams = require("./strip-import-path-params")/** * Checks whether or not a given node is a callee. * * @param {ASTNode} node - A node to check. * @returns {boolean} `true` if the node is a callee. */function isCallee(node) {    return node.parent.type === "CallExpression" && node.parent.callee === node}/** * Gets references of "require". * * @param {escope.Scope} scope - The global scope. * @returns {escope.Reference[]} References of "require". */function getReferencesOfRequire(scope) {    const variable = scope.set.get("require")    if (!variable) {        // Not found.        return []    }    return variable.references}/** * Gets a list of `require()` targets. * * Core modules of Node.js (e.g. `fs`, `http`) are excluded. * * @param {RuleContext} context - The rule context. * @param {boolean} includeCore - The flag to include core modules. * @returns {ImportTarget[]} A list of found target's information. */module.exports = function getRequireTargets(context, includeCore) {    const retv = []    const basedir = path.dirname(path.resolve(context.getFilename()))    const paths = getResolvePaths(context)    const references = getReferencesOfRequire(context.getScope())    const extensions = getTryExtensions(context)    const options = { basedir, paths, extensions }    for (const reference of references) {        const node = reference.identifier        // Skips if it's not a call of `require`.        if (!isCallee(node)) {            continue        }        // Gets the target module.        const targetNode = node.parent.arguments[0]        const rawName = getStringIfConstant(targetNode)        const name = rawName && stripImportPathParams(rawName)        if (name && (includeCore || !resolve.isCore(name))) {            retv.push(new ImportTarget(targetNode, name, options))        }    }    return retv}
 |