Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Metadata
ID:javascript-code-style/func-name-matching
Language: JavaScript
Severity: Notice
Category: Code Style
Description
Use the same name between your function declaration and the variable or property to which it is assigned.
Non-Compliant Code Examples
letfoo=functionbar(){};letfoo=functionbar(){};foo=functionbar(){};foo&&=functionbar(){};obj.foo||=functionbar(){};obj['foo']??=functionbar(){};obj.foo=functionbar(){};obj.bar.foo=functionbar(){};obj['foo']=functionbar(){};letobj={foo:functionbar(){}};letobj={'foo':functionbar(){}};({['foo']:functionbar(){}});// NOT SUPPORTED
// module.exports = function foo(name) {};
// module.exports = function foo(name) {};
// module.exports = function exports(name) {};
// module['exports'] = function foo(name) {};
// module['exports'] = function foo(name) {};
// module['exports'] = function exports(name) {};
// Object.defineProperty(foo, 'bar', { value: function baz() {} });
// Object.defineProperties(foo, { bar: { value: function baz() {} } });
// Object.create(proto, { bar: { value: function baz() {} } });
// var obj = { value: function foo(name) {} };
// Object.defineProperty(foo, 'bar', { value: function bar() {} });
// Object.defineProperties(foo, { bar: { value: function bar() {} } });
// Object.create(proto, { bar: { value: function bar() {} } });
// Reflect.defineProperty(foo, 'bar', { value: function baz() {} });
// Reflect.defineProperty(foo, 'bar', { value: function bar() {} });
foo({value:functionbar(){}});(obj?.aaa).foo=functionbar(){};// Object?.defineProperty(foo, 'bar', { value: function baz() {} });
// (Object?.defineProperty)(foo, 'bar', { value: function baz() {} });
// Object?.defineProperty(foo, 'bar', { value: function bar() {} });
// (Object?.defineProperty)(foo, 'bar', { value: function bar() {} });
// Object?.defineProperties(foo, { bar: { value: function baz() {} } });
// (Object?.defineProperties)(foo, { bar: { value: function baz() {} } });
// Object?.defineProperties(foo, { bar: { value: function bar() {} } });
// (Object?.defineProperties)(foo, { bar: { value: function bar() {} } });
classC{x=functiony(){};}classC{'x'=functiony(){};}classC{['x']=functiony(){};}classC{staticx=functiony(){};}(class{x=functiony(){};})varobj={'\u1885':functionfoo(){}};
Compliant Code Examples
varfoo;varfoo=functionfoo(){};varfoo=functionfoo(){};varfoo=function(){}varfoo=()=>{}foo=functionfoo(){};foo=functionfoo(){};foo&&=functionfoo(){};obj.foo||=functionfoo(){};obj['foo']??=functionfoo(){};obj.foo=functionfoo(){};obj.foo=functionfoo(){};obj.foo=function(){};obj.foo=function(){};obj.bar.foo=functionfoo(){};obj.bar.foo=functionfoo(){};obj['foo']=functionfoo(){};obj['foo']=functionfoo(){};// This are not equal not sure why eslint skips them
// obj['foo//bar'] = function foo() {};
// obj['foo//bar'] = function foo() {};
// obj['foo//bar'] = function foo() {};
obj[foo]=functionbar(){};obj[foo]=functionbar(){};varobj={foo:functionfoo(){}};varobj={foo:functionfoo(){}};varobj={'foo':functionfoo(){}};varobj={'foo':functionfoo(){}};varobj={foo:function(){}};varobj={foo:function(){}};varobj={foo:function(){}};varobj={[foo]:functionbar(){}}varobj={['x'+2]:functionbar(){}};obj['x'+2]=functionbar(){};var[bar]=[functionbar(){}];functiona(foo=functionbar(){}){}// NOT SUPPORTED
// module.exports = function foo(name) {};
// module['exports'] = function foo(name) {};
// module.exports = function foo(name) {};
// module.exports = function foo(name) {};
// module.exports = function foo(name) {};
// module['exports'] = function foo(name) {};
// module['exports'] = function foo(name) {};
// module['exports'] = function foo(name) {};
({['foo']:functionfoo(){}})({['foo']:functionfoo(){}})({[foo]:functionbar(){}})({[null]:functionfoo(){}})({[1]:functionfoo(){}})({[true]:functionfoo(){}})({[`x`]:functionfoo(){}})({[/abc/]:functionfoo(){}})({[[1,2,3]]:functionfoo(){}})({[{x:1}]:functionfoo(){}})[]=functionfoo(){}({}=functionfoo(){})[a]=functionfoo(){}({a}=functionfoo(){})var[]=functionfoo(){}var{}=functionfoo(){}var[a]=functionfoo(){}var{a}=functionfoo(){}({value:functionvalue(){}})obj.foo=functionfoo(){};obj.bar.foo=functionfoo(){};varobj={foo:functionfoo(){}};varobj={foo:function(){}};varobj={value:functionvalue(){}}// NOT SUPPORTED
// Object.defineProperty(foo, 'bar', { value: function bar() {} })
// Object.defineProperties(foo, { bar: { value: function bar() {} } })
// Object.create(proto, { bar: { value: function bar() {} } })
// Object.defineProperty(foo, 'b' + 'ar', { value: function bar() {} })
// Object.defineProperties(foo, { ['bar']: { value: function bar() {} } })
// Object.create(proto, { ['bar']: { value: function bar() {} } })
// Object.defineProperty(foo, 'bar', { value() {} })
// Object.defineProperties(foo, { bar: { value() {} } })
// Object.create(proto, { bar: { value() {} } })
// Reflect.defineProperty(foo, 'bar', { value: function bar() {} })
// Reflect.defineProperty(foo, 'b' + 'ar', { value: function baz() {} })
// Reflect.defineProperty(foo, 'bar', { value() {} })
foo({value:functionvalue(){}})classC{x=function(){};}classC{x=function(){};}classC{'x'=function(){};}classC{#x=function(){};}classC{#x=function(){};}classC{[x]=function(){};}classC{[x]=function(){};}classC{['x']=function(){};}classC{x=functionx(){};}classC{'x'=functionx(){};}classC{#x=functionx(){};}classC{#x=functionx(){};}classC{#x=functiony(){};}classC{#x=functiony(){};}classC{[x]=functionx(){};}classC{[x]=functionx(){};}classC{[x]=functiony(){};}classC{[x]=functiony(){};}classC{['x']=functionx(){};}classC{1=functionx0(){};}classC{1=functionx1(){};}classC{[1]=functionx0(){};}classC{[1]=functionx1(){};}classC{[f()]=functiong(){};}classC{[f()]=functionf(){};}classC{staticx=functionx(){};}classC{x=(functiony(){})();}classC{x=(functionx(){})();}(class{x=functionx(){};})classC{#x;foo(){this.#x=functionx(){};}}classC{#x;foo(){this.#x=functionx(){};}}classC{#x;foo(){this.#x=functiony(){};}}classC{#x;foo(){this.#x=functiony(){};}}classC{#x;foo(){a.b.#x=functionx(){};}}classC{#x;foo(){a.b.#x=functionx(){};}}classC{#x;foo(){a.b.#x=functiony(){};}}classC{#x;foo(){a.b.#x=functiony(){};}}
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- javascript-code-style # Rules to enforce JavaScript code style.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines