Files
renovate/lib/modules/versioning/ruby/range.ts
T
Sebastian Poxhofer e0cec3dc95 refactor(lint): add types-location rule and enable it for lib/modules (#45497)
feat(lint): add `types-location` rule and enable it for `lib/modules`

Reviewers consistently ask for exported types to be moved into a
colocated `types.ts`. Add a `renovate/types-location` oxlint rule that
reports `export interface` / `export type` declarations outside such a
file, and turn it on across `lib/modules`.

Re-exports (`export type { X }`, `export type *`), file-local types, and
values (enums, classes, functions) are left alone. `schema.ts` files and
`schema/` directories are excluded via config, matching how
`zod-schema-location` is configured — inferred types belong next to the
schema they are derived from.

Relocating the 87 existing violations means:

- new `types.ts` files for the modules that had none
- `terragrunt/common.ts` folded into its `types.ts`, as it held nothing
  else
- `RangeBound` in `versioning/gradle/compare.ts` un-exported, since only
  a file-local interface referenced it
- `bazel-module/parser/fragments.ts` split from its inferred types, which
  now live in `parser/types.ts` and infer through a namespace import so
  each schema keeps its merged name
2026-08-27 19:52:09 +00:00

118 lines
3.3 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';
import type { Range } from './types.ts';
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 };