Visitar URL original
feat: add ternaryOperandBinaryExpressions option to no-extra-parens rule by kecrily · Pull Request #17270 · eslint/eslint · GitHub
Skip to content

feat: add ternaryOperandBinaryExpressions option to no-extra-parens rule - #17270

Merged
mdjermanovic merged 11 commits into
mainfrom
feat/no-extra-parens
Jun 30, 2023
Merged

mdjermanovic merged 11 commits into
mainfrom
feat/no-extra-parens

Conversation

@kecrily

@kecrily kecrily commented Jun 10, 2023

Copy link
Copy Markdown
Member

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[x] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

a && b ? foo : bar

Such code may not be clear to readers who do not know enough about priorities, whether it is

(a && b) ? foo : bar

or

a && (b ? foo : bar)

To better express the priority, one may choose to add parentheses to it and use no-mixed-operators rule with groups: [['||' , '&&', '?:']] option to check it.

However, no-extra-parens currently reports an error for first case. If there are no parentheses, then no-mixed-operators rule will report another error.

What rule do you want to change?

no-extra-parens

What change do you want to make (place an "X" next to just one item)?

[ ] Generate more warnings
[x] Generate fewer warnings
[ ] Implement autofix
[ ] Implement suggestions

How will the change be implemented (place an "X" next to just one item)?

[x] A new option
[ ] A new default behavior
[ ] Other

Please provide some example code that this change will affect:

(a && b) ? foo : bar

What does the rule currently do for this code?

error  Unnecessary parentheses around expression                              no-extra-parens

What will the rule do after it's changed?

Is there anything you'd like reviewers to focus on?

@kecrily
kecrily requested a review from a team as a code owner June 10, 2023 09:31
@eslint-github-bot eslint-github-bot Bot added the feature This change adds a new feature to ESLint label Jun 10, 2023
@kecrily kecrily changed the title feat: add conditionalTernary option to no-extra-parens ru;e feat: add conditionalTernary option to no-extra-parens rule Jun 10, 2023
@mdjermanovic

Copy link
Copy Markdown
Member

To better express the priority, one may choose to add parentheses to it and use no-mixed-operators rule with groups: [['||' , '&&', '?:']] option to check it.

However, no-extra-parens currently reports an error for first case. If there are no parentheses, then no-mixed-operators rule will report another error.

I agree there should be an option in the no-extra-parens rule to avoid these conflicts with the no-mixed-operators rule, but I think the option should be aligned with the no-mixed-operators rule, i.e., the option should allow those and only those parentheses that no-mixed-operator requires.

As currently implemented in this PR, the new option allows some parentheses that no-mixed-operators doesn't require, for example (foo()) ? bar : baz, and still disallows some parentheses that no-mixed-operators does require, for example foo ? bar : (baz || qux).

@kecrily

kecrily commented Jun 13, 2023

Copy link
Copy Markdown
Member Author

I improved the coverage and both of the ones you mentioned have been resolved. But please let me know if there are other cases that are not covered

Comment thread lib/rules/no-extra-parens.js Resolved
Comment thread lib/rules/no-extra-parens.js Resolved
@kecrily
kecrily force-pushed the feat/no-extra-parens branch from fbf3c0d to b12f5e8 Compare June 14, 2023 16:31
@kecrily
kecrily requested a review from mdjermanovic June 14, 2023 16:36
Comment thread lib/rules/no-extra-parens.js Resolved
@mdjermanovic

Copy link
Copy Markdown
Member

I think the option name should be something like ternaryOperandBinaryExpressions or binaryExpressionsAsTernaryOperands.

@kecrily

kecrily commented Jun 15, 2023

Copy link
Copy Markdown
Member Author

I think the option name should be something like ternaryOperandBinaryExpressions or binaryExpressionsAsTernaryOperands.

My confusion is that the option allows not only BinaryExpressions but also LogicalExpression. But there is only BinaryExpressions in the option name, so will this cause confusion.

