Skip to content

Commit

Permalink
feat: autofix checkMinRangeSatisfies
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehurpeau committed Dec 30, 2022
1 parent 934247f commit 63b20f7
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 20 deletions.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/definitions/check-package.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion dist/definitions/checks/checkMinRangeSatisfies.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createReportError } from '../utils/createReportError';
import type { DependencyTypes } from '../utils/packageTypes';
export interface CheckMinRangeSatisfiesOptions {
customCreateReportError?: typeof createReportError;
tryToAutoFix?: boolean;
}
export declare function checkMinRangeSatisfies(pkgPathName: string, pkg: PackageJson, type1?: DependencyTypes, type2?: DependencyTypes, { customCreateReportError, }?: CheckMinRangeSatisfiesOptions): void;
export declare function checkMinRangeSatisfies(pkgPathName: string, pkg: PackageJson, type1?: DependencyTypes, type2?: DependencyTypes, { tryToAutoFix, customCreateReportError, }?: CheckMinRangeSatisfiesOptions): void;
//# sourceMappingURL=checkMinRangeSatisfies.d.ts.map
2 changes: 1 addition & 1 deletion dist/definitions/checks/checkMinRangeSatisfies.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions dist/index-node16.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import util from 'util';
import chalk from 'chalk';
import semver from 'semver';
import semverUtils from 'semver-utils';
import fs, { readFileSync, writeFileSync } from 'fs';
import { resolve } from 'import-meta-resolve';
import glob from 'glob';
Expand Down Expand Up @@ -273,6 +274,7 @@ function checkIdenticalVersionsThanDependency(pkg, pkgPathName, type, depKeys, d
}

function checkMinRangeSatisfies(pkgPathName, pkg, type1 = 'dependencies', type2 = 'devDependencies', {
tryToAutoFix = false,
customCreateReportError = createReportError
} = {}) {
const dependencies1 = pkg[type1];
Expand All @@ -288,7 +290,12 @@ function checkMinRangeSatisfies(pkgPathName, pkg, type1 = 'dependencies', type2
if (!semver.satisfies(minDepRange1, depRange2, {
includePrerelease: true
})) {
reportError(`Invalid "${depName}" in ${type1}`, `"${depRange1}" should satisfies "${depRange2}" from "${type2}".`);
if (tryToAutoFix) {
const depRange1Parsed = semverUtils.parseRange(depRange1);
dependencies1[depName] = (depRange1Parsed[0]?.operator || '') + (semver.minVersion(depRange2)?.version || depRange2);
} else {
reportError(`Invalid "${depName}" in ${type1}`, `"${depRange1}" should satisfies "${depRange2}" from "${type2}".`);
}
}
}
}
Expand Down Expand Up @@ -829,7 +836,9 @@ function createCheckPackage({
},
checkMinRangeDependenciesSatisfiesDevDependencies() {
jobs.push(new Job(this.checkSatisfiesVersionsInDependency.name, async () => {
checkMinRangeSatisfies(pkgPathName, pkg, 'dependencies', 'devDependencies');
checkMinRangeSatisfies(pkgPathName, pkg, 'dependencies', 'devDependencies', {
tryToAutoFix
});
}));
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/index-node16.mjs.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"glob": "^8.0.0",
"import-meta-resolve": "^2.1.0",
"semver": "^7.3.8",
"semver-utils": "^1.1.4",
"type-fest": "^3.0.0"
},
"devDependencies": {
Expand All @@ -103,6 +104,7 @@
"@types/glob": "8.0.0",
"@types/jest": "29.2.4",
"@types/semver": "7.3.13",
"@types/semver-utils": "1.1.1",
"eslint": "8.30.0",
"jest": "29.3.1",
"pob-babel": "35.4.0",
Expand Down
1 change: 1 addition & 0 deletions src/check-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ export function createCheckPackage({
pkg,
'dependencies',
'devDependencies',
{ tryToAutoFix },
);
}),
);
Expand Down
60 changes: 50 additions & 10 deletions src/checks/checkMinRangeSatisfies.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PackageJson } from '../utils/packageTypes';
import { checkMinRangeSatisfies } from './checkMinRangeSatisfies';

