Skip to content

Commit 3ffa728

Browse files
committed
Merge pull request #131 from benmosher/docs
separate rule docs
2 parents dbd358c + ff32e5a commit 3ffa728

File tree

10 files changed

+458
-203
lines changed

10 files changed

+458
-203
lines changed

README.md

Lines changed: 22 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,33 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a
1111

1212
## Rules
1313

14-
* Ensure imports point to a file/module that can be resolved. ([`no-unresolved`](#no-unresolved))
15-
* Ensure named imports correspond to a named export in the remote file. ([`named`](#named))
16-
* Ensure a default export is present, given a default import. ([`default`](#default))
17-
* Ensure imported namespaces contain dereferenced properties as they are dereferenced. ([`namespace`](#namespace))
18-
* Report any invalid exports, i.e. re-export of the same name ([`export`](#export))
14+
* Ensure imports point to a file/module that can be resolved. ([`no-unresolved`])
15+
* Ensure named imports correspond to a named export in the remote file. ([`named`])
16+
* Ensure a default export is present, given a default import. ([`default`])
17+
* Ensure imported namespaces contain dereferenced properties as they are dereferenced. ([`namespace`])
18+
* Report any invalid exports, i.e. re-export of the same name ([`export`])
19+
20+
[`no-unresolved`]: ./docs/rules/no-unresolved.md
21+
[`named`]: ./docs/rules/named.md
22+
[`default`]: ./docs/rules/default.md
23+
[`namespace`]: ./docs/rules/namespace.md
24+
[`export`]: ./docs/rules/export.md
1925

2026
Helpful warnings:
2127

22-
* Report CommonJS `require` calls. ([`no-require`](#no-require))
23-
* Report use of exported name as identifier of default export ([`no-named-as-default`](#no-named-as-default))
24-
* Report repeated import of the same module in multiple places ([`no-duplicates`](#no-duplicates), warning by default)
28+
* Report use of exported name as identifier of default export ([`no-named-as-default`])
2529

26-
Style rules:
30+
[`no-named-as-default`]: ./docs/rules/no-named-as-default.md
2731

28-
* Ensure all imports appear before other statements ([`imports-first`](#imports-first))
32+
Style guide:
33+
34+
* Report CommonJS `require` calls. ([`no-require`])
35+
* Ensure all imports appear before other statements ([`imports-first`])
36+
* Report repeated import of the same module in multiple places ([`no-duplicates`])
37+
38+
[`no-require`]: ./docs/rules/no-require.md
39+
[`imports-first`]: ./docs/rules/imports-first.md
40+
[`no-duplicates`]: ./docs/rules/no-duplicates.md
2941

3042
## Installation
3143

@@ -63,199 +75,6 @@ rules:
6375
# etc...
6476
```
6577

66-
67-
# Rule Details
68-
69-
### `no-unresolved`
70-
71-
Ensures an imported module can be resolved to a module on the local filesystem,
72-
as defined by standard Node `require.resolve` behavior.
73-
74-
See [settings](#settings) for customization options for the resolution (i.e.
75-
additional filetypes, `NODE_PATH`, etc.)
76-
77-
This rule can also optionally report on unresolved modules in CommonJS `require('./foo')` calls and AMD `require(['./foo'], function (foo){...})` and `define(['./foo'], function (foo){...})`.
78-
79-
To enable this, send `{ commonjs: true/false, amd: true/false }` as a rule option.
80-
Both are disabled by default.
81-
82-
If you are using Webpack, see the section on [resolver plugins](#resolver-plugins).
83-
84-
### `named`
85-
86-
Verifies that all named imports are part of the set of named exports in the referenced module.
87-
88-
For `export`, verifies that all named exports exist in the referenced module.
89-
90-
### `default`
91-
92-
If a default import is requested, this rule will report if there is no default
93-
export in the imported module.
94-
95-
For [ES7], reports if a default is named and exported but is not found in the
96-
referenced module.
97-
98-
### `namespace`
99-
100-
Enforces names exist at the time they are dereferenced, when imported as a full namespace (i.e. `import * as foo from './foo'; foo.bar();` will report if `bar` is not exported by `./foo`.).
101-
102-
Will report at the import declaration if there are _no_ exported names found.
103-
104-
Also, will report for computed references (i.e. `foo["bar"]()`).
105-
106-
Reports on assignment to a member of an imported namespace.
107-
108-
**Implementation note**: currently, this rule does not check for possible
109-
redefinition of the namespace in an intermediate scope. Adherence to the ESLint
110-
`no-shadow` rule for namespaces will prevent this from being a problem.
111-
112-
For [ES7], reports if an exported namespace would be empty (no names exported from the referenced module.)
113-
114-
### `no-require`
115-
116-
Reports `require([string])` function calls. Will not report if >1 argument,
117-
or single argument is not a literal string.
118-
119-
Intended for temporary use when migrating to pure ES6 modules.
120-
121-
Given:
122-
```js
123-
// ./mod.js
124-
export const foo = 'bar'
125-
export function bar() { return foo }
126-
127-
// ./common.js
128-
exports.something = 'whatever'
129-
```
130-
131-
This would be reported:
132-
133-
```js
134-
var mod = require('./mod')
135-
, common = require('./common')
136-
, fs = require('fs')
137-
, whateverModule = require('./not-found')
138-
```
139-
140-
### `no-named-as-default`
141-
142-
Reports use of an exported name as the locally imported name of a default export.
143-
144-
Given:
145-
```js
146-
// foo.js
147-
export default 'foo';
148-
export const bar = 'baz';
149-
```
150-
151-
...this would be valid:
152-
```js
153-
import foo from './foo.js';
154-
```
155-
156-
...and this would be reported:
157-
```js
158-
// message: Using exported name 'bar' as identifier for default export.
159-
import bar from './foo.js';
160-
```
161-
162-
Rationale: using an exported name as the name of the default export is likely...
163-
164-
- *misleading*: others familiar with `foo.js` probably expect the name to be `foo`
165-
- *a mistake*: only needed to import `bar` and forgot the brackets (the case that is prompting this)
166-
167-
For [ES7], this also prevents exporting the default from a referenced module as a name within than module, for the same reasons:
168-
169-
```js
170-
// valid:
171-
export foo from './foo.js'
172-
173-
// message: Using exported name 'bar' as identifier for default export.
174-
export bar from './foo.js';
175-
```
176-
177-
### `export`
178-
179-
Reports funny business with exports, such as
180-
181-
```js
182-
export default class MyClass { /*...*/ } // Multiple default exports.
183-
184-
function makeClass() { return new MyClass(...arguments) }
185-
186-
export default makeClass // Multiple default exports.
187-
```
188-
189-
or
190-
```js
191-
export const foo = function () { /*...*/ } // Multiple exports of name 'foo'.
192-
193-
function bar() { /*...*/ }
194-
export { bar as foo } // Multiple exports of name 'foo'.
195-
```
196-
197-
In the case of named/default re-export, all `n` re-exports will be reported,
198-
as at least `n-1` of them are clearly mistakes, but it is not clear which one
199-
(if any) is intended. Could be the result of copy/paste, code duplication with
200-
intent to rename, etc.
201-
202-
203-
[ES7]: https://github.com/leebyron/ecmascript-more-export-from
204-
205-
206-
### `no-duplicates`
207-
208-
Reports if a resolved path is imported more than once.
209-
210-
Valid:
211-
```js
212-
import SomeDefaultClass, * as names from './mod'
213-
```
214-
215-
...whereas here, both `./mod` imports will be reported:
216-
217-
```js
218-
import SomeDefaultClass from './mod'
219-
220-
// oops, some other import separated these lines
221-
import foo from './some-other-mod'
222-
223-
import * as names from './mod'
224-
```
225-
226-
The motivation is that this is likely a result of two developers importing different
227-
names from the same module at different times (and potentially largely different
228-
locations in the file.) This rule brings both (or n-many) to attention.
229-
230-
This rule is only set to a warning, by default.
231-
232-
233-
### `imports-first`
234-
235-
By popular demand, this rule reports any imports that come after non-import
236-
statments:
237-
238-
```js
239-
import foo from './foo'
240-
241-
// some module-level initializer
242-
initWith(foo)
243-
244-
import bar from './bar' // <- reported
245-
```
246-
247-
Providing `absolute-first` as an option will report any absolute imports (i.e.
248-
packages) that come after any relative imports:
249-
250-
```js
251-
import foo from 'foo'
252-
import bar from './bar'
253-
254-
import * as _ from 'lodash' // <- reported
255-
```
256-
257-
This rule is disabled by default.
258-
25978
# Resolver plugins
26079

26180
With the advent of module bundlers and the current state of modules and module

docs/rules/default.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# default
2+
3+
If a default import is requested, this rule will report if there is no default
4+
export in the imported module.
5+
6+
For [ES7], reports if a default is named and exported but is not found in the
7+
referenced module.
8+
9+
Note: for modules, the plugin will find exported names (including defaults)
10+
from [`jsnext:main`], if present in `package.json`.
11+
Redux's npm module includes this key, and thereby is lintable, for example. Otherwise,
12+
the whole `node_modules` folder is ignored by default ([`import/ignore`]) as most published modules are
13+
formatted in CommonJS, which [at time of this writing](https://github.com/benmosher/eslint-plugin-import/issues/13)
14+
is not able to be analyzed for exports.
15+
16+
17+
## Rule Details
18+
19+
Given:
20+
21+
```js
22+
// ./foo.js
23+
export default function () { return 42 }
24+
25+
// ./bar.js
26+
export function bar() { return null }
27+
28+
// ./baz.js
29+
module.exports = function () { /* ... */ }
30+
31+
// node_modules/some-module/index.js
32+
exports.sharedFunction = function shared() { /* ... */ }
33+
```
34+
35+
The following is considered valid:
36+
37+
```js
38+
import foo from './foo'
39+
40+
// assuming 'node_modules' are ignored (true by default)
41+
import someModule from 'some-module'
42+
```
43+
44+
...and the following cases are reported:
45+
46+
```js
47+
import bar from './bar' // no default export found in ./bar
48+
import baz from './baz' // no default export found in ./baz
49+
```
50+
51+
52+
## When Not To Use It
53+
54+
If you are using CommonJS and/or modifying the exported namespace of any module at
55+
runtime, you will likely see false positives with this rule.
56+
57+
This rule currently does not interpret `module.exports = ...` as a `default` export,
58+
either, so such a situation will be reported in the importing module.
59+
60+
## Further Reading
61+
62+
- Lee Byron's [ES7] export proposal
63+
- [`import/ignore`] setting
64+
- [`jsnext:main`] (Rollup)
65+
66+
67+
[ES7]: https://github.com/leebyron/ecmascript-more-export-from
68+
[`import/ignore`]: ../../README.md#importignore
69+
[`jsnext:main`]: https://github.com/rollup/rollup/wiki/jsnext:main

docs/rules/export.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# export
2+
3+
Reports funny business with exports, like repeated exports of names or defaults.
4+
5+
## Rule Details
6+
7+
```js
8+
export default class MyClass { /*...*/ } // Multiple default exports.
9+
10+
function makeClass() { return new MyClass(...arguments) }
11+
12+
export default makeClass // Multiple default exports.
13+
```
14+
15+
or
16+
```js
17+
export const foo = function () { /*...*/ } // Multiple exports of name 'foo'.
18+
19+
function bar() { /*...*/ }
20+
export { bar as foo } // Multiple exports of name 'foo'.
21+
```
22+
23+
In the case of named/default re-export, all `n` re-exports will be reported,
24+
as at least `n-1` of them are clearly mistakes, but it is not clear which one
25+
(if any) is intended. Could be the result of copy/paste, code duplication with
26+
intent to rename, etc.
27+
28+
## Further Reading
29+
30+
- Lee Byron's [ES7] export proposal
31+
32+
[ES7]: https://github.com/leebyron/ecmascript-more-export-from

docs/rules/imports-first.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# imports-first
2+
3+
By popular demand, this rule reports any imports that come after non-import
4+
statments.
5+
6+
## Rule Details
7+
8+
```js
9+
import foo from './foo'
10+
11+
// some module-level initializer
12+
initWith(foo)
13+
14+
import bar from './bar' // <- reported
15+
```
16+
17+
Providing `absolute-first` as an option will report any absolute imports (i.e.
18+
packages) that come after any relative imports:
19+
20+
```js
21+
import foo from 'foo'
22+
import bar from './bar'
23+
24+
import * as _ from 'lodash' // <- reported
25+
```
26+
27+
TODO: add explanation of imported name hoisting
28+
29+
## When Not To Use It
30+
31+
If you don't mind imports being sprinkled throughout, you may not want to
32+
enable this rule.

0 commit comments

Comments
 (0)