diff --git a/docs/rules/no-missing-require.md b/docs/rules/no-missing-require.md index 16ef307e..32e493ec 100644 --- a/docs/rules/no-missing-require.md +++ b/docs/rules/no-missing-require.md @@ -42,7 +42,8 @@ var foo = require(FOO_NAME); "node/no-missing-require": ["error", { "allowModules": [], "resolvePaths": ["/path/to/a/modules/directory"], - "tryExtensions": [".js", ".json", ".node"] + "tryExtensions": [".js", ".json", ".node"], + "onlyRelativePath": true }] } } @@ -80,6 +81,12 @@ When an import path does not exist, this rule checks whether or not any of `path Default is `[".js", ".json", ".node"]`. +#### onlyRelativePath + +Checks only relative path. + +Default is `false` + ### Shared Settings The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings). diff --git a/lib/rules/no-missing-require.js b/lib/rules/no-missing-require.js index a4bd3f98..48d55d35 100644 --- a/lib/rules/no-missing-require.js +++ b/lib/rules/no-missing-require.js @@ -29,6 +29,10 @@ module.exports = { allowModules: getAllowModules.schema, tryExtensions: getTryExtensions.schema, resolvePaths: getResolvePaths.schema, + onlyRelativePath: { + type: "boolean", + default: false, + }, }, additionalProperties: false, }, diff --git a/lib/util/check-existence.js b/lib/util/check-existence.js index 99d0d50d..7a51a5b5 100644 --- a/lib/util/check-existence.js +++ b/lib/util/check-existence.js @@ -19,9 +19,15 @@ const getAllowModules = require("./get-allow-modules") */ module.exports = function checkForExistence(context, targets) { const allowed = new Set(getAllowModules(context)) + const onlyRelativePath = + (context.options && + context.options[0] && + context.options[0].onlyRelativePath) || + false for (const target of targets) { const missingModule = + !onlyRelativePath && target.moduleName != null && !allowed.has(target.moduleName) && target.filePath == null diff --git a/tests/lib/rules/no-missing-require.js b/tests/lib/rules/no-missing-require.js index d72b0c3e..7170dfbb 100644 --- a/tests/lib/rules/no-missing-require.js +++ b/tests/lib/rules/no-missing-require.js @@ -216,6 +216,20 @@ ruleTester.run("no-missing-require", rule, { code: "require.resolve('eslint');", env: { node: true }, }, + + // onlyRelativePath option + { + filename: fixture("test.js"), + code: "require('no-exist-package-0');", + options: [{ onlyRelativePath: true }], + env: { node: true }, + }, + { + filename: fixture("test.js"), + code: "require('./a.js');", + options: [{ onlyRelativePath: true }], + env: { node: true }, + }, ], invalid: [ { @@ -292,6 +306,15 @@ ruleTester.run("no-missing-require", rule, { env: { node: true }, errors: ['"no-exist-package-0" is not found.'], }, + + // onlyRelativePath option + { + filename: fixture("test.js"), + code: "require('./c');", + options: [{ onlyRelativePath: true }], + env: { node: true }, + errors: ['"./c" is not found.'], + }, ], })