Skip to content

Commit 0c27093

Browse files
authored
Show new version in autofix output (#322)
1 parent 5a663c1 commit 0c27093

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

lib/dependency-versions.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,15 @@ export function fixMismatchingVersions(
209209
const versions = mismatchingVersion.versions.map(
210210
(object) => object.version
211211
);
212-
let sortedVersions;
212+
let fixedVersion;
213213
try {
214-
sortedVersions = versions.sort(compareRanges);
214+
fixedVersion = getHighestVersion(versions);
215215
} catch {
216-
// Unable to sort so skip this dependency.
216+
// Skip this dependency.
217217
notFixed.push(mismatchingVersion);
218218
continue;
219219
}
220220

221-
const fixedVersion = sortedVersions[sortedVersions.length - 1]; // Highest version will be sorted to end of list.
222-
223221
let isFixed = false;
224222
for (const package_ of packages) {
225223
if (
@@ -296,3 +294,8 @@ export function compareRanges(a: string, b: string): 0 | -1 | 1 {
296294
// Greater version considered higher.
297295
return semver.gt(aVersion, bVersion) ? 1 : -1;
298296
}
297+
298+
export function getHighestVersion(versions: string[]): string {
299+
const sortedVersions = versions.sort(compareRanges);
300+
return sortedVersions[sortedVersions.length - 1]; // Highest version will be sorted to end of list.
301+
}

lib/output.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import chalk from 'chalk';
22
import type { MismatchingDependencyVersions } from './dependency-versions.js';
3-
import { compareRanges } from './dependency-versions.js';
3+
import { compareRanges, getHighestVersion } from './dependency-versions.js';
44
import { table } from 'table';
55

66
export function mismatchingVersionsToOutput(
@@ -69,10 +69,26 @@ export function mismatchingVersionsFixedToOutput(
6969
throw new Error('No fixes to output.');
7070
}
7171

72-
const dependencies = mismatchingDependencyVersions
73-
.map((mismatchingVersion) => mismatchingVersion.dependency)
74-
.sort();
75-
return `Fixed versions for ${dependencies.length} ${
76-
dependencies.length === 1 ? 'dependency' : 'dependencies'
77-
}: ${dependencies.join(', ')}`;
72+
const dependenciesAndFixedVersions = mismatchingDependencyVersions
73+
.flatMap((mismatchingVersion) => {
74+
let version;
75+
try {
76+
version = getHighestVersion(
77+
mismatchingVersion.versions.map((v) => v.version)
78+
);
79+
} catch {
80+
return []; // Ignore this dependency since unable to get the version that we would have fixed it to.
81+
}
82+
return {
83+
dependency: mismatchingVersion.dependency,
84+
version,
85+
};
86+
})
87+
.sort((a, b) => a.dependency.localeCompare(b.dependency));
88+
89+
return `Fixed versions for ${dependenciesAndFixedVersions.length} ${
90+
dependenciesAndFixedVersions.length === 1 ? 'dependency' : 'dependencies'
91+
}: ${dependenciesAndFixedVersions
92+
.map((object) => `${object.dependency}@${object.version}`)
93+
.join(', ')}`;
7894
}

test/lib/dependency-versions-test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
filterOutIgnoredDependencies,
55
fixMismatchingVersions,
66
compareRanges,
7+
getHighestVersion,
78
} from '../../lib/dependency-versions.js';
89
import { getPackages } from '../../lib/workspace.js';
910
import {
@@ -546,4 +547,18 @@ describe('Utils | dependency-versions', function () {
546547
).toThrowErrorMatchingInlineSnapshot('"Invalid Version: foo"');
547548
});
548549
});
550+
551+
describe('#getHighestVersion', function () {
552+
it('correctly chooses the higher range', function () {
553+
// Just basic sanity checks to ensure the data is passed through to `compareRanges` which has extensive tests.
554+
expect(getHighestVersion(['1.2.3', '1.2.2'])).toStrictEqual('1.2.3');
555+
expect(getHighestVersion(['1.2.2', '1.2.3'])).toStrictEqual('1.2.3');
556+
});
557+
558+
it('throws with invalid version', function () {
559+
expect(() =>
560+
getHighestVersion(['1.2.3', 'foo'])
561+
).toThrowErrorMatchingInlineSnapshot('"Invalid Version: foo"');
562+
});
563+
});
549564
});

test/lib/output-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('Utils | output', function () {
107107
)
108108
)
109109
).toMatchInlineSnapshot(
110-
'"Fixed versions for 4 dependencies: bar, baz, biz, foo"'
110+
'"Fixed versions for 3 dependencies: bar@2.0.0, baz@^2.0.0, foo@4.5.6"'
111111
);
112112
});
113113

@@ -120,7 +120,7 @@ describe('Utils | output', function () {
120120
)
121121
).slice(0, 1)
122122
)
123-
).toMatchInlineSnapshot('"Fixed versions for 1 dependency: bar"');
123+
).toMatchInlineSnapshot('"Fixed versions for 1 dependency: bar@2.0.0"');
124124
});
125125

126126
it('behaves correctly with empty input', function () {

0 commit comments

Comments
 (0)