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.

Proposal: Add JSX-specific overrides to `no-extra-parens`

See original GitHub issue

In order to accommodate best practices established in the JSX world, the rule no-extra-parens needs to be modified to optionally ignore JSX components - the general standard is to wrap at least multiline JSX components in parens. However, this conflicts with the best practices in JavaScript in general where extra parens around expressions are discouraged.

What rule do you want to change?

no-extra-parens

Does this change cause the rule to produce more or fewer warnings?

Neither - the default setting should not cause more or fewer warnings to appear. However, this proposal will allow users to reduce some warnings when working with JSX components.

How will the change be implemented? (New option, new default behavior, etc.)?

New option: ignoreJSX with possible values:

  • all: All JSX components are excluded from this rule
  • multiline: All multiline JSX components are excluded from this rule
  • single-line: All single-line JSX components are excluded from this rule

The default behaviour for this exception will be to to disallow extra parens around any JSX components, just as it is currently implemented.

Please provide some example code that this change will affect:

/* eslint no-extra-parens: ["error", "all", { "ignoreJSX": "all" }] */
// Ok - JSX components are excluded from this rule
const Component = (<div />)
const Component = <div />
const Component = (
  <div>
    <p />
  </div>
)
const Component = 
  <div>
    <p />
  </div>

/* eslint no-extra-parens: ["error", "all", { "ignoreJSX": "multiline" }] */
// Ok (extra parens not allowed)
const Component = <div />
// Ok (multiline JSX components are excluded from this rule)
const Component = (
  <div>
    <p />
  </div>
)
const Component = 
  <div>
    <p />
  </div>
// Warns (extra parens not allowed)
const Component = (<div />)

/* eslint no-extra-parens: ["error", "all", { "ignoreJSX": "single-line" }] */
// Ok (single-line JSX components are excluded from this rule)
const Component = <div />
const Component = (<div />)
// Ok (extra parens not allowed)
const Component = 
  <div>
    <p />
  </div>
// Warns (extra parens not allowed)
const Component = (
  <div>
    <p />
  </div>
)

What does the rule currently do for this code?

The rule currently flags all cases where a JSX component is wrapped in parens. The general best practice, however, is to wrap at least multiline JSX components in parens, for better readability.

What will the rule do after it’s changed?

The rule will allow developers to optionally turn this rule off for JSX components (single-line, multiline or all). The default setting will be to treat parens around JSX components just like any other expression - it will warn if JSX components are wrapped in parens.

See also original discussion in #7350.

/cc @platinumazure @ljharb

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:17 (17 by maintainers)

github_iconTop GitHub Comments

3reactions
robertrossmanncommented, Jan 16, 2017

Yes, already working on this! 🎉 I actually have it ready now, only documentation remains to be updated. I will open a PR later today.

2reactions
kaicataldocommented, Jan 10, 2017

I’ll champion this

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-extra-parens - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
Options - Prettier
This effectively allows using the json5 parser for “JSON with comments and trailing commas”. JSX Quotes. Use single quotes instead of double quotes...
Read more >
eslint/eslint - Gitter
I am looking for something like max-lines should warn if between 250 - 500 lines and error is file has more than 500...
Read more >
How to pass props to {this.props.children} - Stack Overflow
Cloning children with new props. You can use React.Children to iterate over the children, and then clone each element with new props (shallow...
Read more >
React Patterns
Layout component #. Layout components result in some form of static DOM element. It might not need to update frequently, if ever. Consider...
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