Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New:
no-extraneous-*
rules (refs #71)
- Loading branch information
1 parent
deb4f55
commit a75c638
Showing
28 changed files
with
610 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Disallow `import` declarations which import extraneous modules (no-extraneous-import) | ||
|
||
If a `import` declaration's source is extraneous (it's not written in `package.json`), the program works in local, but will not work after dependencies are re-installed. It will cause troubles to your team/contributors. | ||
This rule disallows `import` declarations of extraneous modules. | ||
|
||
## Rule Details | ||
|
||
This rule warns `import` declarations of extraneous modules. | ||
|
||
## Options | ||
|
||
```json | ||
{ | ||
"rules": { | ||
"node/no-extraneous-import": ["error", { | ||
"allowModules": [] | ||
}] | ||
} | ||
} | ||
``` | ||
|
||
### allowModules | ||
|
||
Some platforms have additional embedded modules. | ||
For example, Electron has `electron` module. | ||
|
||
We can specify additional embedded modules with this option. | ||
This option is an array of strings as module names. | ||
|
||
```json | ||
{ | ||
"rules": { | ||
"node/no-extraneous-import": ["error", { | ||
"allowModules": ["electron"] | ||
}] | ||
} | ||
} | ||
``` | ||
|
||
## 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` | ||
|
||
For Example: | ||
|
||
```json | ||
{ | ||
"settings": { | ||
"node": { | ||
"allowModules": ["electron"] | ||
} | ||
}, | ||
"rules": { | ||
"node/no-extraneous-import": "error" | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Disallow `require()`s which import extraneous modules (no-extraneous-require) | ||
|
||
If a `require()`'s target is extraneous (it's not written in `package.json`), the program works in local, but will not work after dependencies are re-installed. It will cause troubles to your team/contributors. | ||
This rule disallows `require()` of extraneous modules. | ||
|
||
## Rule Details | ||
|
||
This rule warns `require()` of extraneous modules. | ||
|
||
## Options | ||
|
||
```json | ||
{ | ||
"rules": { | ||
"node/no-extraneous-require": ["error", { | ||
"allowModules": [] | ||
}] | ||
} | ||
} | ||
``` | ||
|
||
### allowModules | ||
|
||
Some platforms have additional embedded modules. | ||
For example, Electron has `electron` module. | ||
|
||
We can specify additional embedded modules with this option. | ||
This option is an array of strings as module names. | ||
|
||
```json | ||
{ | ||
"rules": { | ||
"node/no-extraneous-require": ["error", { | ||
"allowModules": ["electron"] | ||
}] | ||
} | ||
} | ||
``` | ||
|
||
## 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` | ||
|
||
For Example: | ||
|
||
```json | ||
{ | ||
"settings": { | ||
"node": { | ||
"allowModules": ["electron"] | ||
} | ||
}, | ||
"rules": { | ||
"node/no-extraneous-require": "error" | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @author Toru Nagashima | ||
* @copyright 2017 Toru Nagashima. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const checkExtraneous = require("../util/check-extraneous") | ||
const getAllowModules = require("../util/get-allow-modules") | ||
const getConvertPath = require("../util/get-convert-path") | ||
const getImportTargets = require("../util/get-import-export-targets") | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
/** | ||
* The definition of this rule. | ||
* | ||
* @param {RuleContext} context - The rule context to check. | ||
* @returns {object} The definition of this rule. | ||
*/ | ||
function create(context) { | ||
const filePath = context.getFilename() | ||
if (filePath === "<input>") { | ||
return {} | ||
} | ||
|
||
return { | ||
"Program:exit"(node) { | ||
checkExtraneous( | ||
context, | ||
filePath, | ||
getImportTargets(context, node, false) | ||
) | ||
}, | ||
} | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
description: "disallow `import` declarations of extraneous packages", | ||
category: "Possible Errors", | ||
recommended: false, | ||
}, | ||
fixable: false, | ||
schema: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
allowModules: getAllowModules.schema, | ||
convertPath: getConvertPath.schema, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @author Toru Nagashima | ||
* @copyright 2017 Toru Nagashima. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const checkExtraneous = require("../util/check-extraneous") | ||
const getAllowModules = require("../util/get-allow-modules") | ||
const getConvertPath = require("../util/get-convert-path") | ||
const getRequireTargets = require("../util/get-require-targets") | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
/** | ||
* The definition of this rule. | ||
* | ||
* @param {RuleContext} context - The rule context to check. | ||
* @returns {object} The definition of this rule. | ||
*/ | ||
function create(context) { | ||
const filePath = context.getFilename() | ||
if (filePath === "<input>") { | ||
return {} | ||
} | ||
|
||
return { | ||
"Program:exit"() { | ||
checkExtraneous( | ||
context, | ||
filePath, | ||
getRequireTargets(context, false) | ||
) | ||
}, | ||
} | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
description: "disallow `require()` expressions for extraneous packages", | ||
category: "Possible Errors", | ||
recommended: false, | ||
}, | ||
fixable: false, | ||
schema: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
allowModules: getAllowModules.schema, | ||
convertPath: getConvertPath.schema, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @author Toru Nagashima | ||
* @copyright 2017 Toru Nagashima. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const path = require("path") | ||
const resolve = require("resolve") | ||
const getAllowModules = require("./get-allow-modules") | ||
const getConvertPath = require("./get-convert-path") | ||
const getPackageJson = require("./get-package-json") | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Check whether the given package exists or not. | ||
* @param {string} name The package name to check. | ||
* @param {string} basedir The path to starting directory. | ||
* @returns {boolean} `true` if the package was found. | ||
*/ | ||
function exists(name, basedir) { | ||
try { | ||
resolve.sync(name, {basedir}) | ||
return true | ||
} | ||
catch (_err) { | ||
return false | ||
} | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Public Interface | ||
//------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Checks whether or not each requirement target is published via package.json. | ||
* | ||
* It reads package.json and checks the target exists in `dependencies`. | ||
* | ||
* @param {RuleContext} context - A context to report. | ||
* @param {string} filePath - The current file path. | ||
* @param {ImportTarget[]} targets - A list of target information to check. | ||
* @returns {void} | ||
*/ | ||
module.exports = function checkForExtraeous(context, filePath, targets) { | ||
const packageInfo = getPackageJson(filePath) | ||
if (!packageInfo) { | ||
return | ||
} | ||
|
||
const allowed = new Set(getAllowModules(context)) | ||
const convertPath = getConvertPath(context) | ||
const rootPath = path.dirname(packageInfo.filePath) | ||
const convertedPath = path.resolve( | ||
rootPath, | ||
convertPath( | ||
path.relative(rootPath, path.resolve(filePath)) | ||
) | ||
) | ||
const basedir = path.dirname(convertedPath) | ||
const dependencies = new Set( | ||
[].concat( | ||
Object.keys(packageInfo.dependencies || {}), | ||
Object.keys(packageInfo.devDependencies || {}), | ||
Object.keys(packageInfo.peerDependencies || {}), | ||
Object.keys(packageInfo.optionalDependencies || {}) | ||
) | ||
) | ||
|
||
for (const target of targets) { | ||
const name = target.moduleName | ||
const extraneous = ( | ||
name != null && | ||
!dependencies.has(name) && | ||
!allowed.has(name) && | ||
exists(name, basedir) | ||
) | ||
|
||
if (extraneous) { | ||
context.report({ | ||
node: target.node, | ||
loc: target.node.loc, | ||
message: "\"{{moduleName}}\" is extraneous.", | ||
data: target, | ||
}) | ||
} | ||
} | ||
} |
Empty file.
Empty file.
Empty file.
Oops, something went wrong.