Unexpected console statement. Open
const warn = console.warn.bind(console);
- Read upRead up
- Exclude checks
title: no-console ruletype: suggestion relatedrules: - no-alert
- no-debugger
In JavaScript that is designed to be executed in the browser, it's considered a best practice to avoid using methods on console
. Such messages are considered to be for debugging purposes and therefore not suitable to ship to the client. In general, calls using console
should be stripped before being pushed to production.
console.log("Made it here.");
console.error("That shouldn't have happened.");
Rule Details
This rule disallows calls or assignments to methods of the console
object.
Examples of incorrect code for this rule:
::: incorrect
/* eslint no-console: "error" */
console.log("Log a debug level message.");
console.warn("Log a warn level message.");
console.error("Log an error level message.");
console.log = foo();
:::
Examples of correct code for this rule:
::: correct
/* eslint no-console: "error" */
// custom console
Console.log("Hello world!");
:::
Options
This rule has an object option for exceptions:
-
"allow"
has an array of strings which are allowed methods of theconsole
object
Examples of additional correct code for this rule with a sample { "allow": ["warn", "error"] }
option:
::: correct
/* eslint no-console: ["error", { allow: ["warn", "error"] }] */
console.warn("Log a warn level message.");
console.error("Log an error level message.");
:::
When Not To Use It
If you're using Node.js, however, console
is used to output information to the user and so is not strictly used for debugging purposes. If you are developing for Node.js then you most likely do not want this rule enabled.
Another case where you might not use this rule is if you want to enforce console calls and not console overwrites. For example:
/* eslint no-console: ["error", { allow: ["warn"] }] */
console.error = function (message) {
throw new Error(message);
};
With the no-console
rule in the above example, ESLint will report an error. For the above example, you can disable the rule:
// eslint-disable-next-line no-console
console.error = function (message) {
throw new Error(message);
};
// or
console.error = function (message) { // eslint-disable-line no-console
throw new Error(message);
};
However, you might not want to manually add eslint-disable-next-line
or eslint-disable-line
. You can achieve the effect of only receiving errors for console calls with the no-restricted-syntax
rule:
{
"rules": {
"no-console": "off",
"no-restricted-syntax": [
"error",
{
"selector": "CallExpression[callee.object.name='console'][callee.property.name!=/^(log|warn|error|info|trace)$/]",
"message": "Unexpected property on console object was called"
}
]
}
}
Source: http://eslint.org/docs/rules/
Unexpected console statement. Open
console.warn = function (message) {
- Read upRead up
- Exclude checks
title: no-console ruletype: suggestion relatedrules: - no-alert
- no-debugger
In JavaScript that is designed to be executed in the browser, it's considered a best practice to avoid using methods on console
. Such messages are considered to be for debugging purposes and therefore not suitable to ship to the client. In general, calls using console
should be stripped before being pushed to production.
console.log("Made it here.");
console.error("That shouldn't have happened.");
Rule Details
This rule disallows calls or assignments to methods of the console
object.
Examples of incorrect code for this rule:
::: incorrect
/* eslint no-console: "error" */
console.log("Log a debug level message.");
console.warn("Log a warn level message.");
console.error("Log an error level message.");
console.log = foo();
:::
Examples of correct code for this rule:
::: correct
/* eslint no-console: "error" */
// custom console
Console.log("Hello world!");
:::
Options
This rule has an object option for exceptions:
-
"allow"
has an array of strings which are allowed methods of theconsole
object
Examples of additional correct code for this rule with a sample { "allow": ["warn", "error"] }
option:
::: correct
/* eslint no-console: ["error", { allow: ["warn", "error"] }] */
console.warn("Log a warn level message.");
console.error("Log an error level message.");
:::
When Not To Use It
If you're using Node.js, however, console
is used to output information to the user and so is not strictly used for debugging purposes. If you are developing for Node.js then you most likely do not want this rule enabled.
Another case where you might not use this rule is if you want to enforce console calls and not console overwrites. For example:
/* eslint no-console: ["error", { allow: ["warn"] }] */
console.error = function (message) {
throw new Error(message);
};
With the no-console
rule in the above example, ESLint will report an error. For the above example, you can disable the rule:
// eslint-disable-next-line no-console
console.error = function (message) {
throw new Error(message);
};
// or
console.error = function (message) { // eslint-disable-line no-console
throw new Error(message);
};
However, you might not want to manually add eslint-disable-next-line
or eslint-disable-line
. You can achieve the effect of only receiving errors for console calls with the no-restricted-syntax
rule:
{
"rules": {
"no-console": "off",
"no-restricted-syntax": [
"error",
{
"selector": "CallExpression[callee.object.name='console'][callee.property.name!=/^(log|warn|error|info|trace)$/]",
"message": "Unexpected property on console object was called"
}
]
}
}
Source: http://eslint.org/docs/rules/
Unexpected unnamed function. Open
console.warn = function (message) {
- Read upRead up
- Exclude checks
title: func-names ruletype: suggestion furtherreading: - https://web.archive.org/web/20201112040809/http://markdaggett.com/blog/2013/02/15/functions-explained/
- https://2ality.com/2015/09/function-names-es6.html
A pattern that's becoming more common is to give function expressions names to aid in debugging. For example:
Foo.prototype.bar = function bar() {};
Adding the second bar
in the above example is optional. If you leave off the function name then when the function throws an exception you are likely to get something similar to anonymous function
in the stack trace. If you provide the optional name for a function expression then you will get the name of the function expression in the stack trace.
Rule Details
This rule can enforce or disallow the use of named function expressions.
Options
This rule has a string option:
-
"always"
(default) requires function expressions to have a name -
"as-needed"
requires function expressions to have a name, if the name isn't assigned automatically per the ECMAScript specification. -
"never"
disallows named function expressions, except in recursive functions, where a name is needed
This rule has an object option:
-
"generators": "always" | "as-needed" | "never"
-
"always"
require named generators -
"as-needed"
require named generators if the name isn't assigned automatically per the ECMAScript specification. -
"never"
disallow named generators where possible.
-
When a value for generators
is not provided the behavior for generator functions falls back to the base option.
Please note that "always"
and "as-needed"
require function expressions and function declarations in export default
declarations to have a name.
always
Examples of incorrect code for this rule with the default "always"
option:
::: incorrect
/*eslint func-names: ["error", "always"]*/
Foo.prototype.bar = function() {};
const cat = {
meow: function() {}
}
(function() {
// ...
}())
export default function() {}
:::
Examples of correct code for this rule with the default "always"
option:
::: correct
/*eslint func-names: ["error", "always"]*/
Foo.prototype.bar = function bar() {};
const cat = {
meow() {}
}
(function bar() {
// ...
}())
export default function foo() {}
:::
as-needed
ECMAScript 6 introduced a name
property on all functions. The value of name
is determined by evaluating the code around the function to see if a name can be inferred. For example, a function assigned to a variable will automatically have a name
property equal to the name of the variable. The value of name
is then used in stack traces for easier debugging.
Examples of incorrect code for this rule with the "as-needed"
option:
::: incorrect
/*eslint func-names: ["error", "as-needed"]*/
Foo.prototype.bar = function() {};
(function() {
// ...
}())
export default function() {}
:::
Examples of correct code for this rule with the "as-needed"
option:
::: correct
/*eslint func-names: ["error", "as-needed"]*/
var bar = function() {};
const cat = {
meow: function() {}
}
class C {
#bar = function() {};
baz = function() {};
}
quux ??= function() {};
(function bar() {
// ...
}())
export default function foo() {}
:::
never
Examples of incorrect code for this rule with the "never"
option:
::: incorrect
/*eslint func-names: ["error", "never"]*/
Foo.prototype.bar = function bar() {};
(function bar() {
// ...
}())
:::
Examples of correct code for this rule with the "never"
option:
::: correct
/*eslint func-names: ["error", "never"]*/
Foo.prototype.bar = function() {};
(function() {
// ...
}())
:::
generators
Examples of incorrect code for this rule with the "always", { "generators": "as-needed" }
options:
::: incorrect
/*eslint func-names: ["error", "always", { "generators": "as-needed" }]*/
(function*() {
// ...
}())
:::
Examples of correct code for this rule with the "always", { "generators": "as-needed" }
options:
::: correct
/*eslint func-names: ["error", "always", { "generators": "as-needed" }]*/
var foo = function*() {};
:::
Examples of incorrect code for this rule with the "always", { "generators": "never" }
options:
::: incorrect
/*eslint func-names: ["error", "always", { "generators": "never" }]*/
var foo = bar(function *baz() {});
:::
Examples of correct code for this rule with the "always", { "generators": "never" }
options:
::: correct
/*eslint func-names: ["error", "always", { "generators": "never" }]*/
var foo = bar(function *() {});
:::
Examples of incorrect code for this rule with the "as-needed", { "generators": "never" }
options:
::: incorrect
/*eslint func-names: ["error", "as-needed", { "generators": "never" }]*/
var foo = bar(function *baz() {});
:::
Examples of correct code for this rule with the "as-needed", { "generators": "never" }
options:
::: correct
/*eslint func-names: ["error", "as-needed", { "generators": "never" }]*/
var foo = bar(function *() {});
:::
Examples of incorrect code for this rule with the "never", { "generators": "always" }
options:
::: incorrect
/*eslint func-names: ["error", "never", { "generators": "always" }]*/
var foo = bar(function *() {});
:::
Examples of correct code for this rule with the "never", { "generators": "always" }
options:
::: correct
/*eslint func-names: ["error", "never", { "generators": "always" }]*/
var foo = bar(function *baz() {});
:::
Compatibility
- JSCS: requireAnonymousFunctions
- JSCS: disallowAnonymousFunctions Source: http://eslint.org/docs/rules/
Definition for rule 'node/no-restricted-import' was not found. Open
/* eslint-disable import/no-extraneous-dependencies */
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/