Skip to content

Commit

Permalink
Fix: eqeqeq rule reports incorrect locations (#12265)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and mysticatea committed Sep 13, 2019
1 parent 319e4d8 commit 01da7d0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 19 deletions.
26 changes: 7 additions & 19 deletions lib/rules/eqeqeq.js
Expand Up @@ -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
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/cli-engine/cli-engine.js
Expand Up @@ -1641,6 +1641,8 @@ describe("CLIEngine", () => {
{
column: 9,
line: 2,
endColumn: 11,
endLine: 2,
message: "Expected '===' and instead saw '=='.",
messageId: "unexpected",
nodeType: "BinaryExpression",
Expand Down
63 changes: 63 additions & 0 deletions tests/lib/rules/eqeqeq.js
Expand Up @@ -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.
Expand Down

0 comments on commit 01da7d0

Please sign in to comment.