@samitouri / QOS-React-1 / commits / a2aa032276

[eslint] Plugin should never throw

This has caused issues for people when things like Babel cause issues. It's not actionable and it crashes eslint. Just like the Babel plugin, the eslint plugin should never throw. Instead, let's log the error so the data isn't lost.

Lauren Tan committed Nov 3, 2023 at 16:27 UTC a2aa032276a9546a7bc06d118ca7de98a5fae651
1 file changed +11 -2
compiler/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts
+11 -2
@@ -23,6 +23,8 @@ type CompilerErrorDetailWithLoc = Omit<CompilerErrorDetail, "loc"> & {
23 loc: BabelSourceLocation;
24 };
25
26 +type UserProvidedLogger = (...args: unknown[]) => void;
27 +
28 function assertExhaustive(_: never, errorMsg: string): never {
29 throw new Error(errorMsg);
30 }
@@ -77,6 +79,13 @@ const rule: Rule.RuleModule = {
79 hasSuggestions: true,
80 },
81 create(context: Rule.RuleContext) {
82 + let logger: UserProvidedLogger | null = null;
83 + if (
84 + context.options[0] != null &&
85 + typeof context.options[0] === "function"
86 + ) {
87 + logger = context.options[0];
88 + }
89 // Compat with older versions of eslint
90 const sourceCode = context.sourceCode?.text ?? context.getSourceCode().text;
91 const filename = context.filename ?? context.getFilename();
@@ -164,8 +173,8 @@ const rule: Rule.RuleModule = {
173 suggest,
174 });
175 }
167 - } else {
168 - throw err;
176 + } else if (logger != null) {
177 + logger(err);
178 }
179 }
180 }