Skip to content

Commit 47e0cc2

Browse files
Fjandinmysticatea
authored andcommitted
Update: add resolvePaths option to no-missing-require (#84)
1 parent 5634718 commit 47e0cc2

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

docs/rules/no-missing-require.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ var foo = require(FOO_NAME);
3939
"rules": {
4040
"node/no-missing-require": ["error", {
4141
"allowModules": [],
42-
"tryExtensions": [".js", ".json", ".node"]
42+
"tryExtensions": [".js", ".json", ".node"],
43+
"resolvePaths": ["/an/absolute/path"]
4344
}]
4445
}
4546
}
@@ -70,20 +71,30 @@ When an import path does not exist, this rule checks whether or not any of `path
7071

7172
Default is `[".js", ".json", ".node"]`.
7273

74+
### resolvePaths
75+
76+
Adds additional paths to try for when resolving a require.
77+
The paths must be absolute.
78+
79+
Default is `[]`
80+
7381
## Shared Settings
7482

7583
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
7684
Several rules have the same option, but we can set this option at once.
7785

7886
- `allowModules`
7987
- `tryExtensions`
88+
- `resolvePaths`
8089

81-
```json
82-
{
90+
```js
91+
// .eslintrc.js
92+
module.exports = {
8393
"settings": {
8494
"node": {
8595
"allowModules": ["electron"],
86-
"tryExtensions": [".js", ".json", ".node"]
96+
"tryExtensions": [".js", ".json", ".node"],
97+
"resolvePaths": [__dirname]
8798
}
8899
},
89100
"rules": {

lib/util/check-existence.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const path = require("path")
1414
const resolve = require("resolve")
1515
const exists = require("./exists")
1616
const getAllowModules = require("./get-allow-modules")
17+
const getResolvePaths = require("./get-resolve-paths")
1718

1819
//------------------------------------------------------------------------------
1920
// Public Interface
@@ -32,7 +33,8 @@ const getAllowModules = require("./get-allow-modules")
3233
*/
3334
module.exports = function checkForExistence(context, filePath, targets) {
3435
const allowed = getAllowModules(context)
35-
const opts = {basedir: path.dirname(path.resolve(filePath))}
36+
const paths = getResolvePaths(context)
37+
const opts = {paths, basedir: path.dirname(path.resolve(filePath))}
3638

3739
for (const target of targets) {
3840
// Workaround for https://github.com/substack/node-resolve/issues/78

lib/util/get-resolve-paths.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @author Toru Nagashima
3+
* @copyright 2016 Toru Nagashima. All rights reserved.
4+
* See LICENSE file in root directory for full license.
5+
*/
6+
"use strict"
7+
8+
//------------------------------------------------------------------------------
9+
// Helpers
10+
//------------------------------------------------------------------------------
11+
12+
const DEFAULT_VALUE = Object.freeze([])
13+
14+
/**
15+
* Gets `resolvePaths` property from a given option object.
16+
*
17+
* @param {object|undefined} option - An option object to get.
18+
* @returns {string[]|null} The `allowModules` value, or `null`.
19+
*/
20+
function get(option) {
21+
if (option && option.resolvePaths && Array.isArray(option.resolvePaths)) {
22+
return option.resolvePaths.map(String)
23+
}
24+
return null
25+
}
26+
27+
//------------------------------------------------------------------------------
28+
// Public Interface
29+
//------------------------------------------------------------------------------
30+
31+
/**
32+
* Gets "resolvePaths" setting.
33+
*
34+
* 1. This checks `options` property, then returns it if exists.
35+
* 2. This checks `settings.node` property, then returns it if exists.
36+
* 3. This returns `[]`.
37+
*
38+
* @param {RuleContext} context - The rule context.
39+
* @returns {string[]} A list of extensions.
40+
*/
41+
module.exports = function getResolvePaths(context) {
42+
return (
43+
get(context.options && context.options[0]) ||
44+
get(context.settings && context.settings.node) ||
45+
DEFAULT_VALUE
46+
)
47+
}
48+
49+
module.exports.schema = {
50+
type: "array",
51+
items: {
52+
type: "string",
53+
pattern: "^(.+)/([^/]+)$",
54+
},
55+
uniqueItems: true,
56+
}

tests/lib/rules/no-missing-require.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ ruleTester.run("no-missing-require", rule, {
113113
settings: {node: {tryExtensions: [".coffee"]}},
114114
},
115115

116+
// resolvePaths
117+
{
118+
filename: fixture("test.js"),
119+
code: "require('fixtures/no-missing/a');",
120+
env: {node: true},
121+
settings: {node: {resolvePaths: [path.resolve(__dirname, "../../")]}},
122+
},
123+
116124
// Ignores it if not callee.
117125
{
118126
filename: fixture("test.js"),

0 commit comments

Comments
 (0)