Skip to content

Rule API

Archgate rules are TypeScript files that export a plain object typed with satisfies RuleSet. Each rule receives a RuleContext with utilities for searching files, reading content, and reporting violations.

/// <reference path="../rules.d.ts" />
export default {
rules: {
"my-rule-id": {
description: "Human-readable description of what this rule checks",
severity: "error", // optional, defaults to "error"
async check(ctx) {
// Rule logic here
},
},
},
} satisfies RuleSet;

A rules file default-exports a plain object with a rules record keyed by rule ID. Keys become the rule IDs that appear in check output and violation reports. The satisfies RuleSet annotation provides type checking without wrapping in a function call.

type RuleSet = { rules: Record<string, RuleConfig> };

Each rule in the record must conform to the RuleConfig interface.

interface RuleConfig {
description: string;
severity?: Severity;
check: (ctx: RuleContext) => Promise<void>;
}
FieldTypeRequiredDescription
descriptionstringYesHuman-readable description shown in check output
severitySeverityNoDefault severity for violations. Defaults to "error"
check(ctx: RuleContext) => Promise<void>YesAsync function containing the rule logic

The check function receives a RuleContext object with the project state and utility methods.

interface RuleContext {
projectRoot: string;
scopedFiles: string[];
changedFiles: string[];
glob(pattern: string): Promise<string[]>;
grep(file: string, pattern: RegExp): Promise<GrepMatch[]>;
grepFiles(pattern: RegExp, fileGlob: string): Promise<GrepMatch[]>;
readFile(path: string): Promise<string>;
readJSON(path: string): Promise<unknown>;
report: RuleReport;
}
projectRoot: string;

Absolute path to the project root directory (where .archgate/ lives).

scopedFiles: string[];

Files matching the ADR’s files glob patterns from its frontmatter. If the ADR has no files field, this contains all project files. Use this as the primary file list for your rule checks.

changedFiles: string[];

Files that have been modified according to git. When --staged is used, this contains only staged files. When running without --staged, this contains all changed files (staged and unstaged). Empty when no git changes are detected.

report: RuleReport;

The reporting interface for recording violations, warnings, and informational messages. See RuleReport below.

glob(pattern: string): Promise<string[]>;

Find files matching a glob pattern relative to the project root. Returns an array of absolute file paths.

const testFiles = await ctx.glob("tests/**/*.test.ts");
grep(file: string, pattern: RegExp): Promise<GrepMatch[]>;

Search a single file for lines matching a regular expression. Returns an array of GrepMatch objects with file path, line number, column, and matched content.

const matches = await ctx.grep(file, /console\.error\(/);
grepFiles(pattern: RegExp, fileGlob: string): Promise<GrepMatch[]>;

Search multiple files matching a glob pattern for lines matching a regular expression. Combines glob and grep into a single call.

const matches = await ctx.grepFiles(/TODO:/i, "src/**/*.ts");
readFile(path: string): Promise<string>;

Read the contents of a file as a string. The path is relative to the project root.

const content = await ctx.readFile("src/config.ts");
readJSON(path: string): Promise<unknown>;

Read and parse a JSON file. The path is relative to the project root. Returns the parsed value as unknown — cast to the expected type in your rule.

const pkg = (await ctx.readJSON("package.json")) as {
dependencies?: Record<string, string>;
};

The reporting interface for recording check results. Each method accepts a detail object describing the issue.

interface RuleReport {
violation(detail: ReportDetail): void;
warning(detail: ReportDetail): void;
info(detail: ReportDetail): void;
}
report.violation(detail: ReportDetail): void;

Report a rule violation. Violations cause the check to fail with exit code 1. Use for hard constraints that must not be merged.

report.warning(detail: ReportDetail): void;

Report a warning. Warnings appear in check output but do not cause the check to fail. Use for non-blocking guidance.

report.info(detail: ReportDetail): void;

Report an informational message. Does not affect the check exit code. Use for suggestions or notes.

The detail object passed to violation, warning, and info.

interface ReportDetail {
message: string;
file?: string;
line?: number;
endLine?: number;
endColumn?: number;
fix?: string;
}
FieldTypeRequiredDescription
messagestringYesHuman-readable description of the issue
filestringNoFile path where the issue was found
linenumberNoStart line number (1-based)
endLinenumberNoEnd line number (1-based) — for precise editor range highlighting
endColumnnumberNoEnd column number (0-based) — for precise editor range highlighting
fixstringNoSuggested fix or remediation action

When endLine and endColumn are provided, editors (VS Code, Cursor) can highlight the exact expression that violates the rule, rather than the entire line. If omitted, the full line at line is highlighted.


Returned by ctx.grep() and ctx.grepFiles().

interface GrepMatch {
file: string;
line: number;
column: number;
content: string;
}
FieldTypeDescription
filestringAbsolute path to the matched file
linenumberLine number of the match (1-based)
columnnumberColumn number of the match (1-based)
contentstringFull content of the matched line

type Severity = "error" | "warning" | "info";
ValueExit code impactDescription
"error"Causes exit 1Hard constraint, blocks merges
"warning"No impactNon-blocking guidance
"info"No impactInformational, suggestions only

The internal representation of a reported issue, used in check output and JSON results.

interface ViolationDetail {
ruleId: string;
adrId: string;
message: string;
file?: string;
line?: number;
endLine?: number;
endColumn?: number;
fix?: string;
severity: Severity;
}
FieldTypeDescription
ruleIdstringRule ID from the rules object key
adrIdstringADR ID from the frontmatter
messagestringHuman-readable description
filestring?File path where the issue was found
linenumber?Start line number (1-based)
endLinenumber?End line (1-based) — for precise editor highlighting
endColumnnumber?End column (0-based) — for precise editor highlighting
fixstring?Suggested fix
severitySeverityEffective severity of this violation

The type used with satisfies to type-check your rules object. Export a plain object that conforms to this shape.

type RuleSet = { rules: Record<string, RuleConfig> };