[eslint] Make the plugin configurable
As part of this PR, we remove the custom defined logger and use the logger from the plugin options.
Sathya Gunasekaran committed
Dec 14, 2023 at 15:37 UTC
201b46d947195376ffcf47337f8f2e13b009b0f9
1 file changed
+19
-16
compiler/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts
+19
-16
@@ -12,6 +12,8 @@ import type { SourceLocation as BabelSourceLocation } from "@babel/types";
12
import ReactForgetBabelPlugin, {
13
CompilerSuggestionOperation,
14
ErrorSeverity,
15
+ parsePluginOptions,
16
+ validateEnvironmentConfig,
17
type CompilerError,
18
type CompilerErrorDetail,
19
type PluginOptions,
@@ -23,8 +25,6 @@ type CompilerErrorDetailWithLoc = Omit<CompilerErrorDetail, "loc"> & {
25
loc: BabelSourceLocation;
26
};
27
26
-type UserProvidedLogger = (...args: unknown[]) => void;
27
-
28
function assertExhaustive(_: never, errorMsg: string): never {
29
throw new Error(errorMsg);
30
}
@@ -61,10 +61,6 @@ const COMPILER_OPTIONS: Partial<PluginOptions> = {
61
noEmit: true,
62
compilationMode: "infer",
63
panicThreshold: "CRITICAL_ERRORS",
64
- environment: {
65
- validateHooksUsage: true,
66
- validateNoSetStateInRender: true,
67
- },
64
};
65
66
const rule: Rule.RuleModule = {
@@ -76,18 +72,25 @@ const rule: Rule.RuleModule = {
72
},
73
fixable: "code",
74
hasSuggestions: true,
75
+ // validation is done at runtime with zod
76
+ schema: [{ type: "object", additionalProperties: true }],
77
},
78
create(context: Rule.RuleContext) {
81
- let logger: UserProvidedLogger | null = null;
82
- if (
83
- context.options[0] != null &&
84
- typeof context.options[0] === "function"
85
- ) {
86
- logger = context.options[0];
87
- }
79
// Compat with older versions of eslint
80
const sourceCode = context.sourceCode?.text ?? context.getSourceCode().text;
81
const filename = context.filename ?? context.getFilename();
82
+ const options: PluginOptions = {
83
+ ...parsePluginOptions(context.options[0] ?? {}),
84
+ ...COMPILER_OPTIONS,
85
+ };
86
+
87
+ try {
88
+ options.environment = validateEnvironmentConfig(
89
+ options.environment ?? {}
90
+ );
91
+ } catch (err) {
92
+ options.logger?.logEvent("", err);
93
+ }
94
95
const babelAST = HermesParser.parse(sourceCode, {
96
babel: true,
@@ -103,7 +106,7 @@ const rule: Rule.RuleModule = {
106
retainLines: true,
107
plugins: [
108
[PluginProposalPrivateMethods, { loose: true }],
106
- [ReactForgetBabelPlugin, COMPILER_OPTIONS],
109
+ [ReactForgetBabelPlugin, options],
110
],
111
sourceType: "module",
112
});
@@ -172,8 +175,8 @@ const rule: Rule.RuleModule = {
175
suggest,
176
});
177
}
175
- } else if (logger != null) {
176
- logger(err);
178
+ } else {
179
+ options.logger?.logEvent("", err);
180
}
181
}
182
}