Skip to content

JavaScript: Don't remove double negations#6878

Merged
greg-at-moderne merged 1 commit intomainfrom
greg-keep-js-double-negation
Mar 5, 2026
Merged

JavaScript: Don't remove double negations#6878
greg-at-moderne merged 1 commit intomainfrom
greg-keep-js-double-negation

Conversation

@greg-at-moderne
Copy link
Contributor

@greg-at-moderne greg-at-moderne commented Mar 5, 2026

What's changed?

Amending the SimplifyBooleanExpressionVisitor not to remove double negations from non-Java code. Unless the underlying type is already boolean.

What's your motivation?

In some languages, which include JavaScript and Groovy, the !! is an established technique of coercing values of different types into a truthy/falsey value. Therefore it's wrong to remove the !!. Example

function getDisplayName(user) {
    const name = user.name;       // could be undefined, null, or ""
    return !!name ? name : "Anonymous";
}

@greg-at-moderne greg-at-moderne self-assigned this Mar 5, 2026
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Mar 5, 2026
@greg-at-moderne greg-at-moderne marked this pull request as ready for review March 5, 2026 10:11
@greg-at-moderne greg-at-moderne merged commit 0673df5 into main Mar 5, 2026
1 check passed
@greg-at-moderne greg-at-moderne deleted the greg-keep-js-double-negation branch March 5, 2026 10:11
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant