News for hackers. Stuff that matters.
Re:Interesting Bugs Caught by ESLint's no-constant-binary-ex... by SenHeng on Wednesday November 08, 2023 @11:25PM (#38201181)
Prettier oftentimes removes a lot of parentheses that I had hoped it would leave.

I’ve taken naming them as variables instead. Definitely more verbose but more readability.

    const aIsB = a === b;
    … aIsB ?? c …
Re:Interesting Bugs Caught by ESLint's no-constant-binary-ex... by pests on Wednesday November 08, 2023 @11:45PM (#38201280)
That is how it evaluates, not what the original author intended in the example in the OP.

    return a === b ?? c;
The intent was

    const bOrDefault = b ?? c;
    return a === bOrDefault;
You posted the actual (buggy) evaluation. I agree that its better if that is what you intend.
Re:Interesting Bugs Caught by ESLint's no-constant-binary-ex... by pests on Thursday November 09, 2023 @04:36AM (#38202898)
Wait, your example doesn't even make sense to me on second thought (and was the ultimate point in the article)

aIsB is guaranteed to not be null or undefined so the ?? is a no-op. You just did a comparison so it's either true or false.

Re:Interesting Bugs Caught by ESLint's no-constant-binary-ex... by SenHeng on Thursday November 09, 2023 @06:01AM (#38203487)
I was referring particularly to my parent's point about adding parentheses to remove ambiguity. That prettier oftentimes removes parentheses that I explicitly want. So rather than risk that, I remove the ambiguity by moving one of the comparisons off into a named variable.
Re:Interesting Bugs Caught by ESLint's no-constant-binary-ex... by pests on Thursday November 09, 2023 @01:23PM (#38208730)
I agree.

I was just pointing out that this is a case prettier wouldn't do anything. The expression parses differently and adding or removing parentheses to any subexpression will change its meaning.

That by manually doing the transform in your example that you would realize your cody was a no-op or that the intended version was what I posted.