Skip to content

Commit

Permalink
New: no-unpublished-* rules (refs #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jan 9, 2016
1 parent 82958d4 commit af82240
Show file tree
Hide file tree
Showing 26 changed files with 810 additions and 64 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

Additional ESLint's rules for Node.js

Some rules are slow because it searches `package.json` and opens it.

## Install & Usage

```
Expand All @@ -35,7 +33,11 @@ Some rules are slow because it searches `package.json` and opens it.

## Rules

Some rules are slow because it searches `package.json` and opens it.

- [no-missing-import](docs/rules/no-missing-import.md) - Disallow `import` and `export` declarations for files that don't exist.
- [no-missing-require](docs/rules/no-missing-require.md) - Disallow `require()`s for files that don't exist.
- [no-unpublished-import](docs/rules/no-unpublished-import.md) - Disallow `import` and `export` declarations for files that are not published.
- [no-unpublished-require](docs/rules/no-unpublished-require.md) - Disallow `require()`s for files that are not published.
- [no-unsupported-features](docs/rules/no-unsupported-features.md) - Disallow unsupported ECMAScript features on the specified version.
- [shebang](docs/rules/shebang.md) - Suggest correct usage of shebang. (fixable)
8 changes: 6 additions & 2 deletions docs/rules/no-missing-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

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.
**NOTE:** ECMAScript 2015 (ES6) does not define the lookup logic and Node does not support modules yet. So this rule spec might be changed in future.

## Rule Details

This rule checks whether or not the file paths of `import` and `export` declarations.
This rule checks the file paths of `import` and `export` declarations.
If the file paths don't exist, this reports these.

The following patterns are considered problems:

```js
/*eslint node/no-missing-import: 2*/

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
/*eslint node/no-missing-import: 2*/

import existingFile from "./existing-file";
import existingModule from "existing-module";
```
Expand Down
9 changes: 8 additions & 1 deletion docs/rules/no-missing-require.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@ const foo = require("./foo");

## Rule Details

This rule checks whether or not the file paths of `require()`s.
This rule checks the file paths of `require()`s.
If the file paths don't exist, this reports these.

The following patterns are considered problems:

```js
/*eslint node/no-missing-require: 2*/

var typoFile = require("./typo-file"); /*error "./typo-file" is not found.*/
var typoModule = require("typo-module"); /*error "typo-module" is not found.*/
```

The following patterns are considered not problems:

```js
/*eslint node/no-missing-require: 2*/

var existingFile = require("./existing-file");
var existingModule = require("existing-module");

// This rule cannot check for dynamic imports.
var foo = require(FOO_NAME);
```

## When Not To Use It
Expand Down
43 changes: 43 additions & 0 deletions docs/rules/no-unpublished-import.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Disallow `import` and `export` declarations for files that are not published (no-unpublished-import)

This is similar to [no-unpublished-require](no-unpublished-require.md), but this rule handles `import` and `export` declarations.

**NOTE:** ECMAScript 2015 (ES6) does not define the lookup logic and Node does not support modules yet. So this rule spec might be changed in future.

## Rule Details

This rule checks the file paths of `import` and `export` declarations.
If the file paths are not published, this reports these.

"published" is that satisfying the following conditions:

- If it's a file:
- `"files"` field of `package.json` includes the file, or the field is nothing.
- `.npmignore` does not include the file.
- If it's a module:
- `"dependencies"` or `"peerDependencies"` field of `package.json` includes the module.
If the file `require` is written is not published then it's also OK that `"devDependencies"` field of `package.json` includes the module.

The following patterns are considered problems:

```js
/*eslint node/no-unpublished-import: 2*/

import ignoredFile from "./ignored-file"; /*error "./ignored-file" is not published.*/
import notDependedModule from "not-depended-module"; /*error "not-depended-module" is not published.*/
```

The following patterns are considered not problems:

```js
/*eslint node/no-unpublished-import: 2*/

import publishedFile from "./published-file";
import dependedModule from "depended-module";
```

## When Not To Use It

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.
43 changes: 43 additions & 0 deletions docs/rules/no-unpublished-require.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Disallow `require()`s for files that files that are not published (no-unpublished-require)

If `require()` targets are not published, the program works in local, but will not work after published to npm.
This rule catches `require()` for files and modules that not published.

## Rule Details

This rule checks the file paths of `require()`s.
If the file paths are not published, this reports these.

"published" is that satisfying the following conditions:

- If it's a file:
- `"files"` field of `package.json` includes the file, or the field is nothing.
- `.npmignore` does not include the file.
- If it's a module:
- `"dependencies"` or `"peerDependencies"` field of `package.json` includes the module.
If the file `require` is written is not published then it's also OK that `"devDependencies"` field of `package.json` includes the module.

The following patterns are considered problems:

```js
/*eslint node/no-unpublished-require: 2*/

var ignoredFile = require("./ignored-file"); /*error "./ignored-file" is not published.*/
var notDependedModule = require("not-depended-module"); /*error "not-depended-module" is not published.*/
```

The following patterns are considered not problems:

```js
/*eslint node/no-unpublished-require: 2*/

var publishedFile = require("./published-file");
var dependedModule = require("depended-module");

// This rule cannot check for dynamic imports.
var foo = require(FOO_NAME);
```

## When Not To Use It

If you don't want to be notified about usage of `require()`, then it's safe to disable this rule.
12 changes: 6 additions & 6 deletions docs/rules/no-unsupported-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ This rule reports when you used unsupported ECMAScript 2015 features on the spec

**This rule expects to be used with `"env": {"es6": true}` configuration.**

This rule requires to specify a Node.js version always.
This rule requires to specify a Node version always.
For Example:

```json
{
"no-unsupported-features": [2, {"version": 4}]
"node/no-unsupported-features": [2, {"version": 4}]
}
```

Expand All @@ -26,7 +26,7 @@ This rule accepts the following version number:
The following patterns are considered problems:

```js
/*eslint no-unsupported-features: [2, {version: 4}]*/
/*eslint node/no-unsupported-features: [2, {version: 4}]*/
/*eslint-env es6*/

function foo(a = 1) { /*error Default Parameters are not supported yet on Node v4.*/
Expand Down Expand Up @@ -61,7 +61,7 @@ var p = new Proxy(o, { /*error Proxy is not supported yet on Node v4.*/
The following patterns are considered not problems:

```js
/*eslint no-unsupported-features: [2, {version: 4}]*/
/*eslint node/no-unsupported-features: [2, {version: 4}]*/
/*eslint-env es6*/

for (var a of list) {
Expand Down Expand Up @@ -98,7 +98,7 @@ This rule has `"ignores"` option to ignore to use the specified features.

```json
{
"no-unsupported-features": [2, {"version": 4, "ignores": []}]
"node/no-unsupported-features": [2, {"version": 4, "ignores": []}]
}
```

Expand Down Expand Up @@ -136,7 +136,7 @@ This `"ignores"` option accepts the following strings.
The following patterns are considered not problems when it's using `"ignores"`:

```js
/*eslint no-unsupported-features: [2, {version: 4, ignores: ["defaultParameters"]}]*/
/*eslint node/no-unsupported-features: [2, {version: 4, ignores: ["defaultParameters"]}]*/
/*eslint-env es6*/

function foo(a = 1) {
Expand Down
37 changes: 37 additions & 0 deletions lib/rules/no-unpublished-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var checkPublish = require("../util/check-publish");
var getImportExportTargets = require("../util/get-import-export-targets");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
var filePath = context.getFilename();
if (filePath === "<input>") {
return {};
}

return {
"Program:exit": function(node) {
checkPublish(
context,
filePath,
getImportExportTargets(context, node)
);
}
};
};

module.exports.schema = [];
37 changes: 37 additions & 0 deletions lib/rules/no-unpublished-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var checkPublish = require("../util/check-publish");
var getRequireTargets = require("../util/get-require-targets");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
var filePath = context.getFilename();
if (filePath === "<input>") {
return {};
}

return {
"Program:exit": function() {
checkPublish(
context,
filePath,
getRequireTargets(context)
);
}
};
};

module.exports.schema = [];
Loading

0 comments on commit af82240

Please sign in to comment.