Skip to content

Commit

Permalink
remove evaluate-literal-unaryexpression.js to improve codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy authored and fisker committed Aug 23, 2024
1 parent bf5ddf0 commit b88b016
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 45 deletions.
40 changes: 35 additions & 5 deletions rules/consistent-indexof-check.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const evaluateLiteralUnaryExpression = require('./utils/evaluate-literal-unaryexpression.js');
const {isMethodCall} = require('./ast/index.js');

const MESSAGE_ID = 'consistent-indexof-check';
Expand All @@ -14,6 +13,41 @@ const comparisonMap = {
'>=': '<=',
};

/**
Evaluate the value of a UnaryExpression node which contain a Literal node.
Make sure to call this function only when you are sure that the node is a UnaryExpression node or Literal node.
@param {import('estree').Node} node
@returns
*/
function evaluateLiteralUnaryExpression(node) {
// Base case: if the argument is a numeric literal, return its value directly
if (node.type === 'Literal') {
return node.value;
}

// If the argument is another UnaryExpression, recursively evaluate its value
if (node.type === 'UnaryExpression') {
const argumentValue = evaluateLiteralUnaryExpression(node.argument);

switch (node.operator) {
case '-': {
return -argumentValue;
}

case '+': {
return Number(argumentValue);
}

case '~': {
return ~argumentValue; // eslint-disable-line no-bitwise
}

default:
}
}
}

/**
Determine the appropriate replacement based on the operator and value.
Expand Down Expand Up @@ -56,10 +90,6 @@ Find and process references to a given identifier.
function findAndProcessReferences(context, identifierNode) {
const variable = context.sourceCode.getDeclaredVariables(identifierNode.parent).find(variable => variable.name === identifierNode.name);

if (!variable) {
return;
}

for (const reference of variable.references) {
if (reference.identifier === identifierNode) {
continue;
Expand Down
40 changes: 0 additions & 40 deletions rules/utils/evaluate-literal-unaryexpression.js

This file was deleted.

0 comments on commit b88b016

Please sign in to comment.