Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 11, 2024
1 parent e70413c commit 3c91502
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
8 changes: 6 additions & 2 deletions rules/no-negation-in-equality-check.js
Expand Up @@ -3,7 +3,11 @@ const {
fixSpaceAroundKeyword,
addParenthesizesToReturnOrThrowExpression,
} = require('./fix/index.js');
const {needsSemicolon} = require('./utils/index.js');
const {
needsSemicolon,
isParenthesized,
isOnSameLine,
} = require('./utils/index.js');

const MESSAGE_ID_ERROR = 'no-negation-in-equality-check/error';
const MESSAGE_ID_SUGGESTION = 'no-negation-in-equality-check/suggestion';
Expand Down Expand Up @@ -54,7 +58,7 @@ const create = context => ({

const tokenAfterBang = sourceCode.getTokenAfter(bangToken);

const {parent} = node;
const {parent} = binaryExpression;
if (
(parent.type === 'ReturnStatement' || parent.type === 'ThrowStatement')
&& !isParenthesized(binaryExpression, sourceCode)
Expand Down
41 changes: 34 additions & 7 deletions test/snapshots/no-negation-in-equality-check.mjs.md
Expand Up @@ -105,32 +105,59 @@ Generated by [AVA](https://avajs.dev).
3 | }␊
`

## invalid(6): function x() { return! foo === bar; }
## invalid(6): function x() { return! foo === bar; throw! foo === bar; }

> Input
`␊
1 | function x() {␊
2 | return!␊
3 | foo === bar;␊
4 | }␊
4 | throw!␊
5 | foo === bar;␊
6 | }␊
`

> Error 1/1
> Error 1/2
`␊
1 | function x() {␊
> 2 | return!␊
| ^ Negated expression in not allowed here.␊
3 | foo === bar;␊
4 | }␊
4 | throw!␊
5 | foo === bar;␊
6 | }␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Switch to '!==' check.␊
1 | function x() {␊
2 | return (␊
3 | foo !== bar);␊
4 | throw!␊
5 | foo === bar;␊
6 | }␊
`

> Error 2/2
`␊
1 | function x() {␊
2 | return!␊
3 | foo === bar;␊
> 4 | throw!␊
| ^ Negated expression in not allowed here.␊
5 | foo === bar;␊
6 | }␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Switch to '!==' check.␊
1 | function x() {␊
2 | return ␊
3 | foo !== bar;␊
4 | }␊
2 | return!␊
3 | foo === bar;␊
4 | throw (␊
5 | foo !== bar);␊
6 | }␊
`

## invalid(7): foo !(a) === b
Expand Down
Binary file modified test/snapshots/no-negation-in-equality-check.mjs.snap
Binary file not shown.

0 comments on commit 3c91502

Please sign in to comment.