Skip to content

Commit 15f4892

Browse files
committed
chore(gates): guard native scan scope and portable prototype counts
1 parent 0fa0c26 commit 15f4892

5 files changed

Lines changed: 149 additions & 16 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
"fallow:baseline": "fallow health --report-only --save-baseline fallow-baselines/health.json --summary",
148148
"check:fallow": "fallow audit",
149149
"check:native-dupes": "jscpd --config .jscpd-native.json --baseline native-dupes-baseline.json --fail-on-new-clones --fail-on-empty",
150-
"check:native-dupes:test": "node --experimental-strip-types scripts/node-test-tmpdir.ts --experimental-strip-types --test scripts/native-dupes/check.test.ts",
150+
"check:native-dupes:test": "node --experimental-strip-types scripts/node-test-tmpdir.ts --experimental-strip-types --test scripts/native-dupes/*.test.ts",
151151
"native-dupes:report": "jscpd --config .jscpd-native.json --baseline native-dupes-baseline.json --fail-on-empty",
152152
"native-dupes:baseline": "jscpd --config .jscpd-native.json --baseline native-dupes-baseline.json --update-baseline --fail-on-empty",
153153
"native-dupes:prototype": "node --experimental-strip-types scripts/native-dupes/prototype.ts",

‎scripts/check-affected/model.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,31 @@ test('native duplication covers language inputs without selecting metadata or Ty
477477
assert.ok(ids([file]).includes('native-dupes-model'), file);
478478
}
479479
});
480+
481+
test('native duplication selection covers every configured root, format, and custom extension', () => {
482+
const config: { path: string[]; format: string[]; formatsExts: Record<string, string[]> } =
483+
JSON.parse(fs.readFileSync(path.join(repoRoot, '.jscpd-native.json'), 'utf8'));
484+
const defaultExtensions: Record<string, string[]> = {
485+
swift: ['swift'],
486+
objectivec: ['m'],
487+
kotlin: ['kt', 'kts'],
488+
java: ['java'],
489+
python: ['py'],
490+
};
491+
for (const format of config.format) {
492+
const defaults = defaultExtensions[format];
493+
assert.ok(defaults, `Declare the scanner extensions for the added ${format} format`);
494+
const extensions = new Set([...defaults, ...(config.formatsExts[format] ?? [])]);
495+
for (const root of config.path) {
496+
for (const extension of extensions) {
497+
const file = `${root}/duplication-probe/Source.${extension}`;
498+
assert.ok(
499+
plan([file]).reasons.some(
500+
(entry) => entry.check === 'native-dupes' && entry.rule === 'own:native-dupes',
501+
),
502+
file,
503+
);
504+
}
505+
}
506+
}
507+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import assert from 'node:assert/strict';
2+
import { test } from 'node:test';
3+
import { countMotivatingClones, namedConstantRange } from './prototype-report.ts';
4+
5+
const source = `// const IPHONE_DUO_CLOSED = "😀 comment, not the fixture";
6+
const other = { nativeSize: [1, 2] };
7+
const IPHONE_DUO_CLOSED = JSON.stringify({
8+
result: {
9+
displays: [
10+
{ active: true, nativeSize: [1398, 2034] },
11+
{ active: false, nativeSize: [2007, 2853] },
12+
],
13+
},
14+
});
15+
const singlePanel = { active: true };
16+
`;
17+
18+
test('Duo range follows its named constant after line shifts and ignores comment decoys', () => {
19+
assert.deepEqual(namedConstantRange(source, 'IPHONE_DUO_CLOSED'), { start: 3, end: 10 });
20+
assert.deepEqual(namedConstantRange('\n'.repeat(40) + source, 'IPHONE_DUO_CLOSED'), {
21+
start: 43,
22+
end: 50,
23+
});
24+
assert.throws(() => namedConstantRange(source, 'RENAMED_FIXTURE'), /RENAMED_FIXTURE/);
25+
});
26+
27+
for (const separator of ['/', '\\']) {
28+
test(`motivating clone counts accept ${JSON.stringify(separator)} paths and shifted fixture lines`, () => {
29+
const target = ['checkout', 'simctl-facade.test.ts'].join(separator);
30+
const other = ['checkout', 'display-inventory.test.ts'].join(separator);
31+
const fold = ['checkout', 'apple', 'fold-helper', 'Fold.m'].join(separator);
32+
const counts = countMotivatingClones(
33+
[
34+
{
35+
firstFile: { name: fold, start: 2, end: 8 },
36+
secondFile: { name: fold, start: 12, end: 18 },
37+
},
38+
{
39+
firstFile: { name: target, start: 45, end: 47 },
40+
secondFile: { name: other, start: 70, end: 72 },
41+
},
42+
{
43+
firstFile: { name: other, start: 73, end: 75 },
44+
secondFile: { name: target, start: 48, end: 50 },
45+
},
46+
{
47+
firstFile: { name: target, start: 51, end: 60 },
48+
secondFile: { name: other, start: 80, end: 89 },
49+
},
50+
{
51+
firstFile: { name: target, start: 41, end: 42 },
52+
secondFile: { name: other, start: 5, end: 6 },
53+
},
54+
{
55+
firstFile: { name: target, start: 45, end: 47 },
56+
secondFile: { name: target, start: 48, end: 50 },
57+
},
58+
],
59+
namedConstantRange('\n'.repeat(40) + source, 'IPHONE_DUO_CLOSED'),
60+
);
61+
assert.deepEqual(counts, { fold: 1, duoPanels: 2 });
62+
});
63+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { parseSync } from 'oxc-parser';
2+
import { repoRelativePath } from '../lib/repo-path.ts';
3+
4+
type LineRange = { start: number; end: number };
5+
type CloneLocation = LineRange & { name: string };
6+
export type PrototypeClone = { firstFile: CloneLocation; secondFile: CloneLocation };
7+
8+
export function namedConstantRange(source: string, name: string): LineRange {
9+
const parsed = parseSync('fixture.ts', source);
10+
if (parsed.errors.length > 0) throw new Error('Cannot parse the prototype fixture');
11+
const declaration = parsed.program.body
12+
.flatMap((statement) =>
13+
statement.type === 'VariableDeclaration' && statement.kind === 'const'
14+
? statement.declarations
15+
: [],
16+
)
17+
.find((item) => item.id.type === 'Identifier' && item.id.name === name);
18+
if (!declaration?.init) throw new Error(`Cannot locate prototype fixture constant ${name}`);
19+
return {
20+
start: source.slice(0, declaration.init.start).split('\n').length,
21+
end: source.slice(0, declaration.init.end).split('\n').length,
22+
};
23+
}
24+
25+
export function countMotivatingClones(clones: readonly PrototypeClone[], duoRange: LineRange) {
26+
let fold = 0;
27+
let duoPanels = 0;
28+
for (const clone of clones) {
29+
const files = [clone.firstFile, clone.secondFile].map((file) => ({
30+
...file,
31+
name: repoRelativePath(file.name),
32+
}));
33+
if (files.some((file) => file.name.endsWith('/fold-helper/Fold.m'))) fold++;
34+
if (
35+
files[0]!.name !== files[1]!.name &&
36+
files.some(
37+
(file) =>
38+
file.name.endsWith('/simctl-facade.test.ts') &&
39+
file.start <= duoRange.end &&
40+
file.end >= duoRange.start,
41+
)
42+
)
43+
duoPanels++;
44+
}
45+
return { fold, duoPanels };
46+
}

‎scripts/native-dupes/prototype.ts‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import { performance } from 'node:perf_hooks';
44
import { runCmdSync } from '@agent-device/host-kit/command';
5+
import {
6+
countMotivatingClones,
7+
namedConstantRange,
8+
type PrototypeClone,
9+
} from './prototype-report.ts';
510

611
const root = path.resolve(import.meta.dirname, '../..');
712
const cli = path.join(root, 'node_modules/jscpd/run-jscpd.js');
@@ -10,6 +15,10 @@ const pair = [
1015
'packages/platform-apple/src/core/__tests__/display-inventory.test.ts',
1116
'packages/platform-apple/src/__tests__/simctl-facade.test.ts',
1217
];
18+
const duoRange = namedConstantRange(
19+
fs.readFileSync(path.join(root, pair[1]!), 'utf8'),
20+
'IPHONE_DUO_CLOSED',
21+
);
1322
const variants = [
1423
{ name: 'mild-50-5', tokens: 50, lines: 5, mode: 'mild', extra: [] },
1524
{ name: 'weak-50-5', tokens: 50, lines: 5, mode: 'weak', extra: [] },
@@ -31,10 +40,7 @@ type Report = {
3140
statistics: {
3241
total: { clones: number; duplicatedLines: number; percentage: number; sources: number };
3342
};
34-
duplicates: Array<{
35-
firstFile: { name: string; start: number; end: number };
36-
secondFile: { name: string; start: number; end: number };
37-
}>;
43+
duplicates: PrototypeClone[];
3844
};
3945

4046
const rows = [];
@@ -71,17 +77,7 @@ for (const variant of variants) {
7177
fs.readFileSync(path.join(destination, 'jscpd-report.json'), 'utf8'),
7278
);
7379
const { clones, duplicatedLines, percentage, sources } = report.statistics.total;
74-
const fold = report.duplicates.filter((clone) =>
75-
[clone.firstFile, clone.secondFile].some((file) => file.name.endsWith('/fold-helper/Fold.m')),
76-
).length;
77-
const duoPanels = report.duplicates.filter(
78-
(clone) =>
79-
clone.firstFile.name !== clone.secondFile.name &&
80-
[clone.firstFile, clone.secondFile].some(
81-
(file) =>
82-
file.name.endsWith('/simctl-facade.test.ts') && file.start <= 50 && file.end >= 34,
83-
),
84-
).length;
80+
const { fold, duoPanels } = countMotivatingClones(report.duplicates, duoRange);
8581
const row = {
8682
variant: variant.name,
8783
scope,

0 commit comments

Comments
 (0)