dart-seal-type-hierarchies

Detect type hierarchies that are intended to be closed but not declared sealed, and seal them so the compiler can enforce exhaustiveness checks for switch statements. Covers same-library requirements, trade-offs regarding disruptive changes to public APIs, and practical guidance and considerations for migrating from cascaded is checks to exhaustiveness-supporting switch statements.

Last repository push
GitHub Stars
149
Forks
17
Issues / PRs
2

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "dart-seal-type-hierarchies" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-96a4f027105cd72260b31aaa4b09556427f104ae5bf3a389fa2d65324056524c. Verify the upstream source and version against https://github.com/kevmoo/dash_skills before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

github.com

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

Detect type hierarchies that are intended to be closed but not declared sealed, and seal them so the compiler can enforce exhaustiveness checks for switch statements. Covers same-library requirements, trade-offs regarding disruptive changes to public APIs, and practical guidance and considerations for migrating from cascaded is checks to exhaustiveness-supporting switch statements.

0 Downloads

Introduction source: third_party · kevmoo · 0.0.1 Reference

SkillPi interpretation

Tasks
|-
Audience
Use this skill when: A package declares an abstract base type whose subtypes are all defined in the same library, but the base type is not marked `sealed`. Code branches over the members of such a hierarchy with `is` checks or a non-exhaustive `switch`, so adding a subtype later fails silently at runtime instead of loudly at compile time. Reviewing a new type hierarchy and deciding whether it should be open to external subtyping.
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: dart-seal-type-hierarchies
description: |-
  Identify closed type hierarchies that are not declared `sealed`, and seal
  them so the compiler can enforce switch exhaustiveness. Covers the same-library
  requirement, the public-API breaking-change tradeoff, and the migration from
  `is` cascades to exhaustive switches.
key_features:
  - Closed hierarchy detection
  - Exhaustiveness enforcement
  - Public API breaking-change analysis

Seal Type Hierarchies

1. When to use this skill

Use this skill when:

  • A package declares an abstract base type whose subtypes are all defined in the same library, but the base type is not marked sealed.
  • Code branches over the members of such a hierarchy with is checks or a non-exhaustive switch, so adding a subtype later fails silently at runtime instead of loudly at compile time.
  • Reviewing a new type hierarchy and deciding whether it should be open to external subtyping.

When NOT to use (Abstention Guardrails)

Do NOT seal a type when:

  • Subtypes span multiple libraries: sealed requires every direct subtype to be declared in the same library as the base type. If subtypes live in other files (and are not part of the same library), the code will not compile. Either move them or leave the hierarchy open.
  • The type is public API and external extension is intended: Sealing a type exported from a published package is a breaking change for any downstream package that extends or implements it. Plugin interfaces, visitor bases, and extension points are meant to be open. Do not seal them to win exhaustiveness.
  • The base type is concrete and instantiated: sealed implies abstract. If callers construct the base type directly, sealing it breaks them, and the fix is a larger refactor than this skill covers.
  • Fewer than two subtypes: A single subtype is specialization, not an algebraic hierarchy. Exhaustiveness checking buys nothing.
  • SDK constraint below 3.0.0: Class modifiers do not exist before Dart 3. Check environment.sdk in pubspec.yaml first.

2. Why sealing is the correctness move

The value is not switch syntax. It is that the compiler starts rejecting incomplete branching:

seal the hierarchy -> exhaustiveness checking -> adding a subtype becomes
a compile error at every switch, instead of a silent fallthrough

An unsealed hierarchy with is cascades is not wrong today. It becomes wrong the moment someone adds a subtype, and nothing tells them which call sites they missed. Sealing is prophylactic: it converts a future runtime bug into a present-day compile error.

This is a different concern from preferring pattern matching for readability. Pattern matching over an unsealed hierarchy is a style choice. Sealing the hierarchy is a correctness guarantee, and the ergonomic payoff is a consequence, not the goal.

3. Detection

A type is a candidate when all of the following hold:

  1. It is declared abstract and is not already sealed.
  2. It has two or more direct subtypes.
  3. Every direct subtype is declared in the same library as the base type.
  4. Sealing it is not a breaking change: it lives under lib/src/, or is already marked final, or the package is an application rather than a published library.

Conditions 1 through 3 are decidable from the syntax tree. Condition 4 requires knowing the author's intent about the public API and is the part a human or an LLM must confirm.

This detection is not expressible as a single-file regular expression. It is a question about the package-wide type graph: you must collect every type declaration and every extends/implements/with edge before you can tell whether a hierarchy is closed.

4. Applying the change

Before

abstract class Shape {}

class Circle extends Shape {
  final double radius;
  Circle(this.radius);
}

class Square extends Shape {
  final double side;
  Square(this.side);
}

double area(Shape shape) {
  if (shape is Circle) {
    return 3.14159 * shape.radius * shape.radius;
  } else if (shape is Square) {
    return shape.side * shape.side;
  }
  // Silently reached when a new subtype is added.
  throw ArgumentError('Unknown shape: $shape');
}

After

sealed class Shape {}

final class Circle extends Shape {
  final double radius;
  Circle(this.radius);
}

final class Square extends Shape {
  final double side;
  Square(this.side);
}

double area(Shape shape) => switch (shape) {
  Circle(:final radius) => 3.14159 * radius * radius,
  Square(:final side) => side * side,
};

Adding class Triangle extends Shape now produces a compile error at area, naming the missing case. The throw and its unreachable-by-construction error message are gone.

Migration steps

  1. Add sealed to the base type declaration.
  2. Run dart analyze. Every non-exhaustive switch over the hierarchy now reports an error; every subtype declared outside the library reports one too.
  3. Convert is cascades to switch expressions, removing the trailing throw/default that existed only to satisfy the return type.
  4. Consider marking leaf subtypes final to prevent further extension.

5. Interaction with other skills

  • dart-use-pattern-matching / dart-modern-features: apply after sealing. Rewriting an is cascade as a switch over an unsealed type is a readability change only; the same rewrite over a sealed type is checked by the compiler.
  • Do not treat this skill as a reason to seal a hierarchy you do not own the evolution of.

Current listing version

0.0.1

Verify the source revision ↗

Safety and assessment details

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value75/100

Highlights

  • 针对 Dart 开发者解决 sealed 类型层次结构的识别与迁移问题,具有明确的技术实用价值
  • 提供了清晰的适用场景和 abstention guardrails,帮助用户判断何时不应使用该技能
Trigger Precision92/100

Highlights

  • description 精准描述了核心功能:识别未 sealed 的封闭类型层次结构并密封以实现穷尽性检查
  • 涵盖了关键约束条件(同库要求、API 破坏性变更)和迁移路径,触发边界清晰
Structure and Standards95/100

Highlights

  • 文档结构完整,包含使用场景、原理说明、检测方法、应用步骤、迁移指南和与其他技能的交互
  • 提供了清晰的 before/after 代码示例,步骤划分明确,frontmatter 完整规范
Execution Reliability88/100

Highlights

  • 强调了 SDK 版本约束(>=3.0.0)和 public API 破坏性变更的风险,体现了对执行环境的可靠性考量
  • 提供了分步迁移指南,帮助用户安全地应用变更
Evidence Verifiability (mean of the first four)88/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

These common operations were not identified in the main document. This does not mean no permissions or risks exist.

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.