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