Files
renovate/lib/modules/datasource/ruby-version/index.ts
T
Sebastian Poxhofer 97327c3d28 refactor(linters): add prefer-coerce-array rule (#45492)
* refactor(linters): add prefer-coerce-array rule

Refs #45217

`x ?? []` and `coerceArray(x)` say the same thing, but only the second
one is searchable and consistent. The rule flags a `??` whose default is
a bare `[]` literal; a non-empty default or an asserted `([] as Foo[])`
still passes.

Applying it across `lib/` surfaced three spots the helper cannot cover,
which keep `?? []` with a disable comment or a narrower fix:

- `package-files.ts` destructures a tuple, which `coerceArray()` widens
- `generate.spec.ts` defaults `matchAll()`, an iterator rather than an array
- `mise/upgradeable-tooling.ts` declares a `Record` return, so its
  `?? []` fallback was wrong to begin with and becomes `coerceObject()`

* refactor(platform/github): hoist the GHE version header lookup

`Object.entries(...).find()` returns a tuple, and defaulting it with
`?? []` only to destructure the second element reads as an array
default when it is not one. Name the found entry instead, which drops
the `?? []` rather than routing a tuple through `coerceArray()`.
2026-08-28 09:27:58 +00:00

93 lines
3.0 KiB
TypeScript

import { logger } from '../../../logger/index.ts';
import { ExternalHostError } from '../../../types/errors/external-host-error.ts';
import { coerceArray } from '../../../util/array.ts';
import { withCache } from '../../../util/cache/package/with-cache.ts';
import { parse } from '../../../util/html.ts';
import type { HttpError } from '../../../util/http/index.ts';
import { asTimestamp } from '../../../util/timestamp.ts';
import {
isVersion,
id as rubyVersioningId,
} from '../../versioning/ruby/index.ts';
import { Datasource } from '../datasource.ts';
import type { GetReleasesConfig, ReleaseResult } from '../types.ts';
export class RubyVersionDatasource extends Datasource {
static readonly id = 'ruby-version';
constructor() {
super(RubyVersionDatasource.id);
}
override readonly defaultRegistryUrls = ['https://www.ruby-lang.org/'];
override readonly customRegistrySupport = false;
override readonly defaultVersioning = rubyVersioningId;
override readonly releaseTimestampSupport = true;
override readonly releaseTimestampNote =
'The release timestamp is determined from the `release-list` table in the results.';
override readonly sourceUrlSupport = 'package';
override readonly sourceUrlNote =
'We use the URL: https://github.com/ruby/ruby.';
private async _getReleases({
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const res: ReleaseResult = {
homepage: 'https://www.ruby-lang.org',
sourceUrl: 'https://github.com/ruby/ruby',
releases: [],
};
// TODO: types (#22198)
const rubyVersionsUrl = `${registryUrl}en/downloads/releases/`;
try {
const response = await this.http.getText(rubyVersionsUrl);
const root = parse(response.body);
const rows = coerceArray(
root.querySelector('.release-list')?.querySelectorAll('tr'),
);
rows.forEach((row) => {
const tds = row.querySelectorAll('td');
const columns: string[] = [];
tds.forEach((td) => columns.push(td.innerHTML));
if (columns.length) {
const version = columns[0].replace('Ruby ', '');
if (isVersion(version)) {
const releaseTimestamp = asTimestamp(columns[1]);
const changelogUrl = columns[2]
.replace('<a href="', 'https://www.ruby-lang.org')
.replace('">more...</a>', '');
res.releases.push({ version, releaseTimestamp, changelogUrl });
}
}
});
if (!res.releases.length) {
logger.warn({ registryUrl }, 'Missing ruby releases');
return null;
}
} catch (err) {
this.handleGenericErrors(err);
}
return res;
}
getReleases(config: GetReleasesConfig): Promise<ReleaseResult | null> {
return withCache(
{
namespace: `datasource-${RubyVersionDatasource.id}`,
key: 'all',
fallback: true,
},
() => this._getReleases(config),
);
}
override handleHttpErrors(err: HttpError): never | void {
throw new ExternalHostError(err);
}
}