Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Fix an issue where the lockfile object has a nullish value causing yaml.dump to report an error.",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
ProjectSnapshot,
ResolvedDependencies
} from '@pnpm/lockfile.types';
import { removeNullishProps } from '../../utilities/objectUtilities';

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

return {
...from,
specifiers,
dependencies,
devDependencies,
optionalDependencies
...removeNullishProps({
...from,
dependencies,
devDependencies,
optionalDependencies
}),
specifiers
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ describe(convertLockfileV9ToLockfileObject.name, () => {
'pad-left': '^2.1.0'
});
});

it("no nullish values", () => {
const importers = new Map<string, ProjectSnapshot>(Object.entries(lockfile.importers || {}));

const currentPackage = importers.get('.');
const props = Object.keys(currentPackage || {});
expect(props).toContain('dependencies');
expect(props).toContain('specifiers');
expect(props).not.toContain('optionalDependencies');
expect(props).not.toContain('devDependencies');
});
});
18 changes: 18 additions & 0 deletions libraries/rush-lib/src/utilities/objectUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,21 @@ function isStrictComparable<T>(value: T): boolean {
value === value && !(value !== null && value !== undefined && (type === 'object' || type === 'function'))
);
}

/**
* Removes `undefined` and `null` direct properties from an object.
*
* @remarks
* Note that this does not recurse through sub-objects.
*/
export function removeNullishProps<T extends object>(obj: T): Partial<T> {
const result: Partial<T> = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (obj[key] !== undefined && obj[key] !== null) {
result[key] = obj[key];
}
}
}
return result;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import { objectsAreDeepEqual, cloneDeep, merge } from '../objectUtilities';
import { objectsAreDeepEqual, cloneDeep, merge, removeNullishProps } from '../objectUtilities';

describe('objectUtilities', () => {
describe(objectsAreDeepEqual.name, () => {
Expand Down Expand Up @@ -147,4 +147,12 @@ describe('objectUtilities', () => {
expect(merge({ a: { b: 1 } }, { a: { c: 2 } })).toEqual({ a: { b: 1, c: 2 } });
});
});

describe(removeNullishProps.name, () => {
it('can remove undefined and null properties', () => {
expect(removeNullishProps({ a: 1, b: undefined })).toEqual({ a: 1 });
expect(removeNullishProps({ a: 1, b: null })).toEqual({ a: 1 });
expect(removeNullishProps({ a: 1, b: undefined, c: null })).toEqual({ a: 1 });
});
});
});
Loading