question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[no-extra-parens] Allow an exception for (new X()).y

See original GitHub issue

What rule do you want to change? no-extra-parens

Does this change cause the rule to produce more or fewer warnings? Fewer, if configured

How will the change be implemented? (New option, new default behavior, etc.)? New option, similar to the existing ones like returnAssign, nestedBinaryExpressions, etc.

Please provide some example code that this change will affect:

const doc = (new JSDOM()).window.document;

const { document } = (new JSDOM(`<!doctype html><html><head></head><body>
  <div></div><div title=""></div><div title="yes"></div>
</body></html>`)).window;

What does the rule currently do for this code?

It requires removing the parens, i.e. that I write

const doc = new JSDOM().window.document;

const { document } = new JSDOM(`<!doctype html><html><head></head><body>
  <div></div><div title=""></div><div title="yes"></div>
</body></html>`).window;

This is equivalent, but it feels really weird. Although the new operator binds more closely than the . operator in JS, this is a somewhat-surprising feature of JS, as . otherwise binds very high, and most space-separated operators bind quite low.

What will the rule do after it’s changed?

Allow the above code, when a new option is configured, e.g."newExpressions": false.

Are you willing to submit a pull request to implement this change?

Yes, although a community volunteer would be much appreciated.

Other note

This changed in v6.5.0 as part of https://github.com/eslint/eslint/pull/12302.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, Oct 14, 2019

I’m willing to champion and work on this.

1reaction
mdjermanoviccommented, Oct 14, 2019

Just to confirm the details of this enhancement, if I understand correctly the following should be done.

The option applies only to the change made in v6.5.0 (part of #12302):

(new X()).y;

(new X())[y];

Other cases of unnecessary parens around new X() that were warnings in previous versions will remain warnings, regardless of how the option is set. For example:

y = (new X());

f((new X()), y);

(new X())();

new (new X())();

The name of the option could be enforceForNewInMemberExpressions ?

Since it’s default behavior in the actual version, the default value is true:

/* eslint no-extra-parens: "error" */

(new X()).y; // error

(new X())[y]; // error

y = (new X()); // error

f((new X()), y); // error

(new X())(); // error

new (new X())(); // error

When the option is explicitly set to false, (new X()).y and (new X())[y] are allowed:

/* eslint no-extra-parens: ["error", "all", { "enforceForNewInMemberExpressions": false }] */

(new X()).y; // ok

(new X())[y]; // ok

y = (new X()); // error

f((new X()), y); // error

(new X())(); // error

new (new X())(); // error
Read more comments on GitHub >

github_iconTop Results From Across the Web

no-extra-parens - ESLint - Pluggable JavaScript Linter
This rule has an object option for exceptions to the "all" option: "conditionalAssign": false allows extra parentheses around assignments in conditional ...
Read more >
Code Issues - Embold Help Center
Throwing a new exception from a catch block without passing the original exception into the new exception will cause the original stack trace...
Read more >
Diff - 402e1b6e55e9041dfd1a93580e45e5c5dba1db55^!
diff --git a/README.chromium b/README.chromium new file mode ... Fix: Allow objectsInObjects exception when destructuring (fixes #6469) ...
Read more >
stream-chat-react - UNPKG
n return;\n }\n var err = new Error(\n 'Calling PropTypes validators directly is not ... rectInit - Object with rectangle's x/y coordinates and...
Read more >
declaration or statement expected. this '=' follows a block of ...
c# GetCoordinates(out var x, out var y); ...; // x and y not in scope :-( ... c# void M(object o) { if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found