Skip to content

Commit

Permalink
Merge tag 'v1.15.0' into release
Browse files Browse the repository at this point in the history
Tag v1.15.0
  • Loading branch information
benmosher committed Sep 13, 2016
2 parents 7995581 + d03577c commit 2e4e449
Show file tree
Hide file tree
Showing 23 changed files with 486 additions and 22 deletions.
25 changes: 24 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
## [Unreleased]


## [1.15.0] - 2016-09-12
### Added
- Added an `allow` option to [`no-nodejs-modules`] to allow exceptions ([#452], [#509]).
- Added [`no-absolute-path`] rule ([#530], [#538])
- [`max-dependencies`] for specifying the maximum number of dependencies (both `import` and `require`) a module can have. (see [#489], thanks [@tizmagik])
- Added glob option to config for [`no-extraneous-dependencies`], after much bikeshedding. Thanks, [@knpwrs]! ([#527])

### Fixed
- [`no-named-as-default-member`] Allow default import to have a property named "default" ([#507]+[#508], thanks [@jquense] for both!)

## [1.14.0] - 2016-08-22
### Added
- [`import/parsers` setting]: parse some dependencies (i.e. TypeScript!) with a different parser than the ESLint-configured parser. ([#503])
Expand Down Expand Up @@ -289,9 +299,16 @@ for info on changes for earlier releases.
[`no-mutable-exports`]: ./docs/rules/no-mutable-exports.md
[`prefer-default-export`]: ./docs/rules/prefer-default-export.md
[`no-restricted-paths`]: ./docs/rules/no-restricted-paths.md
[`no-absolute-path`]: ./docs/rules/no-absolute-path.md
[`max-dependencies`]: ./docs/rules/max-dependencies.md

[#538]: https://github.com/benmosher/eslint-plugin-import/pull/538
[#527]: https://github.com/benmosher/eslint-plugin-import/pull/527
[#509]: https://github.com/benmosher/eslint-plugin-import/pull/509
[#508]: https://github.com/benmosher/eslint-plugin-import/pull/508
[#503]: https://github.com/benmosher/eslint-plugin-import/pull/503
[#499]: https://github.com/benmosher/eslint-plugin-import/pull/499
[#489]: https://github.com/benmosher/eslint-plugin-import/pull/489
[#461]: https://github.com/benmosher/eslint-plugin-import/pull/461
[#444]: https://github.com/benmosher/eslint-plugin-import/pull/444
[#428]: https://github.com/benmosher/eslint-plugin-import/pull/428
Expand Down Expand Up @@ -328,9 +345,12 @@ for info on changes for earlier releases.
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157
[#314]: https://github.com/benmosher/eslint-plugin-import/pull/314

[#530]: https://github.com/benmosher/eslint-plugin-import/issues/530
[#507]: https://github.com/benmosher/eslint-plugin-import/issues/507
[#478]: https://github.com/benmosher/eslint-plugin-import/issues/478
[#456]: https://github.com/benmosher/eslint-plugin-import/issues/456
[#453]: https://github.com/benmosher/eslint-plugin-import/issues/453
[#452]: https://github.com/benmosher/eslint-plugin-import/issues/452
[#441]: https://github.com/benmosher/eslint-plugin-import/issues/441
[#423]: https://github.com/benmosher/eslint-plugin-import/issues/423
[#416]: https://github.com/benmosher/eslint-plugin-import/issues/416
Expand Down Expand Up @@ -362,7 +382,8 @@ for info on changes for earlier releases.
[#119]: https://github.com/benmosher/eslint-plugin-import/issues/119
[#89]: https://github.com/benmosher/eslint-plugin-import/issues/89

[Unreleased]: https://github.com/benmosher/eslint-plugin-import/compare/v1.14.0...HEAD
[Unreleased]: https://github.com/benmosher/eslint-plugin-import/compare/v1.15.0...HEAD
[1.15.0]: https://github.com/benmosher/eslint-plugin-import/compare/v1.14.0...v1.15.0
[1.14.0]: https://github.com/benmosher/eslint-plugin-import/compare/v1.13.0...v1.14.0
[1.13.0]: https://github.com/benmosher/eslint-plugin-import/compare/v1.12.0...v1.13.0
[1.12.0]: https://github.com/benmosher/eslint-plugin-import/compare/v1.11.1...v1.12.0
Expand Down Expand Up @@ -422,3 +443,5 @@ for info on changes for earlier releases.
[@zloirock]: https://github.com/zloirock
[@rhys-vdw]: https://github.com/rhys-vdw
[@wKich]: https://github.com/wKich
[@tizmagik]: https://github.com/tizmagik
[@knpwrs]: https://github.com/knpwrs
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a
* Ensure a default export is present, given a default import. ([`default`])
* Ensure imported namespaces contain dereferenced properties as they are dereferenced. ([`namespace`])
* Restrict which files can be imported in a given folder ([`no-restricted-paths`])
* Forbid import of modules using absolute paths ([`no-absolute-path`])

[`no-unresolved`]: ./docs/rules/no-unresolved.md
[`named`]: ./docs/rules/named.md
[`default`]: ./docs/rules/default.md
[`namespace`]: ./docs/rules/namespace.md
[`no-restricted-paths`]: ./docs/rules/no-restricted-paths.md
[`no-absolute-path`]: ./docs/rules/no-absolute-path.md

**Helpful warnings:**

Expand Down Expand Up @@ -52,6 +54,7 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a
[`no-amd`]: ./docs/rules/no-amd.md
[`no-nodejs-modules`]: ./docs/rules/no-nodejs-modules.md


**Style guide:**

* Ensure all imports appear before other statements ([`imports-first`])
Expand All @@ -61,6 +64,7 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a
* Enforce a convention in module import order ([`order`])
* Enforce a newline after import statements ([`newline-after-import`])
* Prefer a default export if module exports a single name ([`prefer-default-export`])
* Limit the maximum number of dependencies a module can have. ([`max-dependencies`])

[`imports-first`]: ./docs/rules/imports-first.md
[`no-duplicates`]: ./docs/rules/no-duplicates.md
Expand All @@ -69,7 +73,7 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a
[`order`]: ./docs/rules/order.md
[`newline-after-import`]: ./docs/rules/newline-after-import.md
[`prefer-default-export`]: ./docs/rules/prefer-default-export.md

[`max-dependencies`]: ./docs/rules/max-dependencies.md

## Installation

Expand Down
44 changes: 44 additions & 0 deletions docs/rules/max-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# max-dependencies

Forbid modules to have too many dependencies (`import` or `require` statements).

This is a useful rule because a module with too many dependencies is a code smell, and usually indicates the module is doing too much and/or should be broken up into smaller modules.

Importing multiple named exports from a single module will only count once (e.g. `import {x, y, z} from './foo'` will only count as a single dependency).

### Options

This rule takes the following option:

`max`: The maximum number of dependencies allowed. Anything over will trigger the rule. **Default is 10** if the rule is enabled and no `max` is specified.

You can set the option like this:

```js
"import/max-dependencies": ["error", {"max": 10}]
```


## Example

Given a max value of `{"max": 2}`:

### Fail

```js
import a from './a'; // 1
const b = require('./b'); // 2
import c from './c'; // 3 - exceeds max!
```

### Pass

```js
import a from './a'; // 1
const anotherA = require('./a'); // still 1
import {x, y, z} from './foo'; // 2
```

## When Not To Use It

If you don't care how many dependencies a module has.
27 changes: 27 additions & 0 deletions docs/rules/no-absolute-path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Forbid import of modules using absolute paths

Node.js allows the import of modules using an absolute path such as `/home/xyz/file.js`. That is a bad practice as it ties the code using it to your computer, and therefore makes it unusable in packages distributed on `npm` for instance.

## Rule Details

### Fail

```js
import f from '/foo';
import f from '/some/path';

var f = require('/foo');
var f = require('/some/path');
```

### Pass

```js
import _ from 'lodash';
import foo from 'foo';
import foo from './foo';

var _ = require('lodash');
var foo = require('foo');
var foo = require('./foo');
```
7 changes: 7 additions & 0 deletions docs/rules/no-extraneous-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ You can set the options like this:
"import/no-extraneous-dependencies": ["error", {"devDependencies": false, "optionalDependencies": false, "peerDependencies": false}]
```

You can also use an array of globs instead of literal booleans:

```js
"import/no-extraneous-dependencies": ["error", {"devDependencies": ['*.test.js', '*.spec.js']}]
```

When using an array of globs, the setting will be activated if the name of the file being linted matches a single glob in the array.

## Rule Details

Expand Down
9 changes: 9 additions & 0 deletions docs/rules/no-nodejs-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Forbid the use of Node.js builtin modules. Can be useful for client-side web projects that do not have access to those modules.

### Options

This rule supports the following options:

- `allow`: Array of names of allowed modules. Defaults to an empty array.

## Rule Details

### Fail
Expand All @@ -24,6 +30,9 @@ import foo from './foo';
var _ = require('lodash');
var foo = require('foo');
var foo = require('./foo');

/* eslint import/no-nodejs-modules: ["error", {"allow": ["path"]}] */
import path from 'path';
```

## When Not To Use It
Expand Down
8 changes: 4 additions & 4 deletions docs/rules/prefer-default-export.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# prefer-default-export

When there is only a single export from a module prefer using default export over named export.
When there is only a single export from a module, prefer using default export over named export.

## Rule Details

Expand All @@ -9,7 +9,7 @@ The following patterns are considered warnings:
```javascript
// bad.js

// There is only a single module export and its a named export.
// There is only a single module export and it's a named export.
export const foo = 'foo';

```
Expand All @@ -28,15 +28,15 @@ export default 'bar';
```javascript
// good2.js

// There is more thank one named exports in the module.
// There is more than one named export in the module.
export const foo = 'foo';
export const bar = 'bar';
```

```javascript
// good3.js

// There is more thank one named exports in the module
// There is more than one named export in the module
const foo = 'foo';
const bar = 'bar';
export { foo, bar }
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-import",
"version": "1.14.0",
"version": "1.15.0",
"description": "Import with sanity.",
"main": "lib/index.js",
"directories": {
Expand Down Expand Up @@ -45,7 +45,7 @@
"homepage": "https://github.com/benmosher/eslint-plugin-import",
"devDependencies": {
"babel-eslint": "next",
"babel-plugin-istanbul": "^1.0.3",
"babel-plugin-istanbul": "^2.0.1",
"babel-preset-es2015": "^6.6.0",
"babel-preset-es2015-loose": "^7.0.0",
"babel-register": "6.9.0",
Expand All @@ -64,7 +64,7 @@
"redux": "^3.0.4",
"rimraf": "2.5.2",
"typescript": "^1.8.10",
"typescript-eslint-parser": "^0.1.1"
"typescript-eslint-parser": "^0.2.0"
},
"peerDependencies": {
"eslint": "2.x - 3.x"
Expand All @@ -73,14 +73,15 @@
"builtin-modules": "^1.1.1",
"contains-path": "^0.1.0",
"debug": "^2.2.0",
"doctrine": "1.2.x",
"doctrine": "1.3.x",
"es6-map": "^0.1.3",
"es6-set": "^0.1.4",
"eslint-import-resolver-node": "^0.2.0",
"lodash.cond": "^4.3.0",
"lodash.endswith": "^4.0.1",
"lodash.find": "^4.3.0",
"lodash.findindex": "^4.3.0",
"minimatch": "^3.0.3",
"object-assign": "^4.0.1",
"pkg-dir": "^1.0.0",
"pkg-up": "^1.0.0"
Expand Down
13 changes: 13 additions & 0 deletions resolvers/webpack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ Webpack-literate module resolution plugin for [`eslint-plugin-import`](https://w
Published separately to allow pegging to a specific version in case of breaking
changes.

To use with `eslint-plugin-import`, run:

```
npm i eslint-import-resolver-webpack -g
```

or if you manage ESLint as a dev dependency:

```
# inside your project's working tree
npm install eslint-import-resolver-webpack --save-dev
```

Will look for `webpack.config.js` as a sibling of the first ancestral `package.json`,
or a `config` parameter may be provided with another filename/path either relative to the
`package.json`, or a complete, absolute path.
Expand Down
5 changes: 5 additions & 0 deletions src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ function constant(value) {
return () => value
}

function isAbsolute(name) {
return name.indexOf('/') === 0
}

export function isBuiltIn(name, settings) {
const extras = (settings && settings['import/core-modules']) || []
return builtinModules.indexOf(name) !== -1 || extras.indexOf(name) > -1
Expand Down Expand Up @@ -46,6 +50,7 @@ function isRelativeToSibling(name) {
}

const typeTest = cond([
[isAbsolute, constant('absolute')],
[isBuiltIn, constant('builtin')],
[isExternalModule, constant('external')],
[isScoped, constant('external')],
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export const rules = {
'no-amd': require('./rules/no-amd'),
'no-duplicates': require('./rules/no-duplicates'),
'imports-first': require('./rules/imports-first'),
'max-dependencies': require('./rules/max-dependencies'),
'no-extraneous-dependencies': require('./rules/no-extraneous-dependencies'),
'no-absolute-path': require('./rules/no-absolute-path'),
'no-nodejs-modules': require('./rules/no-nodejs-modules'),
'order': require('./rules/order'),
'newline-after-import': require('./rules/newline-after-import'),
Expand Down
49 changes: 49 additions & 0 deletions src/rules/max-dependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Set from 'es6-set'
import isStaticRequire from '../core/staticRequire'

const DEFAULT_MAX = 10

const countDependencies = (dependencies, lastNode, context) => {
const {max} = context.options[0] || { max: DEFAULT_MAX }

if (dependencies.size > max) {
context.report(
lastNode,
`Maximum number of dependencies (${max}) exceeded.`
)
}
}

module.exports = context => {
const dependencies = new Set() // keep track of dependencies
let lastNode // keep track of the last node to report on

return {
ImportDeclaration(node) {
dependencies.add(node.source.value)
lastNode = node.source
},

CallExpression(node) {
if (isStaticRequire(node)) {
const [ requirePath ] = node.arguments
dependencies.add(requirePath.value)
lastNode = node
}
},

'Program:exit': function () {
countDependencies(dependencies, lastNode, context)
},
}
}

module.exports.schema = [
{
'type': 'object',
'properties': {
'max': { 'type': 'number' },
},
'additionalProperties': false,
},
]
Loading

0 comments on commit 2e4e449

Please sign in to comment.