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

Extra indenting in a nested ternary with offsetTernaryExpressions: true from Standard JS config #13971

Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly
Projects

Comments

@brodybits
Copy link
Contributor

Tell us about your environment

  • ESLint Version: 7.16.0
  • Node Version: 14.5.3
  • npm Version: 6.14.9

What parser (default, @babel/eslint-parser, @typescript-eslint/parser, etc.) are you using?

default

Please show your full configuration:

Here is the configuration from my work in brodybits/react-native-module-init#85 but with Prettier items removed since they are not needed to reproduce this issue:

Configuration
parserOptions:
  ecmaVersion: 9

extends:
  - standard

plugins:
  - jest

env: { jest/globals: true }

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

Reduced reproduction code that I made in a local mock.test.js file:

const mockCallSnapshot = []

jest.mock('execa', () => (cmd, args, opts) => {
  mockCallSnapshot.push({ execa: [cmd, args, opts] })
  return cmd === 'git'
    ? args[1] === 'user.email'
      ? Promise.resolve({ stdout: 'alice@example.com' })
      : Promise.resolve({ stdout: 'Alice' })
    : Promise.resolve()
})
npx eslint mock.test.js

What did you expect to happen?

no errors

What actually happened? Please include the actual, raw output from ESLint.

/Users/brodybits/dev/react-native-module-init/mock.test.js
  7:1  error  Expected indentation of 8 spaces but found 6  indent
  8:1  error  Expected indentation of 8 spaces but found 6  indent

✖ 2 problems (2 errors, 0 warnings)
  2 errors and 0 warnings potentially fixable with the `--fix` option.

npx eslint --fix mock.test.js updates the code with extra indenting in the nested ternary like this:

const mockCallSnapshot = []

jest.mock('execa', () => (cmd, args, opts) => {
  mockCallSnapshot.push({ execa: [cmd, args, opts] })
  return cmd === 'git'
    ? args[1] === 'user.email'
        ? Promise.resolve({ stdout: 'alice@example.com' })
        : Promise.resolve({ stdout: 'Alice' })
    : Promise.resolve()
})

I was able to reproduce this issue more simply with the following fragment in the test suite:

        {
            code: unIndent`
              condition1
                ? condition2
                  ? Promise.resolve(1)
                  : Promise.resolve(2)
                : Promise.resolve(3)
            `,
            options: [2, { offsetTernaryExpressions: true }]
        },

Are you willing to submit a pull request to fix this bug?

Yes.

I was able to make a one-line fix based on bb6cf50 (PR #12556) by @sheerun:

One-line fix
--- a/lib/rules/indent.js
+++ b/lib/rules/indent.js
@@ -1178,6 +1178,7 @@ module.exports = {
                     offsets.setDesiredOffset(colonToken, firstToken, 1);
 
                     offsets.setDesiredOffset(firstConsequentToken, firstToken,
+                        firstConsequentToken.type === "Punctuator" &&
                         options.offsetTernaryExpressions ? 2 : 1);
 
                     /*

This issue is in follow-up to the offsetTernaryExpressions option contributed in PR #12556 and has led to standard/standard#1624.

/cc @sheerun

@brodybits brodybits added bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Jan 1, 2021
brodybits added a commit to brodybits/prettierx that referenced this issue Jan 1, 2021
brodybits added a commit to brodybits/prettierx that referenced this issue Jan 3, 2021
check for consistent formatting on prettierx side as discussed in:

- eslint/eslint#13971
- standard/standard#1624
- #41
brodybits added a commit to brodybits/prettierx that referenced this issue Jan 4, 2021
check for consistent formatting on prettierx side as discussed in:

- eslint/eslint#13971
- standard/standard#1624
- #41
@nzakas nzakas added this to Ready for Dev Team in Triage Jan 26, 2021
@nzakas nzakas moved this from Ready for Dev Team to Needs Triage in Triage Jan 26, 2021
@nzakas nzakas added accepted There is consensus among the team that this change meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Jan 29, 2021
@nzakas nzakas moved this from Needs Triage to Pull Request Opened in Triage Jan 29, 2021
@nzakas
Copy link
Member

nzakas commented Jan 29, 2021

Confirmed.

@brodybits
Copy link
Contributor Author

brodybits commented Jan 29, 2021 via email

Triage automation moved this from Pull Request Opened to Complete Jan 30, 2021
btmills pushed a commit that referenced this issue Jan 30, 2021
… (#13972)

* reproduce extra nested ternary indentation with a test

* add a similar test with proper indenting of nested ternary

* apply condition to avoid extra indenting with offsetTernaryExpressions

Co-authored-by: Adam Stankiewicz <sheerun@sher.pl>

Co-authored-by: Adam Stankiewicz <sheerun@sher.pl>
This was referenced Mar 17, 2021
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jul 30, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jul 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.