diff --git a/lib/rules/eqeqeq.js b/lib/rules/eqeqeq.js index 58ff9030dbb..57926dbed0e 100644 --- a/lib/rules/eqeqeq.js +++ b/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; diff --git a/tests/lib/cli-engine/cli-engine.js b/tests/lib/cli-engine/cli-engine.js index 0c6db3ff93b..b3039e74314 100644 --- a/tests/lib/cli-engine/cli-engine.js +++ b/tests/lib/cli-engine/cli-engine.js @@ -1641,6 +1641,8 @@ describe("CLIEngine", () => { { column: 9, line: 2, + endColumn: 11, + endLine: 2, message: "Expected '===' and instead saw '=='.", messageId: "unexpected", nodeType: "BinaryExpression", diff --git a/tests/lib/rules/eqeqeq.js b/tests/lib/rules/eqeqeq.js index b74e9bc2ca5..05e33bbcfa9 100644 --- a/tests/lib/rules/eqeqeq.js +++ b/tests/lib/rules/eqeqeq.js @@ -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.