Skip to content

Commit

Permalink
Fix: duplicated autofix output for inverted fix ranges (fixes #8116) (#…
Browse files Browse the repository at this point in the history
…8117)

The padded-blocks rule sometimes produces a fix range where the start index of the range is larger than the end index. Due to the changes in fcc38db, when source-code-fixer encountered such a range, it would output the text between the end index and the start index twice. This commit ensures that source-code-fixer behaves the same way as it previously did when it encounters an inverted fix range (it should act as if the start and end indices were equal). In the future, we should also update padded-blocks to avoid producing inverted fix ranges.
  • Loading branch information
not-an-aardvark authored and gyandeeps committed Feb 22, 2017
1 parent a421897 commit ff8a80c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/rules/padded-blocks.js
Expand Up @@ -188,6 +188,9 @@ module.exports = {
node,
loc: { line: openBrace.loc.start.line, column: openBrace.loc.start.column },
fix(fixer) {

// FIXME: The start of this range is sometimes larger than the end.
// https://github.com/eslint/eslint/issues/8116
return fixer.replaceTextRange([openBrace.end, nextToken.start - nextToken.loc.start.column], "\n");
},
message: NEVER_MESSAGE
Expand Down
8 changes: 7 additions & 1 deletion lib/util/source-code-fixer.js
Expand Up @@ -108,7 +108,13 @@ SourceCodeFixer.applyFixes = function(sourceCode, messages) {
// Make output to this fix.
output += text.slice(Math.max(0, lastPos), Math.max(0, start));
output += fix.text;
lastPos = end;

/*
* 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);
}
output += text.slice(Math.max(0, lastPos));

Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/padded-blocks.js
Expand Up @@ -463,6 +463,12 @@ ruleTester.run("padded-blocks", rule, {
line: 9
}
]
},
{
code: "function foo() { // a\n\n b;\n}",
output: "function foo() {\n // a\n\n b;\n}",
options: ["never"],
errors: [NEVER_MESSAGE]
}
]
});
13 changes: 13 additions & 0 deletions tests/lib/util/source-code-fixer.js
Expand Up @@ -122,6 +122,13 @@ const INSERT_AT_END = {
message: "nofix2",
line: 1,
column: 7
},
REVERSED_RANGE = {
message: "reversed range",
fix: {
range: [3, 0],
text: " "
}
};

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

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

assert.equal(result.output, "\uFEFFvar answer = 6 * 7;");
});

});

describe("Text Replacement", () => {
Expand Down

0 comments on commit ff8a80c

Please sign in to comment.