Enforce named function expressions

Metadata

ID: javascript-code-style/func-names

Language: JavaScript

Severity: Notice

Category: Best Practices

Description

It is easier to debug your application code when you avoid anonymous functions so that the stack trace can show you meaningful error messages. This rule enforces all your function to be consistently declared with a name.

Non-Compliant Code Examples

Foo.prototype.bar = function() {};
(function(){}())
f(function(){})
var a = new Date(function() {});
var test = function(d, e, f) {};
new function() {}
Foo.prototype.bar = function() {};
(function(){}())
f(function(){})
var a = new Date(function() {});
new function() {}
var {foo} = function(){};
({ a: obj.prop = function(){} } = foo);
[obj.prop = function(){}] = foo;
var { a: [b] = function(){} } = foo;
function foo({ a } = function(){}) {};
export default function() {}
export default function() {}
export default (function(){});
var foo = bar(function *() {});
var foo = function*() {};
(function*() {}())
var foo = bar(function *() {});
var foo = function*() {};
(function*() {}())
var foo = bar(function *() {});
(function*() {}())
var foo = bar(function *() {});
(function*() {}())
var foo = function*() {};
(function*() {}())
var foo = bar(function *() {});
var foo = function*() {};
(function*() {}())
var foo = bar(function *() {});
var foo = function*() {};
(function*() {}())
var foo = bar(function *() {});
var foo = function*() {};
(function*() {}())
var foo = bar(function *() {});
var foo = function*() {};
(function*() {}())
class C { foo = function() {} }
class C { [foo] = function() {} }
class C { #foo = function() {} }

Compliant Code Examples

Foo.prototype.bar = function bar(){};
Foo.prototype.bar = () => {};
function foo(){}
function test(d, e, f) {}
new function bar(){}
exports = { get foo() { return 1; }, set bar(val) { return val; } };
({ foo() { return 1; } });
class A { constructor(){} foo(){} get bar(){} set baz(value){} static qux(){}}
function foo() {}
var a = function foo() {};
class A { constructor(){} foo(){} get bar(){} set baz(value){} static qux(){}}
({ foo() {} });
function foo() {}
var a = function foo() { foo(); };
class A { constructor(){} foo(){} get bar(){} set baz(value){} static qux(){}}
({ foo() {} });
export default function foo() {}
export default function foo() {}
export default function foo() {}
var foo = bar(function *baz() {});
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis