If you made it to the end of the previous article with everything working, you're in great shape. You can safely call it a day and have a working set-up for many .js files to come. If you want to tailor your environment a bit more, this addendum will walk you through common additional settings. You can enable some or all of these to personalize your environment and/or enforce stricter style adherence than the simple config detailed previously. If you're interested, you can view my complete ESLint config file here.
If you want to explore the settings on your own, the following links are good places to start. For the packages, don't be afraid to dig around in the source code! It's a great way to understand more about how things function and interconnect under-the-hood.
When we updated our VS Code settings in Step 2, we enabled ESLint for all file types, not just Javascript. This shouldn't cause any issues, as ESLint won't parse non-Javascript files. However, if you decide to set up other formatters for non-Javascript files, you'll want change your VS Code settings to target the ESLint extension more narrowly. You can do this with language specific editor settings:
"[javascript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
},
As discussed, Prettier doesn't let us do a whole lot of configuration. We only needed to change two options to match AirBnB, but we can customize a few more if we want. My Prettier config file specifies all the options I'm opinionated about, even though I'm just re-stating the default behavior for most of them.
One set of rules that breaks during Prettier / ESLint integration is string templating. We want to avoid template literals unless necessary, but always prefer template literals over string concatenation. To re-enable this behavior, we need to add an explicit rule in our .eslintrc.json
file:
{
//env, preset, ...
"rules": {
//... other rules
"quotes": [
"error",
"single",
{ "avoidEscape": true, "allowTemplateLiterals": false }
],
//... more rules
}
}
Environments in ESLint are just sets of global variables. By specifying an environment, we tell ESLint to not mark these variables as errors when we use them in a file without having provided our own explicit definition. Globals can include keywords like Set, for ES6 code, or the window object, for browser-based code. You can specify as many different or overlapping environments as you want, but you shouldn't start enabling everything without good reason. If we're working exclusively on browser-based code, leaving Node out of our environment list will ensure we don't sneak in any Node-specific globals by mistake. These are a few of the most common environments you might encounter:
browser
: Covers all the browser-specific globals, like document or window, available to front-end code.node
: Covers the globals available to back-end code within Node's runtime environment.es2020
: This lets us use all the Javascript language features up through the most recent ECMAScript spec, including features from earlier specs like ES6. If your code will be executed in a runtime environment that doesn't support these features yet (and you're not using a transpiler), you may want to specifyes6
instead.jquery
: If you use jQuery, this will save you from$
-operator warnings.jest
: Eliminates errors for built-in Jest syntax likedescribe()
andtest()
.
ESLint supports three levels of warning for most rules. You can set rules to a specific warning level to group your errors in whatever way works for you:
0
or"off"
: the rule will not be flagged whatsoever in your code.1
or"warn"
: you'll see a yellow or orange squiggly, and the rule will be counted in the ⚠ status bar symbol within VS Code.2
or"error"
: normal error, red squiggly, counted with ⓧ in VS Code status bar.
If you've gotten this far, you may have noticed some rules from the AirBnB style guide aren't showing as warnings or errors. If you dig into the package source code, you'll see that not every rule specified in the style guide has actually been enabled! We can re-enable any of these omissions by adding them to the "rules"
object in .eslintrc.json
:
"rules" {
// Not all missing rules are listed here
"default-case-last": "error",
"default-param-last": ["error"],
"no-useless-call": "error",
"prefer-exponentiation-operator": "error",
"prefer-regex-literals": "error",
//...
}
Sometimes you'll clone a project that already contains ESLint configuration file(s) and packages. If you have multiple configuration files in a nested directory structure, ESLint will automatically try to combine all those files until it hits your home directory. To prevent this behavior, add "root": true
to the outermost .eslintrc*
file you want included in the chain. Note that ESLint applies a hierarchy of filetypes when determining how to apply multiple config files within the same folder:
- .eslintrc.js
- .eslintrc.yaml
- .eslintrc.yml
- .eslintrc.json
- .eslintrc
- package.json
See the ESLint docs for more info. Additionally, be aware that Prettier uses a different precedence for it's config file extensions:
- "prettier" key in
package.json
- .prettierrc file (containing either JSON or YAML)
- .prettierrc.json, .prettierrc.yml, or .prettierrc.yaml
- .prettierrc.js or prettier.config.js using
module.exports
- .prettierrc.toml
Guess what - you've already set up coverage for React. The eslint-config-airbnb
package we installed brought along eslint-plugin-react as a dependency, and the AirBnB ruleset we extended includes configuration for React. For maximum utility, we should still tweak a few settings:
- Add
"prettier/react"
as the last item in the"extends"
array."extends": ["airbnb", "prettier", "prettier/react"],
- Update
"parserOptions"
to support JSX syntax:"parserOptions": { "ecmaFeatures": { "jsx": true } }
- Add any additional React-specific rules you may want:
// just a few of the possible rules "react/prefer-stateless-function": ["warn"], "react/jsx-key": "warn", "react/no-direct-mutation-state": "error", "react/no-adjacent-inline-elements": "error"
Note: We don't need to add "react"
as a plug-in, since eslint-config-airbnb
already took care of that for us.
If ESLint is enabled for all filetypes in VS Code, you can skip this step. If you added a Javascript selector to your ESLint settings, as described above, you'll want to target .jsx files as well:
"[javascriptreact]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
},
And that's it! You should be all set to lint and auto-fix all your JS & JSX files within VS Code.
Prettier only fixes a narrow selection of style errors. It cannot fix most of the structural problems that ESLint catches. ESLint will still flag those additional errors, but you will need to manually review the warning-squigglies to address anything Prettier and ESLint couldn't fix automatically.
npm is a package manager. It lets you use bits of code that other people have written, known as packages or modules, on your local machine (ie, your laptop / desktop / hotwired Motorola Razr / etc). These packages can either be installed globally, meaning they are accessible everywhere on your computer, or locally, meaning they are only available in a certain folder (or directory) and it's subfolders (or sub-directories). The folder that contains all of your project files & subfolders, including your npm modules, is sometimes called your project's root directory. Additionally, npm uses a package.json file to store and manage information about your project and its associated packages. This is a file written in JSON that tracks lots of information about your project, including info on the various packages you've installed. We're not working directly with the package.json
file in guide, but it's helpful to know what it is.
Many npm packages also have dependencies. These are other packages that the main package requires in order to run correctly. Often these dependencies will be installed automatically with whatever package you wanted, but sometimes they will need to be installed manually. A normal dependency is one that your project relies on at runtime, like jQuery for a live webpage. A dev-dependency is one that is only required during the development process and is not necessary for your finished application to function. ESLint & Prettier are dev-dependencies. Less common, a peer dependency is one required for another package to run, but which it expects you to already have installed. This is usually done to avoid installing multiple versions of the same package when using plugins.
Dotfiles are hidden files used to set the configuration for many different types of programs, including ESLint, Prettier, VS Code, Bash, and Zsh. They're called dotfiles because the filenames start with a leading .
that renders them hidden from normal file viewers, including the ls
command. To view hidden files within the terminal, you can use:
$ ls -a -l
where -a
shows hidden files and -l
displays the results as a list.
Your ESLint squiggles should appear immediately on any files within your install directory and its sub-directories. However, if error highlighting or fixOnSave doesn't appear to be working, try the steps below before any additional troubleshooting:
- Create a new file in your installation directory (or its sub-directories).
- Save that file once, preferably with at least one line of content.
- Edit the file in some way. You can paste in the test case provided below if you'd like. You should see errors being highlighted by ESLint.
- Save the file again. At this point, many of the style errors (including line-length) should auto-fix.
Feel free to use this code example to check for a few different types of errors, but remember to edit it at least once if included in the initial save!
let testFunc = function funcName (longArgNumberOne, longArgNumberTwo, longArgNumberFour, longArgNumberFive) {
return "should be single quote and needs semicolon"
}