Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: support multiple fixes in a report (fixes #7348) #8101

Merged
merged 9 commits into from Jun 18, 2017
8 changes: 5 additions & 3 deletions lib/rule-context.js
Expand Up @@ -92,12 +92,14 @@ function mergeFixes(fixes, sourceCode) {
const start = fixes[0].range[0];
const end = fixes[fixes.length - 1].range[1];
let text = "";
let lastPos = start;
let lastPos = Math.max(0, start);

for (const fix of fixes) {
assert(fix.range[0] >= lastPos, "Fix objects must not be overlapped in a report.");
assert(fix.range[0] >= lastPos || fix.range[0] < 0, "Fix objects must not be overlapped in a report.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this fail to detect a conflict if two fixes have ranges [-1, 0] and [-1, 5]? It seems like the assertion will always pass for fixes that start at -1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.


text += originalText.slice(lastPos, fix.range[0]);
if (fix.range[0] >= 0) {
text += originalText.slice(lastPos, fix.range[0]);
}
text += fix.text;
lastPos = fix.range[1];
}
Expand Down
74 changes: 74 additions & 0 deletions tests/lib/rule-context.js
Expand Up @@ -161,6 +161,80 @@ describe("RuleContext", () => {

mockESLint.verify();
});


it("should handle inserting BOM correctly.", () => {
const mockESLint = sandbox.mock(eslint);

mockESLint.expects("getSourceCode")
.returns({ text: "var foo = 100;" });
mockESLint.expects("report")
.once()
.withArgs(
sinon.match.any,
sinon.match.any,
sinon.match.any,
sinon.match.any,
sinon.match.any,
sinon.match.any,
sinon.match({
range: [0, 13],
text: "\uFEFFvar bar = 234"
})
);

ruleContext.report({
node: {},
loc: {},
message: "Message",
fix(fixer) {
return [
fixer.insertTextBeforeRange([0, 1], "\uFEFF"),
fixer.replaceTextRange([10, 13], "234"),
fixer.replaceTextRange([4, 7], "bar")
];
}
});

mockESLint.verify();
});


it("should handle removing BOM correctly.", () => {
const mockESLint = sandbox.mock(eslint);

mockESLint.expects("getSourceCode")
.returns({ text: "var foo = 100;" });
mockESLint.expects("report")
.once()
.withArgs(
sinon.match.any,
sinon.match.any,
sinon.match.any,
sinon.match.any,
sinon.match.any,
sinon.match.any,
sinon.match({
range: [-1, 13],
text: "var bar = 234"
})
);

ruleContext.report({
node: {},
loc: {},
message: "Message",
fix(fixer) {
return [
fixer.removeRange([-1, 0]),
fixer.replaceTextRange([10, 13], "234"),
fixer.replaceTextRange([4, 7], "bar")
];
}
});

mockESLint.verify();
});
});
});

Expand Down