Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [no-unnecessary-boolean-literal-compare] add option to check nullable booleans#1983

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
bradzacher merged 11 commits intotypescript-eslint:masterfromzachkirsch:zk/nullable-boolean-literal-compare
Jun 20, 2020
Merged

feat(eslint-plugin): [no-unnecessary-boolean-literal-compare] add option to check nullable booleans#1983

bradzacher merged 11 commits intotypescript-eslint:masterfromzachkirsch:zk/nullable-boolean-literal-compare
Jun 20, 2020

Conversation

@zachkirsch
Copy link
Contributor

No description provided.

@typescript-eslint
Copy link
Contributor

Thanks for the PR,@zachkirsch!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently onhttps://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitorsper day.

@codecov
Copy link

codecovbot commentedMay 6, 2020
edited
Loading

Codecov Report

Merging#1983 intomaster willincrease coverage by0.03%.
The diff coverage is100.00%.

@@            Coverage Diff             @@##           master    #1983      +/-   ##==========================================+ Coverage   93.40%   93.43%   +0.03%==========================================  Files         174      174                Lines        7896     7938      +42       Branches     2256     2274      +18     ==========================================+ Hits         7375     7417      +42  Misses        247      247                Partials      274      274
FlagCoverage Δ
#unittest93.43% <100.00%> (+0.03%)⬆️
Impacted FilesCoverage Δ
packages/eslint-plugin/src/util/types.ts82.08% <ø> (ø)
...rc/rules/no-unnecessary-boolean-literal-compare.ts89.61% <100.00%> (+12.46%)⬆️

@bradzacherbradzacher added awaiting responseIssues waiting for a reply from the OP or another party enhancement: plugin rule optionNew rule option for an existing eslint-plugin rule labelsMay 6, 2020
@bradzacher
Copy link
Member

thanks for the PR@zachkirsch - could you please provide some context behind this PR?
Why do you want this change?

To me it seems like this option removes a large portion of the usefulness of the rule as the most common case would be a nullable boolean.

@zachkirsch
Copy link
ContributorAuthor

zachkirsch commentedMay 7, 2020
edited
Loading

Yeah! Two reasons:

Reason 1: Comparing nullable booleans to literaltrue is unnecessary

BadGood (and equivalent)
myBooleanOrNullVar === truemyBooleanOrNullVar
myBooleanOrNullVar !== true!myBooleanOrNullVar

Reason 2: Comparing nullable booleans to literalfalse is confusing

I seemyOptionalBoolean !== false a lot of the time, usually as optional props (that default totrue) in react components. For example:

interfaceProps{// whether to display title (defaults to true)displayTitle?:boolean}...publicrender(){return(<div>{this.props.displayTitle!==false&&renderTitle()}        ...

Before?? existed,myOptionalProp !== false was a sensible way to check an optional prop and default totrue if it'sundefined. In my opinion, I think thatmyOptionalProp !== false is a very confusing way to express "use this prop but default totrue if it's missing," and IMO it's harder to see bugs that are written that way.

?? is the perfect replacement for this scenario - it's a built-in operator that provides a default if your variable is undefined at runtime.(this.props.displayTitle ?? true) && renderTitle() very clearly means "render the title based on the value ofthis.props.displayTitle (and default totrue if it's not defined)"

To me it seems like this option removes a large portion of the usefulness of the rule as the most common case would be a nullable boolean.

I think this rule is still very useful with this option, as it

  1. catches unnecessary comparisons with boolean variables
    1. e.g.myBoolean !== true --> !myBoolean
  2. catches unnecessary comparisons between nullable boolean variables andtrue
    1. e.g.myOptionalBoolean !== true --> !myOptionalBoolean
  3. cleans up comparisons between optional booleans andfalse (described above in this comment)

@bradzacherbradzacher removed the awaiting responseIssues waiting for a reply from the OP or another party labelMay 17, 2020
Copy link
Member

@bradzacherbradzacher left a comment

Choose a reason for hiding this comment

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

mostly lgtm. one comment.
Thanks for your work

};

const defaults= {
allowComparingNullableBooleans:true,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
allowComparingNullableBooleans:true,
allowComparingNullableBooleans:false,

@bradzacherbradzacher added the awaiting responseIssues waiting for a reply from the OP or another party labelJun 19, 2020
zachkirschand others added2 commitsJune 19, 2020 13:49
…l-compare.tsCo-authored-by: Brad Zacher <brad.zacher@gmail.com>
…al-compare.mdCo-authored-by: Brad Zacher <brad.zacher@gmail.com>
@zachkirsch
Copy link
ContributorAuthor

Sweet! I actually am having some second thoughts haha. I feel like auto-fixingvarBooleanOrNull === false to!(varBooleanOrNull ?? true) is kind of weird - the "fixed" version is also a bit hard to parse visually.

FixingvarBooleanOrNull === true tovarBooleanOrNull andvarBooleanOrNull !== true to!varBooleanOrNull is good IMO. I think fixingvarBooleanOrNull !== false tovarBooleanOrNull ?? true is also good but I could see it either way.

What are your thoughts? We could also have additional config options that more granularly control which of these four comparisons are checked/fixed by the rule.

@bradzacher
Copy link
Member

bradzacher commentedJun 19, 2020
edited
Loading

Hmmm. Sorry, I did a quick eyeball and didn't notice the fix output.
That's my bad.

treat the false and nullish cases the same

  • foo === true =>foo
  • foo !== true =>!foo

treat the true and nullish cases the same

  • foo === false =>foo ?? true
  • foo !== false =>!(foo ?? true)

I think that is probably fine.
Personally, I think that the latter two cases might be good to ignore, as IMO they're clearer the other way.

The other option for the last two is to fix to something likefoo !== undefined && !foo, but that's pretty verbose, and it's probably better to not fix at all there....

@zachkirsch
Copy link
ContributorAuthor

Okay, I brokeallowComparingNullableBooleans into two options:allowComparingNullableBooleansToTrue andallowComparingNullableBooleansToFalse, so users can decide what they want to allow. If you want, I'm happy to just have the rule ignore comparisons between nullable boolean variables and literalfalse, but I think it might be confusing if eslint complains aboutbooleanOrNullVar === true but notbooleanOrNullVar === false.

I also added some more docs to the rule's readme, including a table of what exactly the fixer does in each case.

bradzacher reacted with thumbs up emoji

@bradzacherbradzacher removed the awaiting responseIssues waiting for a reply from the OP or another party labelJun 19, 2020
@zachkirsch
Copy link
ContributorAuthor

Lemme know if you need anything else from me!

Copy link
Member

@bradzacherbradzacher left a comment

Choose a reason for hiding this comment

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

lgtm - thanks!

@bradzacherbradzacher merged commitc0b3057 intotypescript-eslint:masterJun 20, 2020
@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsJul 21, 2020
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@bradzacherbradzacherbradzacher approved these changes

Assignees

No one assigned

Labels

enhancement: plugin rule optionNew rule option for an existing eslint-plugin rule

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@zachkirsch@bradzacher

[8]ページ先頭

©2009-2025 Movatter.jp