getComponents
グラフを無向グラフとみなして連結成分(connected components)を抽出します。BFS で互いに到達可能なノードをグルーピングし、ドキュメントの「クラスタ」を返します。
巨大なリポジトリでも、実際にはドキュメント群が独立した複数のクラスタに分かれていることが多くあります。getComponents を使うと、どのドキュメント群がまとまった文脈を成しているか、孤立したクラスタが存在するかを把握できます。
どこからも参照されておらず、何も参照していない孤立ドキュメント(要素 1 個の成分)の検出にも有用です(→ GRP-003 ルールで自動検出可)。
function getComponents(graph: ContextGraph): string[][];| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
graph | ContextGraph | ✓ | buildContextGraph で構築したグラフ |
string[][] を返します。各成分はファイルパスの配列で、内部はアルファベット順にソートされます。成分そのものも先頭ファイルパスで決定論的にソートされます。
import { buildContextGraph, getComponents, loadDocuments } from "@contextlint/core";
const documents = loadDocuments(["docs/**/*.md"]);const graph = buildContextGraph(documents);
const components = getComponents(graph);
console.log(`${components.length} 個のクラスタを検出`);
for (const [index, component] of components.entries()) { console.log(`\nクラスタ ${index + 1}: ${component.length} ファイル`); for (const file of component) { console.log(` - ${file}`); }}
// 孤立ドキュメントを抽出const orphans = components.filter((c) => c.length === 1).flat();console.log(`孤立ドキュメント: ${orphans.length} 件`);buildContextGraph— 入力となるグラフを構築topologicalSort— 依存順に並べる版(有向)getContextSlice— クエリ起点で関連ドキュメントを取得