Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[react/jsx-indent] False positive for ternary expressions in plain JS files #3222

Closed
meyfa opened this issue Feb 25, 2022 · 4 comments
Closed
Labels

Comments

@meyfa
Copy link

meyfa commented Feb 25, 2022

Using eslint-plugin-react at 7.29.0 or 7.29.1 gives false-positives for the react/jsx-indent rule inside ternary expressions that contain absolutely no JSX (are not even in a JSX file). The problem did not exist with version 7.28.0. This is currently failing builds in some of my repositories.

For a complete minimal reproduction, see https://github.com/meyfa/eslint-react-jsx-indent

Consider the following code:

function test (foo) {
  return foo != null
    ? Math.max(0, Math.min(1, 10))
    : 0
}

with the following .eslintrc.yml:

extends:
  - plugin:react/recommended
  - plugin:react/jsx-runtime

rules:
  'react/jsx-indent': [error, 2]

settings:
  react:
    version: detect

gives the error:

2:3  error  Expected indentation of 2 space characters but found 4  react/jsx-indent

I see that #3218 was apparently fixed in the recent patch. I have chosen to report this as a new issue since it appears to be specific to ternary expressions, which differs from issue #3218. At least every failure I could observe in my projects was related to ternary expressions.

@meyfa
Copy link
Author

meyfa commented Feb 25, 2022

Note that the same problem is reported when the file does contain JSX.

E.g.

function test (foo) {
  return foo != null
    ? <div>foo</div>
    : <div>bar</div>
}

The error appears fixable. The recommended fix is:

function test (foo) {
  return foo != null
    ? <div>foo</div>
  : <div>bar</div>
}

Surely that can't be right.

@ljharb ljharb added the bug label Feb 25, 2022
@ljharb
Copy link
Member

ljharb commented Feb 25, 2022

The first one was easy to fix; I'm looking at the second one now.

ljharb added a commit to ljharb/eslint-plugin-react that referenced this issue Feb 25, 2022
@ljharb ljharb closed this as completed in a8fca2a Feb 25, 2022
@meyfa
Copy link
Author

meyfa commented Feb 25, 2022

Thank you so much for taking care of this issue this quickly @ljharb! Very much appreciated :)

@ljharb
Copy link
Member

ljharb commented Feb 25, 2022

I'll have a patch release out later today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants