When you're working with a big team or on open source projects, there will come a point where the various styles of each person will begin to collide. Some people insist on semicolons, some despise them; some like 2 spaces for indentation, some insist on 4. At a certain point, your codebase begins to look rather inconsistent. Wouldn't it be nice if you could set rules for your codebase that everyone needed to follow so your format was totally uniform throughout your app? Enter ESLint.
ESLint allows you to set formatting rules and will throw errors/warnings if someone attempts to violate those rules. And if you're using an IDE like Atom, you can use a linter in realtime, which will let you know if the code you just wrote is out of format every time you save. If you're using Atom, you can install ESLint by using the Atom package manager, apm (below).
$ npm install linter-eslintThe most commonly used starting point for ESLint is the Airbnb style guide. Almost everyone uses this as the default options when building a React application.
$ npm install --save-dev eslint-config-airbnb eslint \ eslint-plugin-jsx-a11y eslint-plugin-import \ eslint-plugin-react babel-eslintOnce those are installed, you'll need a .eslintrc.js file, in which you specify options for ESLint (you will often see this as simply .eslintrc, but this is deprecated in favor of explicit file extensions like .js). These options include plugins, globals, and rule overrides.
$ touch .eslintrc.jsWithin this file, we will extend the existing airbnb style guide and add the plugins that enable us to use React and jsx. We also define our globals here. ESLint will throw an error if you try to use a function or variable that is not declared in the file, so global namespaces like document or window will not work. In order to get around this, you have to declare all your global variables within your configuration so ESLint knows what to expect.
From there, we define a few rules. These rules are necessarily a matter of opinion. We have included some pretty common overrides and we will go over each of them (and the reason for using them) below the codeblock.
module.exports = { extends: "airbnb", parser: "babel-eslint", plugins: ["react", "jsx-a11y", "import"], globals: { window: true, document: true, fetch: true, localStorage: true, }, rules: { "comma-dangle": [1, "never"], semi: [2, "never"], "arrow-body-style": 0, quotes: ["error", "double"], "react/jsx-closing-bracket-location": [1, "after-props"], "no-param-reassign": ["error", { props: false }], "react/jsx-filename-extension": [1, { extensions: [".js"] }], },}Every rule in ESLint has documentation--pretty darn good documentation, actually. If you want to learn more about a particular rule, you can always find it by using the search bar at the top of the ESLint docs.
Within the configuration of your rules, you can set a rule to 0, which means "off", 1, which means "warning", or 2, which means error and will throw a exit code of 1. Many of these rules will have additional configurations, which are specified in the documentation associated with each rule.
The first rule we're going to override is the comma-dangle (docs). Some people like comma dangles because it can save you from errors caused by a lack of a comma when adding or removing items from lists or objects. We're not going to use them because comma dangles don't work with JSON. But we're just going to turn this into a warning rather than an error, so our code will still pass even if we have a few comma dangles here and there.
The second rule is semi, which requires semicolons. We're setting the configuration to require that semicolons are not used or it will throw an error. This is because semicolons provide nothing and merely serve to clutter your JavaScript code.
The third rule is arrow-body-style, which controls how you use curly braces around your ES2015 arrow functions. We're just going to turn this off because there is too much variation in how these functions can be written in React.
The fourth rule is requiring that all quotes are double quotes. For some reason, this is always controversial, but we're using double quotes because you're more likely to include a single ' within a string than you are a double quote and because Elixir and JSON use double quotes for strings.
The fifth rule is specific to React, where we determine how the closing bracket should be formed. Some people like the extra > on a new line, others do not. We're going to add this extra character at the end of our element properties, like we've been doing so far in the React lessons. For more information on this rule, check the (docs).
The sixth rule, no-param-reassign, is somewhat obscure, but it makes it so that you cannot reassign variables that are passed in as parameters (docs). We're overriding part of the rule so that we still cannot reassign the variable, but we can assign its properties. For example, we cannot reassign foo if it's passed in from myFunction(foo), but we can assign foo.bar.
The last rule we're overriding is the filename extension. We're just going to name all of our files .js rather than specifying .jsx or .es6 or anything like that. There aren't any real advantages to including all the extra filetypes, so we're just going to make everything .js.
You'll also want to create a .eslintignore file so you can ignore files that you don't need to lint.
$ touch .eslintignoreWe're going to add all the generated files, as well as our webpack configuration, our server.js file, and all of our tests.
.nyc_outputdistcoveragenode_moduleswebpack.*server.js**/spec.jsThe reason we're ignoring these files is because they'll each require a litany of ESLint overrides. For example, you can include specific ESLint overrides in a comment on the top of any file. Just to get our server.js file to pass, we'll need to add the following 3 overrides.
/* eslint-disable import/no-extraneous-dependencies *//* eslint-disable no-console *//* eslint-disable no-unused-expressions */And to get each of our spec.js files to pass, we'd have to add even more, and we'de have to add them to every spec file. So unless you want to do that (which you're more than welcome to do--writing an alias would probably speed up the process), it's just easier to skip them.