From 0d01d5f69cd7ded5513a86b72a9d719f837945fd Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Thu, 15 Jun 2023 14:57:10 -0400 Subject: [PATCH] [eslint-plugin] Enable validation passes --- .../babel-plugin-react-forget/src/index.ts | 1 + .../__tests__/ReactForgetDiagnostics-test.ts | 20 +++++++++++++++++++ .../src/rules/ReactForgetDiagnostics.ts | 11 +++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/index.ts b/compiler/forget/packages/babel-plugin-react-forget/src/index.ts index b0d32b600b..5b644863d1 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/index.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/index.ts @@ -13,6 +13,7 @@ export { } from "./CompilerError"; export { CompilerPipelineValue, + PluginOptions, compileFn as compile, compileProgram, parsePluginOptions, diff --git a/compiler/forget/packages/eslint-plugin-react-forget/__tests__/ReactForgetDiagnostics-test.ts b/compiler/forget/packages/eslint-plugin-react-forget/__tests__/ReactForgetDiagnostics-test.ts index cac6559e29..e00821991c 100644 --- a/compiler/forget/packages/eslint-plugin-react-forget/__tests__/ReactForgetDiagnostics-test.ts +++ b/compiler/forget/packages/eslint-plugin-react-forget/__tests__/ReactForgetDiagnostics-test.ts @@ -41,6 +41,26 @@ const tests: ForgetTestCases = { }, ], invalid: [ + { + name: "[InvalidInput] Ref access during render", + code: normalizeIndent` + function Component(props) { + const ref = useRef(null); + const value = ref.current; + return value; + } + `, + errors: [ + { + message: + "[ReactForget] InvalidInput: Ref values (the `current` property) may not be accessed during render. Cannot access ref value at freeze $23:TObject (5:5)", + line: 5, + column: 10, + endColumn: 15, + endLine: 5, + }, + ], + }, { name: "[Invariant] Defined after use", code: normalizeIndent` 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 a0007379c2..acea0390ec 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 @@ -9,6 +9,7 @@ import * as parser from "@babel/parser"; import traverse from "@babel/traverse"; import { CompilerError, + PluginOptions, compileProgram, parsePluginOptions, } from "babel-plugin-react-forget"; @@ -33,8 +34,16 @@ const rule: Rule.RuleModule = { traverse(babelAST, { Program(prog) { try { + const opts: Partial = { + panicOnBailout: false, + environment: { + validateHooksUsage: true, + validateFrozenLambdas: true, + validateRefAccessDuringRender: true, + }, + }; compileProgram(prog, { - opts: parsePluginOptions({ panicOnBailout: false }), + opts: parsePluginOptions(opts), filename: context.filename, comments: babelAST.comments ?? [], });