dart-seal-type-hierarchies

识别未声明为 sealed 的封闭类型层次结构,并将其改为 sealed,以便编译器能对 switch 语句执行穷尽性检查。涵盖同库(library)要求、对公共 API 可能造成的破坏性变更的权衡,以及如何将基于 is 的级联检查迁移为支持穷尽性的 switch 语句的实践步骤与注意事项。

仓库最近推送
GitHub Stars
149
Forks
17
Issues / PRs
2

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "dart-seal-type-hierarchies":https://skillpi.com/zh/skills/deepskill-96a4f027105cd72260b31aaa4b09556427f104ae5bf3a389fa2d65324056524c。安装前核对上游来源和版本:https://github.com/kevmoo/dash_skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

识别未声明为 sealed 的封闭类型层次结构,并将其改为 sealed,以便编译器能对 switch 语句执行穷尽性检查。涵盖同库(library)要求、对公共 API 可能造成的破坏性变更的权衡,以及如何将基于 is 的级联检查迁移为支持穷尽性的 switch 语句的实践步骤与注意事项。

0 下载

介绍来源: third_party · kevmoo · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
适合谁
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
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.

当前收录版本

0.0.1

核对来源版本 ↗

安全与评测信息

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值75/100

亮点

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

亮点

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

亮点

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

亮点

  • 强调了 SDK 版本约束(>=3.0.0)和 public API 破坏性变更的风险,体现了对执行环境的可靠性考量
  • 提供了分步迁移指南,帮助用户安全地应用变更
证据可验 (前四项均值)88/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

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

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。