Skip to content

Commit d34e09c

Browse files
committed
Chore: refactoring
1 parent 47e0cc2 commit d34e09c

17 files changed

+128
-177
lines changed

docs/rules/no-missing-import.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import existingModule from "existing-module";
3434
"rules": {
3535
"node/no-missing-import": ["error", {
3636
"allowModules": [],
37+
"resolvePaths": ["/path/to/a/modules/directory"],
3738
"tryExtensions": [".js", ".json", ".node"]
3839
}]
3940
}
@@ -58,6 +59,13 @@ This option is an array of strings as module names.
5859
}
5960
```
6061

62+
### resolvePaths
63+
64+
Adds additional paths to try for when resolving imports.
65+
If a path is relative, it will be resolved from CWD.
66+
67+
Default is `[]`
68+
6169
### tryExtensions
6270

6371
When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists.
@@ -71,13 +79,16 @@ The following options can be set by [shared settings](http://eslint.org/docs/use
7179
Several rules have the same option, but we can set this option at once.
7280

7381
- `allowModules`
82+
- `resolvePaths`
7483
- `tryExtensions`
7584

76-
```json
77-
{
85+
```js
86+
// .eslintrc.js
87+
module.exports = {
7888
"settings": {
7989
"node": {
8090
"allowModules": ["electron"],
91+
"resolvePaths": [__dirname],
8192
"tryExtensions": [".js", ".json", ".node"]
8293
}
8394
},

docs/rules/no-missing-require.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ var foo = require(FOO_NAME);
3939
"rules": {
4040
"node/no-missing-require": ["error", {
4141
"allowModules": [],
42-
"tryExtensions": [".js", ".json", ".node"],
43-
"resolvePaths": ["/an/absolute/path"]
42+
"resolvePaths": ["/path/to/a/modules/directory"],
43+
"tryExtensions": [".js", ".json", ".node"]
4444
}]
4545
}
4646
}
@@ -64,37 +64,37 @@ This option is an array of strings as module names.
6464
}
6565
```
6666

67+
### resolvePaths
68+
69+
Adds additional paths to try for when resolving a require.
70+
If a path is relative, it will be resolved from CWD.
71+
72+
Default is `[]`
73+
6774
### tryExtensions
6875

6976
When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists.
7077
`tryExtensions` option is the extension list this rule uses at the time.
7178

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

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-
8181
## Shared Settings
8282

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

8686
- `allowModules`
87-
- `tryExtensions`
8887
- `resolvePaths`
88+
- `tryExtensions`
8989

9090
```js
9191
// .eslintrc.js
9292
module.exports = {
9393
"settings": {
9494
"node": {
9595
"allowModules": ["electron"],
96-
"tryExtensions": [".js", ".json", ".node"],
97-
"resolvePaths": [__dirname]
96+
"resolvePaths": [__dirname],
97+
"tryExtensions": [".js", ".json", ".node"]
9898
}
9999
},
100100
"rules": {

lib/rules/no-extraneous-import.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const checkExtraneous = require("../util/check-extraneous")
1313
const getAllowModules = require("../util/get-allow-modules")
1414
const getConvertPath = require("../util/get-convert-path")
1515
const getImportTargets = require("../util/get-import-export-targets")
16+
const getResolvePaths = require("../util/get-resolve-paths")
1617

1718
//------------------------------------------------------------------------------
1819
// Helpers
@@ -60,6 +61,7 @@ module.exports = {
6061
properties: {
6162
allowModules: getAllowModules.schema,
6263
convertPath: getConvertPath.schema,
64+
resolvePaths: getResolvePaths.schema,
6365
},
6466
additionalProperties: false,
6567
},

lib/rules/no-extraneous-require.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const checkExtraneous = require("../util/check-extraneous")
1313
const getAllowModules = require("../util/get-allow-modules")
1414
const getConvertPath = require("../util/get-convert-path")
1515
const getRequireTargets = require("../util/get-require-targets")
16+
const getResolvePaths = require("../util/get-resolve-paths")
1617

1718
//------------------------------------------------------------------------------
1819
// Helpers
@@ -60,6 +61,7 @@ module.exports = {
6061
properties: {
6162
allowModules: getAllowModules.schema,
6263
convertPath: getConvertPath.schema,
64+
resolvePaths: getResolvePaths.schema,
6365
},
6466
additionalProperties: false,
6567
},

lib/rules/no-missing-import.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
const checkExistence = require("../util/check-existence")
1313
const getAllowModules = require("../util/get-allow-modules")
1414
const getImportExportTargets = require("../util/get-import-export-targets")
15+
const getResolvePaths = require("../util/get-resolve-paths")
1516
const getTryExtensions = require("../util/get-try-extensions")
1617

1718
//------------------------------------------------------------------------------
@@ -34,7 +35,6 @@ function create(context) {
3435
"Program:exit"(node) {
3536
checkExistence(
3637
context,
37-
filePath,
3838
getImportExportTargets(context, node)
3939
)
4040
},
@@ -60,6 +60,7 @@ module.exports = {
6060
properties: {
6161
allowModules: getAllowModules.schema,
6262
tryExtensions: getTryExtensions.schema,
63+
resolvePaths: getResolvePaths.schema,
6364
},
6465
additionalProperties: false,
6566
},

lib/rules/no-missing-require.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
const checkExistence = require("../util/check-existence")
1313
const getAllowModules = require("../util/get-allow-modules")
1414
const getRequireTargets = require("../util/get-require-targets")
15+
const getResolvePaths = require("../util/get-resolve-paths")
1516
const getTryExtensions = require("../util/get-try-extensions")
1617

1718
//------------------------------------------------------------------------------
@@ -34,7 +35,6 @@ function create(context) {
3435
"Program:exit"() {
3536
checkExistence(
3637
context,
37-
filePath,
3838
getRequireTargets(context)
3939
)
4040
},
@@ -60,6 +60,7 @@ module.exports = {
6060
properties: {
6161
allowModules: getAllowModules.schema,
6262
tryExtensions: getTryExtensions.schema,
63+
resolvePaths: getResolvePaths.schema,
6364
},
6465
additionalProperties: false,
6566
},

lib/rules/no-unpublished-import.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const checkPublish = require("../util/check-publish")
1313
const getAllowModules = require("../util/get-allow-modules")
1414
const getConvertPath = require("../util/get-convert-path")
1515
const getImportExportTargets = require("../util/get-import-export-targets")
16+
const getResolvePaths = require("../util/get-resolve-paths")
1617
const getTryExtensions = require("../util/get-try-extensions")
1718

1819
//------------------------------------------------------------------------------
@@ -61,6 +62,7 @@ module.exports = {
6162
properties: {
6263
allowModules: getAllowModules.schema,
6364
convertPath: getConvertPath.schema,
65+
resolvePaths: getResolvePaths.schema,
6466
tryExtensions: getTryExtensions.schema,
6567
},
6668
additionalProperties: false,

lib/rules/no-unpublished-require.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const checkPublish = require("../util/check-publish")
1313
const getAllowModules = require("../util/get-allow-modules")
1414
const getConvertPath = require("../util/get-convert-path")
1515
const getRequireTargets = require("../util/get-require-targets")
16+
const getResolvePaths = require("../util/get-resolve-paths")
1617
const getTryExtensions = require("../util/get-try-extensions")
1718

1819
//------------------------------------------------------------------------------
@@ -61,6 +62,7 @@ module.exports = {
6162
properties: {
6263
allowModules: getAllowModules.schema,
6364
convertPath: getConvertPath.schema,
65+
resolvePaths: getResolvePaths.schema,
6466
tryExtensions: getTryExtensions.schema,
6567
},
6668
additionalProperties: false,

lib/util/check-existence.js

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
// Requirements
1111
//------------------------------------------------------------------------------
1212

13-
const path = require("path")
14-
const resolve = require("resolve")
1513
const exists = require("./exists")
1614
const getAllowModules = require("./get-allow-modules")
17-
const getResolvePaths = require("./get-resolve-paths")
1815

1916
//------------------------------------------------------------------------------
2017
// Public Interface
@@ -27,40 +24,30 @@ const getResolvePaths = require("./get-resolve-paths")
2724
* See Also: https://nodejs.org/api/modules.html
2825
*
2926
* @param {RuleContext} context - A context to report.
30-
* @param {string} filePath - The current file path.
3127
* @param {ImportTarget[]} targets - A list of target information to check.
3228
* @returns {void}
3329
*/
34-
module.exports = function checkForExistence(context, filePath, targets) {
35-
const allowed = getAllowModules(context)
36-
const paths = getResolvePaths(context)
37-
const opts = {paths, basedir: path.dirname(path.resolve(filePath))}
30+
module.exports = function checkForExistence(context, targets) {
31+
const allowed = new Set(getAllowModules(context))
3832

3933
for (const target of targets) {
40-
// Workaround for https://github.com/substack/node-resolve/issues/78
41-
if (target.filePath) {
42-
if (exists(target.filePath)) {
43-
continue
44-
}
45-
}
46-
else if (allowed.indexOf(target.moduleName) !== -1) {
47-
continue
48-
}
49-
else {
50-
try {
51-
resolve.sync(target.name, opts)
52-
continue
53-
}
54-
catch (_err) {
55-
// ignore.
56-
}
57-
}
34+
const missingModule = (
35+
target.moduleName != null &&
36+
!allowed.has(target.moduleName) &&
37+
target.filePath == null
38+
)
39+
const missingFile = (
40+
target.moduleName == null &&
41+
!exists(target.filePath)
42+
)
5843

59-
context.report({
60-
node: target.node,
61-
loc: target.node.loc,
62-
message: "\"{{name}}\" is not found.",
63-
data: target,
64-
})
44+
if (missingModule || missingFile) {
45+
context.report({
46+
node: target.node,
47+
loc: target.node.loc,
48+
message: "\"{{name}}\" is not found.",
49+
data: target,
50+
})
51+
}
6552
}
6653
}

lib/util/check-extraneous.js

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,9 @@
99
// Requirements
1010
//------------------------------------------------------------------------------
1111

12-
const path = require("path")
13-
const resolve = require("resolve")
1412
const getAllowModules = require("./get-allow-modules")
15-
const getConvertPath = require("./get-convert-path")
1613
const getPackageJson = require("./get-package-json")
1714

18-
//------------------------------------------------------------------------------
19-
// Helpers
20-
//------------------------------------------------------------------------------
21-
22-
/**
23-
* Check whether the given package exists or not.
24-
* @param {string} name The package name to check.
25-
* @param {string} basedir The path to starting directory.
26-
* @returns {boolean} `true` if the package was found.
27-
*/
28-
function exists(name, basedir) {
29-
try {
30-
resolve.sync(name, {basedir})
31-
return true
32-
}
33-
catch (_err) {
34-
return false
35-
}
36-
}
37-
3815
//------------------------------------------------------------------------------
3916
// Public Interface
4017
//------------------------------------------------------------------------------
@@ -49,24 +26,13 @@ function exists(name, basedir) {
4926
* @param {ImportTarget[]} targets - A list of target information to check.
5027
* @returns {void}
5128
*/
52-
module.exports = function checkForExtraeous(context, filePath, targets) {
29+
module.exports = function checkForExtraneous(context, filePath, targets) {
5330
const packageInfo = getPackageJson(filePath)
5431
if (!packageInfo) {
5532
return
5633
}
5734

5835
const allowed = new Set(getAllowModules(context))
59-
const convertPath = getConvertPath(context)
60-
const rootPath = path.dirname(packageInfo.filePath)
61-
const toRelative = function(fullPath) { // eslint-disable-line func-style
62-
const retv = path.relative(rootPath, fullPath).replace(/\\/g, "/")
63-
return convertPath(retv)
64-
}
65-
const convertedPath = path.resolve(
66-
rootPath,
67-
toRelative(path.resolve(filePath))
68-
)
69-
const basedir = path.dirname(convertedPath)
7036
const dependencies = new Set(
7137
[].concat(
7238
Object.keys(packageInfo.dependencies || {}),
@@ -77,12 +43,11 @@ module.exports = function checkForExtraeous(context, filePath, targets) {
7743
)
7844

7945
for (const target of targets) {
80-
const name = target.moduleName
8146
const extraneous = (
82-
name != null &&
83-
!dependencies.has(name) &&
84-
!allowed.has(name) &&
85-
exists(name, basedir)
47+
target.moduleName != null &&
48+
target.filePath != null &&
49+
!dependencies.has(target.moduleName) &&
50+
!allowed.has(target.moduleName)
8651
)
8752

8853
if (extraneous) {

0 commit comments

Comments
 (0)