ci(client): lint:dup holds copied code to a baseline that only shrinks

Most features have a desktop view and a phone view, and each carried its
own copy of the feature's logic, so SonarCloud's duplication condition
was the gate a pull request touching both failed most often, and only
after a scan of twenty minutes. lint:dup finds the same copies before
the push.

It runs jscpd's detector (@jscpd/core with @jscpd/tokenizer) over the
source files under src/ at the thresholds the SonarCloud gate applies to
TypeScript, 100 tokens over at least 10 lines, and counts per file the
lines inside a block that repeats elsewhere or in the same file. Tests
and declaration files are left out as Sonar leaves them out, and .ts and
.tsx are read as one language, so a hook that repeats a view's code is a
copy too. The files are read sorted and with readText, so every machine
counts the same. scripts/dup-baseline.json holds the files that still
hold copies with their count; a file may hold no more than its entry, a
file without an entry none, and an entry above its file or for a file
that is gone fails until --update lowers it. --list prints every block
with its other side.

The engine packages are used instead of the jscpd command, which walks
the tree in file system order, skips files past its size limits without
a word and drops every result once it meets a file it has no format for.
This commit is contained in:
Maurice
2026-10-08 20:54:53 +00:00
parent 2ccdba78d6
commit 88d196bae8
10 changed files with 404 additions and 4 deletions
+2 -1
View File
@@ -16,6 +16,7 @@ npm run lint:size # no source file or stylesheet past 1000 lines, no tes
npm run lint:format # Prettier: every .ts/.tsx/.mjs/.css file under src/, tests/, e2e/ and scripts/ outside scripts/format-baseline.json must be formatted, the list only shrinks (CI gate; npx prettier --write <file>, then --update)
npm run lint:layers # imports go downwards: components never import pages/ or mobile/, nothing under the views imports a view; per-file counts may only shrink (CI gate; --list, --update)
npm run lint:offline # no view file (components/mobile/pages/hooks) imports src/api/ beyond scripts/offline-baseline.json, which only shrinks (CI gate; --list, --update)
npm run lint:dup # copied code: the lines of a file inside a block that repeats elsewhere (100 tokens over 10 lines, SonarCloud's thresholds; .ts and .tsx read as one language) may only shrink against scripts/dup-baseline.json (CI gate; --list shows each block with its other side, --update lowers it)
npm run lint:skips # no .only anywhere; skipped/todo/fixme tests per file may only shrink against scripts/skip-baseline.json (CI gate; skipIf/runIf and Playwright's test.skip(condition, 'why') are fine, but skipIf(true) and runIf(false) count as skips)
npm run lint:i18n-keys # every translation key src/ names exists in shared en (CI gate; --unused lists en keys nothing reaches)
npm run test # vitest run (tests/** + co-located src/**/*.test.{ts,tsx}); also test:unit / test:integration / test:coverage
@@ -73,7 +74,7 @@ The offline core is flagship work surrounded by a periphery that ignores it. New
## Desktop ↔ mobile shells and the SonarQube duplication gate
Every desktop component with a phone twin is a deliberate copy that is already heavily duplicated. CI's SonarCloud gate allows **≤ 3% duplicated lines on the PR's new code**, so adding the same lines to both shells fails the PR by itself. Put logic in one shared hook/module (`useInstanceSettings`, `useRangeBypass` under `components/Admin/` are the pattern) and keep only markup in each shell. Full gate rules in the root `CLAUDE.md`.
Every desktop component with a phone twin is a deliberate copy that is already heavily duplicated. CI's SonarCloud gate allows **≤ 3% duplicated lines on the PR's new code**, so adding the same lines to both shells fails the PR by itself. Put logic in one shared hook/module (`useInstanceSettings`, `useRangeBypass` under `components/Admin/` are the pattern) and keep only markup in each shell. `npm run lint:dup` finds such copies before the push and holds every file to `scripts/dup-baseline.json`. Full gate rules in the root `CLAUDE.md`.
## Tests
+3
View File
@@ -27,6 +27,7 @@
"lint:format": "node scripts/format-lint.mjs",
"lint:layers": "node scripts/layer-lint.mjs",
"lint:offline": "node scripts/offline-lint.mjs",
"lint:dup": "node scripts/dup-lint.mjs",
"lint:skips": "node scripts/skip-lint.mjs",
"lint:i18n-keys": "node scripts/i18n-keys.mjs",
"check:gl-split": "node scripts/check-gl-split.mjs",
@@ -82,6 +83,8 @@
},
"devDependencies": {
"@eslint/js": "^10.0.1",
"@jscpd/core": "^4.2.5",
"@jscpd/tokenizer": "^4.2.6",
"@playwright/test": "^1.63.0",
"@testing-library/jest-dom": "^6.10.0",
"@testing-library/react": "^16.3.3",
+37
View File
@@ -0,0 +1,37 @@
{
"src/components/Admin/AddonManager.tsx": 23,
"src/components/Files/FileManagerMarkdownPreviewModal.tsx": 19,
"src/components/Files/FileManagerPdfPreviewModal.tsx": 19,
"src/components/Map/MapView.tsx": 56,
"src/components/Map/MapViewGL.tsx": 51,
"src/components/Map/ReservationOverlay.tsx": 75,
"src/components/Map/RouteCalculator.ts": 27,
"src/components/Map/reservationsMapbox.ts": 72,
"src/components/Packing/ApplyTemplateButton.tsx": 19,
"src/components/Packing/PackingListPanelHeader.tsx": 16,
"src/components/Planner/AddressInput.tsx": 38,
"src/components/Planner/AirportSelect.tsx": 44,
"src/components/Planner/DayPlanSidebar.tsx": 326,
"src/components/Planner/LocationSelect.tsx": 56,
"src/components/Planner/ReservationModal.tsx": 26,
"src/components/Planner/TransportModal.tsx": 26,
"src/components/Studio/StudioInspector.tsx": 19,
"src/components/Studio/StudioTravelInspector.tsx": 19,
"src/components/Studio/TravelElements.tsx": 10,
"src/components/Studio/roadRoute.ts": 17,
"src/components/shared/CustomTimePicker.tsx": 24,
"src/mobile/screens/admin/MAdminAddonManager.tsx": 23,
"src/mobile/screens/admin/MAdminStoragePanel.tsx": 38,
"src/mobile/screens/trip/sheets/MReservationSheet.tsx": 79,
"src/mobile/screens/trip/sheets/MTransportFormSheet.tsx": 139,
"src/mobile/screens/trip/tabs/MBookingsTab.tsx": 88,
"src/mobile/screens/trip/tabs/MTransportsTab.tsx": 90,
"src/mobile/screens/vacay/MVacayInviteSheet.tsx": 21,
"src/mobile/screens/vacay/MVacayShareSheet.tsx": 21,
"src/pages/TripPlannerPage.tsx": 82,
"src/pages/admin/GoogleOptions.tsx": 13,
"src/pages/admin/TrekApiCard.tsx": 14,
"src/pages/atlas/AtlasCountrySearch.tsx": 38,
"src/store/slices/budgetSlice.ts": 16,
"src/store/slices/remoteEventHandler.ts": 17
}
+29
View File
@@ -0,0 +1,29 @@
#!/usr/bin/env node
/*
* lint:dup: keeps copied code from coming back.
*
* Most features have a desktop view and a phone view, and each used to carry
* its own copy of the feature's logic, so a fix reached one of them and the
* other kept the bug. The logic now lives in one shared hook or module per
* feature, and this check holds the copies that remain to what they are. It
* counts, per file under src/, the lines inside a block of code that repeats
* elsewhere (or in the same file), at the thresholds SonarCloud uses on a
* pull request. The files that still hold such lines are listed in
* scripts/dup-baseline.json with their count, and a file may hold no more
* than its entry (a file without an entry holds none). An entry above what
* its file holds now, or for a file that is gone, fails as well until
* --update lowers it.
*
* npm run lint:dup check against the baseline (CI)
* npm run lint:dup -- --list print every copied block with its other side
* npm run lint:dup -- --update lower the baseline to what the files hold now;
* it never raises an entry
*
* The measuring lives in scripts/lib/dup.mjs.
*/
import { fileURLToPath } from 'node:url';
import { check } from './lib/dup.mjs';
import { runCli } from './lib/ratchet.mjs';
const root = fileURLToPath(new URL('..', import.meta.url));
await runCli('lint:dup', (args) => check({ root, update: args.includes('--update'), list: args.includes('--list') }));
+139
View File
@@ -0,0 +1,139 @@
/*
* The measuring and the comparison behind lint:dup (scripts/dup-lint.mjs).
*
* Copies are found by jscpd's detector at the thresholds SonarCloud applies to
* TypeScript: a block counts once MIN_TOKENS tokens repeat over at least
* MIN_LINES lines. Both .ts and .tsx are read as TypeScript, because Sonar
* treats them as one language and a hook in a .ts file that repeats a view's
* .tsx code is a copy all the same. A file's count is the number of its lines
* inside a copied block, the duplicated lines Sonar reports. Which of three
* copies a block is matched against depends on the order the files are read
* in, so they are read sorted by path, and with readText, so every machine
* counts the same.
*/
import { Detector, getModeHandler, MemoryStore } from '@jscpd/core';
import { Tokenizer } from '@jscpd/tokenizer';
import { join } from 'node:path';
import {
countMap,
listFiles,
lowerCounts,
readBaseline,
readText,
reportStale,
staleCounts,
TEST_FILE,
toKey,
writeBaseline,
} from './ratchet.mjs';
export const MIN_TOKENS = 100;
export const MIN_LINES = 10;
export const DIRS = ['src'];
/** The files SonarCloud analyses as source: tests and declaration files are left out. */
export function accepts(key) {
return /\.tsx?$/.test(key) && !key.endsWith('.d.ts') && !TEST_FILE.test(key);
}
/**
* Every copied block under root as a pair of line ranges, one per side. Both
* sides can be the same file.
*
* @returns {Promise<{ key: string, start: number, end: number }[][]>}
*/
export async function findCopies(root) {
const keys = listFiles(root, DIRS, accepts)
.map((path) => toKey(root, path))
.sort();
const detector = new Detector(new Tokenizer(), new MemoryStore(), [], {
minTokens: MIN_TOKENS,
minLines: MIN_LINES,
mode: getModeHandler('mild'),
});
const side = ({ sourceId, start, end }) => ({ key: sourceId, start: start.line, end: end.line });
const copies = [];
for (const key of keys) {
const clones = await detector.detect(key, readText(join(root, key)), 'typescript');
for (const clone of clones) copies.push([side(clone.duplicationA), side(clone.duplicationB)]);
}
return copies;
}
/** Per file in path order, how many of its lines sit inside a copied block. */
export function countLines(copies) {
const lines = {};
for (const pair of copies) {
for (const { key, start, end } of pair) {
const set = (lines[key] ??= new Set());
for (let line = start; line <= end; line++) set.add(line);
}
}
return Object.fromEntries(
Object.entries(lines)
.filter(([, set]) => set.size > 0)
.sort(([a], [b]) => (a < b ? -1 : 1))
.map(([key, set]) => [key, set.size])
);
}
/** Per file, its copied blocks as "start-end = other:start-end", in line order. */
export function describeCopies(copies) {
const found = {};
const range = ({ start, end }) => `${start}-${end}`;
for (const [a, b] of copies) {
(found[a.key] ??= []).push({ at: a.start, text: `${range(a)} = ${b.key}:${range(b)}` });
(found[b.key] ??= []).push({ at: b.start, text: `${range(b)} = ${a.key}:${range(a)}` });
}
return Object.fromEntries(
Object.entries(found).map(([key, list]) => [key, list.sort((x, y) => x.at - y.at).map(({ text }) => text)])
);
}
/** Runs the check against the baseline at baselinePath and returns the exit code. */
export async function check({
root,
baselinePath = join(root, 'scripts/dup-baseline.json'),
update = false,
list = false,
log = console.log,
error = console.error,
}) {
const copies = await findCopies(root);
const counts = countLines(copies);
const blocks = describeCopies(copies);
let baseline = readBaseline(baselinePath, countMap);
if (list) {
for (const [key, n] of Object.entries(counts)) {
log(`${key} (${n}, baseline ${baseline[key] ?? 0})`);
for (const line of blocks[key]) log(` ${line}`);
}
}
if (update) {
baseline = lowerCounts(baseline, counts);
writeBaseline(baselinePath, baseline);
}
const grown = Object.entries(counts).filter(([key, n]) => n > (baseline[key] ?? 0));
const stale = staleCounts(baseline, counts);
for (const [key, n] of grown) {
error(`FAIL ${key}: ${n} line(s) in copied blocks, baseline ${baseline[key] ?? 0}:`);
for (const line of blocks[key]) error(` ${line}`);
}
if (grown.length) {
error(
'Put the code both places need in one shared hook or module and import it from both instead of copying it ' +
'(useInstanceSettings and useRangeBypass under src/components/Admin/ are the pattern).'
);
}
reportStale(stale, { file: 'dup-baseline.json', command: 'lint:dup', root, error });
const sum = (map) => Object.values(map).reduce((a, b) => a + b, 0);
log(
`dup: ${sum(counts)} line(s) in copied blocks in ${Object.keys(counts).length} file(s), baseline allows ${sum(baseline)}`
);
return grown.length || stale.length ? 1 : 0;
}
+145
View File
@@ -0,0 +1,145 @@
import { readFileSync } from 'node:fs';
import { afterEach, describe, expect, it } from 'vitest';
import { check, MIN_LINES } from '../../../scripts/lib/dup.mjs';
import { RatchetError } from '../../../scripts/lib/ratchet.mjs';
import { ratchetTree, type RatchetTree } from '../../helpers/ratchetFixture';
let tree: RatchetTree;
afterEach(() => tree?.remove());
const BASELINE = 'scripts/dup-baseline.json';
/**
* n lines of about twenty tokens each, every line different, so a block of
* MIN_LINES + 4 of them is long enough to count as a copy and repeats nothing
* inside itself. Two blocks with the same name are the same code.
*/
function block(name = 'shared', n = MIN_LINES + 4): string {
return Array.from({ length: n }, (_, i) => `export const ${name}${i} = compute(${i}, 'x', [1, 2, 3]);\n`).join('');
}
function run(
baseline: Record<string, number> | string,
files: Record<string, string>,
options: { update?: boolean; list?: boolean } = {}
) {
tree = ratchetTree({
[BASELINE]: typeof baseline === 'string' ? baseline : JSON.stringify(baseline),
...files,
});
return check({ root: tree.root, baselinePath: tree.path(BASELINE), ...options, ...tree.out });
}
describe('lint:dup', () => {
it('DUP-001: fails both sides of a block two files share, naming the other side', async () => {
expect(await run({}, { 'src/a/View.tsx': block(), 'src/b/MView.tsx': block() })).toBe(1);
const out = tree.error.join('\n');
expect(out).toMatch(
/FAIL {2}src\/a\/View\.tsx: 14 line\(s\) in copied blocks, baseline 0:\n {8}1-14 = src\/b\/MView\.tsx:1-14/
);
expect(out).toMatch(
/FAIL {2}src\/b\/MView\.tsx: 14 line\(s\) in copied blocks, baseline 0:\n {8}1-14 = src\/a\/View\.tsx:1-14/
);
expect(out).toMatch(/one shared hook or module/);
expect(tree.log.join('\n')).toMatch(/dup: 28 line\(s\) in copied blocks in 2 file\(s\), baseline allows 0/);
});
it('DUP-002: passes different code, and a repeat too short in lines or tokens to count', async () => {
const shortLines = block('k', MIN_LINES - 1);
const fewTokens = Array.from({ length: MIN_LINES + 4 }, (_, i) => `s${i}()\n`).join('');
const files = {
'src/a.ts': block('a') + fewTokens,
'src/b.ts': block('b') + fewTokens,
'src/c.ts': shortLines,
'src/d.ts': shortLines,
};
expect(await run({}, files)).toBe(0);
expect(tree.error).toEqual([]);
});
it('DUP-003: reads .ts and .tsx as one language, so a hook and a view that repeat it are a copy', async () => {
expect(await run({}, { 'src/components/X/useThing.ts': block(), 'src/mobile/MThing.tsx': block() })).toBe(1);
const out = tree.error.join('\n');
expect(out).toMatch(/FAIL {2}src\/components\/X\/useThing\.ts: 14 line/);
expect(out).toMatch(/FAIL {2}src\/mobile\/MThing\.tsx: 14 line/);
});
it('DUP-004: leaves tests and declaration files out, as SonarCloud does', async () => {
const files = {
'src/a.test.tsx': block(),
'src/b.test.ts': block(),
'src/c.d.ts': block(),
'src/real.ts': block('real'),
};
expect(await run({}, files)).toBe(0);
expect(tree.error).toEqual([]);
});
it('DUP-005: counts a block one file repeats inside itself', async () => {
expect(await run({}, { 'src/a.ts': block() + block('mid') + block() })).toBe(1);
expect(tree.error.join('\n')).toMatch(/FAIL {2}src\/a\.ts: 28 line\(s\)[^\n]*\n {8}1-14 = src\/a\.ts:29-42/);
});
it('DUP-006: holds a baselined file at its entry and fails a new copy or a grown one', async () => {
const held = { 'src/a.ts': 14, 'src/b.ts': 14 };
expect(await run(held, { 'src/a.ts': block(), 'src/b.ts': block() })).toBe(0);
tree.remove();
// A third copy of code that is already copied is still a new copy.
expect(await run(held, { 'src/a.ts': block(), 'src/b.ts': block(), 'src/c.ts': block() })).toBe(1);
expect(tree.error.join('\n')).toMatch(/FAIL {2}src\/c\.ts: 14 line\(s\) in copied blocks, baseline 0/);
expect(tree.error.join('\n')).not.toMatch(/FAIL {2}src\/[ab]\.ts/);
tree.remove();
const grown = block('x') + block('y');
expect(await run(held, { 'src/a.ts': grown, 'src/b.ts': grown })).toBe(1);
expect(tree.error.join('\n')).toMatch(/FAIL {2}src\/a\.ts: 28 line\(s\) in copied blocks, baseline 14/);
});
it('DUP-007: --update lowers an entry, drops one without copies and never raises', async () => {
const baseline = { 'src/a.ts': 30, 'src/b.ts': 10, 'src/gone.ts': 5, 'src/clean.ts': 10 };
const files = { 'src/a.ts': block(), 'src/b.ts': block(), 'src/clean.ts': block('clean') };
expect(await run(baseline, files, { update: true })).toBe(1);
expect(JSON.parse(readFileSync(tree.path(BASELINE), 'utf8'))).toEqual({ 'src/a.ts': 14, 'src/b.ts': 10 });
expect(tree.error.join('\n')).toMatch(/FAIL {2}src\/b\.ts: 14 line\(s\) in copied blocks, baseline 10/);
});
it('DUP-008: fails an entry above what its file holds, or for a file that is gone, until --update', async () => {
expect(await run({ 'src/a.ts': 20, 'src/b.ts': 14 }, { 'src/a.ts': block(), 'src/b.ts': block() })).toBe(1);
expect(tree.error.join('\n')).toMatch(
/src\/a\.ts is held at 20 in scripts\/dup-baseline\.json, but there are 14 now/
);
expect(tree.error.join('\n')).toMatch(/npm run lint:dup -- --update/);
tree.remove();
expect(await run({ 'src/gone.ts': 14 }, { 'src/a.ts': block('a') })).toBe(1);
expect(tree.error.join('\n')).toMatch(/src\/gone\.ts is held at 14 .*but the file is gone/);
tree.remove();
// A file whose copies are all gone keeps no allowance either.
expect(await run({ 'src/a.ts': 14 }, { 'src/a.ts': block('a') })).toBe(1);
expect(tree.error.join('\n')).toMatch(/src\/a\.ts is held at 14 .*but there are 0 now/);
tree.remove();
expect(await run({ 'src/a.ts': 14, 'src/gone.ts': 14 }, { 'src/a.ts': block('a') }, { update: true })).toBe(0);
});
it('DUP-009: counts a file with CRLF line ends the same as one with LF', async () => {
expect(await run({}, { 'src/a.ts': block().replace(/\n/g, '\r\n'), 'src/b.ts': block() })).toBe(1);
expect(tree.error.join('\n')).toMatch(/FAIL {2}src\/a\.ts: 14 line\(s\)[^\n]*\n {8}1-14 = src\/b\.ts:1-14/);
});
it('DUP-010: --list prints every copied block with its other side', async () => {
expect(
await run({ 'src/a.ts': 14, 'src/b.ts': 14 }, { 'src/a.ts': block(), 'src/b.ts': block() }, { list: true })
).toBe(0);
expect(tree.log).toEqual([
'src/a.ts (14, baseline 14)',
' 1-14 = src/b.ts:1-14',
'src/b.ts (14, baseline 14)',
' 1-14 = src/a.ts:1-14',
'dup: 28 line(s) in copied blocks in 2 file(s), baseline allows 28',
]);
});
it('DUP-011: a broken baseline or a missing src/ stops the check', async () => {
await expect(run('{', { 'src/a.ts': block('a') })).rejects.toThrow(RatchetError);
tree.remove();
await expect(run({}, { 'tests/a.ts': block('a') })).rejects.toThrow(/src\/ does not exist/);
});
});