Skip to content

Commit

Permalink
Fix: Fixer to not overlap ranges among fix objects (fixes #4321)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyandeeps committed Nov 3, 2015
1 parent f8c1780 commit 6a76198
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/util/source-code-fixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ SourceCodeFixer.applyFixes = function(sourceCode, messages) {
fixes.forEach(function(problem) {
var fix = problem.fix;

if (fix.range[1] <= lastFixPos) {
if (fix.range[1] < lastFixPos) {
chars.splice(fix.range[0], fix.range[1] - fix.range[0], fix.text);
lastFixPos = fix.range[0];
} else {
Expand Down
25 changes: 21 additions & 4 deletions tests/lib/cli-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,10 +782,27 @@ describe("CLIEngine", function() {
},
{
"filePath": fs.realpathSync(path.resolve(fixtureDir, "fixmode/quotes-semi-eqeqeq.js")),
"messages": [],
"errorCount": 0,
"messages": [
{
"column": 11,
"fix": {
"range": [
10,
14
],
"text": "\"hi\""
},
"line": 1,
"message": "Strings must use doublequote.",
"nodeType": "Literal",
"ruleId": "quotes",
"severity": 2,
"source": "var msg = 'hi'"
}
],
"errorCount": 1,
"warningCount": 0,
"output": "var msg = \"hi\";\nif (msg === \"hi\") {\n\n}\n"
"output": "var msg = 'hi';\nif (msg === \"hi\") {\n\n}\n"
},
{
"filePath": fs.realpathSync(path.resolve(fixtureDir, "fixmode/quotes.js")),
Expand All @@ -805,7 +822,7 @@ describe("CLIEngine", function() {
"output": "var msg = \"hi\" + foo;\n"
}
],
"errorCount": 1,
"errorCount": 2,
"warningCount": 0
});
});
Expand Down
7 changes: 4 additions & 3 deletions tests/lib/util/source-code-fixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,11 @@ describe("SourceCodeFixer", function() {
assert.isTrue(result.fixed);
});

it("should apply two fixes when the end of one range is the same as the start of a previous range overlap", function() {
it("should apply one fix when the end of one range is the same as the start of a previous range overlap", function() {
var result = SourceCodeFixer.applyFixes(sourceCode, [ REMOVE_START, REPLACE_ID ]);
assert.equal(result.output, TEST_CODE.replace("var answer", "foo"));
assert.equal(result.messages.length, 0);
assert.equal(result.output, TEST_CODE.replace("answer", "foo"));
assert.equal(result.messages.length, 1);
assert.equal(result.messages[0].message, "removestart");
assert.isTrue(result.fixed);
});

Expand Down

0 comments on commit 6a76198

Please sign in to comment.