@mdjermanovic

Copy link
Copy Markdown
Member

My confusion is that the option allows not only BinaryExpressions but also LogicalExpression. But there is only BinaryExpressions in the option name, so will this cause confusion.

I think it's fine to use "binary" here. It doesn't match ESTree's representation, but logical operators are binary operators, and the existing option nestedBinaryExpressions applies to them too, so I think it's fine to name the new option in a similar way.

@kecrily
kecrily force-pushed the feat/no-extra-parens branch from 02c3469 to 038e929 Compare June 20, 2023 14:40
@kecrily kecrily changed the title feat: add conditionalTernary option to no-extra-parens rule feat: add ternaryOperandBinaryExpressions option to no-extra-parens rule Jun 20, 2023
@eslint-github-bot

Copy link
Copy Markdown

Hi @kecrily!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the message to match this format, as we use it to generate changelogs and automate releases.

  • The length of the commit message must be less than or equal to 72

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

@kecrily kecrily changed the title feat: add ternaryOperandBinaryExpressions option to no-extra-parens rule feat: add ternaryOperandBinaryExpressions option to no-extra-parens rule Jun 20, 2023
Comment thread docs/src/rules/no-extra-parens.md Resolved
Comment thread docs/src/rules/no-extra-parens.md Resolved
@mdjermanovic

Copy link
Copy Markdown
Member

Marking accepted as this option fixes conflicts with the no-mixed-operators rule.

@mdjermanovic mdjermanovic added rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion labels Jun 26, 2023
Comment thread docs/src/rules/no-extra-parens.md Resolved
Comment thread docs/src/rules/no-extra-parens.md Resolved
Comment thread docs/src/rules/no-extra-parens.md Resolved
Comment thread docs/src/rules/no-extra-parens.md Resolved
Comment thread docs/src/rules/no-extra-parens.md Resolved
Comment thread lib/rules/no-extra-parens.js Resolved
Comment thread docs/src/rules/no-extra-parens.md Resolved
@netlify

netlify Bot commented Jun 30, 2023 •

Copy link
Copy Markdown

✅ Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit 9185479
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/649f1ecbdd5c630008649212
😎 Deploy Preview https://deploy-preview-17270--docs-eslint.netlify.app/rules/no-extra-parens
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@kecrily
kecrily requested a review from mdjermanovic June 30, 2023 16:57
Comment thread docs/src/rules/no-extra-parens.md Resolved
Comment thread docs/src/rules/no-extra-parens.md Resolved

@mdjermanovic mdjermanovic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

I'll leave this open until today's release in case anyone else would like to review it.

