diff --git a/README.md b/README.md index fc533a8f..2b7eb584 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,39 @@ # eslint-plugin-node -I'm working on this now. +[![Build Status](https://travis-ci.org/mysticatea/eslint-plugin-node.svg?branch=master)](https://travis-ci.org/mysticatea/eslint-plugin-node) +[![Coverage Status](https://coveralls.io/repos/mysticatea/eslint-plugin-node/badge.svg?branch=master)](https://coveralls.io/r/mysticatea/eslint-plugin-node?branch=master) +[![Dependency Status](https://david-dm.org/mysticatea/eslint-plugin-node.svg)](https://david-dm.org/mysticatea/eslint-plugin-node) +[![npm version](https://badge.fury.io/js/eslint-plugin-node.svg)](http://badge.fury.io/js/eslint-plugin-node) + +Additional ESLint's rules for Node.js + +Some rules are slow because it searches `package.json` and opens it. + +## Install & Usage + +``` +> npm install --save-dev eslint eslint-plugin-node +``` + +**.eslintrc** + +```json +{ + "extends": "eslint:recommended", + "plugins": ["node"], + "env": { + "node": true + }, + "rules": { + "node/no-missing-import": 2, + "node/no-missing-require": 2, + "node/shebang": 2 + } +} +``` + +## Rules + +- [no-missing-import](docs/rules/no-missing-import.md) - Disallow invalid `import` and `export` declarations. +- [no-missing-require](docs/rules/no-missing-require.md) - Disallow invalid `require()`s. +- [shebang](docs/rules/shebang.md) - Suggest correct usage of shebang. diff --git a/docs/rules/no-missing-import.md b/docs/rules/no-missing-import.md index b68d2ec6..266a7209 100644 --- a/docs/rules/no-missing-import.md +++ b/docs/rules/no-missing-import.md @@ -1,17 +1,25 @@ -# (no-missing-import) +# Disallow invalid `import` and `export` declarations (no-missing-import) + +This is similar to [no-missing-require](no-missing-require.md), but this rule handles `import` and `export` declarations. + +**NOTE:** ECMAScript 2015 (ES6) does not define the lookup logic. So this rule spec might be changed in future. ## Rule Details +See [no-missing-require](no-missing-require.md#rule-details). + The following patterns are considered problems: ```js -``` +import typoFile from "./typo-file"; /*error "./typo-file" is not found.*/ +import typoModule from "typo-module"; /*error "typo-module" is not found.*/ -The following patterns are considered not problems: - -```js +// If the module is not written in "dependencies" and "peerDependencies".... +import someone from "someone"; /*error "someone" is not published.*/ ``` ## When Not To Use It -If you don't want to ***, then it's safe to disable this rule. +This rule should not be used in ES3/5 environments. + +If you don't want to be notified about usage of `import` and `export` declarations, then it's safe to disable this rule. diff --git a/docs/rules/no-missing-require.md b/docs/rules/no-missing-require.md index 03b09eed..14b34d3d 100644 --- a/docs/rules/no-missing-require.md +++ b/docs/rules/no-missing-require.md @@ -1,17 +1,54 @@ -# Check whether or not `require()` is valid (no-missing-require) +# Disallow invalid `require()`s (no-missing-require) + +Maybe we cannot find typo of import paths until run it. +Also, maybe we cannot find lacking of `dependencies` of `package.json` until publish it. + +So this rule checks import paths and `dependencies` of `package.json`. ## Rule Details +This rule does two checks. + +1. This rule checks whether or not the files of import paths exist. + If those do not exist, it reports the invalid `require()`. +2. This rule looks up `package.json` file from each linitng target file. + Starting from the directory of the target file, it goes up ancestor directories until found. + Then it checks whether or not the imported modules are published properly. + +This does not check for dynamic imports. + The following patterns are considered problems: ```js +var typoFile = require("./typo-file"); /*error "./typo-file" is not found.*/ +var typoModule = require("typo-module"); /*error "typo-module" is not found.*/ + +// If the module is not written in "dependencies" and "peerDependencies".... +var someone = require("someone"); /*error "someone" is not published.*/ ``` The following patterns are considered not problems: ```js +var existingFile = require("./existing"); + +// If it's installed and it's written in `dependencies` or `peerDependencies`. +var eslint = require("eslint"); ``` +### Options + +```json +{ + "no-missing-require": [2, {"publish": "+(./*|./{bin,lib,src}/**)"}] +} +``` + +- `publish` (`string`) - A glob pattern. + If a linting target file is matched this pattern, the file is addressed as a published file. + `require()` in the published files cannot import files which are not published. + On the other hand, other files can import files which are not published. + ## When Not To Use It -If you don't want to ***, then it's safe to disable this rule. +If you don't want to be notified about usage of `require()`, then it's safe to disable this rule. diff --git a/docs/rules/shebang.md b/docs/rules/shebang.md index d4cb23f6..ee47278f 100644 --- a/docs/rules/shebang.md +++ b/docs/rules/shebang.md @@ -1,17 +1,48 @@ -# (shebang) +# Suggest correct usage of shebang (node/shebang) + +When we make a CLI tool on Node.js, we add `bin` field to `package.json`, then we add a shebang the entry file. +This rule suggests correct usage of shebang. ## Rule Details +This rule looks up `package.json` file from each linitng target file. +Starting from the directory of the target file, it goes up ancestor directories until found. + +If `package.json` was not found, this rule does nothing. + +This rule checks `bin` field of `package.json`, then if a target file matches one of `bin` files, it checks whether or not there is a correct shebang. +Otherwise it checks whether or not there is not a shebang. + +### For files in `bin` field of `package.json`: + +The following patterns are considered problems: + +```js +console.log("hello"); /*error This file needs shebang "#!/usr/bin/env node".*/ +``` + +The following patterns are considered not problems: + +```js +#!/usr/bin/env node +console.log("hello"); +``` + +### For other files: + The following patterns are considered problems: ```js +#!/usr/bin/env node /*error This file needs no shebang.*/ +console.log("hello"); ``` The following patterns are considered not problems: ```js +console.log("hello"); ``` ## When Not To Use It -If you don't want to ***, then it's safe to disable this rule. +If you don't want to be notified about usage of shebang, then it's safe to disable this rule. diff --git a/lib/rules/no-missing-import.js b/lib/rules/no-missing-import.js index 9418b06f..d3196d50 100644 --- a/lib/rules/no-missing-import.js +++ b/lib/rules/no-missing-import.js @@ -1,5 +1,4 @@ /** - * @fileoverview Rule to * @author Toru Nagashima * @copyright 2015 Toru Nagashima. All rights reserved. * See LICENSE file in root directory for full license. diff --git a/lib/rules/no-missing-require.js b/lib/rules/no-missing-require.js index 090b028a..39846b68 100644 --- a/lib/rules/no-missing-require.js +++ b/lib/rules/no-missing-require.js @@ -1,5 +1,4 @@ /** - * @fileoverview Rule to check whether or not `require()` is valid. * @author Toru Nagashima * @copyright 2015 Toru Nagashima. All rights reserved. * See LICENSE file in root directory for full license. diff --git a/lib/rules/shebang.js b/lib/rules/shebang.js index 17a3d150..9d02b01b 100644 --- a/lib/rules/shebang.js +++ b/lib/rules/shebang.js @@ -1,5 +1,4 @@ /** - * @fileoverview Rule to * @author Toru Nagashima * @copyright 2015 Toru Nagashima. All rights reserved. * See LICENSE file in root directory for full license. @@ -73,12 +72,12 @@ module.exports = function(context) { if (needsShebang) { context.report({ node: node, - message: "This file needs shebang \"#!/usr/bin/env node\"" + message: "This file needs shebang \"#!/usr/bin/env node\"." }); } else if (shebang) { context.report({ node: node, - message: "This file needs no shebang \"#!/usr/bin/env node\"" + message: "This file needs no shebang." }); } } diff --git a/package.json b/package.json index 70f80887..51381854 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "coveralls": "cat coverage/lcov.info | coveralls" }, "peerDependencies": { - "eslint": "^1.0.0" + "eslint": ">=1.0.0" }, "dependencies": { "minimatch": "^3.0.0", diff --git a/tests/lib/rules/shebang.js b/tests/lib/rules/shebang.js index 294f3d66..22547c29 100644 --- a/tests/lib/rules/shebang.js +++ b/tests/lib/rules/shebang.js @@ -66,37 +66,37 @@ ruleTester.run("shebang", rule, { { filename: fixture("string-bin/bin/test.js"), code: "hello();", - errors: ["This file needs shebang \"#!/usr/bin/env node\""] + errors: ["This file needs shebang \"#!/usr/bin/env node\"."] }, { filename: fixture("string-bin/bin/test.js"), code: "#!/usr/bin/node\nhello();", - errors: ["This file needs shebang \"#!/usr/bin/env node\""] + errors: ["This file needs shebang \"#!/usr/bin/env node\"."] }, { filename: fixture("string-bin/lib/test.js"), code: "#!/usr/bin/env node\nhello();", - errors: ["This file needs no shebang \"#!/usr/bin/env node\""] + errors: ["This file needs no shebang."] }, { filename: fixture("object-bin/bin/a.js"), code: "hello();", - errors: ["This file needs shebang \"#!/usr/bin/env node\""] + errors: ["This file needs shebang \"#!/usr/bin/env node\"."] }, { filename: fixture("object-bin/bin/b.js"), code: "#!/usr/bin/node\nhello();", - errors: ["This file needs shebang \"#!/usr/bin/env node\""] + errors: ["This file needs shebang \"#!/usr/bin/env node\"."] }, { filename: fixture("object-bin/bin/c.js"), code: "#!/usr/bin/env node\nhello();", - errors: ["This file needs no shebang \"#!/usr/bin/env node\""] + errors: ["This file needs no shebang."] }, { filename: fixture("no-bin-field/lib/test.js"), code: "#!/usr/bin/env node\nhello();", - errors: ["This file needs no shebang \"#!/usr/bin/env node\""] + errors: ["This file needs no shebang."] } ] });