Skip to content

AI-native design editor. Open-source Figma alternative.

chore: split architecture tooling rules

Danila Poyarkovcd47961919b5
diff --git a/steiger.config.ts b/steiger.config.ts
index 66fb9e929..f7a620efc 100644
--- a/steiger.config.ts
+++ b/steiger.config.ts
@@ -1,7 +1,7 @@
import fsd from '@feature-sliced/steiger-plugin'
import { defineConfig } from 'steiger'
-import { openPencilArchitecturePlugin } from './tools/architecture/src/steiger-rules.ts'
+import { openPencilArchitecturePlugin } from './tools/architecture/src/steiger-rules/index.ts'
// OpenPencil is not laid out as canonical Feature-Sliced Design layers.
// Keep Steiger focused on project-specific architecture boundaries instead of
diff --git a/tools/architecture/src/steiger-rules.ts b/tools/architecture/src/steiger-rules/index.ts
similarity index 73%
rename from tools/architecture/src/steiger-rules.ts
rename to tools/architecture/src/steiger-rules/index.ts
index 8daff720b..e519d0582 100644
--- a/tools/architecture/src/steiger-rules.ts
+++ b/tools/architecture/src/steiger-rules/index.ts
@@ -1,186 +1,19 @@
-import { readFileSync } from 'node:fs'
import path from 'node:path'
-type TreeEntry = {
- type: 'file' | 'folder'
- path: string
- children?: TreeEntry[]
-}
-
-type Diagnostic = {
- message: string
- location: { path: string; line?: number; column?: number }
-}
-
-type RuleResult = { diagnostics: Diagnostic[] }
-type Rule = { name: string; check: (root: TreeEntry) => RuleResult }
-
-type FileRuleCheck = (sourceRel: string) => string | null
-
-type TextRuleCheck = (
- sourceRel: string,
- content: string
-) => Array<{ message: string; line?: number; column?: number }>
-
-const FILE_PREFIX_GROUP_ALLOWLIST = new Set([
- 'packages/core/src/lint/rules::no',
- 'tests/engine::visual'
-])
-
-type ImportRef = {
- specifier: string
- line: number
- column: number
-}
-
-const TEXT_EXTENSIONS = new Set(['.ts', '.tsx', '.vue', '.js', '.jsx', '.mjs', '.mts'])
-const ROOT_MARKDOWN_ALLOWLIST = new Set([
- 'AGENTS.md',
- 'CHANGELOG.md',
- 'CONTRIBUTING.md',
- 'README.md',
- 'SECURITY.md'
-])
-const PACKAGE_ALIASES: Record<string, string> = {
- '#core/': 'packages/core/src/',
- '#vue/': 'packages/vue/src/',
- '#cli/': 'packages/cli/src/',
- '#mcp/': 'packages/mcp/src/'
-}
-
-const PACKAGE_ALIAS_OWNERS: Record<string, string> = {
- '#core/': 'packages/core/src/',
- '#vue/': 'packages/vue/src/',
- '#cli/': 'packages/cli/src/',
- '#mcp/': 'packages/mcp/src/'
-}
-
-function normalizePath(filePath: string) {
- return filePath.split(path.sep).join('/')
-}
-
-function relativePath(rootPath: string, filePath: string) {
- return normalizePath(path.relative(rootPath, filePath))
-}
-
-function collectFiles(entry: TreeEntry, files: string[] = []) {
- if (entry.type === 'file') {
- if (TEXT_EXTENSIONS.has(path.extname(entry.path))) files.push(entry.path)
- return files
- }
- for (const child of entry.children ?? []) collectFiles(child, files)
- return files
-}
-
-function collectFolders(entry: TreeEntry, folders: TreeEntry[] = []) {
- if (entry.type !== 'folder') return folders
- folders.push(entry)
- for (const child of entry.children ?? []) collectFolders(child, folders)
- return folders
-}
-
-function importsIn(content: string): ImportRef[] {
- const imports: ImportRef[] = []
- const patterns = [
- /^\s*(?:import|export)\s+(?:type\s+)?(?:[^'";]*?\s+from\s*)?['"]([^'"]+)['"]/gm,
- /^\s*import\(\s*['"]([^'"]+)['"]\s*\)/gm
- ]
-
- for (const pattern of patterns) {
- for (const match of content.matchAll(pattern)) {
- const before = content.slice(0, match.index)
- const lines = before.split('\n')
- imports.push({
- specifier: match[1],
- line: lines.length,
- column: lines.at(-1)?.length ?? 0
- })
- }
- }
- return imports
-}
-
-function resolveImport(sourceRel: string, specifier: string): string | null {
- if (specifier.startsWith('@/')) return `src/${specifier.slice(2)}`
-
- for (const [alias, target] of Object.entries(PACKAGE_ALIASES)) {
- if (specifier.startsWith(alias)) return `${target}${specifier.slice(alias.length)}`
- }
-
- if (specifier.startsWith('.')) {
- return normalizePath(path.join(path.dirname(sourceRel), specifier))
- }
-
- return null
-}
-
-function createTextRule(name: string, checkText: TextRuleCheck): Rule {
- return {
- name,
- check(root) {
- const diagnostics: Diagnostic[] = []
- for (const file of collectFiles(root)) {
- const sourceRel = relativePath(root.path, file)
- const content = readFileSync(file, 'utf8')
- for (const result of checkText(sourceRel, content)) {
- diagnostics.push({
- message: result.message,
- location: { path: file, line: result.line, column: result.column }
- })
- }
- }
- return { diagnostics }
- }
- }
-}
-
-function createFileRule(name: string, checkFile: FileRuleCheck): Rule {
- return {
- name,
- check(root) {
- const diagnostics: Diagnostic[] = []
- for (const file of collectFiles(root)) {
- const sourceRel = relativePath(root.path, file)
- const message = checkFile(sourceRel)
- if (!message) continue
- diagnostics.push({ message, location: { path: file } })
- }
- return { diagnostics }
- }
- }
-}
-
-function createImportRule(
- name: string,
- checkImport: (sourceRel: string, specifier: string, resolved: string | null) => string | null
-): Rule {
- return {
- name,
- check(root) {
- const diagnostics: Diagnostic[] = []
- for (const file of collectFiles(root)) {
- const sourceRel = relativePath(root.path, file)
- const content = readFileSync(file, 'utf8')
- for (const imported of importsIn(content)) {
- const resolved = resolveImport(sourceRel, imported.specifier)
- const message = checkImport(sourceRel, imported.specifier, resolved)
- if (!message) continue
- diagnostics.push({
- message,
- location: { path: file, line: imported.line, column: imported.column }
- })
- }
- }
- return { diagnostics }
- }
- }
-}
-
-function filePrefix(filePath: string): string | null {
- const name = path.basename(filePath).replace(/\.(test|spec|bench)?\.?[cm]?[tj]sx?$|\.vue$/, '')
- const match = /^([a-z][a-z0-9]+)-[a-z0-9-]+$/.exec(name)
- return match?.[1] ?? null
-}
+import {
+ collectFolders,
+ createFileRule,
+ createImportRule,
+ createTextRule,
+ filePrefix,
+ FILE_PREFIX_GROUP_ALLOWLIST,
+ PACKAGE_ALIASES,
+ PACKAGE_ALIAS_OWNERS,
+ relativePath,
+ ROOT_MARKDOWN_ALLOWLIST,
+ TEXT_EXTENSIONS,
+ type Rule
+} from './support.ts'
const preferDomainFoldersOverFilenamePrefixes: Rule = {
name: 'open-pencil/prefer-domain-folders-over-filename-prefixes',
@@ -214,18 +47,25 @@ const scriptsAreEntrypointShims = createTextRule(
'open-pencil/scripts-are-entrypoint-shims',
(sourceRel, content) => {
if (!sourceRel.startsWith('scripts/')) return []
- if (/^#!\/usr\/bin\/env bun\s+import ['"]\.\.\/tools\/[^'"]+['"]\s*;?$/u.test(content.trim())) return []
- return [{ message: 'Root scripts must be tiny shims. Move implementation logic into tools/<domain>/.' }]
+ if (/^#!\/usr\/bin\/env bun\s+import ['"]\.\.\/tools\/[^'"]+['"]\s*;?$/u.test(content.trim()))
+ return []
+ return [
+ {
+ message: 'Root scripts must be tiny shims. Move implementation logic into tools/<domain>/.'
+ }
+ ]
}
)
-const TOOL_LAYOUT_MESSAGE = 'Tool files must live under tools/<domain>/src/** or tools/<domain>/tests/*.test.ts.'
+const TOOL_LAYOUT_MESSAGE =
+ 'Tool files must live under tools/<domain>/src/** or tools/<domain>/tests/*.test.ts.'
const strictToolsLayout = createFileRule('open-pencil/strict-tools-layout', (sourceRel) => {
if (!sourceRel.startsWith('tools/') || !TEXT_EXTENSIONS.has(path.extname(sourceRel))) return null
if (sourceRel === 'tools/test.ts') return null
const [, domain, segment] = sourceRel.split('/')
- if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(domain)) return 'Tool package folders must use kebab-case domain names.'
+ if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(domain))
+ return 'Tool package folders must use kebab-case domain names.'
if (segment === 'src') return null
if (segment === 'tests' && sourceRel.endsWith('.test.ts')) return null
return TOOL_LAYOUT_MESSAGE
diff --git a/tools/architecture/src/steiger-rules/support.ts b/tools/architecture/src/steiger-rules/support.ts
new file mode 100644
index 000000000..10ae5836b
--- /dev/null
+++ b/tools/architecture/src/steiger-rules/support.ts
@@ -0,0 +1,183 @@
+import { readFileSync } from 'node:fs'
+import path from 'node:path'
+
+export type TreeEntry = {
+ type: 'file' | 'folder'
+ path: string
+ children?: TreeEntry[]
+}
+
+export type Diagnostic = {
+ message: string
+ location: { path: string; line?: number; column?: number }
+}
+
+export type RuleResult = { diagnostics: Diagnostic[] }
+export type Rule = { name: string; check: (root: TreeEntry) => RuleResult }
+
+type FileRuleCheck = (sourceRel: string) => string | null
+
+type TextRuleCheck = (
+ sourceRel: string,
+ content: string
+) => Array<{ message: string; line?: number; column?: number }>
+
+export const FILE_PREFIX_GROUP_ALLOWLIST = new Set([
+ 'packages/core/src/lint/rules::no',
+ 'tests/engine::visual'
+])
+
+type ImportRef = {
+ specifier: string
+ line: number
+ column: number
+}
+
+export const TEXT_EXTENSIONS = new Set(['.ts', '.tsx', '.vue', '.js', '.jsx', '.mjs', '.mts'])
+export const ROOT_MARKDOWN_ALLOWLIST = new Set([
+ 'AGENTS.md',
+ 'CHANGELOG.md',
+ 'CONTRIBUTING.md',
+ 'README.md',
+ 'SECURITY.md'
+])
+export const PACKAGE_ALIASES: Record<string, string> = {
+ '#core/': 'packages/core/src/',
+ '#vue/': 'packages/vue/src/',
+ '#cli/': 'packages/cli/src/',
+ '#mcp/': 'packages/mcp/src/'
+}
+
+export const PACKAGE_ALIAS_OWNERS: Record<string, string> = {
+ '#core/': 'packages/core/src/',
+ '#vue/': 'packages/vue/src/',
+ '#cli/': 'packages/cli/src/',
+ '#mcp/': 'packages/mcp/src/'
+}
+
+function normalizePath(filePath: string) {
+ return filePath.split(path.sep).join('/')
+}
+
+export function relativePath(rootPath: string, filePath: string) {
+ return normalizePath(path.relative(rootPath, filePath))
+}
+
+export function collectFiles(entry: TreeEntry, files: string[] = []) {
+ if (entry.type === 'file') {
+ if (TEXT_EXTENSIONS.has(path.extname(entry.path))) files.push(entry.path)
+ return files
+ }
+ for (const child of entry.children ?? []) collectFiles(child, files)
+ return files
+}
+
+export function collectFolders(entry: TreeEntry, folders: TreeEntry[] = []) {
+ if (entry.type !== 'folder') return folders
+ folders.push(entry)
+ for (const child of entry.children ?? []) collectFolders(child, folders)
+ return folders
+}
+
+function importsIn(content: string): ImportRef[] {
+ const imports: ImportRef[] = []
+ const patterns = [
+ /^\s*(?:import|export)\s+(?:type\s+)?(?:[^'";]*?\s+from\s*)?['"]([^'"]+)['"]/gm,
+ /^\s*import\(\s*['"]([^'"]+)['"]\s*\)/gm
+ ]
+
+ for (const pattern of patterns) {
+ for (const match of content.matchAll(pattern)) {
+ const before = content.slice(0, match.index)
+ const lines = before.split('\n')
+ imports.push({
+ specifier: match[1],
+ line: lines.length,
+ column: lines.at(-1)?.length ?? 0
+ })
+ }
+ }
+ return imports
+}
+
+function resolveImport(sourceRel: string, specifier: string): string | null {
+ if (specifier.startsWith('@/')) return `src/${specifier.slice(2)}`
+
+ for (const [alias, target] of Object.entries(PACKAGE_ALIASES)) {
+ if (specifier.startsWith(alias)) return `${target}${specifier.slice(alias.length)}`
+ }
+
+ if (specifier.startsWith('.')) {
+ return normalizePath(path.join(path.dirname(sourceRel), specifier))
+ }
+
+ return null
+}
+
+export function createTextRule(name: string, checkText: TextRuleCheck): Rule {
+ return {
+ name,
+ check(root) {
+ const diagnostics: Diagnostic[] = []
+ for (const file of collectFiles(root)) {
+ const sourceRel = relativePath(root.path, file)
+ const content = readFileSync(file, 'utf8')
+ for (const result of checkText(sourceRel, content)) {
+ diagnostics.push({
+ message: result.message,
+ location: { path: file, line: result.line, column: result.column }
+ })
+ }
+ }
+ return { diagnostics }
+ }
+ }
+}
+
+export function createFileRule(name: string, checkFile: FileRuleCheck): Rule {
+ return {
+ name,
+ check(root) {
+ const diagnostics: Diagnostic[] = []
+ for (const file of collectFiles(root)) {
+ const sourceRel = relativePath(root.path, file)
+ const message = checkFile(sourceRel)
+ if (!message) continue
+ diagnostics.push({ message, location: { path: file } })
+ }
+ return { diagnostics }
+ }
+ }
+}
+
+export function createImportRule(
+ name: string,
+ checkImport: (sourceRel: string, specifier: string, resolved: string | null) => string | null
+): Rule {
+ return {
+ name,
+ check(root) {
+ const diagnostics: Diagnostic[] = []
+ for (const file of collectFiles(root)) {
+ const sourceRel = relativePath(root.path, file)
+ const content = readFileSync(file, 'utf8')
+ for (const imported of importsIn(content)) {
+ const resolved = resolveImport(sourceRel, imported.specifier)
+ const message = checkImport(sourceRel, imported.specifier, resolved)
+ if (!message) continue
+ diagnostics.push({
+ message,
+ location: { path: file, line: imported.line, column: imported.column }
+ })
+ }
+ }
+ return { diagnostics }
+ }
+ }
+}
+
+export function filePrefix(filePath: string): string | null {
+ const name = path.basename(filePath).replace(/\.(test|spec|bench)?\.?[cm]?[tj]sx?$|\.vue$/, '')
+ const match = /^([a-z][a-z0-9]+)-[a-z0-9-]+$/.exec(name)
+ return match?.[1] ?? null
+}