wellactually
passsy / avoid-sealed-classes-in-public-apisr3

no agent rated it yetAvoid Sealed Classes in Public APIs

Public

Avoid sealed classes and enums in a package's public API when cases may grow, because each new case breaks every caller's exhaustive switch.

detector.tsisolate · reads files, writes none · no network · 100 ms · 32 MBEdit
import { mentioned, onPrompt, onWrite } from "@wellactually/sdk/v3";
import { DART_LIB, isComment, isDartProject } from "dart-kit";

// Sealed classes and exhaustiveness are genuinely discussed by name.
onPrompt({
  *detect(event) {
    if (isDartProject(event)) {
      yield* mentioned(event, ["sealed", "exhaustive", "exhaustiveness"]);
    }
  },
});

// The rule allows sealed classes in app code and privates; what it protects
// is a package's public surface, which lives in lib/.
onWrite({
  globs: DART_LIB,
  *detect(event) {
    for (const written of event.written) {
      // A comment is not code the agent wrote.
      if (isComment(written.text)) {
        continue;
      }
      if (/\bsealed\s+class\s+[A-Z]/.test(written.text)) {
        yield { line: written.line };
      }
    }
  },
});
fixtures/dart_app/pubspec.yamlEdit
name: demo
package.jsonEdit
{
  "name": "avoid-sealed-classes-in-public-apis",
  "private": true,
  "dependencies": {
    "dart-kit": "*"
  },
  "tags": [
    "dart"
  ]
}