@mdjermanovic
mdjermanovic merged commit 4d411e4 into main Jun 30, 2023
@mdjermanovic
mdjermanovic deleted the feat/no-extra-parens branch June 30, 2023 21:38
json-derulo referenced this pull request in json-derulo/angular-ecmascript-intl Jul 4, 2023
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [eslint](https://eslint.org)
([source](https://togithub.com/eslint/eslint)) | devDependencies | minor
| [`8.43.0` ->
`8.44.0`](https://renovatebot.com/diffs/npm/eslint/8.43.0/8.44.0) |

---

### Release Notes

<details>
<summary>eslint/eslint (eslint)</summary>

### [`v8.44.0`](https://togithub.com/eslint/eslint/releases/tag/v8.44.0)

[Compare
Source](https://togithub.com/eslint/eslint/compare/v8.43.0...v8.44.0)

#### Features

-
[`1766771`](https://togithub.com/eslint/eslint/commit/176677180a4a1209fc192771521c9192e1f67578)
feat: add `es2023` and `es2024` environments
(#&#8203;1[https://github.com/eslint/eslint/issues/17328](https://togithub.com/eslint/eslint/issues/17328)/17328))
(Milos Djermanovic)
-
[`4c50400`](https://togithub.com/eslint/eslint/commit/4c5040022639ae804c15b366afc6e64982bd8ae3)
feat: add `ecmaVersion: 2024`, regexp `v` flag parsing
(#&#8203;1[https://github.com/eslint/eslint/issues/17324](https://togithub.com/eslint/eslint/issues/17324)/17324))
(Milos Djermanovic)
-
[`4d411e4`](https://togithub.com/eslint/eslint/commit/4d411e4c7063274d6d346f1b7ee46f7575d0bbd2)
feat: add ternaryOperandBinaryExpressions option to no-extra-parens rule
(#&#8203;1[https://github.com/eslint/eslint/issues/17270](https://togithub.com/eslint/eslint/issues/17270)/17270))
(Percy Ma)
-
[`c8b1f4d`](https://togithub.com/eslint/eslint/commit/c8b1f4d61a256727755d561bf53f889b6cd712e0)
feat: Move `parserServices` to `SourceCode`
(#&#8203;1[https://github.com/eslint/eslint/issues/17311](https://togithub.com/eslint/eslint/issues/17311)/17311))
(Milos Djermanovic)
-
[`ef6e24e`](https://togithub.com/eslint/eslint/commit/ef6e24e42670f321d996948623846d9caaedac99)
feat: treat unknown nodes as having the lowest precedence
(#&#8203;1[https://github.com/eslint/eslint/issues/17302](https://togithub.com/eslint/eslint/issues/17302)/17302))
(Brad Zacher)
-
[`1866e1d`](https://togithub.com/eslint/eslint/commit/1866e1df6175e4ba0ae4a0d88dc3c956bb310035)
feat: allow flat config files to export a Promise
(#&#8203;1[https://github.com/eslint/eslint/issues/17301](https://togithub.com/eslint/eslint/issues/17301)/17301))
(Milos Djermanovic)

#### Bug Fixes

-
[`a36bcb6`](https://togithub.com/eslint/eslint/commit/a36bcb67f26be42c794797d0cc9948b9cfd4ff71)
fix: no-unused-vars false positive with logical assignment operators
(#&#8203;1[https://github.com/eslint/eslint/issues/17320](https://togithub.com/eslint/eslint/issues/17320)/17320))
(Gweesin Chan)
-
[`7620b89`](https://togithub.com/eslint/eslint/commit/7620b891e81c234f30f9dbcceb64a05fd0dde65e)
fix: Remove `no-unused-labels` autofix before potential directives
(#&#8203;1[https://github.com/eslint/eslint/issues/17314](https://togithub.com/eslint/eslint/issues/17314)/17314))
(Francesco Trotta)
-
[`391ed38`](https://togithub.com/eslint/eslint/commit/391ed38b09bd1a3abe85db65b8fcda980ab3d6f4)
fix: Remove `no-extra-semi` autofix before potential directives
(#&#8203;1[https://github.com/eslint/eslint/issues/17297](https://togithub.com/eslint/eslint/issues/17297)/17297))
(Francesco Trotta)

#### Documentation

-
[`526e911`](https://togithub.com/eslint/eslint/commit/526e91106e6fe101578e9478a9d7f4844d4f72ac)
docs: resubmit pr 17115 doc changes
(#&#8203;1[https://github.com/eslint/eslint/issues/17291](https://togithub.com/eslint/eslint/issues/17291)/17291))
(唯然)
-
[`e1314bf`](https://togithub.com/eslint/eslint/commit/e1314bf85a52bb0d05b1c9ca3b4c1732bae22172)
docs: Integration section and tutorial
(#&#8203;1[https://github.com/eslint/eslint/issues/17132](https://togithub.com/eslint/eslint/issues/17132)/17132))
(Ben Perlmutter)
-
[`19a8c5d`](https://togithub.com/eslint/eslint/commit/19a8c5d84596a9f7f2aa428c1696ba86daf854e6)
docs: Update README (GitHub Actions Bot)

#### Chores

-
[`49e46ed`](https://togithub.com/eslint/eslint/commit/49e46edf3c8dc71d691a97fc33b63ed80ae0db0c)
chore: upgrade
[@&#8203;eslint/js](https://togithub.com/eslint/js)[@&#8203;8](https://togithub.com/8).44.0
(#&#8203;1[https://github.com/eslint/eslint/issues/17329](https://togithub.com/eslint/eslint/issues/17329)/17329))
(Milos Djermanovic)
-
[`a1cb642`](https://togithub.com/eslint/eslint/commit/a1cb6421f9d185901cd99e5f696e912226ef6632)
chore: package.json update for
[@&#8203;eslint/js](https://togithub.com/eslint/js) release (ESLint
Jenkins)
-
[`840a264`](https://togithub.com/eslint/eslint/commit/840a26462bbf6c27c52c01b85ee2018062157951)
test: More test cases for no-case-declarations
(#&#8203;1[https://github.com/eslint/eslint/issues/17315](https://togithub.com/eslint/eslint/issues/17315)/17315))
(Elian Cordoba)
-
[`e6e74f9`](https://togithub.com/eslint/eslint/commit/e6e74f9eef0448129dd4775628aba554a2d8c8c9)
chore: package.json update for eslint-config-eslint release (ESLint
Jenkins)
-
[`eb3d794`](https://togithub.com/eslint/eslint/commit/eb3d7946e1e9f70254008744dba2397aaa730114)
chore: upgrade semver@7.5.3
(#&#8203;1[https://github.com/eslint/eslint/issues/17323](https://togithub.com/eslint/eslint/issues/17323)/17323))
(Ziyad El Abid)
-
[`cf88439`](https://togithub.com/eslint/eslint/commit/cf884390ad8071d88eae05df9321100f1770363d)
chore: upgrade optionator@0.9.3
(#&#8203;1[https://github.com/eslint/eslint/issues/17319](https://togithub.com/eslint/eslint/issues/17319)/17319))
(Milos Djermanovic)
-
[`9718a97`](https://togithub.com/eslint/eslint/commit/9718a9781d69d2c40b68c631aed97700b32c0082)
refactor: remove unnecessary code in `flat-eslint.js`
(#&#8203;1[https://github.com/eslint/eslint/issues/17308](https://togithub.com/eslint/eslint/issues/17308)/17308))
(Milos Djermanovic)
-
[`f82e56e`](https://togithub.com/eslint/eslint/commit/f82e56e9acfb9562ece76441472d5657d7d5e296)
perf: various performance improvements
(#&#8203;1[https://github.com/eslint/eslint/issues/17135](https://togithub.com/eslint/eslint/issues/17135)/17135))
(moonlightaria)
-
[`da81e66`](https://togithub.com/eslint/eslint/commit/da81e66e22b4f3d3fe292cf70c388753304deaad)
chore: update eslint-plugin-jsdoc to 46.2.5
(#&#8203;1[https://github.com/eslint/eslint/issues/17245](https://togithub.com/eslint/eslint/issues/17245)/17245))
(唯然)
-
[`b991640`](https://togithub.com/eslint/eslint/commit/b991640176d5dce4750f7cc71c56cd6f284c882f)
chore: switch eslint-config-eslint to the flat format
(#&#8203;1[https://github.com/eslint/eslint/issues/17247](https://togithub.com/eslint/eslint/issues/17247)/17247))
(唯然)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4xNTUuMCIsInVwZGF0ZWRJblZlciI6IjM1LjE1OS4zIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: Renovate Bot <bot@renovateapp.com>
@eslint-github-bot eslint-github-bot Bot locked and limited conversation to collaborators Dec 28, 2023
@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 Dec 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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 feature This change adds a new feature to ESLint rule Relates to ESLint's core rules

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants