1 | # Neutrino ESLint Middleware
|
2 |
|
3 | `neutrino-middleware-eslint` is Neutrino middleware for linting source code using ESLint and eslint-loader.
|
4 |
|
5 | [![NPM version][npm-image]][npm-url]
|
6 | [![NPM downloads][npm-downloads]][npm-url]
|
7 | [![Join the Neutrino community on Spectrum][spectrum-image]][spectrum-url]
|
8 |
|
9 | ## Requirements
|
10 |
|
11 | - Node.js v6.10+
|
12 | - Yarn or npm client
|
13 | - Neutrino v7
|
14 |
|
15 | ## Installation
|
16 |
|
17 | `neutrino-middleware-eslint` can be installed via the Yarn or npm clients.
|
18 |
|
19 | #### Yarn
|
20 |
|
21 | ```bash
|
22 | ❯ yarn add neutrino-middleware-eslint
|
23 | ```
|
24 |
|
25 | #### npm
|
26 |
|
27 | ```bash
|
28 | ❯ npm install --save neutrino-middleware-eslint
|
29 | ```
|
30 |
|
31 | ## Usage
|
32 |
|
33 | `neutrino-middleware-eslint` can be consumed from the Neutrino API, middleware, or presets. Require this package
|
34 | and plug it into Neutrino:
|
35 |
|
36 | ```js
|
37 | // Using function middleware format
|
38 | const eslint = require('neutrino-middleware-eslint');
|
39 |
|
40 | // Usage shows default values
|
41 | neutrino.use(eslint, {
|
42 | test: /\.(js|jsx)$/,
|
43 | include: [], /* Should specify either include or exclude */
|
44 | exclude: [], /* Should specify either include or exclude */
|
45 | eslint: {
|
46 | cwd: neutrino.options.root,
|
47 | useEslintrc: false,
|
48 | root: true,
|
49 | extensions: ['js', 'jsx'],
|
50 | plugins: ['babel'],
|
51 | baseConfig: {},
|
52 | envs: ['es6'],
|
53 | parser: 'babel-eslint',
|
54 | parserOptions: {
|
55 | ecmaVersion: 2017,
|
56 | sourceType: 'module',
|
57 | ecmaFeatures: {
|
58 | objectLiteralDuplicateProperties: false,
|
59 | generators: true,
|
60 | impliedStrict: true
|
61 | }
|
62 | },
|
63 | settings: {},
|
64 | globals: ['process'],
|
65 | rules: {}
|
66 | }
|
67 | });
|
68 | ```
|
69 |
|
70 | - `test`: Test which files should be linted.
|
71 | - `include`: An array of paths to include in linting. Maps to Webpack's [`Rule.include`](https://webpack.js.org/configuration/module/#rule-include)
|
72 | - `exclude`: An array of paths to exclude from linting. Maps to Webpack's [`Rule.exclude`](https://webpack.js.org/configuration/module/#rule-exclude)
|
73 | - `eslint`: An ESLint CLIEngine configuration object for configuring ESLint. Use this to configure rules, plugins, and other [ESLint options](http://eslint.org/docs/user-guide/configuring).
|
74 |
|
75 | ## Customization
|
76 |
|
77 | `neutrino-middleware-eslint` creates some conventions to make overriding the configuration easier once you are ready to
|
78 | make changes.
|
79 |
|
80 | ### Rules
|
81 |
|
82 | The following is a list of rules and their identifiers which can be overridden:
|
83 |
|
84 | | Name | Description | Environments and Commands |
|
85 | | --- | --- | --- |
|
86 | | `lint` | By default, lints JS and JSX files from included directories using ESLint. Contains a single loader named `eslint`. | all |
|
87 |
|
88 | ## Information
|
89 |
|
90 | By default this middleware will show errors and warnings in the console during development, and will cause a failure when
|
91 | creating a build bundle.
|
92 |
|
93 | ---
|
94 |
|
95 | If you want your preset or middleware to also extend from another **ESLint configuration or preset** that you have made
|
96 | a dependency, you must use `baseConfig.extends` rather than just `extends`. This is a limitation of ESLint, not this
|
97 | middleware.
|
98 |
|
99 | ---
|
100 |
|
101 | This middleware only configures a target environment for `es6`, leaving other build middleware free to add their own
|
102 | target environments. If your middleware puts restrictions on which environments it is capable of running, please
|
103 | document that clearly in your middleware.
|
104 |
|
105 | ## eslint CLI
|
106 |
|
107 | _This is the recommended way to perform a one-off lint in a Neutrino project._
|
108 |
|
109 | You can also have Neutrino invoke ESLint for you if you wish to perform a one-time lint. This avoids needing to install
|
110 | ESLint manually, creating a `.eslintrc.js` file, or having to manage includes and ignores. As long as the ESLint
|
111 | middleware is loaded, you have access to a command to run ESLint from the command line.
|
112 |
|
113 | This middleware registers a command named `lint` which programmatically calls ESLint and prints the results to
|
114 | the console.
|
115 |
|
116 | ```bash
|
117 | ❯ neutrino lint
|
118 | ```
|
119 |
|
120 | ```bash
|
121 | ❯ neutrino lint --fix
|
122 | ```
|
123 |
|
124 | ## eslintrc Config
|
125 |
|
126 | If you cannot or do not wish to use Neutrino to execute one-off linting, you can still use ESLint manually.
|
127 |
|
128 | `neutrino-middleware-eslint` also provides a method for getting the ESLint configuration suitable for use in an eslintrc
|
129 | file. Typically this is used for providing hints or fix solutions to the development environment, e.g. IDEs and text
|
130 | editors. Doing this requires [creating an instance of the Neutrino API](https://neutrino.js.org/api) and providing the
|
131 | middleware it uses. If you keep all this information in a `.neutrinorc.js`, this should be relatively straightforward. By
|
132 | providing all the middleware used to Neutrino, you can ensure all the linting options used across all middleware will be
|
133 | merged together for your development environment, without the need for copying, duplication, or loss of organization and
|
134 | separation.
|
135 |
|
136 | This middleware registers another command named `eslintrc` which returns an ESLint configuration object suitable for
|
137 | consumption by the ESLint CLI. Use the Neutrino API's `call` method to invoke this command:
|
138 |
|
139 | _Example: Create a .eslintrc.js file in the root of the project, using `.neutrinorc.js` middleware._
|
140 |
|
141 | ```js
|
142 | // .eslintrc.js
|
143 |
|
144 | // If you do not specify any middleware to call(),
|
145 | // it will use the local .neutrinorc.js file
|
146 |
|
147 | const { Neutrino } = require('neutrino');
|
148 | const api = Neutrino();
|
149 |
|
150 | module.exports = api.call('eslintrc');
|
151 | ```
|
152 |
|
153 | _Example: Create a .eslintrc.js file in the root of the project, using specified middleware._
|
154 |
|
155 | ```js
|
156 | // .eslintrc.js
|
157 | const { Neutrino } = require('neutrino');
|
158 | const api = Neutrino();
|
159 |
|
160 | module.exports = api.call('eslintrc', [
|
161 | ['neutrino-middleware-eslint', {
|
162 | eslint: {
|
163 | rules: { semi: 'off' }
|
164 | }
|
165 | }],
|
166 | 'neutrino-preset-react'
|
167 | ]);
|
168 | ```
|
169 |
|
170 | If you are able, only use a `.eslintrc.js` file for editor hints, and use the Neutrino `lint` command for one-off linting
|
171 | or fixes.
|
172 |
|
173 | Projects may face a problem when their editor or IDE lints all files and highlights errors that were normally excluded
|
174 | from source, i.e. Neutrino's `include` and `exclude` options. This is because the ESLint CLI does not have a way to
|
175 | specify included and excluded files from configuration. If you still wish to use ESLint's CLI for linting, consider
|
176 | setting [CLI flags](http://eslint.org/docs/user-guide/command-line-interface#options) or using an
|
177 | [eslintignore](http://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories) to choose which files to
|
178 | include or exclude from linting.
|
179 |
|
180 | Unfortunately ESLint does not provide the possibility to configure ignored paths from Neutrino configuration and exclude them
|
181 | from linting. Projects authors should define this manually in their project root directory in a `.eslintignore` file. This
|
182 | is one of the main reasons to prefer using the `lint` CLI command with this middleware, as it avoids a lot of manual
|
183 | configuration and boilerplate.
|
184 |
|
185 | `.eslintignore` file:
|
186 |
|
187 | ```
|
188 | /build
|
189 | /*.*
|
190 | ```
|
191 |
|
192 | ESLint will exclude built files and any files in the root directory (e.g. custom Neutrino configuration) but `src` and
|
193 | `test` folders will be still checked. `node_modules` are ignored by default in ESLint. More information can be found
|
194 | in the [ESLint user guide](http://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories).
|
195 |
|
196 | ## Contributing
|
197 |
|
198 | This middleware is part of the [neutrino-dev](https://github.com/mozilla-neutrino/neutrino-dev) repository, a monorepo
|
199 | containing all resources for developing Neutrino and its core presets and middleware. Follow the
|
200 | [contributing guide](https://neutrino.js.org/contributing) for details.
|
201 |
|
202 | [npm-image]: https://img.shields.io/npm/v/neutrino-middleware-eslint.svg
|
203 | [npm-downloads]: https://img.shields.io/npm/dt/neutrino-middleware-eslint.svg
|
204 | [npm-url]: https://npmjs.org/package/neutrino-middleware-eslint
|
205 | [spectrum-image]: https://withspectrum.github.io/badge/badge.svg
|
206 | [spectrum-url]: https://spectrum.chat/neutrino
|