Skip to content

Commit ee984d5

Browse files
committed
refactor(install-dynamic-plugins): address remaining Sonar findings
- `merger.ts isEqual` (complexity 20 → ~6): extract `isArrayEqual` and `isObjectEqual` helpers. Each branch now dispatches to a single- purpose function so the main `isEqual` is a flat type-dispatch instead of nested loops. - `plugin-hash.ts compareCodePoint`: replace the nested ternary `a < b ? -1 : a > b ? 1 : 0` with three early returns. Same behaviour, no nested conditional. 115 tests still pass.
1 parent dd84f75 commit ee984d5

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

scripts/install-dynamic-plugins/dist/install-dynamic-plugins.cjs

Lines changed: 14 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/install-dynamic-plugins/src/merger.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,21 +253,21 @@ function copyPluginFields(src: Plugin, dst: Plugin, skip: ReadonlyArray<string>)
253253
function isEqual(a: unknown, b: unknown): boolean {
254254
if (a === b) return true;
255255
if (typeof a !== typeof b) return false;
256-
if (Array.isArray(a) && Array.isArray(b)) {
257-
if (a.length !== b.length) return false;
258-
for (let i = 0; i < a.length; i++) {
259-
if (!isEqual(a[i], b[i])) return false;
260-
}
261-
return true;
262-
}
263-
if (isPlainObject(a) && isPlainObject(b)) {
264-
const keysA = Object.keys(a);
265-
const keysB = Object.keys(b);
266-
if (keysA.length !== keysB.length) return false;
267-
for (const k of keysA) {
268-
if (!isEqual(a[k], b[k])) return false;
269-
}
270-
return true;
271-
}
256+
if (Array.isArray(a) && Array.isArray(b)) return isArrayEqual(a, b);
257+
if (isPlainObject(a) && isPlainObject(b)) return isObjectEqual(a, b);
272258
return false;
273259
}
260+
261+
function isArrayEqual(a: readonly unknown[], b: readonly unknown[]): boolean {
262+
if (a.length !== b.length) return false;
263+
return a.every((v, i) => isEqual(v, b[i]));
264+
}
265+
266+
function isObjectEqual(
267+
a: Record<string, unknown>,
268+
b: Record<string, unknown>,
269+
): boolean {
270+
const keysA = Object.keys(a);
271+
if (keysA.length !== Object.keys(b).length) return false;
272+
return keysA.every(k => isEqual(a[k], b[k]));
273+
}

scripts/install-dynamic-plugins/src/plugin-hash.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ function localPackageInfo(pkgPath: string): LocalPackageInfo {
6868
* without pulling in `String.localeCompare` which varies per-locale).
6969
*/
7070
function compareCodePoint(a: string, b: string): number {
71-
return a < b ? -1 : a > b ? 1 : 0;
71+
if (a < b) return -1;
72+
if (a > b) return 1;
73+
return 0;
7274
}
7375

7476
function stableStringify(value: unknown): string {

0 commit comments

Comments
 (0)