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

feat!: check for parsing errors in suggestion fixes #16639

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/rule-tester/flat-rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,9 @@ class FlatRuleTester {
messages,
output,
beforeAST,
afterAST: cloneDeeplyExcludesParent(afterAST)
afterAST: cloneDeeplyExcludesParent(afterAST),
configs,
filename
};
}

Expand Down Expand Up @@ -1047,6 +1049,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
15 changes: 14 additions & 1 deletion lib/rule-tester/rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,9 @@ class RuleTester {
messages,
output,
beforeAST,
afterAST: cloneDeeplyExcludesParent(afterAST)
afterAST: cloneDeeplyExcludesParent(afterAST),
config,
filename
};
}

Expand Down Expand Up @@ -1131,6 +1133,17 @@ class RuleTester {
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.config, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,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
41 changes: 41 additions & 0 deletions tests/lib/rule-tester/rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,47 @@ describe("RuleTester", () => {
}, "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