Skip to content

Commit

Permalink
Add documents.
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Nov 27, 2015
1 parent 6f5d1dd commit 5310068
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 24 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 14 additions & 6 deletions docs/rules/no-missing-import.md
Original file line number Diff line number Diff line change
@@ -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.
41 changes: 39 additions & 2 deletions docs/rules/no-missing-require.md
Original file line number Diff line number Diff line change
@@ -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.
35 changes: 33 additions & 2 deletions docs/rules/shebang.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 0 additions & 1 deletion lib/rules/no-missing-import.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion lib/rules/no-missing-require.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/shebang.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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."
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"coveralls": "cat coverage/lcov.info | coveralls"
},
"peerDependencies": {
"eslint": "^1.0.0"
"eslint": ">=1.0.0"
},
"dependencies": {
"minimatch": "^3.0.0",
Expand Down
14 changes: 7 additions & 7 deletions tests/lib/rules/shebang.js
Original file line number Diff line number Diff line change
Expand Up @@ -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."]
}
]
});

0 comments on commit 5310068

Please sign in to comment.