Files
renovate/lib/modules/versioning/semver/common.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

58 lines
1.8 KiB
TypeScript

import { regEx } from '../../../util/regex.ts';
import type { SemVerXRange } from './types.ts';
const SEMVER_X_RANGE: readonly SemVerXRange[] = ['*', 'x', 'X', ''];
const RANGE_SEPARATOR = regEx(/(\s+|,|\|\||[()])/);
const NUMERIC_RELEASE_PART = '(?:0|[1-9]\\d*)';
const X_RELEASE_PART = '[xX*]';
const RELEASE_PART = `(?:${NUMERIC_RELEASE_PART}|${X_RELEASE_PART})`;
const PRERELEASE_IDENTIFIER = `(?:${NUMERIC_RELEASE_PART}|\\d*[A-Za-z-][0-9A-Za-z-]*)`;
const BUILD_IDENTIFIER = '[0-9A-Za-z-]+';
const LEGACY_X_RANGE = regEx(
`^((?:\\^|~|>=|<=|>|<|=)?\\s*v?)(${RELEASE_PART}(?:\\.${RELEASE_PART})+)(?:-${PRERELEASE_IDENTIFIER}(?:\\.${PRERELEASE_IDENTIFIER})*)?(?:\\+${BUILD_IDENTIFIER}(?:[.+]${BUILD_IDENTIFIER})*)?$`,
);
const NUMERIC_RELEASE_PART_RE = regEx(`^${NUMERIC_RELEASE_PART}$`);
/**
* https://docs.npmjs.com/cli/v6/using-npm/semver#x-ranges-12x-1x-12-
*/
export function isSemVerXRange(range: string): range is SemVerXRange {
return SEMVER_X_RANGE.includes(range as SemVerXRange);
}
function isSemVerNumericPart(part: string): boolean {
return NUMERIC_RELEASE_PART_RE.test(part);
}
function normalizeLegacyXRangeToken(input: string): string {
const match = LEGACY_X_RANGE.exec(input);
if (!match) {
return input;
}
const [, operator, release] = match;
const parts = release.split('.');
if (parts.length > 3) {
return input;
}
const xRangeIndex = parts.findIndex(isSemVerXRange);
if (xRangeIndex === -1) {
return input;
}
const hasTrailingNumericPart = parts
.slice(xRangeIndex + 1)
.some(isSemVerNumericPart);
if (!hasTrailingNumericPart) {
return input;
}
return `${operator}${parts.slice(0, xRangeIndex + 1).join('.')}`;
}
export function normalizeLegacyXRanges(input: string): string {
return input.split(RANGE_SEPARATOR).map(normalizeLegacyXRangeToken).join('');
}