Skip to content

Commit

Permalink
feat: better support exact versions
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehurpeau committed Mar 6, 2024
1 parent b8372b0 commit e9e6aa2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
26 changes: 24 additions & 2 deletions src/checks/checkExactVersions.test.ts
Expand Up @@ -115,7 +115,15 @@ describe('checkExactVersions', () => {

it('should return multiple errors when multiple versions have range', async () => {
await checkExactVersions(
{ name: 'test', devDependencies: { test1: '~1.0.0', test2: '~1.0.0' } },
{
name: 'test',
devDependencies: {
test1: '~1.0.0',
test2: '~1.0.0',
test3: '^18',
test4: '^18.1',
},
},
'path',
['devDependencies'],
{
Expand All @@ -124,7 +132,7 @@ describe('checkExactVersions', () => {
},
);
expect(createReportError).toHaveBeenCalled();
expect(mockReportError).toHaveBeenCalledTimes(2);
expect(mockReportError).toHaveBeenCalledTimes(4);
expect(mockReportError).toHaveBeenNthCalledWith(
1,
'Unexpected range dependency in "devDependencies" for "test1"',
Expand All @@ -139,6 +147,20 @@ describe('checkExactVersions', () => {
false,
false,
);
expect(mockReportError).toHaveBeenNthCalledWith(
3,
'Unexpected range dependency in "devDependencies" for "test3"',
'expecting "^18" to be exact "18.0.0".',
false,
false,
);
expect(mockReportError).toHaveBeenNthCalledWith(
4,
'Unexpected range dependency in "devDependencies" for "test4"',
'expecting "^18.1" to be exact "18.1.0".',
false,
false,
);
});

it('should fix and remove range', async () => {
Expand Down
12 changes: 9 additions & 3 deletions src/checks/checkExactVersions.ts
Expand Up @@ -99,11 +99,17 @@ export async function checkExactVersions(
);
}
} else {
let exactVersion = version.slice(version[1] === '=' ? 2 : 1);
if (exactVersion.split('.').length < 3) {
if (exactVersion.split('.').length === 1) {
exactVersion = `${exactVersion}.0.0`;
} else {
exactVersion = `${exactVersion}.0`;
}
}
reportError(
`Unexpected range dependency in "${type}" for "${dependencyName}"`,
`expecting "${version}" to be exact "${version.slice(
version[1] === '=' ? 2 : 1,
)}".`,
`expecting "${version}" to be exact "${exactVersion}".`,
shouldOnlyWarn,
false,
);
Expand Down

0 comments on commit e9e6aa2

Please sign in to comment.