js-compiler: Run autofixes when format is enabled, otherwise do not
MODIFY
packages/@romejs/js-compiler/api/lint.ts
packages/@romejs/js-compiler/api/lint.ts
@@ -18,20 +18,19 @@ export type LintResult = {
diagnostics: PartialDiagnostics;
filters: DiagnosticFilters;
src: string;
- ast: Program;
};
const lintCache: Cache<LintResult> = new Cache();
export default async function lint(req: TransformRequest): Promise<LintResult> {
- const {ast, sourceText: src, project} = req;
+ const {sourceText: src, project} = req;
+ let {ast} = req;
if (!project.config.lint.enabled) {
return {
diagnostics: [],
filters: extractSuppressionsFromProgram(ast),
src,
- ast,
};
}
@@ -41,6 +40,9 @@ export default async function lint(req: TransformRequest): Promise<LintResult> {
return cached;
}
+ let formattedCode = src;
+ if (project.config.format.enabled) {
+ // Perform autofixes
const context = new Context({
ast,
project,
@@ -48,10 +50,10 @@ export default async function lint(req: TransformRequest): Promise<LintResult> {
category: 'lint',
},
});
- const newAst = program.assert(context.reduce(ast, lintTransforms));
+ const newAst = program.assert(
+ context.reduce(ast, lintTransforms, {frozen: false}),
+ );
- let formattedCode = src;
- if (project.config.format.enabled) {
const generator = generateJS(
newAst,
{
@@ -62,9 +64,18 @@ export default async function lint(req: TransformRequest): Promise<LintResult> {
formattedCode = generator.getCode() + '\n';
}
+ // Run lints (could be with the autofixed AST)
+ const context = new Context({
+ ast,
+ project,
+ origin: {
+ category: 'lint',
+ },
+ });
+ program.assert(context.reduce(ast, lintTransforms, {frozen: true}));
+
const result: LintResult = {
filters: extractSuppressionsFromProgram(ast),
- ast: newAst,
diagnostics: [...ast.diagnostics, ...context.diagnostics],
src: formattedCode,
};

js-compiler: Run autofixes when format is enabled, otherwise do not