Skip to content

Commit caf681c

Browse files
authored
Merge pull request #28 from olehwebdev/feature/case-clash-check
Refuse names that differ only in case in lint:structure
2 parents b42471c + df9ae1a commit caf681c

11 files changed

Lines changed: 139 additions & 6 deletions

File tree

‎.claude/skills/code-structure/SKILL.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The rules live in `CLAUDE.md › Code structure`; `npm run lint:structure` (`scr
1111

1212
1. Find where the code belongs by layer and concern: main process (`src/main/<area>/`), shared contract (`src/shared/`), renderer slice (`app → pages → widgets → features → entities → shared`, see `docs/DESIGN_SYSTEM.md`).
1313
2. Open the target folder. If the file you'd edit is near 150 lines, or your change adds a second concern to it, split first (below), then add your code in its own file.
14-
3. Name the new file after the one thing it holds: `sinceInput.ts`, `useDragCursor.ts`, `ResourceTracker.ts`, `EntryRow.tsx`.
14+
3. Name the new file after the one thing it holds: `sinceInput.ts`, `useDragCursor.ts`, `ResourceTracker.ts`, `EntryRow.tsx`. Never a name a sibling has in another case (`actionFields.ts` beside `ActionFields.tsx`): macOS and Windows ignore case, so an import can load the wrong one there. Say what sets it apart instead (`ruleActionFields.ts`).
1515

1616
## How to split
1717

‎CLAUDE.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Before pushing:
99
```bash
1010
npm run typecheck
1111
npm run lint:fsd
12-
npm run lint:structure # the Code structure rules below: thin files, one function each, no switch
12+
npm run lint:structure # the Code structure rules below: thin files, one function each, no switch, no case clashes
1313
npm run lint # oxlint, type-aware: React's rules (hooks, refs, purity) and misused promises
1414
npm run lint:unused # knip: no unused file, dependency or export (knip.jsonc says what counts)
1515
npm run lint:duplicates # jscpd: no copy of code that isn't in .jscpd-baseline.json
@@ -56,6 +56,7 @@ Agents: when the environment assigns a generated branch (`claude/…`) and says
5656
- **No magic values.** Name every literal that drives logic: ids, keys and prefixes, the app's own channel and event names (IPC channels, `AppEvent` types), durations (a step of `DURATION` in `shared/lib/motion.ts`), sizes used in more than one place, and any value that has to match another system (Monaco command ids, `execCommand` names, `KeyboardEvent.key`, env vars). The platform's own event names (`'keydown'`, `'did-navigate'`), which its typed listeners check, stay inline. A constant goes at the top of the file that uses it, or in the folder's `constants.ts` when several files do; app-wide ones go in `shared/config` (renderer) or `src/shared/constants.ts` (both processes). Display copy, Tailwind classes and identity values (`0`, `1`, `''`, `true`) stay inline.
5757
- **No copies.** `npm run lint:duplicates` refuses code copied from elsewhere in `src` or `scripts` (jscpd, 50 tokens or more): move it to a function both use. `.jscpd-baseline.json` lists the copies there were when the check came in; it only shrinks. After removing one, run `npm run lint:duplicates -- --update-baseline` and commit the smaller file; never add a copy to it to get a commit through.
5858
- **No `switch`.** Dispatch through a typed table (`Record<Union, Handler>`, or a mapped type when each handler takes its own member), so a new union member fails typecheck until it's handled (see `app/model/bridge/`). An if/else chain or nested ternary over one value counts as a switch.
59+
- **No names that differ only in case** in one folder of `src`, `scripts` or `test` (`ActionFields.tsx` beside `actionFields.ts`, or a folder `foo/` beside `Foo.ts`). macOS and Windows ignore case, so `import './ActionFields'` can load the other file there, which no Linux build shows. Name the helper after what it holds (`ruleActionFields.ts`). Files imported with their extension (`Markdown.tsx` beside `markdown.css`) are fine.
5960

6061
## Commits and pull requests
6162

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ npm run demo-site # in another terminal: pages to try it on (port 5174)
2929
```bash
3030
npm run typecheck
3131
npm run lint:fsd
32-
npm run lint:structure # files of at most 150 lines, one function each, no switch (CLAUDE.md › Code structure)
32+
npm run lint:structure # files of at most 150 lines, one function each, no switch, no names differing only in case (CLAUDE.md › Code structure)
3333
npm run lint # oxlint with type information: React's rules and misused promises (.oxlintrc.json)
3434
npm run lint:unused # no unused files, dependencies or exports (knip; types in a slice's index.ts are its public API)
3535
npm run lint:duplicates # no new copies of code (jscpd; .jscpd-baseline.json lists the older ones, and only shrinks)

‎docs/SPEC.md‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎scripts/check-structure.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
* Checks the source (src/, scripts/; tests are exempt) against CLAUDE.md ›
33
* Code structure: files of at most 150 lines, one function, component, class
44
* or store per file named after it, data-only constants/types/index files, and
5-
* no `switch` (nor if/else chains or nested ternaries over one value).
5+
* no `switch` (nor if/else chains or nested ternaries over one value). Also
6+
* checks src/, scripts/ and test/ for names that differ only in case, which
7+
* macOS and Windows take for one while Linux (and CI) tell them apart.
68
* Exits with 1 and lists what to fix when a file breaks a rule.
79
*
810
* npm run lint:structure
911
*/
1012
import { readFileSync } from 'node:fs';
1113
import { join } from 'node:path';
1214
import { checkFile } from './structure/checkFile.ts';
15+
import { findCaseClashes } from './structure/findCaseClashes.ts';
1316
import { sourceFiles } from './structure/sourceFiles.ts';
1417

1518
const root = join(import.meta.dirname, '..');
1619
const files = sourceFiles(root);
17-
const violations = files.flatMap((file) => checkFile(file, readFileSync(join(root, file), 'utf8')));
20+
const violations = [...files.flatMap((file) => checkFile(file, readFileSync(join(root, file), 'utf8'))), ...findCaseClashes(root)];
1821

1922
for (const v of violations) console.error(`${v.file}${v.line ? `:${v.line}` : ''} ${v.rule}: ${v.detail}`);
2023
if (violations.length) {

‎scripts/structure/caseClashes.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { lookupNames } from './lookupNames.ts';
2+
import type { FolderEntry } from './types.ts';
3+
4+
/**
5+
* The entries of one folder that a file system ignoring case (macOS, Windows) can't tell apart, in groups of names:
6+
* two whose names differ only in case (`Foo.css`, `foo.css`), or two modules an import without an extension reaches
7+
* by names that do (`./ActionFields` finds `actionFields.ts` there before `ActionFields.tsx`). `Markdown.tsx` beside
8+
* `markdown.css` is fine: a stylesheet is imported by its whole name.
9+
*/
10+
export function caseClashes(entries: FolderEntry[]): string[][] {
11+
// Each name a lookup can use, lower-cased → the entries it reaches, each with the spelling that reaches it.
12+
const reached = new Map<string, Map<string, string>>();
13+
for (const entry of entries) {
14+
for (const spelling of lookupNames(entry)) {
15+
const key = spelling.toLowerCase();
16+
reached.set(key, (reached.get(key) ?? new Map<string, string>()).set(entry.name, spelling));
17+
}
18+
}
19+
// A lookup that reaches entries spelled alike is no clash (`Foo.ts` beside `Foo.tsx`); one pair can clash twice.
20+
const groups = new Map<string, string[]>();
21+
for (const spellings of reached.values()) {
22+
if (new Set(spellings.values()).size < 2) continue;
23+
const names = [...spellings.keys()].sort();
24+
groups.set(JSON.stringify(names), names);
25+
}
26+
return [...groups.values()];
27+
}

‎scripts/structure/constants.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@ export const EQUALITY_OPERATORS = ['===', '=='];
2424

2525
/** A value compared with literals this many times in one if/else chain or nested ternary makes it a switch. */
2626
export const SWITCH_LIKE_COMPARISONS = 2;
27+
28+
/**
29+
* Folders checked for names that macOS and Windows, whose file systems ignore case, can't tell apart. Tests too:
30+
* they are built and run there.
31+
*/
32+
export const CASE_CHECKED_ROOTS = ['src', 'scripts', 'test'];
33+
34+
/** What an import without an extension may find (Vite's `resolve.extensions`): `./Foo` reaches `foo.ts` there. */
35+
export const MODULE_EXTENSIONS = ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { readdirSync } from 'node:fs';
2+
import { join, relative } from 'node:path';
3+
import { caseClashes } from './caseClashes.ts';
4+
import { CASE_CHECKED_ROOTS } from './constants.ts';
5+
import type { Violation } from './types.ts';
6+
7+
/** Every group of names under the checked folders that macOS and Windows can't tell apart, one violation each. */
8+
export function findCaseClashes(root: string): Violation[] {
9+
const violations: Violation[] = [];
10+
const visit = (dir: string) => {
11+
const entries = readdirSync(dir, { withFileTypes: true });
12+
for (const names of caseClashes(entries.map((entry) => ({ name: entry.name, directory: entry.isDirectory() })))) {
13+
const detail = `${names.join(', ')}: macOS and Windows ignore case, so they take one for the other (an import may load the wrong one): rename one`;
14+
violations.push({ file: relative(root, dir), rule: 'no case clashes', detail });
15+
}
16+
for (const entry of entries) if (entry.isDirectory()) visit(join(dir, entry.name));
17+
};
18+
for (const dir of CASE_CHECKED_ROOTS) visit(join(root, dir));
19+
return violations;
20+
}

‎scripts/structure/lookupNames.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { extname } from 'node:path';
2+
import { MODULE_EXTENSIONS } from './constants.ts';
3+
import type { FolderEntry } from './types.ts';
4+
5+
/**
6+
* The names a lookup can reach `entry` by: its own, and for a module file the one an import without the extension
7+
* uses (`Foo.tsx` is also `Foo`). A folder is a module by its own name, through its index.
8+
*/
9+
export function lookupNames({ name, directory }: FolderEntry): string[] {
10+
const extension = extname(name);
11+
if (directory || !MODULE_EXTENSIONS.includes(extension)) return [name];
12+
return [name, name.slice(0, -extension.length)];
13+
}

‎scripts/structure/types.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ export interface TopLevelFunction {
1313
name: string;
1414
line: number;
1515
}
16+
17+
/** An entry of a folder, as the case check reads it. */
18+
export interface FolderEntry {
19+
name: string;
20+
directory: boolean;
21+
}

0 commit comments

Comments
 (0)