Skip to content

Commit

Permalink
Fix: Support autofix at the very start of blocks
Browse files Browse the repository at this point in the history
Previously, if a rule added an autofix at index 0 of a code block, all
code from the start of the markdown file until the start of that code
block would be removed. Now, the autofix is applied as expected.

Fixes lydell/eslint-plugin-simple-import-sort#22
  • Loading branch information
lydell committed Sep 7, 2019
1 parent 2de2490 commit 407d6d0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,11 @@ function adjustBlock(block) {
range: message.fix.range.map(range => {

// Advance through the block's range map to find the last
// matching range by finding the first range too far and
// then going back one.
// matching range by finding the first equal or greater
// range and then going back one.
let i = 1;

while (i < block.rangeMap.length && block.rangeMap[i].js < range) {
while (i < block.rangeMap.length && block.rangeMap[i].js <= range) {
i++;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,27 @@ describe("plugin", () => {
assert.strictEqual(actual, expected);
});

it("at the very start of a block", () => {
const input = [
"This is Markdown.",
"",
"```js",
"'use strict'",
"```"
].join("\n");
const expected = [
"This is Markdown.",
"",
"```js",
"\"use strict\"",
"```"
].join("\n");
const report = cli.executeOnText(input, "test.md");
const actual = report.results[0].output;

assert.strictEqual(actual, expected);
});

it("in blocks with uncommon tags", () => {
const input = [
"This is Markdown.",
Expand Down

0 comments on commit 407d6d0

Please sign in to comment.