Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"ios": "cd apps/expo && bun ios",
"lefthook": "lefthook install",
"lint": "biome check --write",
"lint:custom": "bun run scripts/lint/no-raw-typeof.ts && bun run scripts/lint/no-raw-regex.ts && bun run packages/env/scripts/no-raw-process-env.ts && bun run scripts/lint/no-duplicate-guards.ts && bun run scripts/lint/no-unauth-routes.ts",
"lint:custom": "bun run scripts/lint/no-raw-typeof.ts && bun run scripts/lint/no-raw-regex.ts && bun run packages/env/scripts/no-raw-process-env.ts && bun run scripts/lint/no-duplicate-guards.ts && bun run scripts/lint/no-unauth-routes.ts && bun run check:custom",
"lint:strict": "biome check && bun run lint:custom",
"lint-unsafe": "biome check --write --unsafe",
"mcp": "bun run --cwd packages/mcp dev",
Expand All @@ -49,7 +49,9 @@
"test:landing": "vitest run --config apps/landing/vitest.config.ts",
"test:mcp": "bun run --cwd packages/mcp test",
"trails": "bun run --cwd apps/trails dev",
"web": "bun run --cwd apps/web dev"
"web": "bun run --cwd apps/web dev",
"check:max-params": "bun run --cwd packages/checks check:max-params",
"check:custom": "bun run --cwd packages/checks check"
},
"overrides": {
"@packrat-ai/nativewindui": "2.0.3-2",
Expand Down
4 changes: 3 additions & 1 deletion packages/checks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"scripts": {
"check:casts": "bun ./src/check-type-casts.ts",
"check:casts:strict": "bun ./src/check-type-casts.ts --strict",
"check:magic-strings": "bun ./src/check-magic-strings.ts"
"check:magic-strings": "bun ./src/check-magic-strings.ts",
"check:max-params": "bun ./src/check-max-params.ts",
"check": "bun ./src/check-all.ts"
}
}
31 changes: 31 additions & 0 deletions packages/checks/src/check-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bun

const checks = [
{ name: 'check:casts', cmd: ['bun', './check-type-casts.ts'] },
{ name: 'check:magic-strings', cmd: ['bun', './check-magic-strings.ts'] },
{ name: 'check:max-params', cmd: ['bun', './check-max-params.ts'] },
];

let hasFailure = false;

for (const check of checks) {
console.log(`\n▶ Running ${check.name}`);
const proc = Bun.spawnSync(check.cmd, {
cwd: import.meta.dir,
stdout: 'inherit',
stderr: 'inherit',
});

if (proc.exitCode !== 0) {
hasFailure = true;
console.error(`✖ ${check.name} failed with exit code ${proc.exitCode}`);
} else {
console.log(`✔ ${check.name} passed`);
}
}

if (hasFailure) {
process.exit(1);
}

console.log('\n✓ All checks passed.');
162 changes: 162 additions & 0 deletions packages/checks/src/check-max-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#!/usr/bin/env bun

import { readdirSync, readFileSync, statSync } from 'node:fs';
import { extname, join } from 'node:path';
import ts from 'typescript';

const ROOT = join(import.meta.dir, '..', '..', '..');
const SCAN_ROOTS = ['apps', 'packages'];
const EXCLUDED_DIRS = new Set([
'node_modules',
'dist',
'build',
'.next',
'.expo',
'drizzle',
'coverage',
'ios',
'android',
]);
const TARGET_EXTENSIONS = new Set(['.ts', '.tsx', '.mts', '.cts']);
const EXCLUDED_FILE_PATTERNS = [
/\.test\./,
/\.spec\./,
/\.stories\./,
/\.d\.ts$/,
/\/__tests__\//,
/\/test\//,
/\/tests\//,
/\/node_modules\//,
/\/dist\//,
/\/build\//,
];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Path patterns may not work correctly on Windows.

