Files
renovate/lib/modules/versioning/ruby/range.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

131 lines
3.6 KiB
TypeScript

import { satisfies } from '@renovatebot/ruby-semver';
import { parse as _parse } from '@renovatebot/ruby-semver/dist/ruby/requirement.js';
import type { Version } from '@renovatebot/ruby-semver/dist/ruby/version.js';
import { create } from '@renovatebot/ruby-semver/dist/ruby/version.js';
import { logger } from '../../../logger/index.ts';
import { regEx } from '../../../util/regex.ts';
import { EQUAL, GT, GTE, LT, LTE, NOT_EQUAL, PGTE } from './operator.ts';
export interface Range {
version: string;
operator: string;
delimiter: string;
/**
* If the range is `~>` and immediately followed by `>=`,
* the latter range is considered the former's companion
* and assigned here instead of being an independent range.
*
* Example: `'~> 6.2', '>= 6.2.1'`
*/
companion?: Range;
}
function parse(range: string): Range {
const regExp = regEx(
/^(?<operator>[^\d\s]+)?(?<delimiter>\s*)(?<version>[0-9a-zA-Z-.]+)$/,
);
const value = (range || '').trim();
const match = regExp.exec(value);
if (match?.groups) {
const { version, operator = '', delimiter } = match.groups;
return { version, operator, delimiter };
}
return {
version: '',
operator: '',
delimiter: ' ',
};
}
/** Wrapper for {@link satisfies} for {@link Range} record. */
export function satisfiesRange(ver: string, range: Range): boolean {
if (range.companion) {
return (
satisfies(ver, `${range.operator}${range.version}`) &&
satisfiesRange(ver, range.companion)
);
}
return satisfies(ver, `${range.operator}${range.version}`);
}
/**
* Parses a comma-delimited list of range parts,
* with special treatment for a pair of `~>` and `>=` parts.
*/
export function parseRanges(range: string): Range[] {
const originalRanges = range.split(',').map(parse);
const ranges: Range[] = [];
for (let i = 0; i < originalRanges.length; ) {
if (
i + 1 < originalRanges.length &&
originalRanges[i].operator === PGTE &&
originalRanges[i + 1].operator === GTE
) {
ranges.push({
...originalRanges[i],
companion: originalRanges[i + 1],
});
i += 2;
} else {
ranges.push(originalRanges[i]);
i++;
}
}
return ranges;
}
/**
* Stringifies a list of range parts into a comma-separated string,
* with special treatment for a pair of `~>` and `>=` parts.
*/
export function stringifyRanges(ranges: Range[]): string {
return ranges
.map((r) => {
if (r.companion) {
return `${r.operator}${r.delimiter}${r.version}, ${r.companion.operator}${r.companion.delimiter}${r.companion.version}`;
}
return `${r.operator}${r.delimiter}${r.version}`;
})
.join(', ');
}
type GemRequirement = [string, Version];
function ltr(version: string, range: string): boolean {
const gemVersion = create(version);
if (!gemVersion) {
logger.warn({ version }, `Invalid ruby version`);
return false;
}
const requirements: GemRequirement[] = range.split(',').map(_parse);
const results = requirements.map(([operator, ver]) => {
switch (operator) {
case GT:
case LT:
return gemVersion.compare(ver) <= 0;
case GTE:
case LTE:
case EQUAL:
case NOT_EQUAL:
return gemVersion.compare(ver) < 0;
case PGTE:
return (
gemVersion.compare(ver) < 0 &&
gemVersion.release().compare(ver.bump()) <= 0
);
// istanbul ignore next
default:
logger.warn({ operator }, `Unsupported operator`);
return false;
}
});
return results.reduce((accumulator, value) => accumulator && value, true);
}
export { ltr, parse };