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 |
|
9 | This 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 | * Forbid import of modules using absolute paths ([`no-absolute-path`])
|
23 | * Forbid `require()` calls with expressions ([`no-dynamic-require`])
|
24 | * Prevent importing the submodules of other modules ([`no-internal-modules`])
|
25 | * Forbid webpack loader syntax in imports ([`no-webpack-loader-syntax`])
|
26 | * Forbid a module from importing itself ([`no-self-import`])
|
27 | * Forbid a module from importing a module with a dependency path back to itself ([`no-cycle`])
|
28 | * Prevent unnecessary path segments in import and require statements ([`no-useless-path-segments`])
|
29 | * Forbid importing modules from parent directories ([`no-relative-parent-imports`])
|
30 |
|
31 | [`no-unresolved`]: ./docs/rules/no-unresolved.md
|
32 | [`named`]: ./docs/rules/named.md
|
33 | [`default`]: ./docs/rules/default.md
|
34 | [`namespace`]: ./docs/rules/namespace.md
|
35 | [`no-restricted-paths`]: ./docs/rules/no-restricted-paths.md
|
36 | [`no-absolute-path`]: ./docs/rules/no-absolute-path.md
|
37 | [`no-dynamic-require`]: ./docs/rules/no-dynamic-require.md
|
38 | [`no-internal-modules`]: ./docs/rules/no-internal-modules.md
|
39 | [`no-webpack-loader-syntax`]: ./docs/rules/no-webpack-loader-syntax.md
|
40 | [`no-self-import`]: ./docs/rules/no-self-import.md
|
41 | [`no-cycle`]: ./docs/rules/no-cycle.md
|
42 | [`no-useless-path-segments`]: ./docs/rules/no-useless-path-segments.md
|
43 | [`no-relative-parent-imports`]: ./docs/rules/no-relative-parent-imports.md
|
44 |
|
45 | ### Helpful warnings
|
46 |
|
47 |
|
48 | * Report any invalid exports, i.e. re-export of the same name ([`export`])
|
49 | * Report use of exported name as identifier of default export ([`no-named-as-default`])
|
50 | * Report use of exported name as property of default export ([`no-named-as-default-member`])
|
51 | * Report imported names marked with `@deprecated` documentation tag ([`no-deprecated`])
|
52 | * Forbid the use of extraneous packages ([`no-extraneous-dependencies`])
|
53 | * Forbid the use of mutable exports with `var` or `let`. ([`no-mutable-exports`])
|
54 |
|
55 | [`export`]: ./docs/rules/export.md
|
56 | [`no-named-as-default`]: ./docs/rules/no-named-as-default.md
|
57 | [`no-named-as-default-member`]: ./docs/rules/no-named-as-default-member.md
|
58 | [`no-deprecated`]: ./docs/rules/no-deprecated.md
|
59 | [`no-extraneous-dependencies`]: ./docs/rules/no-extraneous-dependencies.md
|
60 | [`no-mutable-exports`]: ./docs/rules/no-mutable-exports.md
|
61 |
|
62 | ### Module systems
|
63 |
|
64 | * Report potentially ambiguous parse goal (`script` vs. `module`) ([`unambiguous`])
|
65 | * Report CommonJS `require` calls and `module.exports` or `exports.*`. ([`no-commonjs`])
|
66 | * Report AMD `require` and `define` calls. ([`no-amd`])
|
67 | * No Node.js builtin modules. ([`no-nodejs-modules`])
|
68 |
|
69 | [`unambiguous`]: ./docs/rules/unambiguous.md
|
70 | [`no-commonjs`]: ./docs/rules/no-commonjs.md
|
71 | [`no-amd`]: ./docs/rules/no-amd.md
|
72 | [`no-nodejs-modules`]: ./docs/rules/no-nodejs-modules.md
|
73 |
|
74 |
|
75 | ### Style guide
|
76 |
|
77 | * Ensure all imports appear before other statements ([`first`])
|
78 | * Ensure all exports appear after other statements ([`exports-last`])
|
79 | * Report repeated import of the same module in multiple places ([`no-duplicates`])
|
80 | * Report namespace imports ([`no-namespace`])
|
81 | * Ensure consistent use of file extension within the import path ([`extensions`])
|
82 | * Enforce a convention in module import order ([`order`])
|
83 | * Enforce a newline after import statements ([`newline-after-import`])
|
84 | * Prefer a default export if module exports a single name ([`prefer-default-export`])
|
85 | * Limit the maximum number of dependencies a module can have ([`max-dependencies`])
|
86 | * Forbid unassigned imports ([`no-unassigned-import`])
|
87 | * Forbid named default exports ([`no-named-default`])
|
88 | * Forbid default exports ([`no-default-export`])
|
89 | * Forbid named exports ([`no-named-export`])
|
90 | * Forbid anonymous values as default exports ([`no-anonymous-default-export`])
|
91 | * Prefer named exports to be grouped together in a single export declaration ([`group-exports`])
|
92 | * Enforce a leading comment with the webpackChunkName for dynamic imports ([`dynamic-import-chunkname`])
|
93 |
|
94 | [`first`]: ./docs/rules/first.md
|
95 | [`exports-last`]: ./docs/rules/exports-last.md
|
96 | [`no-duplicates`]: ./docs/rules/no-duplicates.md
|
97 | [`no-namespace`]: ./docs/rules/no-namespace.md
|
98 | [`extensions`]: ./docs/rules/extensions.md
|
99 | [`order`]: ./docs/rules/order.md
|
100 | [`newline-after-import`]: ./docs/rules/newline-after-import.md
|
101 | [`prefer-default-export`]: ./docs/rules/prefer-default-export.md
|
102 | [`max-dependencies`]: ./docs/rules/max-dependencies.md
|
103 | [`no-unassigned-import`]: ./docs/rules/no-unassigned-import.md
|
104 | [`no-named-default`]: ./docs/rules/no-named-default.md
|
105 | [`no-anonymous-default-export`]: ./docs/rules/no-anonymous-default-export.md
|
106 | [`group-exports`]: ./docs/rules/group-exports.md
|
107 | [`no-default-export`]: ./docs/rules/no-default-export.md
|
108 | [`no-named-export`]: ./docs/rules/no-named-export.md
|
109 | [`dynamic-import-chunkname`]: ./docs/rules/dynamic-import-chunkname.md
|
110 |
|
111 | ## Installation
|
112 |
|
113 | ```sh
|
114 | npm install eslint-plugin-import -g
|
115 | ```
|
116 |
|
117 | or if you manage ESLint as a dev dependency:
|
118 |
|
119 | ```sh
|
120 | # inside your project's working tree
|
121 | npm install eslint-plugin-import --save-dev
|
122 | ```
|
123 |
|
124 | All rules are off by default. However, you may configure them manually
|
125 | in your `.eslintrc.(yml|json|js)`, or extend one of the canned configs:
|
126 |
|
127 | ```yaml
|
128 | ---
|
129 | extends:
|
130 | - eslint:recommended
|
131 | - plugin:import/errors
|
132 | - plugin:import/warnings
|
133 |
|
134 | # or configure manually:
|
135 | plugins:
|
136 | - import
|
137 |
|
138 | rules:
|
139 | import/no-unresolved: [2, {commonjs: true, amd: true}]
|
140 | import/named: 2
|
141 | import/namespace: 2
|
142 | import/default: 2
|
143 | import/export: 2
|
144 | # etc...
|
145 | ```
|
146 |
|
147 | # Resolvers
|
148 |
|
149 | With the advent of module bundlers and the current state of modules and module
|
150 | syntax specs, it's not always obvious where `import x from 'module'` should look
|
151 | to find the file behind `module`.
|
152 |
|
153 | Up through v0.10ish, this plugin has directly used substack's [`resolve`] plugin,
|
154 | which implements Node's import behavior. This works pretty well in most cases.
|
155 |
|
156 | However, webpack allows a number of things in import module source strings that
|
157 | Node does not, such as loaders (`import 'file!./whatever'`) and a number of
|
158 | aliasing schemes, such as [`externals`]: mapping a module id to a global name at
|
159 | runtime (allowing some modules to be included more traditionally via script tags).
|
160 |
|
161 | In the interest of supporting both of these, v0.11 introduces resolvers.
|
162 |
|
163 | Currently [Node] and [webpack] resolution have been implemented, but the
|
164 | resolvers are just npm packages, so [third party packages are supported](https://github.com/benmosher/eslint-plugin-import/wiki/Resolvers) (and encouraged!).
|
165 |
|
166 | You can reference resolvers in several ways (in order of precedence):
|
167 |
|
168 | - as a conventional `eslint-import-resolver` name, like `eslint-import-resolver-foo`:
|
169 |
|
170 | ```yaml
|
171 | # .eslintrc.yml
|
172 | settings:
|
173 | # uses 'eslint-import-resolver-foo':
|
174 | import/resolver: foo
|
175 | ```
|
176 | ```js
|
177 | // .eslintrc.js
|
178 | module.exports = {
|
179 | settings: {
|
180 | 'import/resolver': {
|
181 | foo: { someConfig: value }
|
182 | }
|
183 | }
|
184 | }
|
185 | ```
|
186 |
|
187 | - with a full npm module name, like `my-awesome-npm-module`:
|
188 |
|
189 | ```yaml
|
190 | # .eslintrc.yml
|
191 | settings:
|
192 | import/resolver: 'my-awesome-npm-module'
|
193 | ```
|
194 | ```js
|
195 | // .eslintrc.js
|
196 | module.exports = {
|
197 | settings: {
|
198 | 'import/resolver': {
|
199 | 'my-awesome-npm-module': { someConfig: value }
|
200 | }
|
201 | }
|
202 | }
|
203 | ```
|
204 |
|
205 | - with a filesystem path to resolver, defined in this example as a `computed property` name:
|
206 |
|
207 | ```js
|
208 | // .eslintrc.js
|
209 | module.exports = {
|
210 | settings: {
|
211 | 'import/resolver': {
|
212 | [path.resolve('../../../my-resolver')]: { someConfig: value }
|
213 | }
|
214 | }
|
215 | }
|
216 | ```
|
217 |
|
218 | Relative paths will be resolved relative to the source's nearest `package.json` or
|
219 | the process's current working directory if no `package.json` is found.
|
220 |
|
221 |
|
222 |
|
223 | If you are interesting in writing a resolver, see the [spec](./resolvers/README.md) for more details.
|
224 |
|
225 | [`resolve`]: https://www.npmjs.com/package/resolve
|
226 | [`externals`]: http://webpack.github.io/docs/library-and-externals.html
|
227 |
|
228 | [Node]: https://www.npmjs.com/package/eslint-import-resolver-node
|
229 | [webpack]: https://www.npmjs.com/package/eslint-import-resolver-webpack
|
230 |
|
231 | # Settings
|
232 |
|
233 | You may set the following settings in your `.eslintrc`:
|
234 |
|
235 | #### `import/extensions`
|
236 |
|
237 | A list of file extensions that will be parsed as modules and inspected for
|
238 | `export`s.
|
239 |
|
240 | This defaults to `['.js']`, unless you are using the `react` shared config,
|
241 | in which case it is specified as `['.js', '.jsx']`.
|
242 |
|
243 | ```js
|
244 | "settings": {
|
245 | "import/resolver": {
|
246 | "node": {
|
247 | "extensions": [
|
248 | ".js",
|
249 | ".jsx"
|
250 | ]
|
251 | }
|
252 | }
|
253 | }
|
254 | ```
|
255 |
|
256 | Note that this is different from (and likely a subset of) any `import/resolver`
|
257 | extensions settings, which may include `.json`, `.coffee`, etc. which will still
|
258 | factor into the `no-unresolved` rule.
|
259 |
|
260 | Also, the following `import/ignore` patterns will overrule this list.
|
261 |
|
262 | #### `import/ignore`
|
263 |
|
264 | A list of regex strings that, if matched by a path, will
|
265 | not report the matching module if no `export`s are found.
|
266 | In practice, this means rules other than [`no-unresolved`](./docs/rules/no-unresolved.md#ignore) will not report on any
|
267 | `import`s with (absolute filesystem) paths matching this pattern.
|
268 |
|
269 | `no-unresolved` has its own [`ignore`](./docs/rules/no-unresolved.md#ignore) setting.
|
270 |
|
271 | ```yaml
|
272 | settings:
|
273 | import/ignore:
|
274 | - \.coffee$ # fraught with parse errors
|
275 | - \.(scss|less|css)$ # can't parse unprocessed CSS modules, either
|
276 | ```
|
277 |
|
278 | #### `import/core-modules`
|
279 |
|
280 | An array of additional modules to consider as "core" modules--modules that should
|
281 | be considered resolved but have no path on the filesystem. Your resolver may
|
282 | already define some of these (for example, the Node resolver knows about `fs` and
|
283 | `path`), so you need not redefine those.
|
284 |
|
285 | For example, Electron exposes an `electron` module:
|
286 |
|
287 | ```js
|
288 | import 'electron' // without extra config, will be flagged as unresolved!
|
289 | ```
|
290 |
|
291 | that would otherwise be unresolved. To avoid this, you may provide `electron` as a
|
292 | core module:
|
293 |
|
294 | ```yaml
|
295 | # .eslintrc.yml
|
296 | settings:
|
297 | import/core-modules: [ electron ]
|
298 | ```
|
299 |
|
300 | In Electron's specific case, there is a shared config named `electron`
|
301 | that specifies this for you.
|
302 |
|
303 | Contribution of more such shared configs for other platforms are welcome!
|
304 |
|
305 | #### `import/external-module-folders`
|
306 |
|
307 | An 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".
|
308 |
|
309 | #### `import/parsers`
|
310 |
|
311 | A map from parsers to file extension arrays. If a file extension is matched, the
|
312 | dependency parser will require and use the map key as the parser instead of the
|
313 | configured ESLint parser. This is useful if you're inter-op-ing with TypeScript
|
314 | directly using webpack, for example:
|
315 |
|
316 | ```yaml
|
317 | # .eslintrc.yml
|
318 | settings:
|
319 | import/parsers:
|
320 | typescript-eslint-parser: [ .ts, .tsx ]
|
321 | ```
|
322 |
|
323 | In this case, [`typescript-eslint-parser`](https://github.com/eslint/typescript-eslint-parser) must be installed and require-able from
|
324 | the running `eslint` module's location (i.e., install it as a peer of ESLint).
|
325 |
|
326 | This is currently only tested with `typescript-eslint-parser` but should theoretically
|
327 | work with any moderately ESTree-compliant parser.
|
328 |
|
329 | It's difficult to say how well various plugin features will be supported, too,
|
330 | depending on how far down the rabbit hole goes. Submit an issue if you find strange
|
331 | behavior beyond here, but steel your heart against the likely outcome of closing
|
332 | with `wontfix`.
|
333 |
|
334 |
|
335 | #### `import/resolver`
|
336 |
|
337 | See [resolvers](#resolvers).
|
338 |
|
339 | #### `import/cache`
|
340 |
|
341 | Settings 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.
|
342 |
|
343 | For 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)
|
344 |
|
345 | For long-lasting processes, like [`eslint_d`] or [`eslint-loader`], however, it's important that there be some notion of staleness.
|
346 |
|
347 | If you never use [`eslint_d`] or [`eslint-loader`], you may set the cache lifetime to `Infinity` and everything should be fine:
|
348 |
|
349 | ```yaml
|
350 | # .eslintrc.yml
|
351 | settings:
|
352 | import/cache:
|
353 | lifetime: ∞ # or Infinity
|
354 | ```
|
355 |
|
356 | Otherwise, set some integer, and cache entries will be evicted after that many seconds have elapsed:
|
357 |
|
358 | ```yaml
|
359 | # .eslintrc.yml
|
360 | settings:
|
361 | import/cache:
|
362 | lifetime: 5 # 30 is the default
|
363 | ```
|
364 |
|
365 | [`eslint_d`]: https://www.npmjs.com/package/eslint_d
|
366 | [`eslint-loader`]: https://www.npmjs.com/package/eslint-loader
|
367 |
|
368 |
|
369 | ## SublimeLinter-eslint
|
370 |
|
371 | SublimeLinter-eslint introduced a change to support `.eslintignore` files
|
372 | which altered the way file paths are passed to ESLint when linting during editing.
|
373 | This change sends a relative path instead of the absolute path to the file (as ESLint
|
374 | normally provides), which can make it impossible for this plugin to resolve dependencies
|
375 | on the filesystem.
|
376 |
|
377 | This workaround should no longer be necessary with the release of ESLint 2.0, when
|
378 | `.eslintignore` will be updated to work more like a `.gitignore`, which should
|
379 | support proper ignoring of absolute paths via `--stdin-filename`.
|
380 |
|
381 | In the meantime, see [roadhump/SublimeLinter-eslint#58](https://github.com/roadhump/SublimeLinter-eslint/issues/58)
|
382 | for more details and discussion, but essentially, you may find you need to add the following
|
383 | `SublimeLinter` config to your Sublime project file:
|
384 |
|
385 | ```json
|
386 | {
|
387 | "folders":
|
388 | [
|
389 | {
|
390 | "path": "code"
|
391 | }
|
392 | ],
|
393 | "SublimeLinter":
|
394 | {
|
395 | "linters":
|
396 | {
|
397 | "eslint":
|
398 | {
|
399 | "chdir": "${project}/code"
|
400 | }
|
401 | }
|
402 | }
|
403 | }
|
404 | ```
|
405 |
|
406 | Note that `${project}/code` matches the `code` provided at `folders[0].path`.
|
407 |
|
408 | The purpose of the `chdir` setting, in this case, is to set the working directory
|
409 | from which ESLint is executed to be the same as the directory on which SublimeLinter-eslint
|
410 | bases the relative path it provides.
|
411 |
|
412 | See the SublimeLinter docs on [`chdir`](http://www.sublimelinter.com/en/latest/linter_settings.html#chdir)
|
413 | for more information, in case this does not work with your project.
|
414 |
|
415 | If you are not using `.eslintignore`, or don't have a Sublime project file, you can also
|
416 | do the following via a `.sublimelinterrc` file in some ancestor directory of your
|
417 | code:
|
418 |
|
419 | ```json
|
420 | {
|
421 | "linters": {
|
422 | "eslint": {
|
423 | "args": ["--stdin-filename", "@"]
|
424 | }
|
425 | }
|
426 | }
|
427 | ```
|
428 |
|
429 | I also found that I needed to set `rc_search_limit` to `null`, which removes the file
|
430 | hierarchy search limit when looking up the directory tree for `.sublimelinterrc`:
|
431 |
|
432 | In Package Settings / SublimeLinter / User Settings:
|
433 | ```json
|
434 | {
|
435 | "user": {
|
436 | "rc_search_limit": null
|
437 | }
|
438 | }
|
439 | ```
|
440 |
|
441 | I believe this defaults to `3`, so you may not need to alter it depending on your
|
442 | project folder max depth.
|