From 201b46d947195376ffcf47337f8f2e13b009b0f9 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Thu, 14 Dec 2023 15:37:41 +0000 Subject: [PATCH] [eslint] Make the plugin configurable As part of this PR, we remove the custom defined logger and use the logger from the plugin options. --- .../src/rules/ReactForgetDiagnostics.ts | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/compiler/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts b/compiler/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts index 3f64efa695..ee28a09ec4 100644 --- a/compiler/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts +++ b/compiler/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts @@ -12,6 +12,8 @@ import type { SourceLocation as BabelSourceLocation } from "@babel/types"; import ReactForgetBabelPlugin, { CompilerSuggestionOperation, ErrorSeverity, + parsePluginOptions, + validateEnvironmentConfig, type CompilerError, type CompilerErrorDetail, type PluginOptions, @@ -23,8 +25,6 @@ type CompilerErrorDetailWithLoc = Omit & { loc: BabelSourceLocation; }; -type UserProvidedLogger = (...args: unknown[]) => void; - function assertExhaustive(_: never, errorMsg: string): never { throw new Error(errorMsg); } @@ -61,10 +61,6 @@ const COMPILER_OPTIONS: Partial = { noEmit: true, compilationMode: "infer", panicThreshold: "CRITICAL_ERRORS", - environment: { - validateHooksUsage: true, - validateNoSetStateInRender: true, - }, }; const rule: Rule.RuleModule = { @@ -76,18 +72,25 @@ const rule: Rule.RuleModule = { }, fixable: "code", hasSuggestions: true, + // validation is done at runtime with zod + schema: [{ type: "object", additionalProperties: true }], }, create(context: Rule.RuleContext) { - let logger: UserProvidedLogger | null = null; - if ( - context.options[0] != null && - typeof context.options[0] === "function" - ) { - logger = context.options[0]; - } // Compat with older versions of eslint const sourceCode = context.sourceCode?.text ?? context.getSourceCode().text; const filename = context.filename ?? context.getFilename(); + const options: PluginOptions = { + ...parsePluginOptions(context.options[0] ?? {}), + ...COMPILER_OPTIONS, + }; + + try { + options.environment = validateEnvironmentConfig( + options.environment ?? {} + ); + } catch (err) { + options.logger?.logEvent("", err); + } const babelAST = HermesParser.parse(sourceCode, { babel: true, @@ -103,7 +106,7 @@ const rule: Rule.RuleModule = { retainLines: true, plugins: [ [PluginProposalPrivateMethods, { loose: true }], - [ReactForgetBabelPlugin, COMPILER_OPTIONS], + [ReactForgetBabelPlugin, options], ], sourceType: "module", }); @@ -172,8 +175,8 @@ const rule: Rule.RuleModule = { suggest, }); } - } else if (logger != null) { - logger(err); + } else { + options.logger?.logEvent("", err); } } }