@samitouri / QOS-React-2 / commits / e378eed0f3

[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

Lauren Tan committed Jun 16, 2023 at 16:23 UTC e378eed0f3ec04319e9501d0a1424f5f71499590
1 file changed +38 -35
compiler/forget/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts
+38 -35
@@ -5,13 +5,11 @@
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 +import { transformFromAstSync } from "@babel/core";
9 import * as parser from "@babel/parser";
9 -import traverse from "@babel/traverse";
10 -import {
10 +import ReactForgetBabelPlugin, {
11 CompilerError,
12 - PluginOptions,
13 - compileProgram,
14 - parsePluginOptions,
12 + type PluginOptions,
13 } from "babel-plugin-react-forget";
14 import type { Rule } from "eslint";
15
@@ -26,43 +24,48 @@ const rule: Rule.RuleModule = {
24 create(context: Rule.RuleContext) {
25 // Compat with older versions of eslint
26 const sourceCode = context.sourceCode?.text ?? context.getSourceCode().text;
27 + const filename = context.filename ?? context.getFilename();
28 +
29 + const opts: Partial<PluginOptions> = {
30 + panicOnBailout: false,
31 + environment: {
32 + validateHooksUsage: true,
33 + validateFrozenLambdas: true,
34 + validateRefAccessDuringRender: true,
35 + },
36 + };
37 const babelAST = parser.parse(sourceCode, {
38 + sourceFilename: filename,
39 plugins: ["jsx", "flow"],
40 sourceType: "module",
41 });
42 if (babelAST != null) {
34 - traverse(babelAST, {
35 - Program(prog) {
36 - try {
37 - const opts: Partial<PluginOptions> = {
38 - panicOnBailout: false,
39 - environment: {
40 - validateHooksUsage: true,
41 - validateFrozenLambdas: true,
42 - validateRefAccessDuringRender: true,
43 - },
44 - };
45 - compileProgram(prog, {
46 - opts: parsePluginOptions(opts),
47 - filename: context.filename,
48 - comments: babelAST.comments ?? [],
49 - });
50 - } catch (err) {
51 - if (err instanceof CompilerError) {
52 - for (const detail of err.details) {
53 - if (detail.loc != null) {
54 - context.report({
55 - message: detail.toString(),
56 - loc: detail.loc,
57 - });
58 - }
59 - }
60 - } else {
61 - throw new Error(err);
43 + try {
44 + transformFromAstSync(babelAST, sourceCode, {
45 + filename,
46 + highlightCode: false,
47 + retainLines: true,
48 + plugins: [
49 + [ReactForgetBabelPlugin, opts],
50 + "babel-plugin-fbt",
51 + "babel-plugin-fbt-runtime",
52 + ],
53 + sourceType: "module",
54 + });
55 + } catch (err) {
56 + if (err instanceof CompilerError) {
57 + for (const detail of err.details) {
58 + if (detail.loc != null) {
59 + context.report({
60 + message: detail.toString(),
61 + loc: detail.loc,
62 + });
63 }
64 }
64 - },
65 - });
65 + } else {
66 + throw new Error(err);
67 + }
68 + }
69 }
70 return {};
71 },