mirror of
https://github.com/renovatebot/renovate.git
synced 2026-10-31 09:41:46 +00:00
e0cec3dc95
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
94 lines
2.6 KiB
TypeScript
94 lines
2.6 KiB
TypeScript
import semver from 'semver';
|
|
import upath from 'upath';
|
|
import { logger } from '../../../../logger/index.ts';
|
|
import type { ToolConstraint } from '../../../../util/exec/types.ts';
|
|
import { readLocalFile } from '../../../../util/fs/index.ts';
|
|
import { newlineRegex, regEx } from '../../../../util/regex.ts';
|
|
import type { PostUpdateConfig, Upgrade } from '../../types.ts';
|
|
import type { LazyPackageJson } from './types.ts';
|
|
|
|
async function getNodeFile(filename: string): Promise<string | null> {
|
|
try {
|
|
// TODO #22198
|
|
const constraint = (await readLocalFile(filename, 'utf8'))!
|
|
.split(newlineRegex)[0]
|
|
.replace(regEx(/^v/), '');
|
|
if (semver.validRange(constraint)) {
|
|
logger.debug(`Using node constraint "${constraint}" from ${filename}`);
|
|
return constraint;
|
|
}
|
|
} catch {
|
|
// do nothing
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async function getPackageJsonConstraint(
|
|
pkg: LazyPackageJson,
|
|
): Promise<string | null> {
|
|
const pkgJson = await pkg.getValue();
|
|
if (pkgJson.volta?.node) {
|
|
const constraint = pkgJson.volta.node;
|
|
// v8 ignore else -- TODO: add test #40625
|
|
if (semver.validRange(constraint)) {
|
|
logger.debug(
|
|
`Using node constraint "${constraint}" from package.json volta`,
|
|
);
|
|
return constraint;
|
|
}
|
|
}
|
|
if (pkgJson.engines?.node) {
|
|
const constraint = pkgJson.engines.node;
|
|
// v8 ignore else -- TODO: add test #40625
|
|
if (semver.validRange(constraint)) {
|
|
logger.debug(
|
|
`Using node constraint "${constraint}" from package.json engines`,
|
|
);
|
|
return constraint;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// export only for testing
|
|
export async function getNodeConstraint(
|
|
config: Partial<PostUpdateConfig>,
|
|
upgrades: Upgrade[],
|
|
lockFileDir: string,
|
|
pkg: LazyPackageJson,
|
|
): Promise<string | null> {
|
|
const constraint =
|
|
getNodeUpdate(upgrades) ??
|
|
config.constraints?.node ??
|
|
(await getNodeFile(upath.join(lockFileDir, '.nvmrc'))) ??
|
|
(await getNodeFile(upath.join(lockFileDir, '.node-version'))) ??
|
|
(await getPackageJsonConstraint(pkg));
|
|
if (!constraint) {
|
|
logger.debug('No node constraint found - using latest');
|
|
}
|
|
return constraint;
|
|
}
|
|
|
|
export function getNodeUpdate(upgrades: Upgrade[]): string | undefined {
|
|
return upgrades.find((u) => u.depName === 'node')?.newValue;
|
|
}
|
|
|
|
export async function getNodeToolConstraint(
|
|
config: Partial<PostUpdateConfig>,
|
|
upgrades: Upgrade[],
|
|
lockFileDir: string,
|
|
pkg: LazyPackageJson,
|
|
): Promise<ToolConstraint> {
|
|
const constraint = await getNodeConstraint(
|
|
config,
|
|
upgrades,
|
|
lockFileDir,
|
|
pkg,
|
|
);
|
|
|
|
return {
|
|
toolName: 'node',
|
|
constraint,
|
|
};
|
|
}
|