Software that checks and cleans your code
// one-equals-two.js
let one = 1;
let two = 2;
if (one = two) {
console.log('This should never print!');
} else {
console.log('Phew, the world makes sense');
// You know you're writing javascript, right?
}
[.../LintingMFAO-examples]$ node one-equals-two.js
This should never print!
// print-one.js
// Example from ESLint.org
console.log('printOne:', printOne());
function printOne() {
try {
return 1;
} catch (err) {
return 2;
} finally {
return 3;
}
}
[.../LintingMFAO-examples]$ node printOne.js
printOne: 3
# Local Install (Recommended)
npm init
npm install eslint --save-dev
# -or-
yarn init
yarn add eslint --dev
# Global Install
npm install -g eslint
# -or-
yarn global add eslint
node_modules/.bin/eslint --init
# -or-
eslint --init
[.../LintingMFAO-examples]$ node_modules/.bin/eslint --init
? How would you like to configure ESLint?
❯ Answer questions about your style
Use a popular style guide
Inspect your JavaScript file(s)
? How would you like to configure ESLint? Answer questions about your style
? Are you using ECMAScript 6 features? Yes
? Are you using ES6 modules? No
? Where will your code run? Browser
? Do you use CommonJS? No
? Do you use JSX? No
? What style of indentation do you use? Spaces
? What quotes do you use for strings? Single
? What line endings do you use? Unix
? Do you require semicolons? Yes
? What format do you want your config file to be in? (Use arrow keys)
❯ JavaScript
YAML
JSON
module.exports = {
"env": {
"browser": true,
"es6": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
};
// welcome-to-eslint.js
var foo = bar;
[.../LintingMFAO-examples]$ node_modules/.bin/eslint welcome-to-eslint.js
.../LintingMFAO-examples/welcome-to-eslint.js
1:5 error 'foo' is assigned a value but never used no-unused-vars
1:11 error 'bar' is not defined no-undef
✖ 2 problems (2 errors, 0 warnings)
module.exports = {
// ...
"rules": {
"indent": [ // Rule Name
"error", // Warning Type: off (0), warn (1), error (2)
4 // Options
],
"new-cap": ["error", { // Object Options
"capIsNew": false
}],
"quotes": ["error",
"single ", { // Multiple Option Params
"allowTemplateLiterals": true
}],
}
};
Generally, you'll start with a plug-in and customize
module.exports = {
// env, etc
// Angular
extends: 'plugin:angular/johnpapa',
extends: 'plugin:angular/bestpractices',
// Ember
extends: [
'eslint:recommended',
'plugin:ember-suave/recommended'
],
// Node
extends: 'plugin:node/recommended',
// React
extends: 'plugin:react/recommended',
// Nightmare-mode 😰
extends: 'nightmare-mode',
// Many More ...
rules: {
// project-specific settings
// plugin-overrides!
}
};
[.../LintingMFAO-examples]$ node_modules/.bin/eslint index.js *[master]
.../LintingMFAO-examples/index.js
1:5 error 'foo' is assigned a value but never used no-unused-vars
1:11 error 'bar' is not defined no-undef
✖ 2 problems (2 errors, 0 warnings)
Different based on your environment and framework
All approaches can benefit from pre-push hook
# Local Install (Recommended)
npm init
npm install pre-push --save-dev
# -or-
yarn init
yarn add pre-push --dev
{
// ...
"scripts": {
"pretest": "npm run lint",
"test": "echo \"Unit tests go here\"",
"lint": "eslint *.js",
"lint-pretty": "eslint --format table *.js"
},
"pre-push": ["test"],
// ...
}
root: true
Hierarchy makes it simple to support
[.../LintingMFAO-examples]$ node_modules/.bin/eslint welcome-to-eslint.js
.../LintingMFAO-examples/welcome-to-eslint.js
5:5 error 'foo' is assigned a value but never used no-unused-vars
5:11 error 'bar' is not defined no-undef
7:10 error 'soMuchLint' is defined but never used no-unused-vars
8:3 error Expected indentation of 4 spaces but found 2 indent
8:16 error Infix operators must be spaced space-infix-ops
8:17 error Strings must use singlequote quotes
8:24 error Strings must use singlequote quotes
8:32 error Missing semicolon semi
✖ 8 problems (8 errors, 0 warnings)
[.../LintingMFAO-examples]$ tail -3 welcome-to-eslint.js
function soMuchLint(choice) {
return choice?"tabs":"spaces"
}
[.../LintingMFAO-examples]$ node_modules/.bin/eslint --fix welcome-to-eslint.js
.../LintingMFAO-examples/welcome-to-eslint.js
5:5 error 'foo' is assigned a value but never used no-unused-vars
5:11 error 'bar' is not defined no-undef
7:10 error 'soMuchLint' is defined but never used no-unused-vars
✖ 3 problems (3 errors, 0 warnings)
[.../LintingMFAO-examples]$ tail -3 welcome-to-eslint.js
function soMuchLint(choice) {
return choice ? 'tabs' : 'spaces';
}
[../LintingMFAO-examples]$ node_modules/.bin/eslint-nibble *.js
no-constant-condition: 1|
no-cond-assign: 1|
no-unsafe-finally: 1|
no-unused-vars: 1|
no-undef: 1|
3 files checked. 0 passed. 3 failed. 0 warnings. 5 errors.
? Type in the rule you want to focus on no-undef
✘ http://eslint.org/docs/rules/no-undef 'bar' is not defined
.../LintingMFAO-examples/welcome-to-eslint.js:4:11
var foo = bar;
^
✘ 1 problem (1 error, 0 warnings)
Errors:
1 http://eslint.org/docs/rules/no-undef
{
"name": "lmfao-examples",
// ...
"main": "index.js",
"scripts": {
"pretest": "npm run lint",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint *.js",
"lint-pretty": "eslint --format table *.js"
},
// ...
}
[.../LintingMFAO-examples]$ npm run lint-pretty *[master]
> lmfao-examples@0.0.1 lint-pretty .../LintingMFAO-examples
> eslint --format table *.js
.../LintingMFAO-examples/index.js
║ Line │ Column │ Type │ Message │ Rule ID ║
╟──────────┼──────────┼──────────┼────────────────────────────────────────────────────────┼──────────────────────╢
║ 1 │ 5 │ error │ 'foo' is assigned a value but never used. │ no-unused-vars ║
║ 1 │ 11 │ error │ 'bar' is not defined. │ no-undef ║
╔════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ 2 Errors ║
╟────────────────────────────────────────────────────────────────────────────────────────────────────────────────╢
║ 0 Warnings ║
╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