Showing 364 of 364 total issues
There must be a space inside this paren. Open
allSelected = allSelected.filter((v, i, a) => a.indexOf(v) === i);
- Read upRead up
- Exclude checks
Disallow or enforce spaces inside of parentheses (space-in-parens)
Some style guides require or disallow spaces inside of parentheses:
foo( 'bar' );
var x = ( 1 + 2 ) * 3;
foo('bar');
var x = (1 + 2) * 3;
Rule Details
This rule will enforce consistency of spacing directly inside of parentheses, by disallowing or requiring one or more spaces to the right of (
and to the left of )
. In either case, ()
will still be allowed.
Options
There are two options for this rule:
-
"never"
(default) enforces zero spaces inside of parentheses -
"always"
enforces a space inside of parentheses
Depending on your coding conventions, you can choose either option by specifying it in your configuration:
"space-in-parens": ["error", "always"]
"never"
Examples of incorrect code for this rule with the default "never"
option:
/*eslint space-in-parens: ["error", "never"]*/
foo( 'bar');
foo('bar' );
foo( 'bar' );
var foo = ( 1 + 2 ) * 3;
( function () { return 'bar'; }() );
Examples of correct code for this rule with the default "never"
option:
/*eslint space-in-parens: ["error", "never"]*/
foo();
foo('bar');
var foo = (1 + 2) * 3;
(function () { return 'bar'; }());
"always"
Examples of incorrect code for this rule with the "always"
option:
/*eslint space-in-parens: ["error", "always"]*/
foo( 'bar');
foo('bar' );
foo('bar');
var foo = (1 + 2) * 3;
(function () { return 'bar'; }());
Examples of correct code for this rule with the "always"
option:
/*eslint space-in-parens: ["error", "always"]*/
foo();
foo( 'bar' );
var foo = ( 1 + 2 ) * 3;
( function () { return 'bar'; }() );
Exceptions
An object literal may be used as a third array item to specify exceptions, with the key "exceptions"
and an array as the value. These exceptions work in the context of the first option. That is, if "always"
is set to enforce spacing, then any "exception" will disallow spacing. Conversely, if "never"
is set to disallow spacing, then any "exception" will enforce spacing.
The following exceptions are available: ["{}", "[]", "()", "empty"]
.
Examples of incorrect code for this rule with the "never", { "exceptions": ["{}"] }
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["{}"] }]*/
foo({bar: 'baz'});
foo(1, {bar: 'baz'});
Examples of correct code for this rule with the "never", { "exceptions": ["{}"] }
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["{}"] }]*/
foo( {bar: 'baz'} );
foo(1, {bar: 'baz'} );
Examples of incorrect code for this rule with the "always", { "exceptions": ["{}"] }
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["{}"] }]*/
foo( {bar: 'baz'} );
foo( 1, {bar: 'baz'} );
Examples of correct code for this rule with the "always", { "exceptions": ["{}"] }
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["{}"] }]*/
foo({bar: 'baz'});
foo( 1, {bar: 'baz'});
Examples of incorrect code for this rule with the "never", { "exceptions": ["[]"] }
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["[]"] }]*/
foo([bar, baz]);
foo([bar, baz], 1);
Examples of correct code for this rule with the "never", { "exceptions": ["[]"] }
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["[]"] }]*/
foo( [bar, baz] );
foo( [bar, baz], 1);
Examples of incorrect code for this rule with the "always", { "exceptions": ["[]"] }
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["[]"] }]*/
foo( [bar, baz] );
foo( [bar, baz], 1 );
Examples of correct code for this rule with the "always", { "exceptions": ["[]"] }
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["[]"] }]*/
foo([bar, baz]);
foo([bar, baz], 1 );
Examples of incorrect code for this rule with the "never", { "exceptions": ["()"] }]
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["()"] }]*/
foo((1 + 2));
foo((1 + 2), 1);
Examples of correct code for this rule with the "never", { "exceptions": ["()"] }]
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["()"] }]*/
foo( (1 + 2) );
foo( (1 + 2), 1);
Examples of incorrect code for this rule with the "always", { "exceptions": ["()"] }]
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["()"] }]*/
foo( ( 1 + 2 ) );
foo( ( 1 + 2 ), 1 );
Examples of correct code for this rule with the "always", { "exceptions": ["()"] }]
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["()"] }]*/
foo(( 1 + 2 ));
foo(( 1 + 2 ), 1 );
The "empty"
exception concerns empty parentheses, and works the same way as the other exceptions, inverting the first option.
Example of incorrect code for this rule with the "never", { "exceptions": ["empty"] }]
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["empty"] }]*/
foo();
Example of correct code for this rule with the "never", { "exceptions": ["empty"] }]
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["empty"] }]*/
foo( );
Example of incorrect code for this rule with the "always", { "exceptions": ["empty"] }]
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["empty"] }]*/
foo( );
Example of correct code for this rule with the "always", { "exceptions": ["empty"] }]
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["empty"] }]*/
foo();
You can include multiple entries in the "exceptions"
array.
Examples of incorrect code for this rule with the "always", { "exceptions": ["{}", "[]"] }]
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["{}", "[]"] }]*/
bar( {bar:'baz'} );
baz( 1, [1,2] );
foo( {bar: 'baz'}, [1, 2] );
Examples of correct code for this rule with the "always", { "exceptions": ["{}", "[]"] }]
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["{}", "[]"] }]*/
bar({bar:'baz'});
baz( 1, [1,2]);
foo({bar: 'baz'}, [1, 2]);
When Not To Use It
You can turn this rule off if you are not concerned with the consistency of spacing between parentheses.
Related Rules
- [space-in-brackets](space-in-brackets.md) (deprecated) Source: http://eslint.org/docs/rules/
Missing JSDoc for parameter 'button'. Open
/**
- Read upRead up
- Exclude checks
enforce valid JSDoc comments (valid-jsdoc)
JSDoc generates application programming interface (API) documentation from specially-formatted comments in JavaScript code. For example, this is a JSDoc comment for a function:
/**
* Add two numbers.
* @param {number} num1 The first number.
* @param {number} num2 The second number.
* @returns {number} The sum of the two numbers.
*/
function add(num1, num2) {
return num1 + num2;
}
If comments are invalid because of typing mistakes, then documentation will be incomplete.
If comments are inconsistent because they are not updated when function definitions are modified, then readers might become confused.
Rule Details
This rule enforces valid and consistent JSDoc comments. It reports any of the following problems:
- missing parameter tag:
@arg
,@argument
, or@param
- inconsistent order of parameter names in a comment compared to the function or method
- missing return tag:
@return
or@returns
- missing parameter or return type
- missing parameter or return description
- syntax error
This rule does not report missing JSDoc comments for classes, functions, or methods.
Note: This rule does not support all of the Google Closure documentation tool's use cases. As such, some code such as (/**number*/ n => n * 2);
will be flagged as missing appropriate function JSDoc comments even though /**number*/
is intended to be a type hint and not a documentation block for the function. We don't recommend using this rule if you use type hints in this way.
Examples of incorrect code for this rule:
/*eslint valid-jsdoc: "error"*/
// expected @param tag for parameter num1 but found num instead
// missing @param tag for parameter num2
// missing return type
/**
* Add two numbers.
* @param {number} num The first number.
* @returns The sum of the two numbers.
*/
function add(num1, num2) {
return num1 + num2;
}
// missing brace
// missing @returns tag
/**
* @param {string name Whom to greet.
*/
function greet(name) {
console.log("Hello " + name);
}
// missing parameter type for num1
// missing parameter description for num2
/**
* Represents a sum.
* @constructor
* @param num1 The first number.
* @param {number} num2
*/
function sum(num1, num2) {
this.num1 = num1;
this.num2 = num2;
}
Examples of correct code for this rule:
/*eslint valid-jsdoc: "error"*/
/*eslint-env es6*/
/**
* Add two numbers.
* @param {number} num1 The first number.
* @param {number} num2 The second number.
* @returns {number} The sum of the two numbers.
*/
function add(num1, num2) {
return num1 + num2;
}
// default options allow missing function description
// return type `void` means the function has no `return` statement
/**
* @param {string} name Whom to greet.
* @returns {void}
*/
function greet(name) {
console.log("Hello " + name);
}
// @constructor tag allows missing @returns tag
/**
* Represents a sum.
* @constructor
* @param {number} num1 The first number.
* @param {number} num2 The second number.
*/
function sum(num1, num2) {
this.num1 = num1;
this.num2 = num2;
}
// class constructor allows missing @returns tag
/**
* Represents a sum.
*/
class Sum {
/**
* @param {number} num1 The first number.
* @param {number} num2 The second number.
*/
constructor(num1, num2) {
this.num1 = num1;
this.num2 = num2;
}
}
// @abstract tag allows @returns tag without `return` statement
class Widget {
/**
* When the state changes, does it affect the rendered appearance?
* @abstract
* @param {Object} state The new state of the widget.
* @returns {boolean} Is current appearance inconsistent with new state?
*/
mustRender (state) {
throw new Error("Widget subclass did not implement mustRender");
}
}
// @override tag allows missing @param and @returns tags
class WonderfulWidget extends Widget {
/**
* @override
*/
mustRender (state) {
return state !== this.state; // shallow comparison
}
}
Options
This rule has an object option:
-
"prefer"
enforces consistent documentation tags specified by an object whose properties mean instead of key use value (for example,"return": "returns"
means instead of@return
use@returns
) -
"preferType"
enforces consistent type strings specified by an object whose properties mean instead of key use value (for example,"object": "Object"
means instead ofobject
useObject
) -
"requireReturn"
requires a return tag:-
true
(default) even if the function or method does not have areturn
statement (this option value does not apply to constructors) -
false
if and only if the function or method has areturn
statement (this option value does apply to constructors)
-
-
"requireReturnType": false
allows missing type in return tags -
"matchDescription"
specifies (as a string) a regular expression to match the description in each JSDoc comment (for example,".+"
requires a description; this option does not apply to descriptions in parameter or return tags) -
"requireParamDescription": false
allows missing description in parameter tags -
"requireReturnDescription": false
allows missing description in return tags
prefer
Examples of additional incorrect code for this rule with sample "prefer": { "arg": "param", "argument": "param", "class": "constructor", "return": "returns", "virtual": "abstract" }
options:
/*eslint valid-jsdoc: ["error", { "prefer": { "arg": "param", "argument": "param", "class": "constructor", "return": "returns", "virtual": "abstract" } }]*/
/*eslint-env es6*/
/**
* Add two numbers.
* @arg {int} num1 The first number.
* @arg {int} num2 The second number.
* @return {int} The sum of the two numbers.
*/
function add(num1, num2) {
return num1 + num2;
}
/**
* Represents a sum.
* @class
* @argument {number} num1 The first number.
* @argument {number} num2 The second number.
*/
function sum(num1, num2) {
this.num1 = num1;
this.num2 = num2;
}
class Widget {
/**
* When the state changes, does it affect the rendered appearance?
* @virtual
* @argument {Object} state The new state of the widget.
* @return {boolean} Is current appearance inconsistent with new state?
*/
mustRender (state) {
throw new Error("Widget subclass did not implement mustRender");
}
}
preferType
Examples of additional incorrect code for this rule with sample "preferType": { "Boolean": "boolean", "Number": "number", "object": "Object", "String": "string" }
options:
/*eslint valid-jsdoc: ["error", { "preferType": { "Boolean": "boolean", "Number": "number", "object": "Object", "String": "string" } }]*/
/*eslint-env es6*/
/**
* Add two numbers.
* @param {Number} num1 The first number.
* @param {Number} num2 The second number.
* @returns {Number} The sum of the two numbers.
*/
function add(num1, num2) {
return num1 + num2;
}
/**
* Output a greeting as a side effect.
* @param {String} name Whom to greet.
* @returns {void}
*/
function greet(name) {
console.log("Hello " + name);
}
class Widget {
/**
* When the state changes, does it affect the rendered appearance?
* @abstract
* @param {object} state The new state of the widget.
* @returns {Boolean} Is current appearance inconsistent with new state?
*/
mustRender (state) {
throw new Error("Widget subclass did not implement mustRender");
}
}
requireReturn
Examples of additional incorrect code for this rule with the "requireReturn": false
option:
/*eslint valid-jsdoc: ["error", { "requireReturn": false }]*/
// unexpected @returns tag because function has no `return` statement
/**
* @param {string} name Whom to greet.
* @returns {string} The greeting.
*/
function greet(name) {
console.log("Hello " + name);
}
// add @abstract tag to allow @returns tag without `return` statement
class Widget {
/**
* When the state changes, does it affect the rendered appearance?
* @param {Object} state The new state of the widget.
* @returns {boolean} Is current appearance inconsistent with new state?
*/
mustRender (state) {
throw new Error("Widget subclass did not implement mustRender");
}
}
Example of additional correct code for this rule with the "requireReturn": false
option:
/*eslint valid-jsdoc: ["error", { "requireReturn": false }]*/
/**
* @param {string} name Whom to greet.
*/
function greet(name) {
console.log("Hello " + name);
}
requireReturnType
Example of additional correct code for this rule with the "requireReturnType": false
option:
/*eslint valid-jsdoc: ["error", { "requireReturnType": false }]*/
/**
* Add two numbers.
* @param {number} num1 The first number.
* @param {number} num2 The second number.
* @returns The sum of the two numbers.
*/
function add(num1, num2) {
return num1 + num2;
}
matchDescription
Example of additional incorrect code for this rule with a sample "matchDescription": ".+"
option:
/*eslint valid-jsdoc: ["error", { "matchDescription": ".+" }]*/
// missing function description
/**
* @param {string} name Whom to greet.
* @returns {void}
*/
function greet(name) {
console.log("Hello " + name);
}
requireParamDescription
Example of additional correct code for this rule with the "requireParamDescription": false
option:
/*eslint valid-jsdoc: ["error", { "requireParamDescription": false }]*/
/**
* Add two numbers.
* @param {int} num1
* @param {int} num2
* @returns {int} The sum of the two numbers.
*/
function add(num1, num2) {
return num1 + num2;
}
requireReturnDescription
Example of additional correct code for this rule with the "requireReturnDescription": false
option:
/*eslint valid-jsdoc: ["error", { "requireReturnDescription": false }]*/
/**
* Add two numbers.
* @param {number} num1 The first number.
* @param {number} num2 The second number.
* @returns {number}
*/
function add(num1, num2) {
return num1 + num2;
}
When Not To Use It
If you aren't using JSDoc, then you can safely turn this rule off.
Further Reading
Related Rules
- [require-jsdoc](require-jsdoc.md) Source: http://eslint.org/docs/rules/
Expected line before comment. Open
// load available options if the Salesforce object changes
- Read upRead up
- Exclude checks
require empty lines around comments (lines-around-comment)
Many style guides require empty lines before or after comments. The primary goal of these rules is to make the comments easier to read and improve readability of the code.
Rule Details
This rule requires empty lines before and/or after comments. It can be enabled separately for both block (/*
) and line (//
) comments. This rule does not apply to comments that appear on the same line as code and does not require empty lines at the beginning or end of a file.
Options
This rule has an object option:
-
"beforeBlockComment": true
(default) requires an empty line before block comments -
"afterBlockComment": true
requires an empty line after block comments -
"beforeLineComment": true
requires an empty line before line comments -
"afterLineComment": true
requires an empty line after line comments -
"allowBlockStart": true
allows comments to appear at the start of block statements -
"allowBlockEnd": true
allows comments to appear at the end of block statements -
"allowObjectStart": true
allows comments to appear at the start of object literals -
"allowObjectEnd": true
allows comments to appear at the end of object literals -
"allowArrayStart": true
allows comments to appear at the start of array literals -
"allowArrayEnd": true
allows comments to appear at the end of array literals -
"applyDefaultIgnorePatterns"
enables or disables the default comment patterns to be ignored by the rule -
"ignorePattern"
custom patterns to be ignored by the rule
beforeBlockComment
Examples of incorrect code for this rule with the default { "beforeBlockComment": true }
option:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/
var night = "long";
/* what a great and wonderful day */
var day = "great"
Examples of correct code for this rule with the default { "beforeBlockComment": true }
option:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/
var night = "long";
/* what a great and wonderful day */
var day = "great"
afterBlockComment
Examples of incorrect code for this rule with the { "afterBlockComment": true }
option:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true }]*/
var night = "long";
/* what a great and wonderful day */
var day = "great"
Examples of correct code for this rule with the { "afterBlockComment": true }
option:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true }]*/
var night = "long";
/* what a great and wonderful day */
var day = "great"
beforeLineComment
Examples of incorrect code for this rule with the { "beforeLineComment": true }
option:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true }]*/
var night = "long";
// what a great and wonderful day
var day = "great"
Examples of correct code for this rule with the { "beforeLineComment": true }
option:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true }]*/
var night = "long";
// what a great and wonderful day
var day = "great"
afterLineComment
Examples of incorrect code for this rule with the { "afterLineComment": true }
option:
/*eslint lines-around-comment: ["error", { "afterLineComment": true }]*/
var night = "long";
// what a great and wonderful day
var day = "great"
Examples of correct code for this rule with the { "afterLineComment": true }
option:
/*eslint lines-around-comment: ["error", { "afterLineComment": true }]*/
var night = "long";
// what a great and wonderful day
var day = "great"
allowBlockStart
Examples of correct code for this rule with the { "beforeLineComment": true, "allowBlockStart": true }
options:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowBlockStart": true }]*/
function foo(){
// what a great and wonderful day
var day = "great"
return day;
}
Examples of correct code for this rule with the { "beforeBlockComment": true, "allowBlockStart": true }
options:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowBlockStart": true }]*/
function foo(){
/* what a great and wonderful day */
var day = "great"
return day;
}
allowBlockEnd
Examples of correct code for this rule with the { "afterLineComment": true, "allowBlockEnd": true }
option:
/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowBlockEnd": true }]*/
function foo(){
var day = "great"
return day;
// what a great and wonderful day
}
Examples of correct code for this rule with the { "afterBlockComment": true, "allowBlockEnd": true }
option:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowBlockEnd": true }]*/
function foo(){
var day = "great"
return day;
/* what a great and wonderful day */
}
allowObjectStart
Examples of correct code for this rule with the { "beforeLineComment": true, "allowObjectStart": true }
option:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowObjectStart": true }]*/
var foo = {
// what a great and wonderful day
day: "great"
};
const {
// what a great and wonderful day
foo: someDay
} = {foo: "great"};
const {
// what a great and wonderful day
day
} = {day: "great"};
Examples of correct code for this rule with the { "beforeBlockComment": true, "allowObjectStart": true }
option:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowObjectStart": true }]*/
var foo = {
/* what a great and wonderful day */
day: "great"
};
const {
/* what a great and wonderful day */
foo: someDay
} = {foo: "great"};
const {
/* what a great and wonderful day */
day
} = {day: "great"};
allowObjectEnd
Examples of correct code for this rule with the { "afterLineComment": true, "allowObjectEnd": true }
option:
/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowObjectEnd": true }]*/
var foo = {
day: "great"
// what a great and wonderful day
};
const {
foo: someDay
// what a great and wonderful day
} = {foo: "great"};
const {
day
// what a great and wonderful day
} = {day: "great"};
Examples of correct code for this rule with the { "afterBlockComment": true, "allowObjectEnd": true }
option:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowObjectEnd": true }]*/
var foo = {
day: "great"
/* what a great and wonderful day */
};
const {
foo: someDay
/* what a great and wonderful day */
} = {foo: "great"};
const {
day
/* what a great and wonderful day */
} = {day: "great"};
allowArrayStart
Examples of correct code for this rule with the { "beforeLineComment": true, "allowArrayStart": true }
option:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowArrayStart": true }]*/
var day = [
// what a great and wonderful day
"great",
"wonderful"
];
const [
// what a great and wonderful day
someDay
] = ["great", "not great"];
Examples of correct code for this rule with the { "beforeBlockComment": true, "allowArrayStart": true }
option:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowArrayStart": true }]*/
var day = [
/* what a great and wonderful day */
"great",
"wonderful"
];
const [
/* what a great and wonderful day */
someDay
] = ["great", "not great"];
allowArrayEnd
Examples of correct code for this rule with the { "afterLineComment": true, "allowArrayEnd": true }
option:
/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowArrayEnd": true }]*/
var day = [
"great",
"wonderful"
// what a great and wonderful day
];
const [
someDay
// what a great and wonderful day
] = ["great", "not great"];
Examples of correct code for this rule with the { "afterBlockComment": true, "allowArrayEnd": true }
option:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowArrayEnd": true }]*/
var day = [
"great",
"wonderful"
/* what a great and wonderful day */
];
const [
someDay
/* what a great and wonderful day */
] = ["great", "not great"];
ignorePattern
By default this rule ignores comments starting with the following words: eslint
, jshint
, jslint
, istanbul
, global
, exported
, jscs
. An alternative regular expression can be provided.
Examples of correct code for the ignorePattern
option:
/*eslint lines-around-comment: ["error"]*/
foo();
/* eslint mentioned in this comment */,
bar();
/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */
foo();
/* a valid comment using pragma in it */
Examples of incorrect code for the ignorePattern
option:
/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */
1 + 1;
/* something else */
applyDefaultIgnorePatterns
Default ignore patterns are applied even when ignorePattern
is provided. If you want to omit default patterns, set this option to false
.
Examples of correct code for the { "applyDefaultIgnorePatterns": false }
option:
/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma", applyDefaultIgnorePatterns: false }] */
foo();
/* a valid comment using pragma in it */
Examples of incorrect code for the { "applyDefaultIgnorePatterns": false }
option:
/*eslint lines-around-comment: ["error", { "applyDefaultIgnorePatterns": false }] */
foo();
/* eslint mentioned in comment */
When Not To Use It
Many people enjoy a terser code style and don't mind comments bumping up against code. If you fall into that category this rule is not for you.
Related Rules
- [space-before-blocks](space-before-blocks.md)
- [spaced-comment](spaced-comment.md) Source: http://eslint.org/docs/rules/
There must be a space inside this paren. Open
allSelected = allSelected.filter((v, i, a) => a.indexOf(v) === i);
- Read upRead up
- Exclude checks
Disallow or enforce spaces inside of parentheses (space-in-parens)
Some style guides require or disallow spaces inside of parentheses:
foo( 'bar' );
var x = ( 1 + 2 ) * 3;
foo('bar');
var x = (1 + 2) * 3;
Rule Details
This rule will enforce consistency of spacing directly inside of parentheses, by disallowing or requiring one or more spaces to the right of (
and to the left of )
. In either case, ()
will still be allowed.
Options
There are two options for this rule:
-
"never"
(default) enforces zero spaces inside of parentheses -
"always"
enforces a space inside of parentheses
Depending on your coding conventions, you can choose either option by specifying it in your configuration:
"space-in-parens": ["error", "always"]
"never"
Examples of incorrect code for this rule with the default "never"
option:
/*eslint space-in-parens: ["error", "never"]*/
foo( 'bar');
foo('bar' );
foo( 'bar' );
var foo = ( 1 + 2 ) * 3;
( function () { return 'bar'; }() );
Examples of correct code for this rule with the default "never"
option:
/*eslint space-in-parens: ["error", "never"]*/
foo();
foo('bar');
var foo = (1 + 2) * 3;
(function () { return 'bar'; }());
"always"
Examples of incorrect code for this rule with the "always"
option:
/*eslint space-in-parens: ["error", "always"]*/
foo( 'bar');
foo('bar' );
foo('bar');
var foo = (1 + 2) * 3;
(function () { return 'bar'; }());
Examples of correct code for this rule with the "always"
option:
/*eslint space-in-parens: ["error", "always"]*/
foo();
foo( 'bar' );
var foo = ( 1 + 2 ) * 3;
( function () { return 'bar'; }() );
Exceptions
An object literal may be used as a third array item to specify exceptions, with the key "exceptions"
and an array as the value. These exceptions work in the context of the first option. That is, if "always"
is set to enforce spacing, then any "exception" will disallow spacing. Conversely, if "never"
is set to disallow spacing, then any "exception" will enforce spacing.
The following exceptions are available: ["{}", "[]", "()", "empty"]
.
Examples of incorrect code for this rule with the "never", { "exceptions": ["{}"] }
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["{}"] }]*/
foo({bar: 'baz'});
foo(1, {bar: 'baz'});
Examples of correct code for this rule with the "never", { "exceptions": ["{}"] }
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["{}"] }]*/
foo( {bar: 'baz'} );
foo(1, {bar: 'baz'} );
Examples of incorrect code for this rule with the "always", { "exceptions": ["{}"] }
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["{}"] }]*/
foo( {bar: 'baz'} );
foo( 1, {bar: 'baz'} );
Examples of correct code for this rule with the "always", { "exceptions": ["{}"] }
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["{}"] }]*/
foo({bar: 'baz'});
foo( 1, {bar: 'baz'});
Examples of incorrect code for this rule with the "never", { "exceptions": ["[]"] }
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["[]"] }]*/
foo([bar, baz]);
foo([bar, baz], 1);
Examples of correct code for this rule with the "never", { "exceptions": ["[]"] }
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["[]"] }]*/
foo( [bar, baz] );
foo( [bar, baz], 1);
Examples of incorrect code for this rule with the "always", { "exceptions": ["[]"] }
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["[]"] }]*/
foo( [bar, baz] );
foo( [bar, baz], 1 );
Examples of correct code for this rule with the "always", { "exceptions": ["[]"] }
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["[]"] }]*/
foo([bar, baz]);
foo([bar, baz], 1 );
Examples of incorrect code for this rule with the "never", { "exceptions": ["()"] }]
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["()"] }]*/
foo((1 + 2));
foo((1 + 2), 1);
Examples of correct code for this rule with the "never", { "exceptions": ["()"] }]
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["()"] }]*/
foo( (1 + 2) );
foo( (1 + 2), 1);
Examples of incorrect code for this rule with the "always", { "exceptions": ["()"] }]
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["()"] }]*/
foo( ( 1 + 2 ) );
foo( ( 1 + 2 ), 1 );
Examples of correct code for this rule with the "always", { "exceptions": ["()"] }]
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["()"] }]*/
foo(( 1 + 2 ));
foo(( 1 + 2 ), 1 );
The "empty"
exception concerns empty parentheses, and works the same way as the other exceptions, inverting the first option.
Example of incorrect code for this rule with the "never", { "exceptions": ["empty"] }]
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["empty"] }]*/
foo();
Example of correct code for this rule with the "never", { "exceptions": ["empty"] }]
option:
/*eslint space-in-parens: ["error", "never", { "exceptions": ["empty"] }]*/
foo( );
Example of incorrect code for this rule with the "always", { "exceptions": ["empty"] }]
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["empty"] }]*/
foo( );
Example of correct code for this rule with the "always", { "exceptions": ["empty"] }]
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["empty"] }]*/
foo();
You can include multiple entries in the "exceptions"
array.
Examples of incorrect code for this rule with the "always", { "exceptions": ["{}", "[]"] }]
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["{}", "[]"] }]*/
bar( {bar:'baz'} );
baz( 1, [1,2] );
foo( {bar: 'baz'}, [1, 2] );
Examples of correct code for this rule with the "always", { "exceptions": ["{}", "[]"] }]
option:
/*eslint space-in-parens: ["error", "always", { "exceptions": ["{}", "[]"] }]*/
bar({bar:'baz'});
baz( 1, [1,2]);
foo({bar: 'baz'}, [1, 2]);
When Not To Use It
You can turn this rule off if you are not concerned with the consistency of spacing between parentheses.
Related Rules
- [space-in-brackets](space-in-brackets.md) (deprecated) Source: http://eslint.org/docs/rules/