Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,8 @@ The mobile app lives in `mobile/` — a Flutter app using Riverpod + Hooks.
over raw `Theme.of(context)` calls.
- **Keep widgets small and composable.** One public widget per file; push
private sub-widgets (`_Foo`) into sibling `part` files under a
`<page>/` folder rather than growing the page file. Hard ceiling:
**1000 lines/file**, enforced across Desktop, Web, and Mobile by the
`<page>/` folder rather than growing the page file. Mobile's hard ceiling is
**1200 lines/file**, enforced with the other surface-specific limits by the
repository-level `just file-size-check` gate (`just check`, CI, and every
pre-push). If the guard trips, **split the file — never bump the limit or add
an override to slip under it.**
Expand Down
62 changes: 9 additions & 53 deletions desktop/scripts/check-file-sizes.mjs
Original file line number Diff line number Diff line change
@@ -1,61 +1,17 @@
import path from "node:path";
import { fileURLToPath } from "node:url";
import { runFileSizeCheck } from "../../scripts/check-file-sizes-core.mjs";
import { rules } from "./file-size-policy.mjs";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const projectRoot = path.resolve(__dirname, "..");
const scriptPath = fileURLToPath(import.meta.url);
const projectRoot = path.resolve(path.dirname(scriptPath), "..");

const MAX_LINES = 1000;

const rules = [
{ root: "src-tauri/src", extensions: new Set([".rs"]), maxLines: MAX_LINES },
// Workspace member crates. Without this the ratchet's only Rust root is
// `src-tauri/src`, and a crate under `src-tauri/crates/` is born outside the
// repo's one size discipline -- silently, since the check still exits 0.
{
root: "src-tauri/crates",
extensions: new Set([".rs"]),
maxLines: MAX_LINES,
},
{
root: "src/app",
extensions: new Set([".ts", ".tsx"]),
maxLines: MAX_LINES,
},
{
root: "src/features",
extensions: new Set([".ts", ".tsx"]),
maxLines: MAX_LINES,
},
{
root: "src/shared/api",
extensions: new Set([".ts", ".tsx"]),
maxLines: MAX_LINES,
},
{
root: "src/shared/context",
extensions: new Set([".ts", ".tsx"]),
maxLines: MAX_LINES,
},
{
root: "src/shared/lib",
extensions: new Set([".ts", ".tsx"]),
maxLines: MAX_LINES,
},
{
root: "src/shared/ui",
extensions: new Set([".ts", ".tsx"]),
maxLines: MAX_LINES,
},
{
root: "src/shared/styles",
extensions: new Set([".css"]),
maxLines: MAX_LINES,
},
];

await runFileSizeCheck({
export const policy = {
projectRoot,
rules,
label: "Desktop",
});
};

if (process.argv[1] && path.resolve(process.argv[1]) === scriptPath) {
await runFileSizeCheck(policy);
}
53 changes: 53 additions & 0 deletions desktop/scripts/file-size-policy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const DESKTOP_FRONTEND_MAX_LINES = 1200;
const DESKTOP_RUST_MAX_LINES = 1500;

