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

fix: adjust suggestion fix ranges in processor #309

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading