Skip to content
Permalink
Browse files

Fix: eqeqeq rule reports incorrect locations (#12265)

  • Loading branch information
mdjermanovic authored and mysticatea committed Sep 13, 2019
1 parent 319e4d8 commit 01da7d04c4e5a7376cf241ec02db7971726a1bf9
Showing with 72 additions and 19 deletions.
  1. +7 −19 lib/rules/eqeqeq.js
  2. +2 −0 tests/lib/cli-engine/cli-engine.js
  3. +63 −0 tests/lib/rules/eqeqeq.js
@@ -116,18 +116,6 @@ module.exports = {
return astUtils.isNullLiteral(node.right) || astUtils.isNullLiteral(node.left);
}

/**
* Gets the location (line and column) of the binary expression's operator
* @param {ASTNode} node The binary expression node to check
* @returns {Object} { line, column } location of operator
* @private
*/
function getOperatorLocation(node) {
const opToken = sourceCode.getTokenAfter(node.left);

return { line: opToken.loc.start.line, column: opToken.loc.start.column };
}

/**
* Reports a message for this rule.
* @param {ASTNode} node The binary expression node that was checked
@@ -136,21 +124,21 @@ module.exports = {
* @private
*/
function report(node, expectedOperator) {
const operatorToken = sourceCode.getFirstTokenBetween(
node.left,
node.right,
token => token.value === node.operator
);

context.report({
node,
loc: getOperatorLocation(node),
loc: operatorToken.loc,
messageId: "unexpected",
data: { expectedOperator, actualOperator: node.operator },
fix(fixer) {

// If the comparison is a `typeof` comparison or both sides are literals with the same type, then it's safe to fix.
if (isTypeOfBinary(node) || areLiteralsAndSameType(node)) {
const operatorToken = sourceCode.getFirstTokenBetween(
node.left,
node.right,
token => token.value === node.operator
);

return fixer.replaceText(operatorToken, expectedOperator);
}
return null;
@@ -1641,6 +1641,8 @@ describe("CLIEngine", () => {
{
column: 9,
line: 2,
endColumn: 11,
endLine: 2,
message: "Expected '===' and instead saw '=='.",
messageId: "unexpected",
nodeType: "BinaryExpression",
@@ -105,6 +105,69 @@ ruleTester.run("eqeqeq", rule, {
{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression", line: 1 },
{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression", line: 1 }
]
},

// location tests
{
code: "a == b;",
errors: [
{
messageId: "unexpected",
data: wantedEqEqEq,
type: "BinaryExpression",
column: 3,
endColumn: 5
}
]
},
{
code: "a!=b;",
errors: [
{
messageId: "unexpected",
data: wantedNotEqEq,
type: "BinaryExpression",
column: 2,
endColumn: 4
}
]
},
{
code: "(a + b) == c;",
errors: [
{
messageId: "unexpected",
data: wantedEqEqEq,
type: "BinaryExpression",
column: 9,
endColumn: 11
}
]
},
{
code: "(a + b) != c;",
errors: [
{
messageId: "unexpected",
data: wantedNotEqEq,
type: "BinaryExpression",
column: 10,
endColumn: 12
}
]
},
{
code: "((1) ) == (2);",
output: "((1) ) === (2);",
errors: [
{
messageId: "unexpected",
data: wantedEqEqEq,
type: "BinaryExpression",
column: 9,
endColumn: 11
}
]
}

// If no output is provided, assert that no output is produced.

0 comments on commit 01da7d0

Please sign in to comment.
You can’t perform that action at this time.