export const rules = [
{
root: "src-tauri/src",
extensions: new Set([".rs"]),
maxLines: DESKTOP_RUST_MAX_LINES,
},
// Workspace member crates. Without this the ratchet's only Rust root is
// `src-tauri/src`, and a crate under `src-tauri/crates/` is born outside the
// repo's one size discipline -- silently, since the check still exits 0.
{
root: "src-tauri/crates",
extensions: new Set([".rs"]),
maxLines: DESKTOP_RUST_MAX_LINES,
},
{
root: "src/app",
extensions: new Set([".ts", ".tsx"]),
maxLines: DESKTOP_FRONTEND_MAX_LINES,
},
{
root: "src/features",
extensions: new Set([".ts", ".tsx"]),
maxLines: DESKTOP_FRONTEND_MAX_LINES,
},
{
root: "src/shared/api",
extensions: new Set([".ts", ".tsx"]),
maxLines: DESKTOP_FRONTEND_MAX_LINES,
},
{
root: "src/shared/context",
extensions: new Set([".ts", ".tsx"]),
maxLines: DESKTOP_FRONTEND_MAX_LINES,
},
{
root: "src/shared/lib",
extensions: new Set([".ts", ".tsx"]),
maxLines: DESKTOP_FRONTEND_MAX_LINES,
},
{
root: "src/shared/ui",
extensions: new Set([".ts", ".tsx"]),
maxLines: DESKTOP_FRONTEND_MAX_LINES,
},
{
root: "src/shared/styles",
extensions: new Set([".css"]),
maxLines: DESKTOP_FRONTEND_MAX_LINES,
},
];
23 changes: 9 additions & 14 deletions mobile/scripts/check-file-sizes.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import path from "node:path";
import { fileURLToPath } from "node:url";
import { runFileSizeCheck } from "../../scripts/check-file-sizes-core.mjs";
import { rules } from "./file-size-policy.mjs";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const projectRoot = path.resolve(__dirname, "..");
const scriptPath = fileURLToPath(import.meta.url);
const projectRoot = path.resolve(path.dirname(scriptPath), "..");

const MAX_LINES = 1000;

const rules = [
{
root: "lib",
extensions: new Set([".dart"]),
maxLines: MAX_LINES,
},
];

await runFileSizeCheck({
export const policy = {
projectRoot,
rules,
label: "Mobile",
});
};

