Unexpected function expression. Open
$(function() {
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Require using arrow functions for callbacks (prefer-arrow-callback)
Arrow functions can be an attractive alternative to function expressions for callbacks or function arguments.
For example, arrow functions are automatically bound to their surrounding scope/context. This provides an alternative to the pre-ES6 standard of explicitly binding function expressions to achieve similar behavior.
Additionally, arrow functions are:
less verbose, and easier to reason about.
bound lexically regardless of where or when they are invoked.
Rule Details
This rule locates function expressions used as callbacks or function arguments. An error will be produced for any that could be replaced by an arrow function without changing the result.
The following examples will be flagged:
/* eslint prefer-arrow-callback: "error" */
foo(function(a) { return a; }); // ERROR
// prefer: foo(a => a)
foo(function() { return this.a; }.bind(this)); // ERROR
// prefer: foo(() => this.a)
Instances where an arrow function would not produce identical results will be ignored.
The following examples will not be flagged:
/* eslint prefer-arrow-callback: "error" */
/* eslint-env es6 */
// arrow function callback
foo(a => a); // OK
// generator as callback
foo(function*() { yield; }); // OK
// function expression not used as callback or function argument
var foo = function foo(a) { return a; }; // OK
// unbound function expression callback
foo(function() { return this.a; }); // OK
// recursive named function callback
foo(function bar(n) { return n && n + bar(n - 1); }); // OK
Options
Access further control over this rule's behavior via an options object.
Default: { allowNamedFunctions: false, allowUnboundThis: true }
allowNamedFunctions
By default { "allowNamedFunctions": false }
, this boolean
option prohibits using named functions as callbacks or function arguments.
Changing this value to true
will reverse this option's behavior by allowing use of named functions without restriction.
{ "allowNamedFunctions": true }
will not flag the following example:
/* eslint prefer-arrow-callback: [ "error", { "allowNamedFunctions": true } ] */
foo(function bar() {});
allowUnboundThis
By default { "allowUnboundThis": true }
, this boolean
option allows function expressions containing this
to be used as callbacks, as long as the function in question has not been explicitly bound.
When set to false
this option prohibits the use of function expressions as callbacks or function arguments entirely, without exception.
{ "allowUnboundThis": false }
will flag the following examples:
/* eslint prefer-arrow-callback: [ "error", { "allowUnboundThis": false } ] */
/* eslint-env es6 */
foo(function() { this.a; });
foo(function() { (() => this); });
someArray.map(function(itm) { return this.doSomething(itm); }, someObject);
When Not To Use It
In environments that have not yet adopted ES6 language features (ES3/5).
In ES6+ environments that allow the use of function expressions when describing callbacks or function arguments.
Further Reading
Use the rest parameters instead of 'arguments'. Open
return '\u00BB' + i18n.gettext.apply(i18n, arguments) + '\u00AB';
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Suggest using the rest parameters instead of arguments
(prefer-rest-params)
There are rest parameters in ES2015.
We can use that feature for variadic functions instead of the arguments
variable.
arguments
does not have methods of Array.prototype
, so it's a bit of an inconvenience.
Rule Details
This rule is aimed to flag usage of arguments
variables.
Examples
Examples of incorrect code for this rule:
function foo() {
console.log(arguments);
}
function foo(action) {
var args = Array.prototype.slice.call(arguments, 1);
action.apply(null, args);
}
function foo(action) {
var args = [].slice.call(arguments, 1);
action.apply(null, args);
}
Examples of correct code for this rule:
function foo(...args) {
console.log(args);
}
function foo(action, ...args) {
action.apply(null, args); // or `action(...args)`, related to the `prefer-spread` rule.
}
// Note: the implicit arguments can be overwritten.
function foo(arguments) {
console.log(arguments); // This is the first argument.
}
function foo() {
var arguments = 0;
console.log(arguments); // This is a local variable.
}
When Not To Use It
This rule should not be used in ES3/5 environments.
In ES2015 (ES6) or later, if you don't want to be notified about arguments
variables, then it's safe to disable this rule.
Related Rules
- [prefer-spread](prefer-spread.md) Source: http://eslint.org/docs/rules/
Unexpected string concatenation. Open
return '\u00BB' + i18n.gettext.apply(i18n, arguments) + '\u00AB';
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Suggest using template literals instead of string concatenation. (prefer-template)
In ES2015 (ES6), we can use template literals instead of string concatenation.
var str = "Hello, " + name + "!";
/*eslint-env es6*/
var str = `Hello, ${name}!`;
Rule Details
This rule is aimed to flag usage of +
operators with strings.
Examples
Examples of incorrect code for this rule:
/*eslint prefer-template: "error"*/
var str = "Hello, " + name + "!";
var str = "Time: " + (12 * 60 * 60 * 1000);
Examples of correct code for this rule:
/*eslint prefer-template: "error"*/
/*eslint-env es6*/
var str = "Hello World!";
var str = `Hello, ${name}!`;
var str = `Time: ${12 * 60 * 60 * 1000}`;
// This is reported by `no-useless-concat`.
var str = "Hello, " + "World!";
When Not To Use It
This rule should not be used in ES3/5 environments.
In ES2015 (ES6) or later, if you don't want to be notified about string concatenation, you can safely disable this rule.
Related Rules
- [no-useless-concat](no-useless-concat.md)
- [quotes](quotes.md) Source: http://eslint.org/docs/rules/
Use the spread operator instead of '.apply()'. Open
return '\u00BB' + i18n.ngettext.apply(i18n, arguments) + '\u00AB';
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Suggest using spread syntax instead of .apply()
. (prefer-spread)
Before ES2015, one must use Function.prototype.apply()
to call variadic functions.
var args = [1, 2, 3, 4];
Math.max.apply(Math, args);
In ES2015, one can use spread syntax to call variadic functions.
/*eslint-env es6*/
var args = [1, 2, 3, 4];
Math.max(...args);
Rule Details
This rule is aimed to flag usage of Function.prototype.apply()
in situations where spread syntax could be used instead.
Examples
Examples of incorrect code for this rule:
/*eslint prefer-spread: "error"*/
foo.apply(undefined, args);
foo.apply(null, args);
obj.foo.apply(obj, args);
Examples of correct code for this rule:
/*eslint prefer-spread: "error"*/
// Using spread syntax
foo(...args);
obj.foo(...args);
// The `this` binding is different.
foo.apply(obj, args);
obj.foo.apply(null, args);
obj.foo.apply(otherObj, args);
// The argument list is not variadic.
// Those are warned by the `no-useless-call` rule.
foo.apply(undefined, [1, 2, 3]);
foo.apply(null, [1, 2, 3]);
obj.foo.apply(obj, [1, 2, 3]);
Known limitations:
This rule analyzes code statically to check whether or not the this
argument is changed. So, if the this
argument is computed in a dynamic expression, this rule cannot detect a violation.
/*eslint prefer-spread: "error"*/
// This warns.
a[i++].foo.apply(a[i++], args);
// This does not warn.
a[++i].foo.apply(a[i], args);
When Not To Use It
This rule should not be used in ES3/5 environments.
In ES2015 (ES6) or later, if you don't want to be notified about Function.prototype.apply()
callings, you can safely disable this rule.
Related Rules
- [no-useless-call](no-useless-call.md) Source: http://eslint.org/docs/rules/
Unexpected string concatenation. Open
return '\u00BB' + i18n.ngettext.apply(i18n, arguments) + '\u00AB';
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Suggest using template literals instead of string concatenation. (prefer-template)
In ES2015 (ES6), we can use template literals instead of string concatenation.
var str = "Hello, " + name + "!";
/*eslint-env es6*/
var str = `Hello, ${name}!`;
Rule Details
This rule is aimed to flag usage of +
operators with strings.
Examples
Examples of incorrect code for this rule:
/*eslint prefer-template: "error"*/
var str = "Hello, " + name + "!";
var str = "Time: " + (12 * 60 * 60 * 1000);
Examples of correct code for this rule:
/*eslint prefer-template: "error"*/
/*eslint-env es6*/
var str = "Hello World!";
var str = `Hello, ${name}!`;
var str = `Time: ${12 * 60 * 60 * 1000}`;
// This is reported by `no-useless-concat`.
var str = "Hello, " + "World!";
When Not To Use It
This rule should not be used in ES3/5 environments.
In ES2015 (ES6) or later, if you don't want to be notified about string concatenation, you can safely disable this rule.
Related Rules
- [no-useless-concat](no-useless-concat.md)
- [quotes](quotes.md) Source: http://eslint.org/docs/rules/
Use the rest parameters instead of 'arguments'. Open
return '\u00BB' + i18n.ngettext.apply(i18n, arguments) + '\u00AB';
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Suggest using the rest parameters instead of arguments
(prefer-rest-params)
There are rest parameters in ES2015.
We can use that feature for variadic functions instead of the arguments
variable.
arguments
does not have methods of Array.prototype
, so it's a bit of an inconvenience.
Rule Details
This rule is aimed to flag usage of arguments
variables.
Examples
Examples of incorrect code for this rule:
function foo() {
console.log(arguments);
}
function foo(action) {
var args = Array.prototype.slice.call(arguments, 1);
action.apply(null, args);
}
function foo(action) {
var args = [].slice.call(arguments, 1);
action.apply(null, args);
}
Examples of correct code for this rule:
function foo(...args) {
console.log(args);
}
function foo(action, ...args) {
action.apply(null, args); // or `action(...args)`, related to the `prefer-spread` rule.
}
// Note: the implicit arguments can be overwritten.
function foo(arguments) {
console.log(arguments); // This is the first argument.
}
function foo() {
var arguments = 0;
console.log(arguments); // This is a local variable.
}
When Not To Use It
This rule should not be used in ES3/5 environments.
In ES2015 (ES6) or later, if you don't want to be notified about arguments
variables, then it's safe to disable this rule.
Related Rules
- [prefer-spread](prefer-spread.md) Source: http://eslint.org/docs/rules/
Use the spread operator instead of '.apply()'. Open
return '\u00BB' + i18n.gettext.apply(i18n, arguments) + '\u00AB';
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Suggest using spread syntax instead of .apply()
. (prefer-spread)
Before ES2015, one must use Function.prototype.apply()
to call variadic functions.
var args = [1, 2, 3, 4];
Math.max.apply(Math, args);
In ES2015, one can use spread syntax to call variadic functions.
/*eslint-env es6*/
var args = [1, 2, 3, 4];
Math.max(...args);
Rule Details
This rule is aimed to flag usage of Function.prototype.apply()
in situations where spread syntax could be used instead.
Examples
Examples of incorrect code for this rule:
/*eslint prefer-spread: "error"*/
foo.apply(undefined, args);
foo.apply(null, args);
obj.foo.apply(obj, args);
Examples of correct code for this rule:
/*eslint prefer-spread: "error"*/
// Using spread syntax
foo(...args);
obj.foo(...args);
// The `this` binding is different.
foo.apply(obj, args);
obj.foo.apply(null, args);
obj.foo.apply(otherObj, args);
// The argument list is not variadic.
// Those are warned by the `no-useless-call` rule.
foo.apply(undefined, [1, 2, 3]);
foo.apply(null, [1, 2, 3]);
obj.foo.apply(obj, [1, 2, 3]);
Known limitations:
This rule analyzes code statically to check whether or not the this
argument is changed. So, if the this
argument is computed in a dynamic expression, this rule cannot detect a violation.
/*eslint prefer-spread: "error"*/
// This warns.
a[i++].foo.apply(a[i++], args);
// This does not warn.
a[++i].foo.apply(a[i], args);
When Not To Use It
This rule should not be used in ES3/5 environments.
In ES2015 (ES6) or later, if you don't want to be notified about Function.prototype.apply()
callings, you can safely disable this rule.
Related Rules
- [no-useless-call](no-useless-call.md) Source: http://eslint.org/docs/rules/