JSCS — JavaScript Code Style.
jscs
is a code style checker. jscs
can check cases, which are not implemeted in jshint,
but it does not duplicate jshint
functionality, so you should use jscs
and jshint
together.
- Grunt task: https://github.com/gustavohenke/grunt-jscs-checker
- Gulp task: https://github.com/sindresorhus/gulp-jscs
jscs
can be installed using npm
:
npm install jscs
To run jscs
, you can use the following command from the project root:
./node_modules/.bin/jscs path[ path[...]]
jscs
is configured using .jscs.json file, located in the project root.
Requires curly braces after statements.
Type: Array
Values: Arrow of quoted keywords
"requireCurlyBraces": [
"if",
"else",
"for",
"while",
"do",
"try",
"catch",
"case",
"default"
]
if (x) {
x++;
}
if (x) x++;
Requires space after keyword.
Type: Array
Values: Array of quoted keywords
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"return",
"try",
"catch"
]
return true;
if(x) {
x++;
}
Disallows space after keyword.
Type: Array
Values: Array of quoted keywords
"disallowSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"try",
"catch"
]
if(x > y) {
y++;
}
Requires parentheses around immediately invoked function expressions.
Type: Boolean
Values: true
"requireParenthesesAroundIIFE": true
var a = (function(){ return 1; })();
var b = (function(){ return 2; }());
var c = (function(){ return 3; }).call(this, arg1);
var d = (function(){ return 3; }.call(this, arg1));
var e = (function(){ return d; }).apply(this, args);
var f = (function(){ return d; }.apply(this, args));
var a = function(){ return 1; }();
var c = function(){ return 3; }.call(this, arg1);
var d = function(){ return d; }.apply(this, args);
Requires space before ()
or {}
in function declarations.
Type: Object
Values: beforeOpeningRoundBrace
and beforeOpeningCurlyBrace
as child properties. Child properties must be set to true
.
"requireSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
function () {}
function a () {}
function() {}
function (){}
Disallows space before ()
or {}
in function declarations.
Type: Object
Values: "beforeOpeningRoundBrace"
and "beforeOpeningCurlyBrace"
as child properties. Child properties must be set to true
.
"disallowSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
function(){}
function a(){}
function () {}
function a (){}
Disallows multiple var
declaration (except for-loop).
Type: Boolean
Values: true
"disallowMultipleVarDecl": true
var x = 1;
var y = 2;
for (var i = 0, j = arr.length; i < j; i++) {}
var x = 1,
y = 2;
Requires multiple var
declaration.
Type: Boolean
Values: true
"requireMultipleVarDecl": true
var x = 1,
y = 2;
var x = 1;
var y = 2;
Disallows empty blocks (except for catch blocks).
Type: Boolean
Values: true
"disallowEmptyBlocks": true
if ( a == b ) { c = d; }
try { a = b; } catch( e ){}
if ( a == b ) { } else { c = d; }
Disallows space after opening object curly brace and before closing.
Type: Boolean
Values: true
"disallowSpacesInsideObjectBrackets": true
var x = {a: 1};
var x = { a: 1 };
Disallows space after opening array square bracket and before closing.
Type: Boolean
Values: true
"disallowSpacesInsideArrayBrackets": true
var x = [1];
var x = [ 1 ];
Disallows space after opening round bracket and before closing.
Type: Boolean
Values: true
"disallowSpacesInsideParentheses": true
var x = (1 + 2) * 3;
var x = ( 1 + 2 ) * 3;
Requires space after opening object curly brace and before closing.
Type: String
Values: "all"
for strict mode, "allButNested"
ignores closing brackets in a row.
"requireSpacesInsideObjectBrackets": "all"
var x = { a: { b: 1 } };
var x = { a: { b: 1 }};
var x = {a: 1};
Requires space after opening array square bracket and before closing.
Type: String
Values: "all" for strict mode, "allButNested" ignores closing brackets in a row.
"requireSpacesInsideArrayBrackets": "all"
var x = [ 1 ];
var x = [[ 1 ], [ 2 ]];
var x = [1];
Disallows quoted keys in object if possible.
Type: String
or Boolean
Values:
true
for strict mode"allButReserved"
allows ES3+ reserved words to remain quoted which is helpfull when using this option with JSHint'ses3
flag.
"disallowQuotedKeysInObjects": true
var x = { a: { default: 1 } };
var x = {a: 1, 'default': 2};
var x = {'a': 1};
Disallows identifiers that start or end in _
, except for some popular exceptions:
_
(underscore.js)__filename
(node.js global)__dirname
(node.js global)
Type: Boolean
Values: true
"disallowDanglingUnderscores": true
var x = 1;
var y = _.extend;
var z = __dirname;
var w = __filename;
var x_y = 1;
var _x = 1;
var x_ = 1;
var x_y_ = 1;
Disallows space after object keys.
Type: Boolean
Values: true
"disallowSpaceAfterObjectKeys": true
var x = {a: 1};
var x = {a : 1};
Requires space after object keys.
Type: Boolean
Values: true
"requireSpaceAfterObjectKeys": true
var x = {a : 1};
var x = {a: 1};
Disallows commas as last token on a line in lists.
Type: Boolean
Values: true
"disallowCommaBeforeLineBreak": true
var x = {
one: 1
, two: 2
};
var y = { three: 3, four: 4};
var x = {
one: 1,
two: 2
};
Requires commas as last token on a line in lists.
Type: Boolean
Values: true
"requireCommaBeforeLineBreak": true
var x = {
one: 1,
two: 2
};
var y = { three: 3, four: 4};
var x = {
one: 1
, two: 2
};
Requires proper alignment in object literals.
Type: String
Values:
- "all"
for strict mode,
- "skipWithFunction"
ignores objects if one of the property values is a function expression,
- "skipWithLineBreak"
ignores objects if there are line breaks between properties
"requireAlignedObjectValues": "all"
var x = {
a : 1,
bcd : 2,
ef : 'str'
};
var x = {
a : 1,
bcd : 2,
ef : 'str'
};
Requires operators to appear before line breaks and not after.
Type: Array
Values: Array of quoted operators
"requireOperatorBeforeLineBreak": [
"?",
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
]
x = y ? 1 : 2;
x = y ?
1 : 2;
x = y
? 1 : 2;
Disallows sticking operators to the left.
Type: Array
Values: Array of quoted operators
"disallowLeftStickedOperators": [
"?",
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
]
x = y ? 1 : 2;
x = y? 1 : 2;
Requires sticking operators to the right.
Type: Array
Values: Array of quoted operators
"requireRightStickedOperators": ["!"]
x = !y;
x = ! y;
Disallows sticking operators to the right.
Type: Array
Values: Array of quoted operators
"disallowRightStickedOperators": [
"?",
"+",
"/",
"*",
":",
"=",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
]
x = y + 1;
x = y +1;
Requires sticking operators to the left.
Type: Array
Values: Array of quoted operators
"requireLeftStickedOperators": [","]
x = [1, 2];
x = [1 , 2];
Requires sticking unary operators to the right.
Type: Array
Values: Array of quoted operators
"disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"]
x = !y; y = ++z;
x = ! y; y = ++ z;
Disallows sticking unary operators to the right.
Type: Array
Values: Array of quoted operators
"requireSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"]
x = ! y; y = ++ z;
x = !y; y = ++z;
Requires sticking unary operators to the left.
Type: Array
Values: Array of quoted operators
"disallowSpaceBeforePostfixUnaryOperators": ["++", "--"]
x = y++; y = z--;
x = y ++; y = z --;
Disallows sticking unary operators to the left.
Type: Array
Values: Array of quoted operators
"requireSpaceBeforePostfixUnaryOperators": ["++", "--"]
x = y ++; y = z --;
x = y++; y = z--;
Requires sticking binary operators to the left.
Type: Array
Values: Array of quoted operators
"disallowSpaceBeforeBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
]
x+ y;
x + y;
Disallows sticking binary operators to the left.
Type: Array
Values: Array of quoted operators
"requireSpaceBeforeBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
]
x !== y;
x!== y;
Requires sticking binary operators to the right.
Type: Array
Values: Array of quoted operators
"disallowSpaceAfterBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
]
x +y;
x+ y;
Disallows sticking binary operators to the right.
Type: Array
Values: Array of quoted operators
"requireSpaceAfterBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
]
x + y;
x +y;
Disallows implicit type conversion.
Type: Array
Values: Array of quoted types
"disallowImplicitTypeConversion": ["numeric", "boolean", "binary", "string"]
x = Boolean(y);
x = Number(y);
x = String(y);
x = s.indexOf('.') !== -1;
x = !!y;
x = +y;
x = '' + y;
x = ~s.indexOf('.');
Requires identifiers to be camelCased or UPPERCASE_WITH_UNDERSCORES
Type: Boolean
Values: true
"requireCamelCaseOrUpperCaseIdentifiers": true
var camelCase = 0;
var CamelCase = 1;
var _camelCase = 2;
var camelCase_ = 3;
var UPPER_CASE = 4;
var lower_case = 1;
var Mixed_case = 2;
var mixed_Case = 3;
Disallows usage of specified keywords.
Type: Array
Values: Array of quoted keywords
"disallowKeywords": ["with"]
with (x) {
prop++;
}
Disallows strings that span multiple lines without using concatenation.
Type: Boolean
Values: true
"disallowMultipleLineStrings": true
var x = "multi" +
"line";
var y = "single line";
var x = "multi \
line";
Disallows multiple blank lines in a row.
Type: Boolean
Values: true
"disallowMultipleLineBreaks": true
var x = 1;
x++;
var x = 1;
x++;
Option to check line break characters
Type: String
Values: "CR"
, "LF"
, "CRLF"
"validateLineBreaks": "LF"
var x = 1;<LF>
x++;
var x = 1;<CRLF>
x++;
Requires all quote marks to be either the supplied value, or consistent if true
Type: String
Values: "\""
, "'"
, true
"validateQuoteMarks": "\""
var x = "x";
var x = 'x';
var x = "x", y = 'y';
Validates indentation for arrays, objects, switch statements, and block statements
Type: Integer
or String
Values: A positive integer or "\t"
"validateIndentation": "\t",
if (a) {
b=c;
function(d) {
e=f;
}
}
if (a) {
b=c;
function(d) {
e=f;
}
}
if (a) {
b=c;
function(d) {
e=f;
}
}
if (a) {
b=c;
function(d) {
e=f;
}
}
Requires lines to not contain both spaces and tabs consecutively, or spaces after tabs only for alignment if "smart"
Type: Boolean
or String
Values: true
or "smart"
"disallowMixedSpacesAndTabs": true
\tvar foo = "blah blah";
\s\s\s\svar foo = "blah blah";
\t/**
\t\s*
\t\s*/ //a single space to align the star in a docblock is allowed
\t\svar foo = "blah blah";
\s\tsvar foo = "blah blah";
\tvar foo = "blah blah";
\t\svar foo = "blah blah";
\s\s\s\svar foo = "blah blah";
\t/**
\t\s*
\t\s*/ //a single space to align the star in a docblock is allowed
\s\tsvar foo = "blah blah";
Requires all lines to end on a non-whitespace character
Type: Boolean
Values: true
"disallowTrailingWhitespace": true
var foo = "blah blah";
var foo = "blah blah"; //<-- whitespace character here
Disallows placing keywords on a new line.
Type: Array
Values: Array of quoted keywords
"disallowKeywordsOnNewLine": ["else"]
if (x < 0) {
x++;
} else {
x--;
}
if (x < 0) {
x++;
}
else {
x--;
}
Requires placing keywords on a new line.
Type: Array
Values: Array of quoted keywords
"requireKeywordsOnNewLine": ["else"]
if (x < 0) {
x++;
}
else {
x--;
}
if (x < 0) {
x++;
} else {
x--;
}
Requires placing line feed at file end.
Type: Boolean
Values: true
"requireLineFeedAtFileEnd": true
Requires all lines to be at most the number of characters specified
Type: Integer
Values: A positive integer
"maximumLineLength": 40
var aLineOf40Chars = 123456789012345678;
var aLineOf41Chars = 1234567890123456789;
Requires constructors to be capitalized (except for this
)
Type: Boolean
Values: true
"requireCapitalizedConstructors": true
var a = new B();
var c = new this();
var d = new e();
Option to check var that = this
expressions
Type: String
Values: String value used for context local declaration
"safeContextKeyword": "that"
var that = this;
var _this = this;
Requires member expressions to use dot notation when possible
Type: Boolean
Values: true
"requireDotNotation": true
var a = b[c];
var a = b.c;
var a = b[c.d];
var a = b[1];
var a = b['while']; //reserved word
var a = b['c'];
Enables JSDoc validation.
Type: Object
Values:
- "checkParamNames" ensures param names in jsdoc and in function declaration are equal
- "requireParamTypes" ensures params in jsdoc contains type
- "checkRedundantParams" reports redundant params in jsdoc
"validateJSDoc": {
"checkParamNames": true,
"checkRedundantParams": true,
"requireParamTypes": true
}
/**
* Adds style error to the list
*
* @param {String} message
* @param {Number|Object} line
* @param {Number} [column]
*/
add: function(message, line, column) {
}
/**
* Adds style error to the list
*
* @param {String} message
* @param {Number|Object} line
* @param {Number} [column]
*/
add: function() {
}
Disables style checking for specified paths.
Type: Array
Values: Array of file matching patterns
"excludeFiles": ["node_modules/**"]
Path to load additional rules
Type: Array
Values: Array of file matching patterns
"additionalRules": ["project-rules/*.js"]
Extends defined rules with preset rules
Type: String
Values: "jquery"
"preset": "jquery"
File jscs-browser.js contains browser-compatible version of jscs
.
Download and include jscs-browser.js
into your page.
<script type="text/javascript" src="jscs-browser.js"></script>
<script type="text/javascript">
var checker = new JscsStringChecker();
checker.registerDefaultRules();
checker.configure({disallowMultipleVarDecl: true});
var errors = checker.checkString('var x, y = 1;');
errors.getErrorList().forEach(function(error) {
console.log(errors.explainError(error));
});
</script>