rules

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package rules は verify の検証ルール群を実装する。 各ルールは Context を受け取り、指摘(Issue)の一覧を返す純粋な関数。

Index

Constants

View Source
const DefaultSectionsFile = "sections.yaml"

DefaultSectionsFile は必須セクション定義の既定のファイル名。

Variables

This section is empty.

Functions

func LoadSections

func LoadSections(path string) (map[string]SectionSpec, error)

LoadSections は sections.yaml を読み込む。

未存在はエラーにする。読めなかったぶんを黙って空として扱うと、 定義ファイルを消したり綴りを誤ったりしたときに検査が素通りする。

func MarshalSections

func MarshalSections(sections map[string]SectionSpec) ([]byte, error)

MarshalSections は必須セクション定義を YAML へ整形する。

func SectionsFile

func SectionsFile(cfg *config.Config) (string, error)

SectionsFile は設定が指す必須セクション定義のパスを返す(設定ファイル基準)。

Types

type Context

type Context struct {
	Cfg      *config.Config
	Docs     []*parser.Document
	Sections map[string]SectionSpec
	Config   Settings
	// contains filtered or unexported fields
}

Context はルールが参照する解析済みデータ。 組み立ては NewContext だけが行う。

func NewContext

func NewContext(cfg *config.Config, docs []*parser.Document,
	sections map[string]SectionSpec, settings Settings) *Context

NewContext はルールが参照する文脈を組み立てる。

定義の索引をここで作るのは、別の手順に分けると呼び忘れが起きるためである。 呼び忘れると索引が空になり、ref_integrity が全参照を「未定義」と報告する。

func (*Context) Definition

func (c *Context) Definition(id string) (defLoc, bool)

Definition は ID の定義位置を返す。

type Issue

type Issue struct {
	Rule     string   `json:"rule"`
	Kind     Kind     `json:"kind"`
	File     string   `json:"file"`
	Line     int      `json:"line"`
	Message  string   `json:"message"`
	Expected string   `json:"expected,omitempty"`
	Severity Severity `json:"severity"`
}

Issue は検証結果の指摘 1 件。

func DepOrder

func DepOrder(c *Context) []Issue

DepOrder は依存関係(要件→設計→実装)の順序を検証する。

  • depends_on に挙げたファイルが存在するか
  • depends_on のまとめ指定(glob)が実ファイルに 1 件でも一致するか(警告)
  • 参照先 ID が、依存関係として宣言済みの文書で定義されているか
  • ファイル依存に循環が無いか(警告)

未宣言の文書は判定材料が無いため対象外とする。 chain に載らない文書タイプ(用語集など)で定義された ID も対象外とする。 どこからでも引かれる補助文書なので、参照のたびに depends_on を書かせない。

func IDHierarchy

func IDHierarchy(c *Context) []Issue

IDHierarchy は階層識別子(R-1.2 のようにドットで繋いだ ID)の名前上の親が、 構造上の親(直近の ID 付き祖先見出し)と一致するかを検証する。

関係(含有辺)を作るのは見出しの入れ子だけで、ドットは名前でしかない。 名前と構造が食い違うと、名前が示す親子と実際に辿られる親子が別物になる。 ドットを含まない ID は階層を主張していないので、対象にしない。

func IDUniqueness

func IDUniqueness(c *Context) []Issue

IDUniqueness は ID の重複と(任意で)連番の欠番を検証する。

func RefIntegrity

func RefIntegrity(c *Context) []Issue

RefIntegrity は参照された ID が実際に定義されているかを検証する。

func RequiredSections

func RequiredSections(c *Context) []Issue

RequiredSections は文書タイプごとの必須セクションを検証する。

ID 付き見出しがある文書では主レベル(parser.PrimaryLevels)の ID セクションの直下を、 ID が無い文書では文書全体を検査対象とする。深いレベルの ID は主要素の細目であり、 必須セクションを備える単位ではないため対象にしない。

func TermConsistency

func TermConsistency(c *Context) []Issue

TermConsistency は用語集との一貫性を検証する。

検査するのは 1 つだけ。用語集が別表記として挙げている語が、正式な用語の 代わりに使われていないか。用語集が「FR = 機能要件 (Functional Requirement)」と 定めている以上、文書側は FR を使うべきなので、別表記の出現は指摘する。

探す語は用語集から与えられるので、判定は照合であって推測ではない。 日本語でも確実に効くので、深刻度は error にしてある。

