Skip to content

Commit

Permalink
💥 no-*-require rules check require.resolve calls as well
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed May 3, 2019
1 parent 528282a commit b94731a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
49 changes: 11 additions & 38 deletions lib/util/get-require-targets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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))) {
Expand Down
16 changes: 15 additions & 1 deletion tests/lib/rules/no-missing-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b94731a

Please sign in to comment.