Skip to content

Add suggestions for no-unsafe-negation #12591

Closed

Description

@mdjermanovic

It might be useful to have some Suggestions in the core rules, as a working example for rule plugins and a ready test for editor integrations? This rule looks to me like a place where suggestions make sense and should be simple enough to implement for the start.

What rule do you want to change?

no-unsafe-negation

Does this change cause the rule to produce more or fewer warnings?

same

How will the change be implemented? (New option, new default behavior, etc.)?

new default behavior

Please provide some example code that this change will affect:

/*eslint no-unsafe-negation: "error"*/

if (!key in object) {
    // ...
}

What does the rule currently do for this code?

Just an error without the autofix. The fixer was recently removed in #12157 because it was changing behavior.

What will the rule do after it's changed?

An error with two suggestions.

The first suggestion would be to negate the whole relational expression. This changes behavior, but probably from a bug to the intended one (that's how the removed fixer was working):

/*eslint no-unsafe-negation: "error"*/

if (!(key in object)) {
    // ...
}

The second suggestion would be to make the existing intended behavior explicit, by wrapping the negation in parentheses, which is a documented exception allowed by this rule and does not produce an error after the fix:

/*eslint no-unsafe-negation: "error"*/

if ((!key) in object) {
    // ...
}

Does this make sense?

Are you willing to submit a pull request to implement this change?

Yes.

Activity

  1. added
    enhancementThis change enhances an existing feature of ESLint
    ruleRelates to ESLint's core rules
    evaluatingThe team will evaluate this issue to decide whether it meets the criteria for inclusion
    on Nov 22, 2019
  2. self-assigned this
    on Nov 22, 2019
  3. platinumazure commented on Nov 23, 2019

    @platinumazure
    Member

    Yes! This is a great example/test case for the Suggestions feature. Let's do this!

  4. added
    acceptedThere is consensus among the team that this change meets the criteria for inclusion
    and removed
    evaluatingThe team will evaluate this issue to decide whether it meets the criteria for inclusion
    on Nov 23, 2019
  5. added a commit that references this issue on Nov 27, 2019
    e198148
  6. locked and limited conversation to collaborators on Jun 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionenhancementThis change enhances an existing feature of ESLintruleRelates to ESLint's core rules

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions