Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Add onlyRelativePath option to no-missing-require (fixes #100) #241

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/rules/no-missing-require.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}]
}
}
Expand Down Expand Up @@ -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).
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/no-missing-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ module.exports = {
allowModules: getAllowModules.schema,
tryExtensions: getTryExtensions.schema,
resolvePaths: getResolvePaths.schema,
onlyRelativePath: {
type: "boolean",
default: false,
},
},
additionalProperties: false,
},
Expand Down
6 changes: 6 additions & 0 deletions lib/util/check-existence.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/rules/no-missing-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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.'],
},
],
})

Expand Down