mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[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.
This commit is contained in:
@@ -23,6 +23,8 @@ type CompilerErrorDetailWithLoc = Omit<CompilerErrorDetail, "loc"> & {
|
||||
loc: BabelSourceLocation;
|
||||
};
|
||||
|
||||
type UserProvidedLogger = (...args: unknown[]) => void;
|
||||
|
||||
function assertExhaustive(_: never, errorMsg: string): never {
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
@@ -77,6 +79,13 @@ const rule: Rule.RuleModule = {
|
||||
hasSuggestions: 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();
|
||||
@@ -164,8 +173,8 @@ const rule: Rule.RuleModule = {
|
||||
suggest,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
throw err;
|
||||
} else if (logger != null) {
|
||||
logger(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user