UNPKG

7.84 kBMarkdownView Raw
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
31and plug it into Neutrino:
32
33```js
34// Using function middleware format
35const eslint = require('neutrino-middleware-eslint');
36
37// Usage shows default values
38neutrino.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
77make changes.
78
79### Rules
80
81The 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
89By default this middleware will show errors and warnings in the console during development, and will cause a failure when
90creating a build bundle.
91
92---
93
94If you want your preset or middleware to also extend from another **ESLint configuration or preset** that you have made
95a dependency, you must use `baseConfig.extends` rather than just `extends`. This is a limitation of ESLint, not this
96middleware.
97
98---
99
100This middleware only configures a target environment for `es6`, leaving other build middleware free to add their own
101target environments. If your middleware puts restrictions on which environments it is capable of running, please
102document 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
108You can also have Neutrino invoke ESLint for you if you wish to perform a one-time lint. This avoids needing to install
109ESLint manually, creating a `.eslintrc.js` file, or having to manage includes and ignores. As long as the ESLint
110middleware is loaded, you have access to a command to run ESLint from the command line.
111
112This middleware registers a command named `lint` which programmatically calls ESLint and prints the results to
113the console.
114
115```bash
116❯ neutrino lint
117```
118
119```bash
120❯ neutrino lint --fix
121```
122
123## eslintrc Config
124
125If 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
128file. Typically this is used for providing hints or fix solutions to the development environment, e.g. IDEs and text
129editors. Doing this requires [creating an instance of the Neutrino API](https://neutrino.js.org/api) and providing the
130middleware it uses. If you keep all this information in a `.neutrinorc.js`, this should be relatively straightforward. By
131providing all the middleware used to Neutrino, you can ensure all the linting options used across all middleware will be
132merged together for your development environment, without the need for copying, duplication, or loss of organization and
133separation.
134
135This middleware registers another command named `eslintrc` which returns an ESLint configuration object suitable for
136consumption 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
146const { Neutrino } = require('neutrino');
147const api = Neutrino();
148
149module.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
155const { Neutrino } = require('neutrino');
156const api = Neutrino();
157
158module.exports = api.call('eslintrc', [
159 ['neutrino-middleware-eslint', {
160 eslint: {
161 rules: { semi: 'off' }
162 }
163 }],
164 'neutrino-preset-react'
165]);
166```
167
168If you are able, only use a `.eslintrc.js` file for editor hints, and use the Neutrino `lint` command for one-off linting
169or fixes.
170
171Projects may face a problem when their editor or IDE lints all files and highlights errors that were normally excluded
172from source, i.e. Neutrino's `include` and `exclude` options. This is because the ESLint CLI does not have a way to
173specify included and excluded files from configuration. If you still wish to use ESLint's CLI for linting, consider
174setting [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
176include or exclude from linting.
177
178Unfortunately ESLint does not provide the possibility to configure ignored paths from Neutrino configuration and exclude them
179from linting. Projects authors should define this manually in their project root directory in a `.eslintignore` file. This
180is one of the main reasons to prefer using the `lint` CLI command with this middleware, as it avoids a lot of manual
181configuration and boilerplate.
182
183`.eslintignore` file:
184
185```
186/build
187/*.*
188```
189
190ESLint 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
192in the [ESLint user guide](http://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories).
193
194## Contributing
195
196This middleware is part of the [neutrino-dev](https://github.com/mozilla-neutrino/neutrino-dev) repository, a monorepo
197containing 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/