A man named Crockford thought there were only a few good parts to JavaScript.
And made JSLint to police how you write JavaScript
JSHint
ESLint
{
"env": {
"browser": true,
"node": true
},
"globals": {
"angular": true
},
"rules": {
"semi": 2,
"camelcase": 2,
"curly": 2,
"brace-style": [2, "1tbs"],
"quotes": [2, "single"],
"semi": [2, "always"],
"space-in-brackets": [2, "never"],
"space-infix-ops": 2,
"space-after-keywords": [2, "always"],
"dot-notation": 2,
"eqeqeq": [2, "smart"],
"no-use-before-define": 2,
"no-redeclare": 2,
"no-inner-declarations": "both",
"no-unused-vars": [2, {"vars": "all", "args": "after-used"}],
"no-floating-decimal": 2,
"no-spaced-func": 2,
"no-extra-parens": 1,
"no-underscore-dangle": 0,
"valid-jsdoc": 1,
"space-after-keywords": 2,
"max-len": [2, 120],
"no-use-before-define": [2, "nofunc"],
"no-warning-comments": 0,
"strict": 0,
"eol-last": 0
}
}/* eslint-disable */
var obj = { key: 'value', }; // I don't care about IE8
/* eslint-enable *//*eslint-disable no-alert */
alert('doing awful things');
/* eslint-enable no-alert *//* eslint no-comma-dangle:1 */
var obj = { key: 'value', } // Make this just a warning, not an error
your-project
├── .eslintrc
├── lib
│ └── source.js
└─┬ tests
├── .eslintrc
└── test.jsProject specific settings, and directory overrides.
As you type ...
On save ...
/**
* JavaScript Linting Task
*/
gulp.task('lint', function() {
return gulp.src('client/app/**/*.js')
.pipe(reload({stream: true, once: true}))
.pipe(eslint())
.pipe(eslint.format());
});module.exports = function(context) {
'use strict';
function report(node) {
context.report(node,
'You should use the $timeout service instead of the default window.setTimeout method',
{});
}
return {
'CallExpression': function(node) {
if (node.callee.type === 'MemberExpression' && node.callee.object.name ===
'window' && node.callee.property.name === 'setTimeout') {
report(node);
}
}
};
};eslint-plugin-angular - ng_timeout_service
[18:52:10] Finished 'jhint' after 1.3 s
[18:53:04] Finished 'lint' after 2.24 sWhat is the practical impact on...
JSHint
ESLint
It uses Espree to construct an AST,
THEN
evaluates your code
JSHint evaluates your code as it parses.
no-shadow
var a = 3;
function b() {
var a = 10;
}no-redeclare
var a = 3;
var a = 10;no-inner-declarations
// Good
var foo = 42;
// Bad
while (test) {
var bar;
}
can help prevent issues with hoisting
no-unreachable
function fn() {
x = 1;
return x;
x = 3; // this will never execute
}valid-jsdoc
/**
* Adds two numbers together.
* @param {int} num1 The first number.
* @param {int} num2 The second number.
* @returns {int} The sum of the two numbers.
*/
function sum(number1, num2) {
return number1 + num2;
}if your going to document, document it right.
block-scoped-var
function doSomething() {
if (true) {
var build = true;
}
console.log(build);
}... some of these are already available
... some of these are already available