func-names
Require or disallow names for function expressions
A callback written as function () {} shows up as <anonymous> in stack traces and profiles. When several callbacks are involved, you can't tell from the trace which one failed.
Arrow functions and methods aren't checked.
Configure#
Off by default. To turn it on:
[lint.rules]
"func-names" = "error" # or "warn"Every option, set to its default. The ones that are commented out are unset by default.
[lint.rules]
"func-names" = [
"error",
"always", # "always", "as-needed" or "never"
{
# generators = <"always", "as-needed" or "never">,
},
]Examples#
button.addEventListener("click", function Unexpected unnamed function.() {
save();
});export default function Unexpected unnamed function.() {
return "ready";
}button.addEventListener("click", function onClick() {
save();
});
button.addEventListener("blur", () => save());Options#
[lint.rules]
func-names = ["error", "as-needed"]| Option | What it does |
|---|---|
"always" (default) | Every function expression needs a name. |
"as-needed" | A name is only needed where JavaScript doesn't give the function one on its own. |
"never" | No function expression can have a name, unless the function refers to itself by that name. |
A second option, { "generators": "always" | "as-needed" | "never" }, sets the same choice for generator functions only. Without it, generators follow the first option.
"as-needed"#
A function that's assigned to a variable or used as the value of an object key takes that name, so it doesn't need its own:
"as-needed"No problemsconst save = function () {};
const handlers = {
close: function () {},
};With "always", both are reported:
const save = function Unexpected unnamed function.() {};
const handlers = {
close: function Unexpected unnamed method close.() {},
};A callback doesn't get a name that way, and neither does a function assigned to object.property. Both are still reported:
"as-needed"2 problemsitems.forEach(function Unexpected unnamed function.(item) {
render(item);
});
handlers.open = function Unexpected unnamed function.() {};"never"#
"never"1 problemitems.forEach(function renderItemUnexpected named function renderItem.(item) {
render(item);
});"never"No problemsitems.forEach(function (item) {
render(item);
});
const factorial = function fact(n) {
return n <= 1 ? 1 : n * fact(n - 1);
};generators#
"always", { "generators": "never" }No problemsrun(function* () {
yield 1;
});