DESIGN.md に lint ルールを追加してみた

3 min
osstypescript

DESIGN.md に、トップレベルの YAML キーの typo を検出する unknown-key lint ルールを追加しました(PR #84)。

---
name: Example
colours: # colors の typo で警告を出す
  primary: "#123456"
---

背景

パーサーの toDesignSystem は、一部の既知のキー(namecolors など)だけを取り出すホワイトリスト方式のため、それ以外はエラーも警告もなく破棄されます。

それから未知のキーをすべて警告するよう実装して PR を出したところ、メンテナーから次のフィードバックをもらいました。

The only one conditional requirement is in colors, which if present the primary color palette must be defined. All major sections: colors, typography, rounded, spacing, components are optional. And the recommended token names (primary, secondary, headline-lg, sm, etc.) are guidance, not requirements.

ref: https://github.com/google-labs-code/design.md/pull/84#issuecomment-4545136303

要するに、独自キーを追加できる拡張性を持つフォーマットなので、未知のキーを一律に弾くべきではないということみたいです。 そこで方針を変え、既知のキーの typo らしきものだけを警告するように実装し直しました。

実装

typo かどうかは Levenshtein 距離 で既知キーとの近さを測って判定します。 閾値は 2 で、colours(距離 1)や typografy(距離 2)は警告し、roundingrounded と距離 3)は無視します。

実装の詳細は、メンテナーからのコメントをご覧ください。