From e378eed0f3ec04319e9501d0a1424f5f71499590 Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Fri, 16 Jun 2023 16:23:18 -0400 Subject: [PATCH] [eslint-plugin] Use babel plugin instead of manual traversal This was causing some issues in the eslint plugin where the babel `hub` wasn't defined. afaik the hub is only setup when running the plugin as part of a babel pipeline, instead of a manual parse/traversal. We're using some of that infra for printing codeframes --- .../src/rules/ReactForgetDiagnostics.ts | 73 ++++++++++--------- 1 file changed, 38 insertions(+), 35 deletions(-) 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 {}; },