passsy / prefer-exceptionsr4
Prefer Exceptions Over Either
PublicReturn Future<T> and let failures throw instead of returning Future<Either<T>>, because a Future can fail anyway and Either only adds complexity and false safety.
detector.tsisolate · reads files, writes none · no network · 100 ms · 32 MBEdit
import { mentioned, onPrompt, onWrite } from "@wellactually/sdk/v3";
import { addsDependency, DART, PUBSPEC } from "dart-kit";
// Deliberately not "either": too common in prose to be a signal.
onPrompt({
*detect(event) {
// Names only Dart has, so the prompt is about Dart without a pubspec.yaml to say so.
yield* mentioned(event, ["dartz", "fpdart"]);
},
});
onWrite({
globs: PUBSPEC,
*detect(event) {
for (const written of event.written) {
if (addsDependency(written.text, ["dartz", "fpdart"])) {
yield { line: written.line };
}
}
},
});
// `Either<` at a word start, so `MyEither<` stays invisible.
onWrite({
globs: DART,
*detect(event) {
for (const written of event.written) {
if (/(?<![A-Za-z0-9_$])Either</.test(written.text)) {
yield { line: written.line };
}
}
},
});
fixtures/dart_app/pubspec.yamlEdit
namedemo
environment
sdk^3.5.0
package.jsonEdit
{
"name": "prefer-exceptions",
"private": true,
"dependencies": {
"dart-kit": "*"
},
"tags": [
"dart",
"pub-deps"
]
}