Skip to content

Commit

Permalink
Merge pull request #4544 from platinumazure/eqeqeq-autofix-issue
Browse files Browse the repository at this point in the history
Fix: Bugfix for eqeqeq autofix (fixes #4540)
  • Loading branch information
nzakas committed Nov 27, 2015
2 parents d24519f + 03ff54f commit 1a3d095
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/rules/eqeqeq.js
Expand Up @@ -93,7 +93,18 @@ module.exports = function(context) {
message: "Expected '{{op}}=' and instead saw '{{op}}'.",
data: { op: node.operator },
fix: function(fixer) {
return fixer.replaceText(sourceCode.getTokenAfter(node.left), replacements[node.operator]);
var tokens = sourceCode.getTokensBetween(node.left, node.right),
opToken,
i;

for (i = 0; i < tokens.length; ++i) {
if (tokens[i].value === node.operator) {
opToken = tokens[i];
break;
}
}

return fixer.replaceTextRange(opToken.range, replacements[node.operator]);
}
});

Expand Down
10 changes: 9 additions & 1 deletion tests/lib/rules/eqeqeq.js
Expand Up @@ -51,6 +51,14 @@ ruleTester.run("eqeqeq", rule, {
{ code: "'hello' != 'world'", output: "'hello' !== 'world'", options: ["allow-null"], errors: [{ message: "Expected '!==' and instead saw '!='.", type: "BinaryExpression"}] },
{ code: "2 == 3", output: "2 === 3", options: ["allow-null"], errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression"}] },
{ code: "true == true", output: "true === true", options: ["allow-null"], errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression"}] },
{ code: "a\n==\nb", output: "a\n===\nb", errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression", line: 2 }] }
{ code: "a\n==\nb", output: "a\n===\nb", errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression", line: 2 }] },
{ code: "(a) == b", output: "(a) === b", errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression", line: 1 }] },
{ code: "(a) != b", output: "(a) !== b", errors: [{ message: "Expected '!==' and instead saw '!='.", type: "BinaryExpression", line: 1 }] },
{ code: "a == (b)", output: "a === (b)", errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression", line: 1 }] },
{ code: "a != (b)", output: "a !== (b)", errors: [{ message: "Expected '!==' and instead saw '!='.", type: "BinaryExpression", line: 1 }] },
{ code: "(a) == (b)", output: "(a) === (b)", errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression", line: 1 }] },
{ code: "(a) != (b)", output: "(a) !== (b)", errors: [{ message: "Expected '!==' and instead saw '!='.", type: "BinaryExpression", line: 1 }] },
{ code: "(a == b) == (c)", output: "(a === b) === (c)", errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression", line: 1 }, { message: "Expected '===' and instead saw '=='.", type: "BinaryExpression", line: 1 }] },
{ code: "(a != b) != (c)", output: "(a !== b) !== (c)", errors: [{ message: "Expected '!==' and instead saw '!='.", type: "BinaryExpression", line: 1 }, { message: "Expected '!==' and instead saw '!='.", type: "BinaryExpression", line: 1 }] }
]
});

0 comments on commit 1a3d095

Please sign in to comment.