Files
renovate/lib/modules/versioning/ruby/version.ts
T
Sebastian Poxhofer 58771bb183 refactor(linters): enabled oxlints func-style (#44691)
* refactor(oxlint): enable func-style

Enable the built-in func-style rule as ["warn", "declaration"] so that
named functions are written as function declarations instead of arrow or
function expressions assigned to variables, matching the convention in
docs/development/best-practices.md (Functions section). Inline arrow
callbacks are unaffected; prefer-arrow-callback continues to cover the
callback direction.

Severity is "warn" instead of "error" as a fallback: a repo-wide run
found 196 pre-existing violations (70 in spec files, 126 in production
code), exceeding the 80-violation fix threshold for this change, so no
violations were fixed here. Follow-up work can convert the offenders
(largest clusters: lib/modules/versioning/ruby, lib/util/cache/package,
lib/modules/versioning/maven, lib/modules/versioning/gradle) and then
raise the rule to "error". Note oxlint 1.72 also supports an
allowTypeAnnotation option if typed consts should remain arrows.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* ci(linter): enforce func-style at error

* refactor(func-style): convert const-arrow functions to declarations in with-cache spec and ruby versioning

* refactor(func-style): convert const-arrow functions to declarations in maven/gradle modules

* refactor(func-style): convert const-arrow functions to declarations in remaining versioning modules

* refactor(func-style): convert const-arrow functions to declarations in platform modules

Discriminated-union closures (ensureCommentRemoval byTopic/byContent
helpers in github, gitlab, bitbucket, bitbucket-server) needed the
narrowed field passed explicitly as a parameter, since function
declarations are hoisted and lose the outer if-narrowing that inline
arrow-function closures previously relied on.

* refactor(func-style): convert const-arrow functions to declarations across util/manager/datasource modules

One case (logger/renovate-logger.ts logOnceFn) is kept as an arrow
function with a documented oxlint-disable: it is stored on
this.logger.once[level] and relies on lexically-bound this, which a
hoisted function declaration would not preserve.

* refactor(func-style): convert remaining const-arrow test helpers to declarations

get-updated.spec.ts: hoisted expectPipCompilePackageAndLockFile to
module scope (it only closes over the module-level pipCompile mock)
and added a documented oxlint-disable for vitest/expect-expect on its
caller test, since oxlint's expect-expect check does not trace
assertions through an externally-defined helper function.

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-19 11:14:22 +00:00

161 lines
4.1 KiB
TypeScript

import { eq, major, minor, patch, prerelease } from '@renovatebot/ruby-semver';
import type { SegmentElement } from '@renovatebot/ruby-semver/dist/ruby/version.js';
import { create } from '@renovatebot/ruby-semver/dist/ruby/version.js';
import { regEx } from '../../../util/regex.ts';
interface RubyVersion {
major: number;
minor: number;
patch: number;
prerelease: string[] | null;
}
function releaseSegments(version: string): SegmentElement[] {
const v = create(version);
if (v) {
return v.release().getSegments();
}
/* istanbul ignore next */
return [];
}
function parse(version: string): RubyVersion {
return {
major: major(version),
minor: minor(version),
patch: patch(version),
prerelease: prerelease(version),
};
}
function floor(version: string): string {
const segments = releaseSegments(version);
if (segments.length <= 1) {
// '~> 2' is equivalent to '~> 2.0', thus no need to floor
return segments.join('.');
}
return [...segments.slice(0, -1), 0].join('.');
}
function adapt(left: string, right: string): string {
return left.split('.').slice(0, right.split('.').length).join('.');
}
function trimZeroes(version: string): string {
const segments = version.split('.');
while (segments.length > 0 && segments.at(-1) === '0') {
segments.pop();
}
return segments.join('.');
}
// Returns the upper bound of `~>` operator.
function pgteUpperBound(version: string): string {
const segments = releaseSegments(version);
if (segments.length > 1) {
segments.pop();
}
return incrementLastSegment(segments.join('.'));
}
// istanbul ignore next
function incrementLastSegment(version: string): string {
const segments = releaseSegments(version);
const nextLast = parseInt(segments.pop() as string, 10) + 1;
return [...segments, nextLast].join('.');
}
// istanbul ignore next
function incrementMajor(
maj: number,
min: number,
ptch: number,
pre: string[],
): number {
return min === 0 || ptch === 0 || pre.length === 0 ? maj + 1 : maj;
}
// istanbul ignore next
function incrementMinor(min: number, ptch: number, pre: string[]): number {
return ptch === 0 || pre.length === 0 ? min + 1 : min;
}
// istanbul ignore next
function incrementPatch(ptch: number, pre: string[]): number {
return pre.length === 0 ? ptch + 1 : ptch;
}
// istanbul ignore next
function increment(from: string, to: string): string {
const parsed = parse(from);
const { major: maj, prerelease: pre } = parsed;
let { minor: min, patch: ptch } = parsed;
min = min || 0;
ptch = ptch || 0;
let nextVersion: string;
const adapted = adapt(to, from);
if (eq(from, adapted)) {
return incrementLastSegment(from);
}
function isStable(x: string): boolean {
return regEx(/^[0-9.-/]+$/).test(x);
}
if (major(from) !== major(adapted)) {
nextVersion = [incrementMajor(maj, min, ptch, pre ?? []), 0, 0].join('.');
} else if (minor(from) !== minor(adapted)) {
nextVersion = [maj, incrementMinor(min, ptch, pre ?? []), 0].join('.');
} else if (patch(from) !== patch(adapted)) {
nextVersion = [maj, min, incrementPatch(ptch, pre ?? [])].join('.');
} else if (isStable(from) && isStable(adapted)) {
nextVersion = [maj, min, incrementPatch(ptch, pre ?? [])].join('.');
} else {
nextVersion = [maj, min, ptch].join('.');
}
return increment(nextVersion, to);
}
// istanbul ignore next
function decrement(version: string): string {
const segments = releaseSegments(version);
const nextSegments = segments
.reverse()
.reduce(
(
accumulator: number[],
segment: SegmentElement,
index: number,
): number[] => {
if (index === 0) {
return [(segment as number) - 1];
}
if (accumulator[index - 1] === -1) {
return [
...accumulator.slice(0, index - 1),
0,
(segment as number) - 1,
];
}
return [...accumulator, segment as number];
},
[],
);
return nextSegments.reverse().join('.');
}
export {
adapt,
decrement,
floor,
increment,
parse,
pgteUpperBound,
trimZeroes,
};