Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: eqeqeq rule reports incorrect locations #12265

Merged
merged 1 commit into from Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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