The regex patterns use forward slashes (e.g., /\/node_modules\//), but on Windows, file paths use backslashes. This could cause the exclusion patterns to fail, leading to unnecessary scanning or false positives. Normalize paths to use forward slashes before testing against these patterns.

🔧 Suggested fix

Update isTargetFile to normalize the path separator:

 function isTargetFile(filePath: string): boolean {
   if (!TARGET_EXTENSIONS.has(extname(filePath))) return false;
-  return !EXCLUDED_FILE_PATTERNS.some((p) => p.test(filePath));
+  const normalized = filePath.replace(/\\/g, '/');
+  return !EXCLUDED_FILE_PATTERNS.some((p) => p.test(normalized));
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/checks/src/check-max-params.ts` around lines 21 - 32, The exclusion
regexes in EXCLUDED_FILE_PATTERNS will fail on Windows because they expect
forward slashes; update isTargetFile to normalize incoming file paths (e.g., use
path.normalize or replace all backslashes with '/') and convert path separators
to forward slashes before testing against EXCLUDED_FILE_PATTERNS so patterns
like /\/node_modules\// match on Windows as well.


const ALLOW_ANNOTATION = /@allow-multi-param/;

interface Violation {
file: string;
line: number;
col: number;
params: number;
kind: string;
snippet: string;
}

function isTargetFile(filePath: string): boolean {
if (!TARGET_EXTENSIONS.has(extname(filePath))) return false;
return !EXCLUDED_FILE_PATTERNS.some((p) => p.test(filePath));
}

function getKindLabel(node: ts.Node): string {
if (ts.isFunctionDeclaration(node)) return 'function declaration';
if (ts.isMethodDeclaration(node)) return 'method declaration';
if (ts.isArrowFunction(node)) return 'arrow function';
if (ts.isFunctionExpression(node)) return 'function expression';
if (ts.isConstructorDeclaration(node)) return 'constructor';
return 'function-like';
}

function isCallbackForInvocation(node: ts.Node): boolean {
const parent = node.parent;
if (!parent || !ts.isCallExpression(parent)) return false;
return parent.arguments.some((arg) => arg === node);
}

function hasAllowAnnotation(source: ts.SourceFile, node: ts.Node): boolean {
const fullText = source.getFullText();
const leading = ts.getLeadingCommentRanges(fullText, node.getFullStart()) ?? [];
for (const range of leading) {
if (ALLOW_ANNOTATION.test(fullText.slice(range.pos, range.end))) {
return true;
}
}
return false;
}

function collectViolations(filePath: string): Violation[] {
const code = readFileSync(filePath, 'utf8');
const source = ts.createSourceFile(filePath, code, ts.ScriptTarget.Latest, true);
const violations: Violation[] = [];

const visit = (node: ts.Node): void => {
if (
ts.isFunctionLike(node) &&
node.parameters.length > 1 &&
!hasAllowAnnotation(source, node) &&
!isCallbackForInvocation(node)
) {
const start = source.getLineAndCharacterOfPosition(node.getStart());
const end = Math.min(node.getEnd(), node.getStart() + 120);
const snippet = source.getText().slice(node.getStart(), end).replace(/\s+/g, ' ').trim();
violations.push({
file: filePath.replace(`${ROOT}/`, ''),
line: start.line + 1,
col: start.character + 1,
params: node.parameters.length,
kind: getKindLabel(node),
snippet,
});
}
ts.forEachChild(node, visit);
};

visit(source);
return violations;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Use path.relative for more robust path handling.

Line 92 uses string replacement to make paths relative (filePath.replace(\${ROOT}/`, '')). This approach can fail if there are path normalization differences (e.g., symbolic links, double slashes, or case differences on case-insensitive filesystems). Use path.relative(ROOT, filePath)` instead for robust, cross-platform path relativization.

♻️ Refactor with path.relative
+import { extname, join, relative } from 'node:path';
 
 function collectViolations(filePath: string): Violation[] {
   // ...
   violations.push({
-    file: filePath.replace(`${ROOT}/`, ''),
+    file: relative(ROOT, filePath),
     line: start.line + 1,
     col: start.character + 1,
     params: node.parameters.length,
     kind: getKindLabel(node),
     snippet,
   });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/checks/src/check-max-params.ts` around lines 76 - 105, In
collectViolations replace the brittle string replace of filePath
(filePath.replace(`${ROOT}/`, '')) with a robust relative path using
path.relative(ROOT, filePath); ensure you import/require path if not present and
use that value when setting the violation.file property so ROOT, filePath and
collectViolations are referenced and path normalization is handled
cross-platform.


const targetFiles: string[] = [];

function walkDir(dir: string): void {
let entries: string[];
try {
entries = readdirSync(dir);
} catch {
return;
}

for (const entry of entries) {
if (EXCLUDED_DIRS.has(entry)) continue;
const fullPath = join(dir, entry);
let isDir = false;
try {
isDir = statSync(fullPath).isDirectory();
} catch {
continue;
}

if (isDir) {
walkDir(fullPath);
continue;
}

if (isTargetFile(fullPath)) {
targetFiles.push(fullPath);
}
}
}

for (const root of SCAN_ROOTS) {
walkDir(join(ROOT, root));
}

const violations = targetFiles.flatMap(collectViolations);

if (violations.length === 0) {
console.log('✓ No multi-parameter functions found.');
process.exit(0);
}

console.log(`Found ${violations.length} function(s) with more than 1 parameter:\n`);
let lastFile = '';
for (const v of violations) {
if (v.file !== lastFile) {
console.log(`\n ${v.file}`);
lastFile = v.file;
}
console.log(` ${v.line}:${v.col} ${v.kind} (${v.params} params)`);
console.log(` ${v.snippet}`);
}

console.log('\nPrefer a single typed object parameter.');
console.log('Add @allow-multi-param in a leading comment only when a workaround is required.');
process.exit(1);