用語集に無い語を推測して指摘することはしない。手がかりが英大文字の略語と 強調しか無く、語の区切りが無い言語では原理的に効かない。目安にしかならない 指摘で文書を埋めるより、何を用語とするかの判断は人に委ねる。

type Kind

type Kind string

Kind は指摘の種類。メッセージの文言に依らず指摘を機械で識別する鍵で、 1 つのルールが複数の種類を発行することがある。

const (
	// KindRefUndefined は未定義 ID への参照。
	KindRefUndefined Kind = "ref_undefined"
	// KindTermAlias は正式な用語の代わりに使われた別表記。
	KindTermAlias Kind = "term_alias"
	// KindSectionEmpty は中身の無い必須セクション。
	KindSectionEmpty Kind = "section_empty"
	// KindSectionMissing は必須セクションの欠落。
	KindSectionMissing Kind = "section_missing"
	// KindSectionOrder は必須セクションの順序違反。
	KindSectionOrder Kind = "section_order"
	// KindIDDuplicate は ID の重複定義。
	KindIDDuplicate Kind = "id_duplicate"
	// KindIDGap は連番の欠番。
	KindIDGap Kind = "id_gap"
	// KindDepMissing は存在しないファイルへの依存宣言。
	KindDepMissing Kind = "dep_missing"
	// KindDepGlobEmpty はどのファイルにも一致しない依存のまとめ指定。
	KindDepGlobEmpty Kind = "dep_glob_empty"
	// KindDepUndeclared は依存関係に無いファイルで定義された ID への参照。
	KindDepUndeclared Kind = "dep_undeclared"
	// KindDepInverted は下流で定義された ID への参照(依存順序違反)。
	KindDepInverted Kind = "dep_inverted"
	// KindDepCycle はファイル依存の循環。
	KindDepCycle Kind = "dep_cycle"
	// KindHierarchyMismatch は階層 ID と構造上の親の不一致(親の不在を含む)。
	KindHierarchyMismatch Kind = "hierarchy_mismatch"
)

type Rule

type Rule struct {
	Name  string
	Desc  string
	Check func(*Context) []Issue
}

Rule は 1 つの検証ルール。

func All

func All() []Rule

All は登録済みルールを実行順に返す。

type SectionSpec

type SectionSpec struct {
	Required []string `yaml:"required"`
	Order    bool     `yaml:"order,omitempty"`
}

SectionSpec は文書タイプごとの必須セクション定義(sections.yaml)。

type Settings

type Settings struct {
	RefIntegrity struct {
		Enabled *bool `yaml:"enabled"`
	} `yaml:"ref_integrity"`
	TermConsistency struct {
		Enabled *bool `yaml:"enabled"`
		// GlossaryType は厳密デコードが glossary_type を既知のキーとして
		// 受けるための宣言。値の解決は cfg.GlossaryType の 1 経路に寄せて
		// あるので、ルール実装はこのフィールドを読まないこと(読むと
		// 経路によって答えが割れる状態へ逆戻りする)
		GlossaryType string `yaml:"glossary_type"`
	} `yaml:"term_consistency"`
	RequiredSections struct {
		Enabled *bool  `yaml:"enabled"`
		Config  string `yaml:"config"`
	} `yaml:"required_sections"`
	IDUniqueness struct {
		Enabled       *bool `yaml:"enabled"`
		CheckSequence bool  `yaml:"check_sequence"`
	} `yaml:"id_uniqueness"`
	DepOrder struct {
		Enabled *bool `yaml:"enabled"`
	} `yaml:"dep_order"`
	IDHierarchy struct {
		Enabled *bool `yaml:"enabled"`
	} `yaml:"id_hierarchy"`
}

Settings は設定ファイルの rules セクション。

func (Settings) Enabled

func (s Settings) Enabled(name string) bool

Enabled はルール名から有効/無効を返す(未指定は有効)。

対応する項目は Settings の yaml タグから引く。名前の一覧をここへ 書き写すと、ルールを足したときに 1 か所だけ直し忘れて enabled: false が黙って無視される。Settings に項目があることは テストが全ルールについて確かめる。

type Severity

type Severity string

Severity は指摘の深刻度。

const (
	// SeverityError は CI を失敗させる指摘。
	SeverityError Severity = "error"
	// SeverityWarning は情報提供にとどまる指摘。
	SeverityWarning Severity = "warning"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL