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(rule-tester): check for parsing errors in suggestion fixes #9052

Merged
28 changes: 24 additions & 4 deletions packages/rule-tester/src/RuleTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ export class RuleTester extends TestFramework {
throw new Error(DUPLICATE_PARSER_ERROR_MESSAGE);
}
if (!test.filename) {
return {
...test,
filename: getFilename(test.parserOptions),
};
return { ...test, filename: getFilename(test.parserOptions) };
}
return test;
};
Expand Down Expand Up @@ -444,6 +441,8 @@ export class RuleTester extends TestFramework {
output: string;
beforeAST: TSESTree.Program;
afterAST: TSESTree.Program;
config: RuleTesterConfig;
filename?: string;
} {
let config: TesterConfigWithDefaults = merge({}, this.#testerConfig);
let code;
Expand Down Expand Up @@ -617,6 +616,8 @@ export class RuleTester extends TestFramework {
// is definitely assigned within the `rule-tester/validate-ast` rule
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
afterAST: cloneDeeplyExcludesParent(afterAST!),
config,
filename,
};
}

Expand Down Expand Up @@ -981,6 +982,25 @@ export class RuleTester extends TestFramework {
[actualSuggestion],
).output;

// Verify if suggestion fix makes a syntax error or not.
const errorMessageInSuggestion = this.#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,
Expand Down