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-best-practices/new-parens
Language: JavaScript
Severity: Notice
Category: Best Practices
Description
This rule enforces the consistent use of parentheses in new statements. In JavaScript, you can omit parentheses when the constructor has no arguments, but you should always use them for consistency.
Non-Compliant Code Examples
// Default (Always)
vara=newDate;vara=newDatevara=new(Date);vara=new(Date)vara=(newDate)// This `()` is `CallExpression`'s. This is a call of the result of `new Date`.
vara=(newDate)()vara=newfoo.Bar;vara=(newFoo).bar;// Explicit always
vara=newDate;vara=newfoo.Bar;vara=(newFoo).bar;vara=newnewFoo()// OPTION never not supported
// Never
// var a = new Date();
// var a = new Date()
// var a = new (Date)();
// var a = new (Date)()
// var a = (new Date())
// var a = (new Date())()
// var a = new foo.Bar();
// var a = (new Foo()).bar;
// var a = new new Foo()
Compliant Code Examples
// Default (Always)
vara=newDate();vara=newDate(function(){});vara=new(Date)();vara=new((Date))();vara=(newDate());vara=newfoo.Bar();vara=(newFoo()).bar;// Explicit Always
vara=newDate();vara=newfoo.Bar();vara=(newFoo()).bar;// OPTION never not supported
// Never
// var a = new Date;
// var a = new Date(function() {});
// var a = new (Date);
// var a = new ((Date));
// var a = (new Date);
// var a = new foo.Bar;
// var a = (new Foo).bar;
// var a = new Person('Name')
// var a = new Person('Name', 12)
// var a = new ((Person))('Name');
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-best-practices # Rules to enforce JavaScript best practices.
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