Skip to content

Commit cbdc87f

Browse files
committed
Fixed the issue where the loclfile object has an undefined property causing yaml.dump to report an error.
1 parent 724aa51 commit cbdc87f

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rush",
5+
"comment": "Fixed the issue where the lockfile object has an nullish value causing yaml.dump to report an error.",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush"
10+
}

libraries/rush-lib/src/logic/pnpm/PnpmShrinkWrapFileConverters.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
ProjectSnapshot,
2525
ResolvedDependencies
2626
} from '@pnpm/lockfile.types';
27+
import { removeNullishProps } from '../../utilities/objectUtilities';
2728

2829
type DepPath = string & { __brand: 'DepPath' };
2930
// eslint-disable-next-line @typescript-eslint/typedef
@@ -56,11 +57,13 @@ function revertProjectSnapshot(from: InlineSpecifiersProjectSnapshot): ProjectSn
5657
from.optionalDependencies == null ? from.optionalDependencies : moveSpecifiers(from.optionalDependencies);
5758

5859
return {
59-
...from,
60-
specifiers,
61-
dependencies,
62-
devDependencies,
63-
optionalDependencies
60+
...removeNullishProps({
61+
...from,
62+
dependencies,
63+
devDependencies,
64+
optionalDependencies
65+
}),
66+
specifiers
6467
};
6568
}
6669

libraries/rush-lib/src/logic/pnpm/test/PnpmShrinkwrapConverters.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,15 @@ describe(convertLockfileV9ToLockfileObject.name, () => {
3838
'pad-left': '^2.1.0'
3939
});
4040
});
41+
42+
it("no nullish values", () => {
43+
const importers = new Map<string, ProjectSnapshot>(Object.entries(lockfile.importers || {}));
44+
45+
const currentPackage = importers.get('.');
46+
const props = Object.keys(currentPackage || {});
47+
expect(props).toContain('dependencies');
48+
expect(props).toContain('specifiers');
49+
expect(props).not.toContain('optionalDependencies');
50+
expect(props).not.toContain('devDependencies');
51+
});
4152
});

libraries/rush-lib/src/utilities/objectUtilities.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,18 @@ function isStrictComparable<T>(value: T): boolean {
153153
value === value && !(value !== null && value !== undefined && (type === 'object' || type === 'function'))
154154
);
155155
}
156+
157+
/**
158+
* Removes `undefined` and `null` properties from an object.
159+
*/
160+
export function removeNullishProps<T extends object>(obj: T): Partial<T> {
161+
const result: Partial<T> = {};
162+
for (const key in obj) {
163+
if (obj.hasOwnProperty(key)) {
164+
if (obj[key] !== undefined && obj[key] !== null) {
165+
result[key] = obj[key];
166+
}
167+
}
168+
}
169+
return result;
170+
}

libraries/rush-lib/src/utilities/test/objectUtilities.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import { objectsAreDeepEqual, cloneDeep, merge } from '../objectUtilities';
4+
import { objectsAreDeepEqual, cloneDeep, merge, removeNullishProps } from '../objectUtilities';
55

66
describe('objectUtilities', () => {
77
describe(objectsAreDeepEqual.name, () => {
@@ -147,4 +147,12 @@ describe('objectUtilities', () => {
147147
expect(merge({ a: { b: 1 } }, { a: { c: 2 } })).toEqual({ a: { b: 1, c: 2 } });
148148
});
149149
});
150+
151+
describe(removeNullishProps.name, () => {
152+
it('can remove undefined and null properties', () => {
153+
expect(removeNullishProps({ a: 1, b: undefined })).toEqual({ a: 1 });
154+
expect(removeNullishProps({ a: 1, b: null })).toEqual({ a: 1 });
155+
expect(removeNullishProps({ a: 1, b: undefined, c: null })).toEqual({ a: 1 });
156+
});
157+
});
150158
});

0 commit comments

Comments
 (0)