if (process.argv[1] && path.resolve(process.argv[1]) === scriptPath) {
await runFileSizeCheck(policy);
}
7 changes: 7 additions & 0 deletions mobile/scripts/file-size-policy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const rules = [
{
root: "lib",
extensions: new Set([".dart"]),
maxLines: 1200,
},
];
157 changes: 155 additions & 2 deletions scripts/check-file-sizes-core.test.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { mkdtempSync } from "node:fs";
import { execFileSync, spawnSync } from "node:child_process";
import {
copyFileSync,
mkdirSync,
mkdtempSync,
realpathSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import test from "node:test";
import { policy as desktopPolicy } from "../desktop/scripts/check-file-sizes.mjs";
import { policy as mobilePolicy } from "../mobile/scripts/check-file-sizes.mjs";
import { policy as webPolicy } from "../web/scripts/check-file-sizes.mjs";
import {
allowedLineCount,
countLines,
Expand All @@ -12,6 +21,8 @@ import {
resolveBaseRef,
} from "./check-file-sizes-core.mjs";

const repoRoot = path.resolve(import.meta.dirname, "..");

function git(repo, ...args) {
// These fixture repositories inherit both hook configuration and Git's
// repository-local environment when this test runs from pre-push. Isolate
Expand All @@ -26,6 +37,53 @@ function git(repo, ...args) {
}).trim();
}

function createEntrypointFixture({
surface,
relativeFile,
extension,
maxLines,
}) {
const repo = realpathSync(
mkdtempSync(path.join(tmpdir(), `file-size-${surface}-`)),
);
const scriptsDir = path.join(repo, "scripts");
const surfaceScriptsDir = path.join(repo, surface, "scripts");
mkdirSync(scriptsDir, { recursive: true });
mkdirSync(surfaceScriptsDir, { recursive: true });
copyFileSync(
path.join(repoRoot, "scripts/check-file-sizes-core.mjs"),
path.join(scriptsDir, "check-file-sizes-core.mjs"),
);
copyFileSync(
path.join(repoRoot, surface, "scripts/check-file-sizes.mjs"),
path.join(surfaceScriptsDir, "check-file-sizes.mjs"),
);
writeFileSync(
path.join(surfaceScriptsDir, "file-size-policy.mjs"),
`export const rules = [{ root: ${JSON.stringify(path.posix.dirname(relativeFile))}, extensions: new Set([${JSON.stringify(extension)}]), maxLines: ${maxLines} }];\n`,
);

git(repo, "init", "-b", "main");
git(repo, "config", "user.name", "Test");
git(repo, "config", "user.email", "test@example.com");
git(repo, "add", ".");
git(repo, "commit", "-m", "base");
const base = git(repo, "rev-parse", "HEAD");
git(repo, "switch", "-c", "feature");

const governedFile = path.join(repo, surface, relativeFile);
mkdirSync(path.dirname(governedFile), { recursive: true });
writeFileSync(governedFile, `${"line\n".repeat(maxLines)}line`);

const entrypointPath = path.join(surfaceScriptsDir, "check-file-sizes.mjs");
const result = spawnSync(realpathSync(process.execPath), [entrypointPath], {
cwd: repo,
encoding: "utf8",
env: { ...process.env, CHECK_FILE_SIZES_BASE: base },
});
return { result, relativeFile };
}

test("local base resolution uses the branch merge-base and fails without origin/main", () => {
const repo = mkdtempSync(path.join(tmpdir(), "file-size-base-"));
git(repo, "init", "-b", "main");
Expand All @@ -47,12 +105,107 @@ test("local base resolution uses the branch merge-base and fails without origin/
);
});

test("surface entrypoints execute their exported policies", () => {
const cases = [
{
surface: "desktop",
relativeFile: "src-tauri/src/oversized.rs",
extension: ".rs",
maxLines: 1500,
},
{
surface: "mobile",
relativeFile: "lib/oversized.dart",
extension: ".dart",
maxLines: 1200,
},
{
surface: "web",
relativeFile: "src/app/oversized.ts",
extension: ".ts",
maxLines: 1000,
},
];

for (const fixture of cases) {
const { result, relativeFile } = createEntrypointFixture(fixture);
assert.equal(
result.status,
1,
`${fixture.surface} should reject ceiling + 1: ${result.stderr || result.stdout}`,
);
assert.ok(result.stderr.includes(relativeFile), result.stderr);
}
});

test("counts empty, LF, and CRLF content with the existing semantics", () => {
assert.equal(countLines(""), 0);
assert.equal(countLines("one\n"), 2);
assert.equal(countLines("one\r\ntwo"), 2);
});

test("surface entrypoints expose the exact ordered production policies", () => {
const policies = [
[
desktopPolicy,
[
["src-tauri/src", [".rs"], 1500],
["src-tauri/crates", [".rs"], 1500],
["src/app", [".ts", ".tsx"], 1200],
["src/features", [".ts", ".tsx"], 1200],
["src/shared/api", [".ts", ".tsx"], 1200],
["src/shared/context", [".ts", ".tsx"], 1200],
["src/shared/lib", [".ts", ".tsx"], 1200],
["src/shared/ui", [".ts", ".tsx"], 1200],
["src/shared/styles", [".css"], 1200],
],
],
[mobilePolicy, [["lib", [".dart"], 1200]]],
[
webPolicy,
[
["src/app", [".ts", ".tsx"], 1000],
["src/features", [".ts", ".tsx"], 1000],
["src/shared/api", [".ts", ".tsx"], 1000],
],
],
];

for (const [policy, expectedRules] of policies) {
const actualRules = policy.rules.map((rule) => [
rule.root,
[...rule.extensions],
rule.maxLines,
]);
assert.deepEqual(
actualRules,
expectedRules,
`${policy.label} production rules`,
);

for (const rule of policy.rules) {
assert.equal(
evaluateFileSize({
baseLines: null,
candidateLines: rule.maxLines,
maxLines: rule.maxLines,
}).violates,
false,
`${policy.label} ${rule.root} should allow the ceiling`,
);
assert.equal(
evaluateFileSize({
baseLines: null,
candidateLines: rule.maxLines + 1,
maxLines: rule.maxLines,
}).violates,
true,
`${policy.label} ${rule.root} should reject ceiling + 1`,
);
}
}
});

test("new files use the configured ceiling", () => {
assert.equal(allowedLineCount(null, 1000), 1000);
assert.deepEqual(
Expand Down
Loading
Loading