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
feat: Add rule no-constant-binary-expression #15296
feat: Add rule no-constant-binary-expression #15296
Conversation
|
Hi @captbaritone!, thanks for the Pull Request The first commit message 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.
Read more about contributing to ESLint here |
7333916
to
0ec999e
Compare
0ec999e
to
394c297
Compare
394c297
to
2ec6645
Compare
When first reading through this PR, I thought this rule prohibited constant operands in binary expressions (such as the 1 in |
a131be0
to
b7961dd
Compare
I believe all previous feedback has been addressed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, I hope this is ok, but I just noticed you are spelling always
and short-circuiting
wrong in a few comments. It seems to be fairly consistent, so a find a replace should sort it :)
Per #13752 (comment), the name should be |
b7961dd
to
5f8a79b
Compare
Ah thanks. Will fix. Edit I've updated the PR, commit and PR description to use the name |
5f8a79b
to
e2088d2
Compare
e2088d2
to
0f95d9b
Compare
@mdjermanovic Thanks for the detailed review with examples of code that would behave give incorrect results. Made it super easy to address the feedback and add the missing tests cases. Updated and ready for another round. |
@mdjermanovic Again, great feedback thanks! Ready for another pass. |
@mdjermanovic Feedback addressed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one small typo to fix, and then LGTM.
@mdjermanovic Fixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, thanks!
I'll leave this open for @nzakas to confirm his feedback has been addressed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I just left a few suggestions for cleaning up the docs and JSDoc.
Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
@nzakas Thanks! I've merged committed them all. |
I'll update the doc file for #15782 in a follow-up PR. |
@captbaritone thanks for contributing! |
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [eslint](https://eslint.org) ([source](https://github.com/eslint/eslint)) | devDependencies | minor | [`8.13.0` -> `8.14.0`](https://renovatebot.com/diffs/npm/eslint/8.13.0/8.14.0) | --- ### Release Notes <details> <summary>eslint/eslint</summary> ### [`v8.14.0`](https://github.com/eslint/eslint/releases/v8.14.0) [Compare Source](eslint/eslint@v8.13.0...v8.14.0) #### Features - [`ab6363d`](eslint/eslint@ab6363d) feat: Add rule no-constant-binary-expression ([#​15296](eslint/eslint#15296)) (Jordan Eldredge) #### Bug Fixes - [`35fa1dd`](eslint/eslint@35fa1dd) fix: allow project paths to have URL-encoded characters ([#​15795](eslint/eslint#15795)) (Milos Djermanovic) - [`413f1d5`](eslint/eslint@413f1d5) fix: update `astUtils.isDirectiveComment` with `globals` and `exported` ([#​15775](eslint/eslint#15775)) (Milos Djermanovic) #### Build Related - [`c2407e8`](eslint/eslint@c2407e8) build: add node v18 ([#​15791](eslint/eslint#15791)) (唯然) #### Chores - [`735458c`](eslint/eslint@735458c) chore: add static frontmatter to no-constant-binary-expression docs ([#​15798](eslint/eslint#15798)) (Milos Djermanovic) - [`db28f2c`](eslint/eslint@db28f2c) chore: Add static frontmatter to docs ([#​15782](eslint/eslint#15782)) (Nicholas C. Zakas) - [`3bca59e`](eslint/eslint@3bca59e) chore: markdownlint autofix on commit ([#​15783](eslint/eslint#15783)) (Nicholas C. Zakas) </details> --- ### Configuration 📅 **Schedule**: 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, click this checkbox. --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). Co-authored-by: cabr2-bot <cabr2.help@gmail.com> Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1318 Reviewed-by: Epsilon_02 <epsilon_02@noreply.codeberg.org> Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org> Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
* feat: Add rule no-constant-binary-expression I proposed the core idea of this rule in eslint#13752 as an addition to `no-constant-condition`, but on the advice of the TSC, it was restructured as a standalone rule. * Share `no-constant-condition`'s `isConstant` Here we extract `isConstant` into ast-utils and share it between `no-constant-condition` and `no-constant-binary-expression`. * Update title of docs page * Avoid false positive on shadowed builtins * Use isLogicalAssignmentOperator * Ensure we don't treat user defined new expressions as always new * Move docs to the new docs directory * Address review feedback * Address more review feedback * Address review feedback * Fix typo * Update lib/rules/utils/ast-utils.js Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * Update docs/src/rules/no-constant-binary-expression.md Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * Update docs/src/rules/no-constant-binary-expression.md Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * Update docs/src/rules/no-constant-binary-expression.md Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
This reverts commit f7a155c.
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[X] New rule (template)
Please describe what the rule should do:
What category of rule is this? (place an "X" next to just one item)
[X] Warns about a potential error
Provide 2-3 code examples that this rule will warn about:
Why should this rule be included in ESLint (instead of a plugin)?
Recommended by TSC in #13752.
What changes did you make? (Give an overview)
Adds a rule which is similar in nature to
no-constant-condition
in that it flags conditions have constant behavior at runtime, since that is highly correlated with developer error. Specifically it checks for:&&
and||
logical expressions where the left hand side is either always truthy or always falsy??
logical expressions where the left hand side is either always nullish or always non-nullish==
,===
,!==
and!=
binary expressions where one side is eithernull
,undefined
,true
orfalse
, and the outcome of the comparison can be determined statically.Is there anything you'd like reviewers to focus on?
The code ended up begin quite verbose with comments, which I personally found helpful during development, but may not be idiomatic in this code base. I'm happy to tone them down if desired.
At its heart this rule boils down to a "best effort". It understands many semantics of the JavaScript language, but it must draw the line somewhere, and those lines are necessarily somewhat arbitrary. I feel pretty good about the lines I ended up setting given the TSC Summary, but I'm open to feedback.
Impact and Representative Examples
I ran this rule on our large monorepo and it uncovered ~450 existing errors. Of those, about one third looked like changes which were intended to be temporary behavior changes, or hard coded feature flags:
Personally I think these would merit an
/* eslint-disable-next-line */
comment to clarify the motivation, but I could also see us wanting to add an option to permit literaltrue
orfalse
on the left hand side of||
or&&
.Below are some representative errors it uncovered, anonymized and reduced to their pure form:
Rule Name
Edit @mdjermanovic pointed out that the TSC had proposed a better name, but I missed the comment.
Isno-constant-binary-expression
the right name for this rule given that it also applies to the left hand side of logical expressions? I'd rather not re-litigate, but my instinct tells me that the logical expression checks should live inno-constant-condition
and this new rule should flag any comparison binary expression whose result is provably static.