From a625a678f0100762f7ee7e7ed06c2f0248f04ddb Mon Sep 17 00:00:00 2001 From: hyex Date: Sat, 8 Aug 2020 18:02:16 +0900 Subject: [PATCH 1/4] feat: add onlyRelativePath option --- lib/rules/no-missing-require.js | 4 ++++ 1 file changed, 4 insertions(+) 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, }, From 6021364969201386cf0d99aed93bea455e9c8353 Mon Sep 17 00:00:00 2001 From: hyex Date: Sat, 8 Aug 2020 18:39:34 +0900 Subject: [PATCH 2/4] feat: add logic to handle option --- lib/util/check-existence.js | 6 ++++++ 1 file changed, 6 insertions(+) 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 From bff91a541b624a8c83cc2a6ea3e9733735706c44 Mon Sep 17 00:00:00 2001 From: hyex Date: Sun, 9 Aug 2020 17:09:56 +0900 Subject: [PATCH 3/4] Chore: Add tests for onlyRelativePath option --- tests/lib/rules/no-missing-require.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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.'], + }, ], }) From b388f2a20fece3eb2c962fad9a9569e782424c76 Mon Sep 17 00:00:00 2001 From: hyex Date: Sun, 9 Aug 2020 22:25:49 +0900 Subject: [PATCH 4/4] Docs: add description of onlyRelativePath option --- docs/rules/no-missing-require.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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).