UNPKG

13.3 kBMarkdownView Raw
1# eslint-plugin-import
2
3[![build status](https://travis-ci.org/benmosher/eslint-plugin-import.svg?branch=master)](https://travis-ci.org/benmosher/eslint-plugin-import)
4[![Coverage Status](https://coveralls.io/repos/github/benmosher/eslint-plugin-import/badge.svg?branch=master)](https://coveralls.io/github/benmosher/eslint-plugin-import?branch=master)
5[![win32 build status](https://ci.appveyor.com/api/projects/status/3mw2fifalmjlqf56/branch/master?svg=true)](https://ci.appveyor.com/project/benmosher/eslint-plugin-import/branch/master)
6[![npm](https://img.shields.io/npm/v/eslint-plugin-import.svg)](https://www.npmjs.com/package/eslint-plugin-import)
7[![npm downloads](https://img.shields.io/npm/dt/eslint-plugin-import.svg?maxAge=2592000)](http://www.npmtrends.com/eslint-plugin-import)
8
9This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, and prevent issues with misspelling of file paths and import names. All the goodness that the ES2015+ static module syntax intends to provide, marked up in your editor.
10
11**IF YOU ARE USING THIS WITH SUBLIME**: see the [bottom section](#sublimelinter-eslint) for important info.
12
13## Rules
14
15**Static analysis:**
16
17* Ensure imports point to a file/module that can be resolved. ([`no-unresolved`])
18* Ensure named imports correspond to a named export in the remote file. ([`named`])
19* Ensure a default export is present, given a default import. ([`default`])
20* Ensure imported namespaces contain dereferenced properties as they are dereferenced. ([`namespace`])
21* Restrict which files can be imported in a given folder ([`no-restricted-paths`])
22
23[`no-unresolved`]: ./docs/rules/no-unresolved.md
24[`named`]: ./docs/rules/named.md
25[`default`]: ./docs/rules/default.md
26[`namespace`]: ./docs/rules/namespace.md
27[`no-restricted-paths`]: ./docs/rules/no-restricted-paths.md
28
29**Helpful warnings:**
30
31* Report any invalid exports, i.e. re-export of the same name ([`export`])
32* Report use of exported name as identifier of default export ([`no-named-as-default`])
33* Report use of exported name as property of default export ([`no-named-as-default-member`])
34* Report imported names marked with `@deprecated` documentation tag ([`no-deprecated`])
35* Forbid the use of extraneous packages ([`no-extraneous-dependencies`])
36* Forbid the use of mutable exports with `var` or `let`. ([`no-mutable-exports`])
37
38[`export`]: ./docs/rules/export.md
39[`no-named-as-default`]: ./docs/rules/no-named-as-default.md
40[`no-named-as-default-member`]: ./docs/rules/no-named-as-default-member.md
41[`no-deprecated`]: ./docs/rules/no-deprecated.md
42[`no-extraneous-dependencies`]: ./docs/rules/no-extraneous-dependencies.md
43[`no-mutable-exports`]: ./docs/rules/no-mutable-exports.md
44
45**Module systems:**
46
47* Report CommonJS `require` calls and `module.exports` or `exports.*`. ([`no-commonjs`])
48* Report AMD `require` and `define` calls. ([`no-amd`])
49* No Node.js builtin modules. ([`no-nodejs-modules`])
50
51[`no-commonjs`]: ./docs/rules/no-commonjs.md
52[`no-amd`]: ./docs/rules/no-amd.md
53[`no-nodejs-modules`]: ./docs/rules/no-nodejs-modules.md
54
55**Style guide:**
56
57* Ensure all imports appear before other statements ([`imports-first`])
58* Report repeated import of the same module in multiple places ([`no-duplicates`])
59* Report namespace imports ([`no-namespace`])
60* Ensure consistent use of file extension within the import path ([`extensions`])
61* Enforce a convention in module import order ([`order`])
62* Enforce a newline after import statements ([`newline-after-import`])
63* Prefer a default export if module exports a single name ([`prefer-default-export`])
64
65[`imports-first`]: ./docs/rules/imports-first.md
66[`no-duplicates`]: ./docs/rules/no-duplicates.md
67[`no-namespace`]: ./docs/rules/no-namespace.md
68[`extensions`]: ./docs/rules/extensions.md
69[`order`]: ./docs/rules/order.md
70[`newline-after-import`]: ./docs/rules/newline-after-import.md
71[`prefer-default-export`]: ./docs/rules/prefer-default-export.md
72
73
74## Installation
75
76```sh
77npm install eslint-plugin-import -g
78```
79
80or if you manage ESLint as a dev dependency:
81
82```sh
83# inside your project's working tree
84npm install eslint-plugin-import --save-dev
85```
86
87All rules are off by default. However, you may configure them manually
88in your `.eslintrc.(yml|json|js)`, or extend one of the canned configs:
89
90```yaml
91---
92extends:
93 - eslint:recommended
94 - plugin:import/errors
95 - plugin:import/warnings
96
97# or configure manually:
98plugins:
99 - import
100
101rules:
102 import/no-unresolved: [2, {commonjs: true, amd: true}]
103 import/named: 2
104 import/namespace: 2
105 import/default: 2
106 import/export: 2
107 # etc...
108```
109
110# Resolvers
111
112With the advent of module bundlers and the current state of modules and module
113syntax specs, it's not always obvious where `import x from 'module'` should look
114to find the file behind `module`.
115
116Up through v0.10ish, this plugin has directly used substack's [`resolve`] plugin,
117which implements Node's import behavior. This works pretty well in most cases.
118
119However, Webpack allows a number of things in import module source strings that
120Node does not, such as loaders (`import 'file!./whatever'`) and a number of
121aliasing schemes, such as [`externals`]: mapping a module id to a global name at
122runtime (allowing some modules to be included more traditionally via script tags).
123
124In the interest of supporting both of these, v0.11 introduces resolvers.
125
126Currently [Node] and [Webpack] resolution have been implemented, but the
127resolvers are just npm packages, so [third party packages are supported](https://github.com/benmosher/eslint-plugin-import/wiki/Resolvers) (and encouraged!).
128
129You can reference resolvers in several ways(in order of precedence):
130
131- as a conventional `eslint-import-resolver` name, like `eslint-import-resolver-foo`:
132
133```yaml
134# .eslintrc.yml
135settings:
136 # uses 'eslint-import-resolver-foo':
137 import/resolver: foo
138```
139```js
140// .eslintrc.js
141module.exports = {
142 settings: {
143 'import/resolver': {
144 foo: { someConfig: value }
145 }
146 }
147}
148```
149
150- with a full npm module name, like `my-awesome-npm-module`:
151
152```yaml
153# .eslintrc.yml
154settings:
155 import/resolver: 'my-awesome-npm-module'
156```
157```js
158// .eslintrc.js
159module.exports = {
160 settings: {
161 'import/resolver': {
162 'my-awesome-npm-module': { someConfig: value }
163 }
164 }
165}
166```
167
168- with a filesystem path to resolver, defined in this example as a `computed property` name:
169
170```js
171// .eslintrc.js
172module.exports = {
173 settings: {
174 'import/resolver': {
175 [path.resolve('../../../my-resolver')]: { someConfig: value }
176 }
177 }
178}
179```
180
181Relative paths will be resolved relative to the source's nearest `package.json` or
182the process's current working directory if no `package.json` is found.
183
184
185
186If you are interesting in writing a resolver, see the [spec](./resolvers/README.md) for more details.
187
188[`resolve`]: https://www.npmjs.com/package/resolve
189[`externals`]: http://webpack.github.io/docs/library-and-externals.html
190
191[Node]: https://www.npmjs.com/package/eslint-import-resolver-node
192[Webpack]: https://www.npmjs.com/package/eslint-import-resolver-webpack
193
194# Settings
195
196You may set the following settings in your `.eslintrc`:
197
198#### `import/extensions`
199
200A list of file extensions that will be parsed as modules and inspected for
201`export`s.
202
203This will default to `['.js']` in the next major revision of this plugin, unless
204you are using the `react` shared config, in which case it is specified as `['.js', '.jsx']`.
205
206Note that this is different from (and likely a subset of) any `import/resolver`
207extensions settings, which may include `.json`, `.coffee`, etc. which will still
208factor into the `no-unresolved` rule.
209
210Also, `import/ignore` patterns will overrule this list, so `node_modules` that
211end in `.js` will still be ignored by default.
212
213#### `import/ignore`
214
215A list of regex strings that, if matched by a path, will
216not report the matching module if no `export`s are found.
217In practice, this means rules other than [`no-unresolved`](./docs/rules/no-unresolved.md#ignore) will not report on any
218`import`s with (absolute) paths matching this pattern, _unless_ `export`s were
219found when parsing. This allows you to ignore `node_modules` but still properly
220lint packages that define a [`jsnext:main`] in `package.json` (Redux, D3's v4 packages, etc.).
221
222`no-unresolved` has its own [`ignore`](./docs/rules/no-unresolved.md#ignore) setting.
223
224**Note**: setting this explicitly will replace the default of `node_modules`, so you
225may need to include it in your own list if you still want to ignore it. Example:
226
227```yaml
228settings:
229 import/ignore:
230 - node_modules # mostly CommonJS (ignored by default)
231 - \.coffee$ # fraught with parse errors
232 - \.(scss|less|css)$ # can't parse unprocessed CSS modules, either
233```
234
235[`jsnext:main`]: https://github.com/rollup/rollup/wiki/jsnext:main
236
237#### `import/core-modules`
238
239An array of additional modules to consider as "core" modules--modules that should
240be considered resolved but have no path on the filesystem. Your resolver may
241already define some of these (for example, the Node resolver knows about `fs` and
242`path`), so you need not redefine those.
243
244For example, Electron exposes an `electron` module:
245
246```js
247import 'electron' // without extra config, will be flagged as unresolved!
248```
249
250that would otherwise be unresolved. To avoid this, you may provide `electron` as a
251core module:
252
253```yaml
254# .eslintrc.yml
255settings:
256 import/core-modules: [ electron ]
257```
258
259In Electron's specific case, there is a shared config named `electron`
260that specifies this for you.
261
262Contribution of more such shared configs for other platforms are welcome!
263
264#### `import/external-module-folders`
265
266An array of folders. Resolved modules only from those folders will be considered as "external". By default - `["node_modules"]`. Makes sense if you have configured your path or webpack to handle your internal paths differently and want to considered modules from some folders, for example `bower_components` or `jspm_modules`, as "external".
267
268#### `import/resolver`
269
270See [resolvers](#resolvers).
271
272#### `import/cache`
273
274Settings for cache behavior. Memoization is used at various levels to avoid the copious amount of `fs.statSync`/module parse calls required to correctly report errors.
275
276For normal `eslint` console runs, the cache lifetime is irrelevant, as we can strongly assume that files should not be changing during the lifetime of the linter process (and thus, the cache in memory)
277
278For long-lasting processes, like [`eslint_d`] or [`eslint-loader`], however, it's important that there be some notion of staleness.
279
280If you never use [`eslint_d`] or [`eslint-loader`], you may set the cache lifetime to `Infinity` and everything should be fine:
281
282```yaml
283# .eslintrc.yml
284settings:
285 import/cache:
286 lifetime: ∞ # or Infinity
287```
288
289Otherwise, set some integer, and cache entries will be evicted after that many seconds have elapsed:
290
291```yaml
292# .eslintrc.yml
293settings:
294 import/cache:
295 lifetime: 5 # 30 is the default
296```
297
298[`eslint_d`]: https://www.npmjs.com/package/eslint_d
299[`eslint-loader`]: https://www.npmjs.com/package/eslint-loader
300
301## SublimeLinter-eslint
302
303SublimeLinter-eslint introduced a change to support `.eslintignore` files
304which altered the way file paths are passed to ESLint when linting during editing.
305This change sends a relative path instead of the absolute path to the file (as ESLint
306normally provides), which can make it impossible for this plugin to resolve dependencies
307on the filesystem.
308
309This workaround should no longer be necessary with the release of ESLint 2.0, when
310`.eslintignore` will be updated to work more like a `.gitignore`, which should
311support proper ignoring of absolute paths via `--stdin-filename`.
312
313In the meantime, see [roadhump/SublimeLinter-eslint#58](https://github.com/roadhump/SublimeLinter-eslint/issues/58)
314for more details and discussion, but essentially, you may find you need to add the following
315`SublimeLinter` config to your Sublime project file:
316
317```json
318{
319 "folders":
320 [
321 {
322 "path": "code"
323 }
324 ],
325 "SublimeLinter":
326 {
327 "linters":
328 {
329 "eslint":
330 {
331 "chdir": "${project}/code"
332 }
333 }
334 }
335}
336```
337
338Note that `${project}/code` matches the `code` provided at `folders[0].path`.
339
340The purpose of the `chdir` setting, in this case, is to set the working directory
341from which ESLint is executed to be the same as the directory on which SublimeLinter-eslint
342bases the relative path it provides.
343
344See the SublimeLinter docs on [`chdir`](http://www.sublimelinter.com/en/latest/linter_settings.html#chdir)
345for more information, in case this does not work with your project.
346
347If you are not using `.eslintignore`, or don't have a Sublime project file, you can also
348do the following via a `.sublimelinterrc` file in some ancestor directory of your
349code:
350
351```json
352{
353 "linters": {
354 "eslint": {
355 "args": ["--stdin-filename", "@"]
356 }
357 }
358}
359```
360
361I also found that I needed to set `rc_search_limit` to `null`, which removes the file
362hierarchy search limit when looking up the directory tree for `.sublimelinterrc`:
363
364In Package Settings / SublimeLinter / User Settings:
365```json
366{
367 "user": {
368 "rc_search_limit": null
369 }
370}
371```
372
373I believe this defaults to `3`, so you may not need to alter it depending on your
374project folder max depth.