Skip to content

Commit

Permalink
revert(fix-mismatches): don't remove nested empty objects
Browse files Browse the repository at this point in the history
Reverts a279e56
Closes #117
  • Loading branch information
JamieMason committed Feb 16, 2023
1 parent 0cf675d commit 393d004
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/bin-fix-mismatches/fix-mismatches.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEmptyObject, isObject, isUndefined } from 'expect-more';
import { isObject, isUndefined } from 'expect-more';
import type { Syncpack } from '../types';

export function fixMismatches(ctx: Syncpack.Ctx): Syncpack.Ctx {
Expand All @@ -19,25 +19,19 @@ export function fixMismatches(ctx: Syncpack.Ctx): Syncpack.Ctx {
});
});

ctx.packageJsonFiles.forEach((file) => {
removeEmptyObjects(file.contents);
/** Remove eg `{"dependencies": {}, "devDependencies": {}}` */
ctx.packageJsonFiles.forEach((packageJsonFile) => {
const contents = packageJsonFile.contents;
Object.keys(contents).forEach((key) => {
const value = contents[key];
if (
isObject<Record<string, unknown>>(value) &&
Object.values(value).every(isUndefined)
) {
delete contents[key];
}
});
});

return ctx;

/** Remove eg { "dependencies": {}, "devDependencies": {} }` */
function removeEmptyObjects(parent: unknown) {
if (isObject(parent)) {
Object.entries(parent).forEach(([key, child]) => {
if (
isObject(child) &&
(isEmptyObject(child) || Object.values(child).every(isUndefined))
) {
delete parent[key];
} else {
removeEmptyObjects(child);
}
});
}
}
}

0 comments on commit 393d004

Please sign in to comment.