From 5aa9c499da48b2d3187270d5d8ece71ad7521f56 Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Wed, 20 Dec 2023 11:36:18 -0500 Subject: [PATCH] feat!: check for parsing errors in suggestion fixes (#16639) * feat!: check for parsing errors in suggestion fixes * revert RuleTester changes --- lib/rule-tester/flat-rule-tester.js | 15 ++++++++- tests/lib/rule-tester/flat-rule-tester.js | 41 +++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/rule-tester/flat-rule-tester.js b/lib/rule-tester/flat-rule-tester.js index c790dab69e7..0a127c749a0 100644 --- a/lib/rule-tester/flat-rule-tester.js +++ b/lib/rule-tester/flat-rule-tester.js @@ -764,7 +764,9 @@ class FlatRuleTester { messages, output, beforeAST, - afterAST: cloneDeeplyExcludesParent(afterAST) + afterAST: cloneDeeplyExcludesParent(afterAST), + configs, + filename }; } @@ -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}"`); } }); diff --git a/tests/lib/rule-tester/flat-rule-tester.js b/tests/lib/rule-tester/flat-rule-tester.js index fe97bd2c851..5f47ce1fe4b 100644 --- a/tests/lib/rule-tester/flat-rule-tester.js +++ b/tests/lib/rule-tester/flat-rule-tester.js @@ -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, {