node-jscs
JSCS — JavaScript Code Style. Twitter | Mailing List
jscs
is a code style checker. You can configure jscs
for your project in detail using over 60 validation rules, including presets from popular style guides like jQuery.
Table of Contents
Presets
Friendly packages
Extensions
Installation
jscs
can be installed using npm
:
npm install jscs -g
To run jscs
, you can use the following command from the project root:
jscs path[ path[...]]
You can also pipe input into jscs:
cat myfile.js | jscs
CLI
--config
Allows to define path to the config file.
jscs path[ path[...]] --config=./.config.json
If there is no --config
option specified, jscs
it will consequentially search for jscsConfig
option in package.json
file then for .jscsrc
(which is a just JSON with comments) and .jscs.json
files in the current working directory then in nearest ancestor until it hits the system root.
--preset
If defined will use predefined rules for specific code style.
jscs path[ path[...]] --preset=jquery
--reporter
jscs
itself provides six reporters: checkstyle
, console
, inline
, junit
and text
.
jscs path[ path[...]] --reporter=console
But you also can specify your own reporter, since this flag accepts relative or absolute paths too.
jscs path[ path[...]] --reporter=./some-dir/my-reporter.js
--esnext
Attempts to parse your code as ES6 using the harmony version of the esprima parser. Please note that this is currently experimental, and will improve over time.
--esprima
Attempts to parse your code with a custom Esprima version.
jscs path[ path[...]] --esprima=esprima-fb
--error-filter
The path to a module that determines whether or not an error should be reported.
jscs path[ path[...]] --error-filter=path/to/my/module.js
--no-colors
Clean output without colors.
--max-errors
Set the maximum number of errors to report
--help
Outputs usage information.
--verbose
Prepends the name of the offending rule to all error messages.
--version
Outputs version of jscs
.
Options
plugins
Paths to load plugins. See the wiki page for more details about the Plugin API
Values: Array of NPM package names or paths
"plugins": ["jscs-plugin", "./lib/project-jscs-plugin"]
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: "airbnb"
, "crockford"
, "google"
, "jquery"
, "mdcs"
, "wikimedia"
, "yandex"
Example
"preset": "jquery"
If you want specifically disable preset rule assign it to null
, like so:
{
"preset": "jquery",
"requireCurlyBraces": null
}
excludeFiles
Disables style checking for specified paths.
Type: Array
Values: Array of file matching patterns
Example
"excludeFiles": ["node_modules/**"]
fileExtensions
Changes the set of file extensions that will be processed.
Type: Array
or String
or "*"
Values: A single file extension or an Array of file extensions, beginning with a .
. The matching is case insensitive. If "*"
is provided, all files regardless of extension will match.
Example
"fileExtensions": [".js", ".jsx"]
Default
"fileExtensions": [".js"]
maxErrors
Set the maximum number of errors to report
Type: Number
Default: Infinity
Example
"maxErrors": 10
esnext
Attempts to parse your code as ES6 using the harmony version of the esprima parser.
Type: Boolean
Value: true
Example
"esnext": true
errorFilter
A filter function that determines whether or not to report an error.
This will be called for every found error.
Type: String
Example
"errorFilter": "path/to/my/filter.js"
See how to define an error filter.
Error Suppression
You can disable and reenable rules inline with two special comments: // jscs:disable
and // jscs:enable
. Spacing in these comments is fairly lenient. All of the following are equivalent:
You can use them to disable rules in several ways.
Disabling All Rules
Simply using // jscs:disable
or // jscs:enable
will disable all rules.
var a = b;
var c = d;
var e = f;
Disabling Specific Rules
Including a comma separated list of rules to modify after // jscs:disable
or // jscs:enable
will modify only those rules.
if (x) y();
if (z) a();
You can enable all rules after disabling a specific rule, and that rule becomes reenabled as well.
if (x) y();
if (z) a();
You can disable multiple rules at once and progressively reeanble them.
if (x['a']) y();
if (z['a']) a();
if (z['a']) a();
Versioning & Semver
We recommend installing JSCS via NPM using ^
, or ~
if you want more stable releases.
Semver (http://semver.org/) dictates that breaking changes be major version bumps. In the context of a linting tool, a bug fix that causes more errors to be reported can be interpreted as a breaking change. However, that would require major version bumps to occur more often than can be desirable. Therefore, as a compromise, we will only release bug fixes that cause more errors to be reported in minor versions.
Below you fill find our versioning strategy, and what you can expect to come out of a new JSCS release.
- Patch release:
- A bug fix in a rule that causes JSCS to report less errors.
- Docs, refactoring and other "invisible" changes for user;
- Minor release:
- Any preset changes.
- A bug fix in a rule that causes JSCS to report more errors.
- New rules or new options for existing rules that don't change existing behavior.
- Modifying rules so they report less errors, and don't cause build failures.
- Major release:
- Purposefully modifying existing rules so that they report more errors or change the meaning of a rule.
- Any architectural changes that could cause builds to fail.
Rules
requireCurlyBraces
Requires curly braces after statements.
Type: Array
or Boolean
Values: Array of quoted keywords or true
to require curly braces after the following keywords:
JSHint: curly
Example
"requireCurlyBraces": [
"if",
"else",
"for",
"while",
"do",
"try",
"catch",
"case",
"default"
]
Valid
if (x) {
x++;
}
Invalid
if (x) x++;
requireSpaceBeforeKeywords
Requires space before keyword.
Type: Array
or Boolean
Values: Array of quoted keywords or true
to require all possible keywords to have a preceding space.
Example
"requireSpaceBeforeKeywords": [
"else",
"while",
"catch"
]
Valid
} else {
x++;
}
Invalid
}else {
x++;
}
requireSpaceAfterKeywords
Requires space after keyword.
Type: Array
or Boolean
Values: Array of quoted keywords or true
to require all of the keywords below to have a space afterward.
Example
"requireSpaceAfterKeywords": [
"do",
"for",
"if",
"else",
"switch",
"case",
"try",
"catch",
"void",
"while",
"with",
"return",
"typeof",
"function"
]
Valid
return true;
Invalid
if(x) {
x++;
}
disallowSpaceAfterKeywords
Disallows space after keyword.
Type: Array
or Boolean
Values: Array of quoted keywords or true
to disallow spaces after all possible keywords.
Example
"disallowSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"try",
"catch"
]
Valid
if(x > y) {
y++;
}
disallowSpaceBeforeKeywords
Disallows space before keyword.
Type: Array
or Boolean
Values: Array of quoted keywords or true
to disallow spaces before all possible keywords.
Example
"disallowSpaceBeforeKeywords": [
"else",
"catch"
]
Valid
}else {
y--;
}
Invalid
} else {
y--;
}
requireSpaceBeforeBlockStatements
Requires space before block statements (for loops, control structures).
Type: Boolean
Values: true
Example
"requireSpaceBeforeBlockStatements": true
Valid
if (cond) {
foo();
}
for (var e in elements) {
bar(e);
}
while (cond) {
foo();
}
Invalid
if (cond){
foo();
}
for (var e in elements){
bar(e);
}
while (cond){
foo();
}
disallowSpaceBeforeBlockStatements
Disallows space before block statements (for loops, control structures).
Type: Boolean
Values: true
Example
"disallowSpaceBeforeBlockStatements": true
Valid
if (cond){
foo();
}
for (var e in elements){
bar(e);
}
while (cond){
foo();
}
Invalid
if (cond) {
foo();
}
for (var e in elements) {
bar(e);
}
while (cond) {
foo();
}
requireParenthesesAroundIIFE
Requires parentheses around immediately invoked function expressions.
Type: Boolean
Values: true
JSHint: immed
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);
requireSpacesInConditionalExpression
Requires space before and/or after ?
or :
in conditional expressions.
Type: Object
or Boolean
Values: "afterTest"
, "beforeConsequent"
, "afterConsequent"
, "beforeAlternate"
as child properties, or true
to set all properties to true
. Child properties must be set to true
.
Example
"requireSpacesInConditionalExpression": {
"afterTest": true,
"beforeConsequent": true,
"afterConsequent": true,
"beforeAlternate": true
}
Valid
var a = b ? c : d;
var a= b ? c : d;
Invalid
var a = b? c : d;
var a = b ?c : d;
var a = b ? c: d;
var a = b ? c :d;
disallowSpacesInConditionalExpression
Disallows space before and/or after ?
or :
in conditional expressions.
Type: Object
or Boolean
Values: "afterTest"
, "beforeConsequent"
, "afterConsequent"
, "beforeAlternate"
as child properties, or true
to set all properties to true. Child properties must be set to true
. These token names correspond to:
var a = b ? c : d;
^ ^ ^ ^
| | | |
| | | └- beforeAlternate
| | └--- afterConsequent
| └-------- beforeConsequent
└---------- afterTest
Example
"disallowSpacesInConditionalExpression": {
"afterTest": true,
"beforeConsequent": true,
"afterConsequent": true,
"beforeAlternate": true
}
Valid
var a = b?c:d;
var a= b?c:d;
Invalid
var a = b ?c:d;
var a = b? c:d;
var a = b?c :d;
var a = b?c: d;
requireSpacesInFunctionExpression
Requires space before ()
or {}
in function expressions (both named and anonymous).
Type: Object
Values: "beforeOpeningRoundBrace"
and "beforeOpeningCurlyBrace"
as child properties. Child properties must be set to true
.
Example
"requireSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
Valid
var x = function () {};
var x = function a () {};
Invalid
var x = function() {};
var x = function a(){};
disallowSpacesInFunctionExpression
Disallows space before ()
or {}
in function expressions (both named and anonymous).
Type: Object
Values: "beforeOpeningRoundBrace"
and "beforeOpeningCurlyBrace"
as child properties. Child properties must be set to true
.
Example
"disallowSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
Valid
var x = function(){};
var x = function a(){};
Invalid
var x = function () {};
var x = function a (){};
requireSpacesInAnonymousFunctionExpression
Requires space before ()
or {}
in anonymous function expressions.
Type: Object
Values: "beforeOpeningRoundBrace"
and "beforeOpeningCurlyBrace"
as child properties. Child properties must be set to true
.
Example
"requireSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
Valid
var foo = function () {};
var Foo = {
foo: function () {};
}
array.map(function () {});
Invalid
var foo = function() {};
var Foo = {
foo: function (){};
}
array.map(function(){});
disallowSpacesInAnonymousFunctionExpression
Disallows space before ()
or {}
in anonymous function expressions.
Type: Object
Values: "beforeOpeningRoundBrace"
and "beforeOpeningCurlyBrace"
as child properties. Child properties must be set to true
.
Example
"disallowSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
Valid
var foo = function(){};
var Foo = {
foo: function(){};
}
array.map(function(){});
Invalid
var foo = function () {};
var Foo = {
foo: function (){};
}
array.map(function() {});
requireSpacesInNamedFunctionExpression
Requires space before ()
or {}
in named function expressions.
Type: Object
Values: "beforeOpeningRoundBrace"
and "beforeOpeningCurlyBrace"
as child properties. Child properties must be set to true
.
Example
"requireSpacesInNamedFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
Valid
var x = function a () {};
Invalid
var x = function a() {};
var x = function a(){};
disallowSpacesInNamedFunctionExpression
Disallows space before ()
or {}
in named function expressions.
Type: Object
Values: "beforeOpeningRoundBrace"
and "beforeOpeningCurlyBrace"
as child properties. Child properties must be set to true
.
Example
"disallowSpacesInNamedFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
Valid
var x = function a(){};
Invalid
var x = function a () {};
var x = function a (){};
requireSpacesInFunctionDeclaration
Requires space before ()
or {}
in function declarations.
Type: Object
Values: "beforeOpeningRoundBrace"
and "beforeOpeningCurlyBrace"
as child properties. Child properties must be set to true
.
Example
"requireSpacesInFunctionDeclaration": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
Valid
function a () {}
Invalid
function a() {}
function a (){}
disallowSpacesInFunctionDeclaration
Disallows space before ()
or {}
in function declarations.
Type: Object
Values: "beforeOpeningRoundBrace"
and "beforeOpeningCurlyBrace"
as child properties. Child properties must be set to true
.
Example
"disallowSpacesInFunctionDeclaration": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
Valid
function a(){}
Invalid
function a () {}
function a (){}
requireSpacesInFunction
Requires space before ()
or {}
in function declarations and expressions.
Type: Object
Values: "beforeOpeningRoundBrace"
and "beforeOpeningCurlyBrace"
as child properties. Child properties must be set to true
.
Example
"requireSpacesInFunction": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
Valid
function a () {}
var x = function a () {};
Invalid
function a() {}
function a (){}
var x = function a() {};
var x = function a () {};
disallowSpacesInFunction
Disallows space before ()
or {}
in function declarations and expressions.
Type: Object
Values: "beforeOpeningRoundBrace"
and "beforeOpeningCurlyBrace"
as child properties. Child properties must be set to true
.
Example
"disallowSpacesInFunction": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
}
Valid
function a(){}
var x = function a(){};
Invalid
function a () {}
function a (){}
var x = function a () {};
var x = function a (){};
requireSpacesInCallExpression
Requires space before ()
in call expressions.
Type: Boolean
Values: true
Example
"requireSpacesInCallExpression": true
Valid
var x = foobar ();
Invalid
var x = foobar();
disallowSpacesInCallExpression
Disallows space before ()
in call expressions.
Type: Boolean
Values: true
Example
"disallowSpacesInCallExpression": true
Valid
var x = foobar();
Invalid
var x = foobar ();
disallowMultipleVarDecl
Disallows multiple var
declaration (except for-loop).
Type: Boolean
or String
Values:
true
disallows multiple variable declarations except within a for loop'strict'
disallows all multiple variable declarations'exceptUndefined'
allows declarations where all variables are not defined
Example
"disallowMultipleVarDecl": true
Valid for true
var x = 1;
var y = 2;
for (var i = 0, j = arr.length; i < j; i++) {}
Valid for strict
var x = 1;
var y = 2;
Valid for exceptUndefined
var a, b;
var x = 1;
var y = 2;
for (var i = 0, j = arr.length; i < j; i++) {}
Invalid
var x = 1,
y = 2;
var x, y = 2, z;
requireMultipleVarDecl
Requires multiple var
declaration.
Type: Boolean
or String
Values: true
or "onevar"
if requireMultipleVarDecl
defined as a true
value, it will report only consecutive vars, if, on the other hand,
value equals to "onevar"
string, requireMultipleVarDecl
will allow only one var
per function scope.
JSHint: onevar
Example
"requireMultipleVarDecl": true
Valid
var x = 1,
y = 2;
Invalid
var x = 1;
var y = 2;
requireBlocksOnNewline
Requires blocks to begin and end with a newline
Type: Boolean
or Integer
Values: true
validates all non-empty blocks, Integer
specifies a minimum number of statements in the block before validating.
Example
"requireBlocksOnNewline": true
Valid for mode true
if (true) {
doSomething();
}
var abc = function() {};
Invalid
if (true) {doSomething();}
Valid for mode 1
if (true) {
doSomething();
doSomethingElse();
}
if (true) { doSomething(); }
var abc = function() {};
Invalid
if (true) { doSomething(); doSomethingElse(); }
requirePaddingNewlinesInBlocks
Requires blocks to begin and end with 2 newlines
Type: Boolean
or Integer
Values: true
validates all non-empty blocks, Integer
specifies a minimum number of statements in the block before validating.
Example
"requirePaddingNewlinesInBlocks": true
Valid for mode true
if (true) {
doSomething();
}
var abc = function() {};
Invalid
if (true) {doSomething();}
if (true) {
doSomething();
}
Valid for mode 1
if (true) {
doSomething();
doSomethingElse();
}
if (true) {
doSomething();
}
if (true) { doSomething(); }
var abc = function() {};
Invalid
if (true) { doSomething(); doSomethingElse(); }
if (true) {
doSomething();
doSomethingElse();
}
disallowPaddingNewlinesInBlocks
Disallows blocks from beginning and ending with 2 newlines.
Type: Boolean
Values: true
validates all non-empty blocks.
Example
"disallowPaddingNewlinesInBlocks": true
Valid
if (true) {
doSomething();
}
if (true) {doSomething();}
var abc = function() {};
Invalid
if (true) {
doSomething();
}
requirePaddingNewLinesInObjects
Requires newline inside curly braces of all objects.
Type: Boolean
Values: true
Example
"requirePaddingNewLinesInObjects": true
Valid
var x = {
a: 1
};
foo({
a: {
b: 1
}
});
Invalid
var x = { a: 1 };
foo({a:{b:1}});
disallowPaddingNewLinesInObjects
Disallows newline inside curly braces of all objects.
Type: Boolean
Values: true
Example
"disallowPaddingNewLinesInObjects": true
Valid
var x = { a: 1 };
foo({a: {b: 1}});
Invalid
var x = {
a: 1
};
foo({
a: {
b: 1
}
});
requirePaddingNewlinesBeforeKeywords
Requires an empty line above the specified keywords unless the keyword is the first expression in a block.
Type: Array
or Boolean
Values: Array of quoted types or true
to require padding new lines before all of the keywords below.
Example
"requirePaddingNewlinesBeforeKeywords": [
"do",
"for",
"if",
"else",
"switch",
"case",
"try",
"catch",
"void",
"while",
"with",
"return",
"typeof",
"function"
]
Valid
function(a) {
if (!a) {
return false;
}
for (var i = 0; i < b; i++) {
if (!a[i]) {
return false;
}
}
return true;
}
Invalid
function(a) {
if (!a) {
return false;
}
for (var i = 0; i < b; i++) {
if (!a[i]) {
return false;
}
}
return true;
}
disallowPaddingNewlinesBeforeKeywords
Disallow an empty line above the specified keywords.
Type: Array
or Boolean
Values: Array of quoted types or true
to disallow padding new lines after all of the keywords below.
Example
"requirePaddingNewlinesBeforeKeywords": [
"do",
"for",
"if",
"else",
"switch",
"case",
"try",
"catch",
"void",
"while",
"with",
"return",
"typeof",
"function"
]
Valid
function(a) {
if (!a) {
return false;
}
for (var i = 0; i < b; i++) {
if (!a[i]) {
return false;
}
}
return true;
}
Invalid
function(a) {
if (!a) {
return false;
}
for (var i = 0; i < b; i++) {
if (!a[i]) {
return false;
}
}
return true;
}
disallowEmptyBlocks
Disallows empty blocks (except for catch blocks).
Type: Boolean
Values: true
JSHint: noempty
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
or String
Values: "all"
or true
for strict mode, "nested"
ignores closing brackets in a row.
Example
"disallowSpacesInsideObjectBrackets": "all"
Valid for mode "all"
var x = {a: {b: 1}};
Valid for mode "nested"
var x = { a: {b: 1} };
Invalid
var x = { a: { b: 1 } };
disallowSpacesInsideArrayBrackets
Disallows space after opening array square bracket and before closing.
Type: Boolean
or String
Values: "all"
or true
for strict mode, "nested"
ignores closing brackets in a row.
Example
"disallowSpacesInsideArrayBrackets": "all"
Valid for mode "all"
var x = [[1]];
Valid for mode "nested"
var x = [ [1] ];
Invalid
var x = [ [ 1 ] ];
disallowSpacesInsideParentheses
Disallows space after opening round bracket and before closing.
Type: Object
or Boolean
Values: true
or Object with either "only"
with array of tokens or "all"
with true
value
Example
"disallowSpacesInsideParentheses": true
Valid for true
value
var x = (1 + 2) * 3;
Valid for only
value
"disallowSpacesInsideParentheses": { "only": [ "{", "}" ] }
var x = ( 1 + 2 );
var x = foo({});
Invalid
var x = foo( {} );
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];
requireSpacesInsideParentheses
Requires space after opening round bracket and before closing.
Type: Object
or String
Values: "all"
for strict mode, "allButNested"
(deprecated use "except": ['(', ')']
) ignores nested brackets in a row, you could also specify token exceptions.
Example
"requireSpacesInsideParentheses": {
"all": true,
"except": [ "{", "}" ]
}
Valid for mode "all"
var x = Math.pow( ( 1 + 2 ), ( 3 + 4 ) );
Valid for mode "allButNested"
var x = Math.pow(( 1 + 2 ), ( 3 + 4 ));
Valid for mode "all"
with except
var x = Math.pow( foo({ test: 1 }) );
Invalid
var x = Math.pow(1 + 2, 3 + 4);
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 helpful when using this option with JSHint's es3
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 _
. Some popular identifiers are automatically listed as exceptions:
__proto__
(javascript)_
(underscore.js)__filename
(node.js global)__dirname
(node.js global)super_
(node.js, used by util.inherits
)
Type: Boolean
or Object
Values:
true
Object
:
allExcept
: array of quoted identifiers
JSHint: nomen
Example
"disallowDanglingUnderscores": { allExcept: ["_exception"] }
Valid
var x = 1;
var o = obj.__proto__;
var y = _.extend;
var z = __dirname;
var w = __filename;
var x_y = 1;
var v = _exception;
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};
disallowSpaceBeforeObjectValues
Disallows space after object keys.
Type: Boolean
Values: true
Example
"disallowSpaceBeforeObjectValues": true
Valid
var x = {a:1};
Invalid
var x = {a: 1};
requireSpaceBeforeObjectValues
Requires space after object keys.
Type: Boolean
Values: true
Example
"requireSpaceBeforeObjectValues": 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
JSHint: laxcomma
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
JSHint: laxcomma
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,"ignoreFunction"
ignores objects if one of the property values is a function expression,"ignoreLineBreak"
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
or Boolean
Values: Array of quoted operators or true
to require all possible binary operators to appear before line breaks
JSHint: laxbreak
Example
"requireOperatorBeforeLineBreak": [
"?",
"=",
"+",
"-",
"/",
"*",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
]
Valid
x = y ? 1 : 2;
x = y ?
1 : 2;
Invalid
x = y
? 1 : 2;
disallowSpaceAfterPrefixUnaryOperators
Requires sticking unary operators to the right.
Type: Array
or Boolean
Values: Array of quoted operators or true
to disallow space after prefix for all unary operators
Example
"disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"]
Valid
x = !y; y = ++z;
Invalid
x = ! y; y = ++ z;
requireSpaceAfterPrefixUnaryOperators
Disallows sticking unary operators to the right.
Type: Array
or Boolean
Values: Array of quoted operators or true
to require space after prefix for all unary operators
Example
"requireSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"]
Valid
x = ! y; y = ++ z;
Invalid
x = !y; y = ++z;
disallowSpaceBeforePostfixUnaryOperators
Requires sticking unary operators to the left.
Type: Array
or Boolean
Values: Array of quoted operators or true
to disallow space before postfix for all unary operators (i.e. increment/decrement operators)
Example
"disallowSpaceBeforePostfixUnaryOperators": ["++", "--"]
Valid
x = y++; y = z--;
Invalid
x = y ++; y = z --;
requireSpaceBeforePostfixUnaryOperators
Disallows sticking unary operators to the left.
Type: Array
or Boolean
Values: Array of quoted operators or true
to require space before postfix for all unary operators (i.e. increment/decrement operators)
Example
"requireSpaceBeforePostfixUnaryOperators": ["++", "--"]
Valid
x = y ++; y = z --;
Invalid
x = y++; y = z--;
disallowSpaceBeforeBinaryOperators
Requires sticking binary operators to the left.
Type: Array
or Boolean
Values: Array of quoted operators or true
to disallow space before all possible binary operators
Example
"disallowSpaceBeforeBinaryOperators": [
"=",
",",
"+",
"-",
"/",
"*",
"==",
"===",
"!=",
"!=="
]
Valid
x+ y;
Invalid
x + y;
requireSpaceBeforeBinaryOperators
Disallows sticking binary operators to the left.
Type: Array
or Boolean
Values: Array of quoted operators or true
to require space before all possible binary operators
without comma operator, since it's rarely used with this rule
Example
"requireSpaceBeforeBinaryOperators": [
"=",
",",
"+",
"-",
"/",
"*",
"==",
"===",
"!=",
"!=="
]
Valid
x !== y;
Invalid
x!== y;
disallowSpaceAfterBinaryOperators
Requires sticking binary operators to the right.
Type: Array
or Boolean
Values: Array of quoted operators or true
to disallow space after all possible binary operators
Example
"disallowSpaceAfterBinaryOperators": [
"=",
",",
"+",
"-",
"/",
"*",
"==",
"===",
"!=",
"!=="
]
Valid
x +y;
Invalid
x+ y;
requireSpaceAfterBinaryOperators
Disallows sticking binary operators to the right.
Type: Array
or Boolean
Values: Array of quoted operators or true
to require space after all possible binary 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
or String
Values: true
or "ignoreProperties"
JSHint: camelcase
Example
"requireCamelCaseOrUpperCaseIdentifiers": true
Valid for mode true
var camelCase = 0;
var CamelCase = 1;
var _camelCase = 2;
var camelCase_ = 3;
var UPPER_CASE = 4;
Invalid for mode true
var lower_case = 1;
var Mixed_case = 2;
var mixed_Case = 3;
Valid for mode ignoreProperties
var camelCase = 0;
var CamelCase = 1;
var _camelCase = 2;
var camelCase_ = 3;
var UPPER_CASE = 4;
var obj.snake_case = 5;
var camelCase = { snake_case: 6 };
Invalid for mode ignoreProperties
var lower_case = 1;
var Mixed_case = 2;
var mixed_Case = 3;
var snake_case = { snake_case: 6 };
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
JSHint: multistr
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++;
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"
JSHint: smarttabs
Example
"disallowMixedSpacesAndTabs": true
Valid example for mode true
\tvar foo = "blah blah";
\s\s\s\svar foo = "blah blah";
\t
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
Invalid example for mode "smart"
\s\tsvar foo = "blah blah";
disallowOperatorBeforeLineBreak
Requires putting certain operators on the next line rather than on the current line before a line break.
Type: Array
or Boolean
Values: Array of operators to apply to or true
Example
"disallowOperatorBeforeLineBreak": ["+", "."]
Valid
$el.on( 'click', fn )
.appendTo( 'body' );
var x = 4 + 5 +
12 + 13;
Invalid
$el.on( 'click', fn ).
appendTo( 'body' );
var x = 4 + 5
+ 12 + 13;
disallowTrailingWhitespace
Requires all lines to end on a non-whitespace character
Type: Boolean
Values: true
JSHint: trailing
Example
"disallowTrailingWhitespace": true
Valid
var foo = "blah blah";
Invalid
var foo = "blah blah";
disallowTrailingComma
Disallows an extra comma following the final element of an array or object literal.
Type: Boolean
Values: true
JSHint: es3
Example
"disallowTrailingComma": true
Valid
var foo = [1, 2, 3];
var bar = {a: "a", b: "b"}
Invalid
var foo = [1, 2, 3, ];
var bar = {a: "a", b: "b", }
requireTrailingComma
Requires an extra comma following the final element of an array or object literal.
Type: Boolean
or Object
Values:
true
: validates all arrays and objectsObject
:
ignoreSingleValue
: allows single property objects and single element arrays to not require a trailing commaignoreSingleLine
: allows objects and arrays on a single line to not require a trailing comma
Example
"requireTrailingComma": true
Valid
var foo = [1, 2, 3,];
var bar = {a: "a", b: "b",}
Valid with ignoreSingleValue
var car = [1];
var dar = {a: "a"};
Valid with ignoreSingleLine
var car = [1, 2, 3];
var dar = {a: "a", b: "b"};
Invalid
var foo = [1, 2, 3];
var bar = {a: "a", b: "b"}
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
or Object
Values:
Integer
: lines should be at most the number of characters specifiedObject
:
value
: (required) lines should be at most the number of characters specifiedtabSize
: (default: 1
) considered the tab character as number of specified spacesallowComments
: (default: false
) allows comments to break the ruleallowUrlComments
: (default: false
) allows comments with long urls to break the ruleallowRegex
: (default: false
) allows regular expression literals to break the rule
JSHint: maxlen
Example
"maximumLineLength": 40
Valid
var aLineOf40Chars = 123456789012345678;
Invalid
var aLineOf41Chars = 1234567890123456789;
requireCapitalizedConstructors
Requires constructors to be capitalized (except for this
)
Type: Boolean
Values: true
JSHint: newcap
Example
"requireCapitalizedConstructors": true
Valid
var a = new B();
var c = new this();
Invalid
var d = new e();
requireDotNotation
Requires member expressions to use dot notation when possible
Type: Boolean
Values: true
JSHint: sub
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'];
Invalid
var a = b['c'];
requireYodaConditions
Requires the variable to be the right hand operator when doing a boolean comparison
Type: Boolean
Values: true
Example
"requireYodaConditions": true
Valid
if (1 == a) {
return
}
Invalid
if (a == 1) {
return
}
disallowYodaConditions
Requires the variable to be the left hand operator when doing a boolean comparison
Type: Boolean
Values: true
Example
"disallowYodaConditions": true
Valid
if (a == 1) {
return
}
Invalid
if (1 == a) {
return
}
Requires that a line comment (//
) be followed by a space.
Type: Boolean
or Object
or String
Values:
true
"allowSlash"
(deprecated use "except": ["/"]
) allows ///
formatObject
:
allExcept
: array of allowed strings before space //(here)
Example
"requireSpaceAfterLineComment": { "allExcept": ["#", "="] }
Valid
Invalid
Requires that a line comment (//
) not be followed by a space.
Type: Boolean
Values: true
Example
"disallowSpaceAfterLineComment": true
Valid
Invalid
disallowAnonymousFunctions
Requires that a function expression be named.
Type: Boolean
Values: true
Example
"disallowAnonymousFunctions": true
Valid
var a = function foo(){
};
$('#foo').click(function bar(){
};)
Invalid
var a = function(){
};
$('#foo').click(function(){
};)
requireAnonymousFunctions
Requires that a function expression be anonymous.
Type: Boolean
Values: true
Example
"requireAnonymousFunctions": true
Valid
var a = function(){
};
$('#foo').click(function(){
})
Invalid
var a = function foo(){
};
$('#foo').click(function bar(){
});
disallowFunctionDeclarations
Disallows function declarations.
Type: Boolean
Values: true
Example
"disallowFunctionDeclarations": true
Valid
var expressed = function() {
};
var expressed = function deeply() {
};
$('#foo').click(function bar() {
};)
Invalid
function stated() {
}
requireFunctionDeclarations
Requires function declarations by disallowing assignment of functions
expressions to variables. Function expressions are allowed in all other
contexts, including when passed as function arguments or immediately invoked.
Assignment of function expressions to object members is also permitted, since
these can't be declared.
Type: Boolean
Values: true
Example
"requireFunctionDeclarations": true
Valid
function declared() {
};
(function iife() {
void 0;
})();
var obj = {
a: function () {}
};
obj.b = function () { };
$('#foo').click(function bar() {
};)
Invalid
var expressed = function() {
};
var expressed = function deeply() {
};
disallowNewlineBeforeBlockStatements
Disallows newline before opening curly brace of all block statements.
Type: Boolean
Values: true
Example
"disallowNewlineBeforeBlockStatements": true
Valid
function good(){
var obj = {
val: true
};
return {
data: obj
};
}
if (cond){
foo();
}
for (var e in elements){
bar(e);
}
while (cond){
foo();
}
Invalid
function bad()
{
var obj =
{
val: true
};
return {
data: obj
};
}
if (cond)
{
foo();
}
for (var e in elements)
{
bar(e);
}
while (cond)
{
foo();
}
requireNewlineBeforeBlockStatements
Requires newline before opening curly brace of all block statements.
Type: Boolean
Values: true
Example
"requireNewlineBeforeBlockStatements": true
Valid
function good()
{
var obj =
{
val: true
};
return {
data: obj
};
}
if (cond)
{
foo();
}
for (var e in elements)
{
bar(e);
}
while (cond)
{
foo();
}
Invalid
function bad(){
var obj = {
val: true
};
return {
data: obj
};
}
if (cond){
foo();
}
for (var e in elements){
bar(e);
}
while (cond){
foo();
}
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
or Object
Values:
"\""
: all strings require double quotes"'"
: all strings require single quotestrue
: all strings require the quote mark first encountered in the source codeObject
:
escape
: allow the "other" quote mark to be used, but only to avoid having to escapemark
: the same effect as the non-object values
JSHint: quotmark
Example
"validateQuoteMarks": "\""
"validateQuoteMarks": { "mark": "\"", "escape": true }
Valid example for mode { "mark": "\"", "escape": true }
var x = "x";
var y = '"x"';
Invalid example for mode { "mark": "\"", "escape": true }
var x = "x";
var y = 'x';
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 switch statements and block statements
Type: Integer
or String
Values: A positive integer or "\t"
JSHint: indent
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;
}
}
validateParameterSeparator
Enable validation of separators between function parameters. Will ignore newlines.
Type: String
Values:
","
: function parameters are immediately followed by a comma", "
: function parameters are immediately followed by a comma and then a space" ,"
: function parameters are immediately followed by a space and then a comma" , "
: function parameters are immediately followed by a space, a comma, and then a space
Example
"validateParameterSeparator": ", "
Valid
function a(b, c) {}
Invalid
function a(b , c) {}
Requires the first alphabetical character of a comment to be uppercase, unless it is part of a multi-line textblock.
Type: Boolean
Value: true
Example
requireCapitalizedComments: true
Valid:
// Valid
//Valid
/*
Valid
*/
/**
* Valid
*/
// A textblock is a set of lines
// that starts with a capitalized letter
// and has one or more non-capitalized lines
// afterwards
// A textblock may also have multiple lines.
// Those lines can be uppercase as well to support
// sentense breaks in textblocks
// 123 or any non-alphabetical starting character
// @are also valid anywhere
Invalid:
// invalid
//invalid
/** invalid */
/**
* invalid
*/
Requires the first alphabetical character of a comment to be lowercase.
Type: String
Value: true
Example
disallowCapitalizedComments: true
Valid:
// valid
//valid
/*
valid
*/
/**
* valid
*/
// 123 or any non-alphabetical starting character
Invalid:
// Invalid
//Invalid
/** Invalid */
/**
* Invalid
*/
validateJSDoc
Please use the JSCS-JSDoc plugin instead.
safeContextKeyword
Option to check var that = this
expressions
Type: Array
or String
Values: String value used for context local declaration
Example
"safeContextKeyword": ["that"]
Valid
var that = this;
Invalid
var _this = this;
Removed Rules
disallowLeftStickedOperators
Use the following rules instead:
- requireSpaceBeforeBinaryOperators
- requireSpaceBeforePostfixUnaryOperators
- requireSpacesInConditionalExpression
disallowRightStickedOperators
Use the following rules instead:
- requireSpaceAfterBinaryOperators
- requireSpaceAfterPrefixUnaryOperators
- requireSpacesInConditionalExpression
requireLeftStickedOperators
Use the following rules instead:
- disallowSpaceBeforeBinaryOperators
- disallowSpaceBeforePostfixUnaryOperators
- disallowSpacesInConditionalExpression
requireRightStickedOperators
Use the following rules instead:
- disallowSpaceAfterBinaryOperators
- disallowSpaceAfterPrefixUnaryOperators
- disallowSpacesInConditionalExpression
Browser Usage
The npm package contains a file named jscs-browser.js
(since version 1.5.7
), which is a browser compatible version of jscs
.
If you'd like to generate this file yourself, run npm run browserify
after cloning this repo.
Use jscs-browser.js
on your page as follows:
<script src="jscs-browser.js"></script>
<script>
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>