Skip to content

Commit

Permalink
feat!: check for parsing errors in suggestion fixes (#16639)
Browse files Browse the repository at this point in the history
* feat!: check for parsing errors in suggestion fixes

* revert RuleTester changes
  • Loading branch information
bmish committed Dec 20, 2023
1 parent b3e0bb0 commit 5aa9c49
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/rule-tester/flat-rule-tester.js
Expand Up @@ -764,7 +764,9 @@ class FlatRuleTester {
messages,
output,
beforeAST,
afterAST: cloneDeeplyExcludesParent(afterAST)
afterAST: cloneDeeplyExcludesParent(afterAST),
configs,
filename
};
}

Expand Down Expand Up @@ -1050,6 +1052,17 @@ class FlatRuleTester {
if (hasOwnProperty(expectedSuggestion, "output")) {
const codeWithAppliedSuggestion = SourceCodeFixer.applyFixes(item.code, [actualSuggestion]).output;

// Verify if suggestion fix makes a syntax error or not.
const errorMessageInSuggestion =
linter.verify(codeWithAppliedSuggestion, result.configs, result.filename).find(m => m.fatal);

assert(!errorMessageInSuggestion, [
"A fatal parsing error occurred in suggestion fix.",
`Error: ${errorMessageInSuggestion && errorMessageInSuggestion.message}`,
"Suggestion output:",
codeWithAppliedSuggestion
].join("\n"));

assert.strictEqual(codeWithAppliedSuggestion, expectedSuggestion.output, `Expected the applied suggestion fix to match the test suggestion output for suggestion at index: ${index} on error with message: "${message.message}"`);
}
});
Expand Down
41 changes: 41 additions & 0 deletions tests/lib/rule-tester/flat-rule-tester.js
Expand Up @@ -2020,6 +2020,47 @@ describe("FlatRuleTester", () => {
}, "Error should have 2 suggestions. Instead found 1 suggestions");
});

it("should throw if suggestion fix made a syntax error.", () => {
assert.throw(() => {
ruleTester.run(
"foo",
{
meta: { hasSuggestions: true },
create(context) {
return {
Identifier(node) {
context.report({
node,
message: "make a syntax error",
suggest: [
{
desc: "make a syntax error",
fix(fixer) {
return fixer.replaceText(node, "one two");
}
}
]
});
}
};
}
},
{
valid: [""],
invalid: [{
code: "one()",
errors: [{
suggestions: [{
desc: "make a syntax error",
output: "one two()"
}]
}]
}]
}
);
}, /A fatal parsing error occurred in suggestion fix\.\nError: .+\nSuggestion output:\n.+/u);
});

it("should throw if the suggestion description doesn't match", () => {
assert.throws(() => {
ruleTester.run("suggestions-basic", require("../../fixtures/testers/rule-tester/suggestions").basic, {
Expand Down

0 comments on commit 5aa9c49

Please sign in to comment.