Skip to content

Commit

Permalink
fix: adjust suggestion fix ranges in processor (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored Dec 29, 2024
1 parent 6575159 commit f164e66
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 24 deletions.
62 changes: 38 additions & 24 deletions src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,36 @@ function preprocess(sourceText, filename) {
});
}

/**
* Adjusts a fix in a code block.
* @param {Block} block A code block.
* @param {Fix} fix A fix to adjust.
* @returns {Fix} The fix with adjusted ranges.
*/
function adjustFix(block, fix) {
return {
range: /** @type {Range} */ (
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.
let i = 1;

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

// Apply the mapping delta for this range.
return range + block.rangeMap[i - 1].md;
})
),
text: fix.text.replace(/\n/gu, `\n${block.baseIndentText}`),
};
}

/**
* Creates a map function that adjusts messages in a code block.
* @param {Block} block A code block.
Expand Down Expand Up @@ -376,33 +406,17 @@ function adjustBlock(block) {
out.endLine = message.endLine - leadingCommentLines + blockStart;
}

if (Array.isArray(message.suggestions)) {
out.suggestions = message.suggestions.map(suggestion => ({
...suggestion,
fix: adjustFix(block, suggestion.fix),
}));
}

const adjustedFix = {};

if (message.fix) {
adjustedFix.fix = {
range: /** @type {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.
let i = 1;

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

// Apply the mapping delta for this range.
return range + block.rangeMap[i - 1].md;
})
),
text: message.fix.text.replace(
/\n/gu,
`\n${block.baseIndentText}`,
),
};
adjustedFix.fix = adjustFix(block, message.fix);
}

return { ...message, ...out, ...adjustedFix };
Expand Down
22 changes: 22 additions & 0 deletions tests/processor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,16 @@ describe("processor", () => {
column: 5,
message: "Unexpected console statement.",
ruleId: "no-console",
suggestions: [
{
desc: "Replace with 'console.warn'.",
messageId: "replace",
fix: {
range: [45, 56],
text: "console.warn",
},
},
],
},
],
[
Expand Down Expand Up @@ -876,6 +886,18 @@ describe("processor", () => {
assert.deepStrictEqual(result[0].fix.range, [7, 8]);
});

it("should adjust suggestion fix range properties", () => {
const result = processor.postprocess(messages);

assert.deepStrictEqual(result[1].suggestions, [
{
desc: "Replace with 'console.warn'.",
messageId: "replace",
fix: { range: [66, 77], text: "console.warn" },
},
]);
});

describe("should exclude messages from unsatisfiable rules", () => {
it("eol-last", () => {
const result = processor.postprocess([
Expand Down

0 comments on commit f164e66

Please sign in to comment.