Skip to content

Commit

Permalink
Update: add resolvePaths option to no-missing-require (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fjandin authored and mysticatea committed Jun 25, 2017
1 parent 5634718 commit 47e0cc2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
19 changes: 15 additions & 4 deletions docs/rules/no-missing-require.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ var foo = require(FOO_NAME);
"rules": {
"node/no-missing-require": ["error", {
"allowModules": [],
"tryExtensions": [".js", ".json", ".node"]
"tryExtensions": [".js", ".json", ".node"],
"resolvePaths": ["/an/absolute/path"]
}]
}
}
Expand Down Expand Up @@ -70,20 +71,30 @@ When an import path does not exist, this rule checks whether or not any of `path

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

### resolvePaths

Adds additional paths to try for when resolving a require.
The paths must be absolute.

Default is `[]`

## Shared Settings

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

- `allowModules`
- `tryExtensions`
- `resolvePaths`

```json
{
```js
// .eslintrc.js
module.exports = {
"settings": {
"node": {
"allowModules": ["electron"],
"tryExtensions": [".js", ".json", ".node"]
"tryExtensions": [".js", ".json", ".node"],
"resolvePaths": [__dirname]
}
},
"rules": {
Expand Down
4 changes: 3 additions & 1 deletion lib/util/check-existence.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const path = require("path")
const resolve = require("resolve")
const exists = require("./exists")
const getAllowModules = require("./get-allow-modules")
const getResolvePaths = require("./get-resolve-paths")

//------------------------------------------------------------------------------
// Public Interface
Expand All @@ -32,7 +33,8 @@ const getAllowModules = require("./get-allow-modules")
*/
module.exports = function checkForExistence(context, filePath, targets) {
const allowed = getAllowModules(context)
const opts = {basedir: path.dirname(path.resolve(filePath))}
const paths = getResolvePaths(context)
const opts = {paths, basedir: path.dirname(path.resolve(filePath))}

for (const target of targets) {
// Workaround for https://github.com/substack/node-resolve/issues/78
Expand Down
56 changes: 56 additions & 0 deletions lib/util/get-resolve-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

const DEFAULT_VALUE = Object.freeze([])

/**
* Gets `resolvePaths` property from a given option object.
*
* @param {object|undefined} option - An option object to get.
* @returns {string[]|null} The `allowModules` value, or `null`.
*/
function get(option) {
if (option && option.resolvePaths && Array.isArray(option.resolvePaths)) {
return option.resolvePaths.map(String)
}
return null
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

/**
* Gets "resolvePaths" setting.
*
* 1. This checks `options` property, then returns it if exists.
* 2. This checks `settings.node` property, then returns it if exists.
* 3. This returns `[]`.
*
* @param {RuleContext} context - The rule context.
* @returns {string[]} A list of extensions.
*/
module.exports = function getResolvePaths(context) {
return (
get(context.options && context.options[0]) ||
get(context.settings && context.settings.node) ||
DEFAULT_VALUE
)
}

module.exports.schema = {
type: "array",
items: {
type: "string",
pattern: "^(.+)/([^/]+)$",
},
uniqueItems: true,
}
8 changes: 8 additions & 0 deletions tests/lib/rules/no-missing-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ ruleTester.run("no-missing-require", rule, {
settings: {node: {tryExtensions: [".coffee"]}},
},

// resolvePaths
{
filename: fixture("test.js"),
code: "require('fixtures/no-missing/a');",
env: {node: true},
settings: {node: {resolvePaths: [path.resolve(__dirname, "../../")]}},
},

// Ignores it if not callee.
{
filename: fixture("test.js"),
Expand Down

0 comments on commit 47e0cc2

Please sign in to comment.