diff --git a/compiler/forget/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts b/compiler/forget/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts index acea0390ec..b879dceaa5 100644 --- a/compiler/forget/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts +++ b/compiler/forget/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts @@ -5,13 +5,11 @@ * LICENSE file in the root directory of this source tree. */ +import { transformFromAstSync } from "@babel/core"; import * as parser from "@babel/parser"; -import traverse from "@babel/traverse"; -import { +import ReactForgetBabelPlugin, { CompilerError, - PluginOptions, - compileProgram, - parsePluginOptions, + type PluginOptions, } from "babel-plugin-react-forget"; import type { Rule } from "eslint"; @@ -26,43 +24,48 @@ const rule: Rule.RuleModule = { create(context: Rule.RuleContext) { // Compat with older versions of eslint const sourceCode = context.sourceCode?.text ?? context.getSourceCode().text; + const filename = context.filename ?? context.getFilename(); + + const opts: Partial = { + panicOnBailout: false, + environment: { + validateHooksUsage: true, + validateFrozenLambdas: true, + validateRefAccessDuringRender: true, + }, + }; const babelAST = parser.parse(sourceCode, { + sourceFilename: filename, plugins: ["jsx", "flow"], sourceType: "module", }); if (babelAST != null) { - traverse(babelAST, { - Program(prog) { - try { - const opts: Partial = { - panicOnBailout: false, - environment: { - validateHooksUsage: true, - validateFrozenLambdas: true, - validateRefAccessDuringRender: true, - }, - }; - compileProgram(prog, { - opts: parsePluginOptions(opts), - filename: context.filename, - comments: babelAST.comments ?? [], - }); - } catch (err) { - if (err instanceof CompilerError) { - for (const detail of err.details) { - if (detail.loc != null) { - context.report({ - message: detail.toString(), - loc: detail.loc, - }); - } - } - } else { - throw new Error(err); + try { + transformFromAstSync(babelAST, sourceCode, { + filename, + highlightCode: false, + retainLines: true, + plugins: [ + [ReactForgetBabelPlugin, opts], + "babel-plugin-fbt", + "babel-plugin-fbt-runtime", + ], + sourceType: "module", + }); + } catch (err) { + if (err instanceof CompilerError) { + for (const detail of err.details) { + if (detail.loc != null) { + context.report({ + message: detail.toString(), + loc: detail.loc, + }); } } - }, - }); + } else { + throw new Error(err); + } + } } return {}; },