Syntax validation to eliminate suspicious code and violations of code style.
Undefined variables
function bad() {
return foo;
}
Lack of brackets
if (someVal)
someVal += otherVal;
someVal++;
return someVal;
Invalid
function BAD_func (b , c){
var a = 3333
return a;
}
Valid
function goodFunc(b, c) {
var a = 3333;
return a;
}
jshint file.js
.jshintrc
, JSON with comments{
"bitwise" : true,
"camelcase" : true,
"quotmark" : "single",
"node": true,
"globals": {
"foo": true,
"bar": false // readonly
}
}
/*globals foo: false, baz: false */
/*jshint camelcase: false */
var bar = 444;
/* jshint ignore:start */
foo = 777;
var FooBar = 888;
/* jshint ignore:end */
var foo = function() {
return a;
};
jshint example.js
example1.js: line 2, col 5, Missing "use strict" statement.
example1.js: line 2, col 12, 'a' is not defined.
example1.js: line 1, col 8, 'foo' is defined but never used.
3 errors
undef
Require all non-global variables to be declared (prevents global leaks)Not valid
function bad() {
return foo;
}
Valid
function good() {
var foo;
return foo;
}
unused
Require all defined variables be usedNot valid
function bad(baz) {
var foo = 5;
return foo;
}
Valid
function good() {
var foo = 5;
return foo;
}
asi
Require semicolonsNot valid
function bad(baz) {
var foo = 5
return foo
}
Valid
function good() {
var foo = 5;
return foo;
}
curly
Require {} for every new block or scopeNot valid
function bad(baz) {
if (baz > 5)
return baz;
return 10;
}
Valid
function good() {
if (baz > 5) {
return baz;
}
return 10;
}
newcap
Require capitalization of all constructor functionscamelcase
Identifiers must be in camelCaseNot valid
var my_bar = new bar();
Valid
var myBar = new Bar();
indent
Spaces to indentquotmark
Quotation markNot valid
function bad(){
return 'foo' + "bar";
}
Valid
function bad(){
return 'foo' + 'bar';
}
.eslintrc
(JSON or YAML)/*eslint ... */
no-dupe-keys
Disallow duplicate keysNot valid
var foo = {
bar: "baz",
bar: "qux"
};
Valid
var foo = {
bar: "baz"
};
block-scoped-vars
Threat var as block scopedNot valid
function doSomething(a) {
if (a) {
var build = true;
}
console.log(build);
}
Valid
function doSomething(a) {
var build;
if (a) {
build = true;
}
console.log(build);
}
consistent-this
Require consistent this
"consistent-this": [2, "self"]
Not valid
var that = this;
Valid
var self = this;
func-style
Enforce function style"func-style": [2, "expression"]
Not valid
function foo(){};
Valid
var foo = function(){};
radix
require radix for parseInt
Not valid
var a = parseInt("055");
Valid
var a = parseInt("055", 10);
semi
can enforce absent of semisemi: [2, "never"]
disallowImplicitTypeConversion
Not valid
var a = !!someVar;
var b = +someInt;
var c = '' + someStr;
Valid
var a = Boolean(someVar);
var b = Integer(someInt);
var c = String(someStr);
requireCurlyBraces
Requires curly braces after statements{
"requireCurlyBraces": [
"if",
"else",
"for",
"while",
"do",
"try",
"catch"
]
}
requireSpaceAfterKeywords
, disallowSpaceAfterKeywords
{
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"return",
"try",
"catch",
"case"
],
"disallowSpaceAfterKeywords": [
"default"
]
}
validateLineBreaks
validateQuoteMarks
validateIndentation
disallowMixedSpacesAndTabs
disallowTrailingWhitespace
.editorconfig
for project and plugins for almost all IDEsroot = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.{md,jade}]
trim_trailing_whitespace = false
{
"indent_size": 4,
"indent_char": " ",
"indent_level": 0,
"preserve_newlines": true,
"max_preserve_newlines": 2,
"jslint_happy": false,
"brace_style": "collapse",
"keep_array_indentation": false,
"keep_function_indentation": false,
"space_before_conditional": true,
"space_in_paren": false,
}
Before
var a = 33;
var obj = { a : 33,
b: "bzzz"
};
if(a>3){console.log( a);}
After
var a = 33;
var obj = {
a: 33,
b: "bzzz"
};
if (a > 3) {
console.log(a);
}
{
"lineBreak": {
"before": {
"AssignmentExpression": 1,
"BlockStatement": 0
// ....
},
"after": {
"AssignmentExpression": 1,
"BlockStatement": 1
// ....
}
},
"whitespace": {
"before": {
"ArrayExpressionOpening": 0,
"ArrayExpressionClosing": 0,
// ...
},
"after": {
"ArrayExpressionOpening": 2,
"ArrayExpressionClosing": 0,
// ...
}
}
}
Before
Q
.when(doSomething())
.then(function() {
return 333;
});
After
Q
.when(doSomething())
.then(function() {
return 333;
});
$ npm install -g generator-linters