Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 11, 2024
1 parent d0f9369 commit 6d8894a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 48 deletions.
4 changes: 2 additions & 2 deletions docs/rules/no-negation-in-equality-check.md
@@ -1,4 +1,4 @@
# Disallow negated expreesion in equality check
# Disallow negated expression in equality check

💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs).

Expand All @@ -7,7 +7,7 @@
<!-- end auto-generated rule header -->
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` -->

<!-- Remove this comment, add more detailed description. -->
Using a negated expression in equality check is most likely a mistake.

## Fail

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -145,7 +145,7 @@ If you don't use the preset, ensure you use the same `env` and `parserOptions` c
| [no-lonely-if](docs/rules/no-lonely-if.md) | Disallow `if` statements as the only statement in `if` blocks without `else`. || 🔧 | |
| [no-magic-array-flat-depth](docs/rules/no-magic-array-flat-depth.md) | Disallow a magic number as the `depth` argument in `Array#flat(…).` || | |
| [no-negated-condition](docs/rules/no-negated-condition.md) | Disallow negated conditions. || 🔧 | |
| [no-negation-in-equality-check](docs/rules/no-negation-in-equality-check.md) | Disallow negated expreesion in equality check. || | 💡 |
| [no-negation-in-equality-check](docs/rules/no-negation-in-equality-check.md) | Disallow negated expression in equality check. || | 💡 |
| [no-nested-ternary](docs/rules/no-nested-ternary.md) | Disallow nested ternary expressions. || 🔧 | |
| [no-new-array](docs/rules/no-new-array.md) | Disallow `new Array()`. || 🔧 | 💡 |
| [no-new-buffer](docs/rules/no-new-buffer.md) | Enforce the use of `Buffer.from()` and `Buffer.alloc()` instead of the deprecated `new Buffer()`. || 🔧 | 💡 |
Expand Down
87 changes: 42 additions & 45 deletions rules/no-negation-in-equality-check.js
Expand Up @@ -20,56 +20,53 @@ const isEqualityCheck = node => node.type === 'BinaryExpression' && EQUALITY_OPE
const isNegatedExpression = node => node.type === 'UnaryExpression' && node.prefix && node.operator === '!';

/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
return {
BinaryExpression(binaryExpression) {
const {operator, left} = binaryExpression;
const create = context => ({
BinaryExpression(binaryExpression) {
const {operator, left} = binaryExpression;

if (
!isEqualityCheck(binaryExpression)
|| !isNegatedExpression(left)
) {
return;
}
if (
!isEqualityCheck(binaryExpression)
|| !isNegatedExpression(left)
) {
return;
}

const {sourceCode} = context;
const bangToken = sourceCode.getFirstToken(left);
const negatedOperator = `${operator.startsWith('!') ? '=' : '!'}${operator.slice(1)}`;

const {sourceCode} = context;
const bangToken = sourceCode.getFirstToken(left);
const negatedOperator = `${operator.startsWith('!') ? '=' : '!'}${operator.slice(1)}`
return {
node: bangToken,
messageId: MESSAGE_ID_ERROR,
/** @param {import('eslint').Rule.RuleFixer} fixer */
suggest: [
{
messageId: MESSAGE_ID_SUGGESTION,
data: {
operator: negatedOperator,
},
/** @param {import('eslint').Rule.RuleFixer} fixer */
* fix(fixer) {
yield * fixSpaceAroundKeyword(fixer, binaryExpression, sourceCode);
yield fixer.remove(bangToken);

return {
node: bangToken,
messageId: MESSAGE_ID_ERROR,
/** @param {import('eslint').Rule.RuleFixer} fixer */
suggest: [
{
messageId: MESSAGE_ID_SUGGESTION,
data: {
operator: negatedOperator,
},
/** @param {import('eslint').Rule.RuleFixer} fixer */
* fix (fixer) {
yield * fixSpaceAroundKeyword(fixer, binaryExpression, sourceCode)
yield fixer.remove(bangToken);
const previousToken = sourceCode.getTokenBefore(bangToken);
const textAfter = sourceCode.getTokenAfter(bangToken).value;
if (needsSemicolon(previousToken, sourceCode, textAfter)) {
yield fixer.insertTextAfter(bangToken, ';');
}

const previousToken = sourceCode.getTokenBefore(bangToken);
const textAfter = sourceCode.getTokenAfter(bangToken).value;
if (needsSemicolon(previousToken, sourceCode, textAfter)) {
yield fixer.insertTextAfter(bangToken, ';');
}

const operatorToken = sourceCode.getTokenAfter(
left,
token => token.type == 'Punctuator' && token.value === operator,
);
yield fixer.replaceText(operatorToken, negatedOperator);
},
}
],
};
},
};
};
const operatorToken = sourceCode.getTokenAfter(
left,
token => token.type == 'Punctuator' && token.value === operator,

Check failure on line 61 in rules/no-negation-in-equality-check.js

View workflow job for this annotation

GitHub Actions / lint-test (ubuntu-latest)

Expected '===' and instead saw '=='.
);
yield fixer.replaceText(operatorToken, negatedOperator);
},
},
],
};
},
});

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
Expand Down
1 change: 1 addition & 0 deletions test/no-negation-in-equality-check.mjs
Expand Up @@ -7,6 +7,7 @@ test.snapshot({
valid: [
'!foo instanceof bar',
'+foo === bar',
'!(foo === bar)',
// We are not checking right side
'foo === !bar',
],
Expand Down

0 comments on commit 6d8894a

Please sign in to comment.