Skip to content

Commit

Permalink
Update: ignore negative ranges in fixes (eslint#8133)
Browse files Browse the repository at this point in the history
  • Loading branch information
alberto authored and not-an-aardvark committed Feb 26, 2017
1 parent 163d751 commit ca1694b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
12 changes: 3 additions & 9 deletions lib/util/source-code-fixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ SourceCodeFixer.applyFixes = function(sourceCode, messages) {
const start = fix.range[0];
const end = fix.range[1];

// Remain it as a problem if it's overlapped.
if (lastPos >= start) {
// Remain it as a problem if it's overlapped or it's a negative range
if (lastPos >= start || start > end) {
remainingMessages.push(problem);
continue;
}
Expand All @@ -108,13 +108,7 @@ SourceCodeFixer.applyFixes = function(sourceCode, messages) {
// Make output to this fix.
output += text.slice(Math.max(0, lastPos), Math.max(0, start));
output += fix.text;

/*
* If the start of the range is larger than the end for some reason, make sure
* the text between the end and the start doesn't get duplicated.
* https://github.com/eslint/eslint/issues/8116
*/
lastPos = Math.max(start, end);
lastPos = end;
}
output += text.slice(Math.max(0, lastPos));

Expand Down
2 changes: 1 addition & 1 deletion tests/lib/rules/padded-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ ruleTester.run("padded-blocks", rule, {
},
{
code: "function foo() { // a\n\n b;\n}",
output: "function foo() {\n // a\n\n b;\n}",
output: "function foo() { // a\n\n b;\n}",
options: ["never"],
errors: [NEVER_MESSAGE]
}
Expand Down
11 changes: 9 additions & 2 deletions tests/lib/util/source-code-fixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ describe("SourceCodeFixer", () => {
assert.equal(result.messages.length, 0);
});


it("should ignore reversed ranges", () => {
const result = SourceCodeFixer.applyFixes(sourceCode, [REVERSED_RANGE]);

assert.equal(result.output, TEST_CODE);
});

});


Expand Down Expand Up @@ -414,10 +421,10 @@ describe("SourceCodeFixer", () => {
assert.equal(result.messages.length, 0);
});

it("should handle reversed ranges gracefully", () => {
it("should ignore reversed ranges", () => {
const result = SourceCodeFixer.applyFixes(sourceCode, [REVERSED_RANGE]);

assert.equal(result.output, "\uFEFFvar answer = 6 * 7;");
assert.equal(result.output, `\uFEFF${TEST_CODE}`);
});

});
Expand Down

0 comments on commit ca1694b

Please sign in to comment.