const jest = import.meta.jest;
Expand Down Expand Up @@ -73,6 +74,9 @@ describe(checkMinRangeSatisfies.name, () => {
},
'Invalid "test1" in dependencies',
'"1.1.0" should satisfies "1.0.0" from "devDependencies".',
{
dependencies: { test1: '1.0.0' },
},
],
[
'exact dev dependency is higher than caret range dependency',
Expand All @@ -82,6 +86,9 @@ describe(checkMinRangeSatisfies.name, () => {
},
'Invalid "test1" in dependencies',
'"^1.0.0" should satisfies "1.1.0" from "devDependencies".',
{
dependencies: { test1: '^1.1.0' },
},
],
[
'exact dev dependency is lower than caret range dependency',
Expand All @@ -91,6 +98,9 @@ describe(checkMinRangeSatisfies.name, () => {
},
'Invalid "test1" in dependencies',
'"^1.1.0" should satisfies "1.0.0" from "devDependencies".',
{
dependencies: { test1: '^1.0.0' },
},
],
[
'exact dev dependency is higher than tilde range dependency',
Expand All @@ -100,6 +110,9 @@ describe(checkMinRangeSatisfies.name, () => {
},
'Invalid "test1" in dependencies',
'"~1.0.0" should satisfies "1.1.0" from "devDependencies".',
{
dependencies: { test1: '~1.1.0' },
},
],
[
'exact dev dependency is lower than tilde range dependency',
Expand All @@ -109,6 +122,9 @@ describe(checkMinRangeSatisfies.name, () => {
},
'Invalid "test1" in dependencies',
'"~1.1.0" should satisfies "1.0.0" from "devDependencies".',
{
dependencies: { test1: '~1.0.0' },
},
],
[
'exact dev dependency is higher than >= range dependency',
Expand All @@ -118,6 +134,9 @@ describe(checkMinRangeSatisfies.name, () => {
},
'Invalid "test1" in dependencies',
'">=1.0.0" should satisfies "1.1.0" from "devDependencies".',
{
dependencies: { test1: '>=1.1.0' },
},
],
[
'exact dev dependency is lower than >= range dependency',
Expand All @@ -127,16 +146,37 @@ describe(checkMinRangeSatisfies.name, () => {
},
'Invalid "test1" in dependencies',
'">=1.1.0" should satisfies "1.0.0" from "devDependencies".',
{
dependencies: { test1: '>=1.0.0' },
},
],
])('should error when %s', (_, pkgContent, errorTitle, errorInfo) => {
checkMinRangeSatisfies(
'path',
{ name: 'test', ...pkgContent },
'dependencies',
'devDependencies',
{ customCreateReportError: createReportError },
);
expect(mockReportError).toHaveBeenCalledWith(errorTitle, errorInfo);
});
])(
'should error when %s',
(_, pkgContent, errorTitle, errorInfo, expectedFix) => {
checkMinRangeSatisfies(
'path',
{ name: 'test', ...pkgContent },
'dependencies',
'devDependencies',
{ customCreateReportError: createReportError },
);
expect(mockReportError).toHaveBeenCalledWith(errorTitle, errorInfo);

if (expectedFix) {
const pkg = JSON.parse(
JSON.stringify({ name: 'test', ...pkgContent }),
) as PackageJson;
checkMinRangeSatisfies(
'path',
pkg,
'dependencies',
'devDependencies',
{ tryToAutoFix: true },
);

expect(pkg).toStrictEqual({ ...pkg, ...expectedFix });
}
},
);
});
});
18 changes: 14 additions & 4 deletions src/checks/checkMinRangeSatisfies.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import semver from 'semver';
import semverUtils from 'semver-utils';
import type { PackageJson } from 'type-fest';
import { createReportError } from '../utils/createReportError';
import { getEntries } from '../utils/object';
import type { DependencyTypes } from '../utils/packageTypes';

export interface CheckMinRangeSatisfiesOptions {
customCreateReportError?: typeof createReportError;
tryToAutoFix?: boolean;
}

export function checkMinRangeSatisfies(
Expand All @@ -14,6 +16,7 @@ export function checkMinRangeSatisfies(
type1: DependencyTypes = 'dependencies',
type2: DependencyTypes = 'devDependencies',
{
tryToAutoFix = false,
customCreateReportError = createReportError,
}: CheckMinRangeSatisfiesOptions = {},
): void {
Expand All @@ -40,10 +43,17 @@ export function checkMinRangeSatisfies(
includePrerelease: true,
})
) {
reportError(
`Invalid "${depName}" in ${type1}`,
`"${depRange1}" should satisfies "${depRange2}" from "${type2}".`,
);
if (tryToAutoFix) {
const depRange1Parsed = semverUtils.parseRange(depRange1);
dependencies1[depName] =
(depRange1Parsed[0]?.operator || '') +
(semver.minVersion(depRange2)?.version || depRange2);
} else {
reportError(
`Invalid "${depName}" in ${type1}`,
`"${depRange1}" should satisfies "${depRange2}" from "${type2}".`,
);
}
}
}
}
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,13 @@ __metadata:
languageName: node
linkType: hard

"@types/semver-utils@npm:1.1.1":
version: 1.1.1
resolution: "@types/semver-utils@npm:1.1.1"
checksum: fd53e2ef9712e20c69caacc608b42050e815573d9755afe344299b8131a14978e35a3b56a13a9bdb58d9a0025e940672d27ddd8d77c67004535af0e991baeb12
languageName: node
linkType: hard

"@types/semver@npm:7.3.13":
version: 7.3.13
resolution: "@types/semver@npm:7.3.13"
Expand Down Expand Up @@ -2530,13 +2537,15 @@ __metadata:
"@types/glob": 8.0.0
"@types/jest": 29.2.4
"@types/semver": 7.3.13
"@types/semver-utils": 1.1.1
chalk: ^5.2.0
eslint: 8.30.0
glob: ^8.0.0
import-meta-resolve: ^2.1.0
jest: 29.3.1
pob-babel: 35.4.0
semver: 7.3.8
semver-utils: ^1.1.4
type-fest: ^3.0.0
typedoc: 0.23.23
typescript: 4.9.4
Expand Down Expand Up @@ -6274,6 +6283,13 @@ __metadata:
languageName: node
linkType: hard

"semver-utils@npm:^1.1.4":
version: 1.1.4
resolution: "semver-utils@npm:1.1.4"
checksum: 93fd955a30f5bdf532163d94981aa03dfbaddf29dad6388415b264c95d7046a6b47d947c6e3e37c0d7867ed3f024aa6e50fc308c9487378354e9d300c9dd68b6
languageName: node
linkType: hard

"semver@npm:2 || 3 || 4 || 5":
version: 5.7.1
resolution: "semver@npm:5.7.1"
Expand Down

0 comments on commit 63b20f7

Please sign in to comment.