Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

build: check one export per file in lint

Luis Ortizb6d2d5c998ba
diff --git a/biome.json b/biome.json
index 17cf711..2d6bea2 100644
--- a/biome.json
+++ b/biome.json
@@ -15,6 +15,7 @@
"packages/ui/src/**/*.tsx",
"apps/desktop/src/**/*.ts",
"apps/desktop/src/**/*.tsx",
+ "scripts/**/*.mjs",
"*.json"
]
},
@@ -114,7 +115,14 @@
}
},
{
- "includes": ["packages/ui/src/**/*.ts", "packages/ui/src/**/*.tsx", "!packages/ui/src/components/ui/**"],
+ "includes": [
+ "packages/ui/src/**/*.ts",
+ "packages/ui/src/**/*.tsx",
+ "src/ui/**",
+ "src/lib/**",
+ "apps/desktop/src/renderer/**",
+ "!packages/ui/src/components/ui/**"
+ ],
"linter": {
"rules": {
"performance": {
diff --git a/package.json b/package.json
index 4e17995..1281d32 100644
--- a/package.json
+++ b/package.json
@@ -16,7 +16,7 @@
"test": "vitest run",
"test:watch": "vitest",
"test:cov": "vitest run --coverage",
- "lint": "biome check .",
+ "lint": "biome check . && node scripts/check-exports.mjs",
"lint:fix": "biome check --write .",
"format": "biome format --write .",
"prepare": "husky",
diff --git a/scripts/check-exports.mjs b/scripts/check-exports.mjs
new file mode 100644
index 0000000..e051d66
--- /dev/null
+++ b/scripts/check-exports.mjs
@@ -0,0 +1,61 @@
+import { readdirSync, readFileSync } from 'node:fs';
+import { join, relative } from 'node:path';
+import ts from 'typescript';
+
+const ROOTS = ['packages/ui/src', 'src/ui', 'src/lib', 'apps/desktop/src/renderer'];
+const SKIP = /\/components\/ui\/|^packages\/ui\/src\/index\.ts$|__tests__|\.test\.tsx?$|\.d\.ts$/;
+const WRAPPERS = /^(React\.)?(forwardRef|memo)$/;
+
+const walk = (dir) =>
+ readdirSync(dir, { withFileTypes: true }).flatMap((e) => {
+ const p = join(dir, e.name);
+ if (e.isDirectory()) return walk(p);
+ return /\.tsx?$/.test(e.name) ? [p] : [];
+ });
+
+const isCallable = (init) =>
+ !!init &&
+ (ts.isArrowFunction(init) ||
+ ts.isFunctionExpression(init) ||
+ (ts.isCallExpression(init) && WRAPPERS.test(init.expression.getText())));
+
+const exported = (s) => s.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) ?? false;
+
+function callables(sf) {
+ const out = [];
+ for (const s of sf.statements) {
+ if ((ts.isFunctionDeclaration(s) && s.body) || ts.isClassDeclaration(s)) {
+ out.push({ name: s.name?.text ?? 'default', exported: exported(s) });
+ } else if (ts.isVariableStatement(s)) {
+ for (const d of s.declarationList.declarations) {
+ if (isCallable(d.initializer)) out.push({ name: d.name.getText(sf), exported: exported(s) });
+ }
+ }
+ }
+ return out;
+}
+
+const problems = [];
+for (const file of ROOTS.flatMap(walk).map((f) => relative('.', f))) {
+ if (SKIP.test(file)) continue;
+ const sf = ts.createSourceFile(file, readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true);
+ const all = callables(sf);
+ const pub = all.filter((c) => c.exported);
+ const priv = all.filter((c) => !c.exported);
+ const component = file.endsWith('.tsx') && pub.some((c) => /^[A-Z]/.test(c.name));
+ const reexports = sf.statements.filter((s) => ts.isExportDeclaration(s) && s.moduleSpecifier);
+ if (pub.length > 1) problems.push(`${file}: exports ${pub.map((c) => c.name).join(', ')}; one per file`);
+ if (component && priv.length)
+ problems.push(
+ `${file}: component file declares ${priv.map((c) => c.name).join(', ')}; move to lib/ or its own file`,
+ );
+ if (!component && priv.length > 1)
+ problems.push(`${file}: private ${priv.map((c) => c.name).join(', ')}; one helper at most`);
+ if (reexports.length) problems.push(`${file}: re-exports; import from the source instead`);
+}
+
+if (problems.length) {
+ console.error(problems.join('\n'));
+ console.error(`\n${problems.length} file(s) break one-export-per-file`);
+ process.exit(1);
+}