项目原始demo,不改动
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
このリポジトリはアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュや、課題・プルリクエストのオープンはできません。

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