[eslint-plugin] Enable validation passes

This commit is contained in:
Lauren Tan
2023-06-15 14:57:10 -04:00
parent 0fdb1e5ac7
commit 0d01d5f69c
3 changed files with 31 additions and 1 deletions
@@ -13,6 +13,7 @@ export {
} from "./CompilerError";
export {
CompilerPipelineValue,
PluginOptions,
compileFn as compile,
compileProgram,
parsePluginOptions,
@@ -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<BuiltInRefValue> (5:5)",
line: 5,
column: 10,
endColumn: 15,
endLine: 5,
},
],
},
{
name: "[Invariant] Defined after use",
code: normalizeIndent`
@@ -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<PluginOptions> = {
panicOnBailout: false,
environment: {
validateHooksUsage: true,
validateFrozenLambdas: true,
validateRefAccessDuringRender: true,
},
};
compileProgram(prog, {
opts: parsePluginOptions({ panicOnBailout: false }),
opts: parsePluginOptions(opts),
filename: context.filename,
comments: babelAST.comments ?? [],
});