node-jscs
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.
Friendly packages
- Grunt task: https://github.com/gustavohenke/grunt-jscs-checker
- Gulp task: https://github.com/sindresorhus/gulp-jscs
Installation
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[...]]
Configuration
jscs
is configured using .jscs.json file, located in the project root.
requireCurlyBraces
Requires curly braces after statements.
Type: Array
Values: Arrow of quoted keywords
Example
"requireCurlyBraces": [
"if",
"else",
"for",
"while",
"do",
"try",
"catch",
"case",
"default"
]
Valid
if (x) {
x++;
}
Invalid
if (x) x++;
requireSpaceAfterKeywords
Requires space after keyword.
Type: Array
Values: Array of quoted keywords
Example
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"return",
"try",
"catch"
]
Valid
return true;
Invalid
if(x) {
x++;
}
disallowSpaceAfterKeywords
Disallows space after keyword.
Type: Array
Values: Array of quoted keywords
Example
"disallowSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"try",
"catch"
]
Valid
if(x > y) {
y++;
}
requireParenthesesAroundIIFE
Requires parentheses around immediately invoked function expressions.
Type: Boolean
Values: true
Example
"requireParenthesesAroundIIFE": true
Valid
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));
Invalid
var a = function(){ return 1; }();
var c = function(){ return 3; }.call(this, arg1);
var d = function(){ return d; }.apply(this, args);
requireSpacesInFunctionExpression
Requires space before ()
or {}
in function declarations.
Type: Object
Values: beforeOpeningRoundBrace
and beforeOpeningCurlyBrace
as child properties. Child properties must be set to true
.
Example
"requireSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
Valid
function () {}
function a () {}
Invalid
function() {}
function (){}
disallowSpacesInFunctionExpression
Disallows space before ()
or {}
in function declarations.
Type: Object
Values: "beforeOpeningRoundBrace"
and "beforeOpeningCurlyBrace"
as child properties. Child properties must be set to true
.
Example
"disallowSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
Valid
function(){}
function a(){}
Invalid
function () {}
function a (){}
disallowMultipleVarDecl
Disallows multiple var
declaration (except for-loop).
Type: Boolean
Values: true
Example
"disallowMultipleVarDecl": true
Valid
var x = 1;
var y = 2;
for (var i = 0, j = arr.length; i < j; i++) {}
Invalid
var x = 1,
y = 2;
requireMultipleVarDecl
Requires multiple var
declaration.
Type: Boolean
Values: true
Example
"requireMultipleVarDecl": true
Valid
var x = 1,
y = 2;
Invalid
var x = 1;
var y = 2;
disallowEmptyBlocks
Disallows empty blocks (except for catch blocks).
Type: Boolean
Values: true
Example
"disallowEmptyBlocks": true
Valid
if ( a == b ) { c = d; }
try { a = b; } catch( e ){}
Invalid
if ( a == b ) { } else { c = d; }
disallowSpacesInsideObjectBrackets
Disallows space after opening object curly brace and before closing.
Type: Boolean
Values: true
Example
"disallowSpacesInsideObjectBrackets": true
Valid
var x = {a: 1};
Invalid
var x = { a: 1 };
disallowSpacesInsideArrayBrackets
Disallows space after opening array square bracket and before closing.
Type: Boolean
Values: true
Example
"disallowSpacesInsideArrayBrackets": true
Valid
var x = [1];
Invalid
var x = [ 1 ];
disallowSpacesInsideParentheses
Disallows space after opening round bracket and before closing.
Type: Boolean
Values: true
Example
"disallowSpacesInsideParentheses": true
Valid
var x = (1 + 2) * 3;
Invalid
var x = ( 1 + 2 ) * 3;
requireSpacesInsideObjectBrackets
Requires space after opening object curly brace and before closing.
Type: String
Values: "all"
for strict mode, "allButNested"
ignores closing brackets in a row.
Example
"requireSpacesInsideObjectBrackets": "all"
Valid for mode "all"
var x = { a: { b: 1 } };
Valid for mode "allButNested"
var x = { a: { b: 1 }};
Invalid
var x = {a: 1};
requireSpacesInsideArrayBrackets
Requires space after opening array square bracket and before closing.
Type: String
Values: "all" for strict mode, "allButNested" ignores closing brackets in a row.
Example
"requireSpacesInsideArrayBrackets": "all"
Valid for mode "all"
var x = [ 1 ];
Valid for mode "allButNested"
var x = [[ 1 ], [ 2 ]];
Invalid
var x = [1];
disallowQuotedKeysInObjects
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.
Example
"disallowQuotedKeysInObjects": true
Valid for mode true
var x = { a: { default: 1 } };
Valid for mode "allButReserved"
var x = {a: 1, 'default': 2};
Invalid
var x = {'a': 1};
disallowDanglingUnderscores
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
Example
"disallowDanglingUnderscores": true
Valid
var x = 1;
var y = _.extend;
var z = __dirname;
var w = __filename;
var x_y = 1;
Invalid
var _x = 1;
var x_ = 1;
var x_y_ = 1;
disallowSpaceAfterObjectKeys
Disallows space after object keys.
Type: Boolean
Values: true
Example
"disallowSpaceAfterObjectKeys": true
Valid
var x = {a: 1};
Invalid
var x = {a : 1};
requireSpaceAfterObjectKeys
Requires space after object keys.
Type: Boolean
Values: true
Example
"requireSpaceAfterObjectKeys": true
Valid
var x = {a : 1};
Invalid
var x = {a: 1};
disallowCommaBeforeLineBreak
Disallows commas as last token on a line in lists.
Type: Boolean
Values: true
Example
"disallowCommaBeforeLineBreak": true
Valid
var x = {
one: 1
, two: 2
};
var y = { three: 3, four: 4};
Invalid
var x = {
one: 1,
two: 2
};
requireCommaBeforeLineBreak
Requires commas as last token on a line in lists.
Type: Boolean
Values: true
Example
"requireCommaBeforeLineBreak": true
Valid
var x = {
one: 1,
two: 2
};
var y = { three: 3, four: 4};
Invalid
var x = {
one: 1
, two: 2
};
requireAlignedObjectValues
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
Example
"requireAlignedObjectValues": "all"
Valid
var x = {
a : 1,
bcd : 2,
ef : 'str'
};
Invalid
var x = {
a : 1,
bcd : 2,
ef : 'str'
};
requireOperatorBeforeLineBreak
Requires operators to appear before line breaks and not after.
Type: Array
Values: Array of quoted operators
Example
"requireOperatorBeforeLineBreak": [
"?",
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
]
Valid
x = y ? 1 : 2;
x = y ?
1 : 2;
Invalid
x = y
? 1 : 2;
disallowLeftStickedOperators
Disallows sticking operators to the left.
Type: Array
Values: Array of quoted operators
Example
"disallowLeftStickedOperators": [
"?",
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
]
Valid
x = y ? 1 : 2;
Invalid
x = y? 1 : 2;
requireRightStickedOperators
Requires sticking operators to the right.
Type: Array
Values: Array of quoted operators
Example
"requireRightStickedOperators": ["!"]
Valid
x = !y;
Invalid
x = ! y;
disallowRightStickedOperators
Disallows sticking operators to the right.
Type: Array
Values: Array of quoted operators
Example
"disallowRightStickedOperators": [
"?",
"+",
"/",
"*",
":",
"=",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
]
Valid
x = y + 1;
Invalid
x = y +1;
requireLeftStickedOperators
Requires sticking operators to the left.
Type: Array
Values: Array of quoted operators
Example
"requireLeftStickedOperators": [","]
Valid
x = [1, 2];
Invalid
x = [1 , 2];
disallowSpaceAfterPrefixUnaryOperators
Requires sticking unary operators to the right.
Type: Array
Values: Array of quoted operators
Example
"disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"]
Valid
x = !y; y = ++z;
Invalid
x = ! y; y = ++ z;
requireSpaceAfterPrefixUnaryOperators
Disallows sticking unary operators to the right.
Type: Array
Values: Array of quoted operators
Example
"requireSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"]
Valid
x = ! y; y = ++ z;
Invalid
x = !y; y = ++z;
disallowSpaceBeforePostfixUnaryOperators
Requires sticking unary operators to the left.
Type: Array
Values: Array of quoted operators
Example
"disallowSpaceBeforePostfixUnaryOperators": ["++", "--"]
Valid
x = y++; y = z--;
Invalid
x = y ++; y = z --;
requireSpaceBeforePostfixUnaryOperators
Disallows sticking unary operators to the left.
Type: Array
Values: Array of quoted operators
Example
"requireSpaceBeforePostfixUnaryOperators": ["++", "--"]
Valid
x = y ++; y = z --;
Invalid
x = y++; y = z--;
disallowSpaceBeforeBinaryOperators
Requires sticking binary operators to the left.
Type: Array
Values: Array of quoted operators
Example
"disallowSpaceBeforeBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
]
Valid
x+ y;
Invalid
x + y;
requireSpaceBeforeBinaryOperators
Disallows sticking binary operators to the left.
Type: Array
Values: Array of quoted operators
Example
"requireSpaceBeforeBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
]
Valid
x !== y;
Invalid
x!== y;
disallowSpaceAfterBinaryOperators
Requires sticking binary operators to the right.
Type: Array
Values: Array of quoted operators
Example
"disallowSpaceAfterBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
]
Valid
x +y;
Invalid
x+ y;
requireSpaceAfterBinaryOperators
Disallows sticking binary operators to the right.
Type: Array
Values: Array of quoted operators
Example
"requireSpaceAfterBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
]
Valid
x + y;
Invalid
x +y;
disallowImplicitTypeConversion
Disallows implicit type conversion.
Type: Array
Values: Array of quoted types
Example
"disallowImplicitTypeConversion": ["numeric", "boolean", "binary", "string"]
Valid
x = Boolean(y);
x = Number(y);
x = String(y);
x = s.indexOf('.') !== -1;
Invalid
x = !!y;
x = +y;
x = '' + y;
x = ~s.indexOf('.');
requireCamelCaseOrUpperCaseIdentifiers
Requires identifiers to be camelCased or UPPERCASE_WITH_UNDERSCORES
Type: Boolean
Values: true
Example
"requireCamelCaseOrUpperCaseIdentifiers": true
Valid
var camelCase = 0;
var CamelCase = 1;
var _camelCase = 2;
var camelCase_ = 3;
var UPPER_CASE = 4;
Invalid
var lower_case = 1;
var Mixed_case = 2;
var mixed_Case = 3;
disallowKeywords
Disallows usage of specified keywords.
Type: Array
Values: Array of quoted keywords
Example
"disallowKeywords": ["with"]
Invalid
with (x) {
prop++;
}
disallowMultipleLineStrings
Disallows strings that span multiple lines without using concatenation.
Type: Boolean
Values: true
Example
"disallowMultipleLineStrings": true
Valid
var x = "multi" +
"line";
var y = "single line";
Invalid
var x = "multi \
line";
disallowMultipleLineBreaks
Disallows multiple blank lines in a row.
Type: Boolean
Values: true
Example
"disallowMultipleLineBreaks": true
Valid
var x = 1;
x++;
Invalid
var x = 1;
x++;
validateLineBreaks
Option to check line break characters
Type: String
Values: "CR"
, "LF"
, "CRLF"
Example
"validateLineBreaks": "LF"
Valid
var x = 1;<LF>
x++;
Invalid
var x = 1;<CRLF>
x++;
validateQuoteMarks
Requires all quote marks to be either the supplied value, or consistent if true
Type: String
Values: "\""
, "'"
, true
Example
"validateQuoteMarks": "\""
Valid example for mode "\""
or mode true
var x = "x";
Valid example for mode "'"
or mode true
var x = 'x';
Invalid example for mode true
var x = "x", y = 'y';
validateIndentation
Validates indentation for arrays, objects, switch statements, and block statements
Type: Integer
or String
Values: A positive integer or "\t"
Example
"validateIndentation": "\t",
Valid example for mode 2
if (a) {
b=c;
function(d) {
e=f;
}
}
Invalid example for mode 2
if (a) {
b=c;
function(d) {
e=f;
}
}
Valid example for mode "\t"
if (a) {
b=c;
function(d) {
e=f;
}
}
Invalid example for mode "\t"
if (a) {
b=c;
function(d) {
e=f;
}
}
disallowMixedSpacesAndTabs
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"
Example
"disallowMixedSpacesAndTabs": true
Valid example for mode 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
Invalid example for mode true
\t\svar foo = "blah blah";
\s\tsvar foo = "blah blah";
Valid example for mode "smart"
\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
Invalid example for mode "smart"
\s\tsvar foo = "blah blah";
disallowTrailingWhitespace
Requires all lines to end on a non-whitespace character
Type: Boolean
Values: true
Example
"disallowTrailingWhitespace": true
Valid
var foo = "blah blah";
Invalid
var foo = "blah blah"; //<-- whitespace character here
disallowKeywordsOnNewLine
Disallows placing keywords on a new line.
Type: Array
Values: Array of quoted keywords
Example
"disallowKeywordsOnNewLine": ["else"]
Valid
if (x < 0) {
x++;
} else {
x--;
}
Invalid
if (x < 0) {
x++;
}
else {
x--;
}
requireKeywordsOnNewLine
Requires placing keywords on a new line.
Type: Array
Values: Array of quoted keywords
Example
"requireKeywordsOnNewLine": ["else"]
Valid
if (x < 0) {
x++;
}
else {
x--;
}
Invalid
if (x < 0) {
x++;
} else {
x--;
}
requireLineFeedAtFileEnd
Requires placing line feed at file end.
Type: Boolean
Values: true
Example
"requireLineFeedAtFileEnd": true
maximumLineLength
Requires all lines to be at most the number of characters specified
Type: Integer
Values: A positive integer
Example
"maximumLineLength": 40
Valid
var aLineOf40Chars = 123456789012345678;
Invalid
var aLineOf41Chars = 1234567890123456789;
requireCapitalizedConstructors
Requires constructors to be capitalized (except for this
)
Type: Boolean
Values: true
Example
"requireCapitalizedConstructors": true
Valid
var a = new B();
var c = new this();
Invalid
var d = new e();
safeContextKeyword
Option to check var that = this
expressions
Type: String
Values: String value used for context local declaration
Example
"safeContextKeyword": "that"
Valid
var that = this;
Invalid
var _this = this;
requireDotNotation
Requires member expressions to use dot notation when possible
Type: Boolean
Values: true
Example
"requireDotNotation": true
Valid
var a = b[c];
var a = b.c;
var a = b[c.d];
var a = b[1];
var a = b['while']; //reserved word
Invalid
var a = b['c'];
validateJSDoc
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
Example
"validateJSDoc": {
"checkParamNames": true,
"checkRedundantParams": true,
"requireParamTypes": true
}
Valid
/**
* Adds style error to the list
*
* @param {String} message
* @param {Number|Object} line
* @param {Number} [column]
*/
add: function(message, line, column) {
}
Invalid
/**
* Adds style error to the list
*
* @param {String} message
* @param {Number|Object} line
* @param {Number} [column]
*/
add: function() {
}
excludeFiles
Disables style checking for specified paths.
Type: Array
Values: Array of file matching patterns
Example
"excludeFiles": ["node_modules/**"]
additionalRules
Path to load additional rules
Type: Array
Values: Array of file matching patterns
Example
"additionalRules": ["project-rules/*.js"]
preset
Extends defined rules with preset rules
Type: String
Values: "jquery"
Example
"preset": "jquery"
Browser Usage
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>