From b94731af0b89c9f0e0ca11298a1cbea40c5dacf0 Mon Sep 17 00:00:00 2001 From: Toru Nagashima Date: Fri, 3 May 2019 16:58:00 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A5=20no-*-require=20rules=20check=20r?= =?UTF-8?q?equire.resolve=20calls=20as=20well?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/util/get-require-targets.js | 49 ++++++--------------------- tests/lib/rules/no-missing-require.js | 16 ++++++++- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/lib/util/get-require-targets.js b/lib/util/get-require-targets.js index 12238faa..4da2c70b 100644 --- a/lib/util/get-require-targets.js +++ b/lib/util/get-require-targets.js @@ -5,38 +5,13 @@ "use strict" const path = require("path") -const { getStringIfConstant } = require("eslint-utils") +const { CALL, ReferenceTracker, 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. * @@ -50,20 +25,18 @@ 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 tracker = new ReferenceTracker(context.getScope()) + const references = tracker.iterateGlobalReferences({ + require: { + [CALL]: true, + resolve: { [CALL]: true }, + }, + }) + + for (const { node } of references) { + const targetNode = node.arguments[0] const rawName = getStringIfConstant(targetNode) const name = rawName && stripImportPathParams(rawName) if (name && (includeCore || !resolve.isCore(name))) { diff --git a/tests/lib/rules/no-missing-require.js b/tests/lib/rules/no-missing-require.js index 45ddbc16..d72b0c3e 100644 --- a/tests/lib/rules/no-missing-require.js +++ b/tests/lib/rules/no-missing-require.js @@ -209,6 +209,13 @@ ruleTester.run("no-missing-require", rule, { options: [{ allowModules: ["jquery.cookie"] }], env: { node: true }, }, + + // require.resolve + { + filename: fixture("test.js"), + code: "require.resolve('eslint');", + env: { node: true }, + }, ], invalid: [ { @@ -277,10 +284,17 @@ ruleTester.run("no-missing-require", rule, { env: { node: true }, errors: ['"./A" is not found.'], }, + + // require.resolve + { + filename: fixture("test.js"), + code: "require.resolve('no-exist-package-0');", + env: { node: true }, + errors: ['"no-exist-package-0" is not found.'], + }, ], }) -/*eslint-env mocha */ describe("On specific working directory:", () => { const filename = fixture("test.js") let originalDir = null