Skip to content

Commit

Permalink
feat(checkResolutionsVersionsMatch): add support for resolutions with…
Browse files Browse the repository at this point in the history
… patch
  • Loading branch information
christophehurpeau committed Sep 15, 2023
1 parent 5f9dc6f commit 42520ad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
30 changes: 25 additions & 5 deletions src/checks/checkResolutionsVersionsMatch.test.ts
Expand Up @@ -31,8 +31,13 @@ describe('checkResolutionsVersionsMatch', () => {
checkResolutionsVersionsMatch(
{
name: 'test',
resolutions: { test1: '1.0.0', test2: '1.0.0', test3: '1.0.1' },
devDependencies: { test1: '1.0.0' },
resolutions: {
test1: '1.0.0',
test2: '1.0.0',
test3: '1.0.1',
'test4@npm:1.1.0': 'patch:1.2.0',
},
devDependencies: { test1: '1.0.0', test4: '1.1.0' },
dependencies: { test2: '1.0.0', test3: '^1.0.0' },
},
'path',
Expand All @@ -45,14 +50,19 @@ describe('checkResolutionsVersionsMatch', () => {
checkResolutionsVersionsMatch(
{
name: 'test',
resolutions: { test1: '1.0.0', test2: '1.0.0' },
resolutions: {
test1: '1.0.0',
test2: '1.0.0',
'test3@npm:1.1.0': 'patch:1.2.0',
'test4@npm:1.1.0': 'patch:1.2.0',
},
devDependencies: { test1: '1.1.0' },
dependencies: { test2: '1.2.0' },
dependencies: { test2: '1.2.0', test3: '1.0.0', test4: '1.2.0' },
},
'path',
{ customCreateReportError: createReportError },
);
expect(mockReportError).toHaveBeenCalledTimes(2);
expect(mockReportError).toHaveBeenCalledTimes(4);
expect(mockReportError).toHaveBeenNthCalledWith(
1,
'Invalid "test1" in devDependencies',
Expand All @@ -63,6 +73,16 @@ describe('checkResolutionsVersionsMatch', () => {
'Invalid "test2" in dependencies',
'expecting "1.2.0" be "1.0.0" from resolutions.',
);
expect(mockReportError).toHaveBeenNthCalledWith(
3,
'Invalid "test3" in dependencies',
'expecting "1.0.0" be "1.1.0" from resolutions.',
);
expect(mockReportError).toHaveBeenNthCalledWith(
4,
'Invalid "test4" in dependencies',
'expecting "1.2.0" be "1.1.0" from resolutions.',
);
});

it('should fix without error when "resolutions" has dependency not matching', () => {
Expand Down
10 changes: 9 additions & 1 deletion src/checks/checkResolutionsVersionsMatch.ts
Expand Up @@ -21,7 +21,15 @@ export function checkResolutionsVersionsMatch(
pkgPathName,
);

Object.entries(pkgResolutions).forEach(([depName, resolutionDepVersion]) => {
Object.entries(pkgResolutions).forEach(([resolutionKey, resolutionValue]) => {
let depName = resolutionKey;
let resolutionDepVersion = resolutionValue;
if (resolutionValue.startsWith('patch:')) {
const matchResolutionInKey = /^(.+)@npm:(.+)$/.exec(resolutionKey);
if (matchResolutionInKey) {
[, depName, resolutionDepVersion] = matchResolutionInKey;
}
}
(['dependencies', 'devDependencies'] as const).forEach((depType) => {
const range = pkg?.[depType]?.[depName];

Expand Down

0 comments on commit 42520ad

Please sign in to comment.