Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(npm): Avoid usage of non-null assertion for constraints #22405

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 22 additions & 9 deletions lib/modules/manager/npm/extract/locked-versions.ts
Expand Up @@ -24,17 +24,22 @@ export async function getLockedVersions(
lockFileCache[yarnLock] = await getYarnLock(yarnLock);
}
const { lockfileVersion, isYarn1 } = lockFileCache[yarnLock];
let yarn: string | undefined;
if (!isYarn1 && !packageFile.extractedConstraints?.yarn) {
if (lockfileVersion && lockfileVersion >= 8) {
// https://github.com/yarnpkg/berry/commit/9bcd27ae34aee77a567dd104947407532fa179b3
packageFile.extractedConstraints!.yarn = '^3.0.0';
yarn = '^3.0.0';
} else if (lockfileVersion && lockfileVersion >= 6) {
// https://github.com/yarnpkg/berry/commit/f753790380cbda5b55d028ea84b199445129f9ba
packageFile.extractedConstraints!.yarn = '^2.2.0';
yarn = '^2.2.0';
} else {
packageFile.extractedConstraints!.yarn = '^2.0.0';
yarn = '^2.0.0';
}
}
if (yarn) {
packageFile.extractedConstraints ??= {};
packageFile.extractedConstraints.yarn = yarn;
}
for (const dep of packageFile.deps) {
dep.lockedVersion =
lockFileCache[yarnLock].lockedVersions[
Expand All @@ -57,30 +62,37 @@ export async function getLockedVersions(
logger.trace('Retrieving/parsing ' + npmLock);
lockFileCache[npmLock] = await getNpmLock(npmLock);
}

const { lockfileVersion } = lockFileCache[npmLock];
let npm: string | undefined;
if (lockfileVersion === 1) {
if (packageFile.extractedConstraints?.npm) {
// Add a <7 constraint if it's not already a fixed version
if (
semver.satisfies('6.14.18', packageFile.extractedConstraints.npm)
) {
packageFile.extractedConstraints.npm += ' <7';
npm = packageFile.extractedConstraints.npm + ' <7';
}
} else {
packageFile.extractedConstraints!.npm = '<7';
npm = '<7';
}
} else if (lockfileVersion === 2) {
if (packageFile.extractedConstraints?.npm) {
// Add a <9 constraint if the latest 8.x is compatible
if (
semver.satisfies('8.19.3', packageFile.extractedConstraints.npm)
) {
packageFile.extractedConstraints.npm += ' <9';
npm = packageFile.extractedConstraints.npm + ' <9';
}
} else {
packageFile.extractedConstraints!.npm = '<9';
npm = '<9';
}
}
if (npm) {
packageFile.extractedConstraints ??= {};
packageFile.extractedConstraints.npm = npm;
}

for (const dep of packageFile.deps) {
// TODO: types (#7154)
dep.lockedVersion = semver.valid(
Expand All @@ -96,9 +108,10 @@ export async function getLockedVersions(
}
const { lockfileVersion } = lockFileCache[pnpmShrinkwrap];
if (lockfileVersion) {
packageFile.extractedConstraints!.pnpm = getConstraints(
packageFile.extractedConstraints ??= {};
packageFile.extractedConstraints.pnpm = getConstraints(
lockfileVersion,
packageFile.extractedConstraints!.pnpm
packageFile.extractedConstraints.pnpm
);
}

Expand Down