Files
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

109 lines
3.9 KiB
TypeScript

import { isNullOrUndefined, isString } from '@sindresorhus/is';
import { logger } from '../../../../logger/index.ts';
import * as hostRules from '../../../../util/host-rules.ts';
import { regEx } from '../../../../util/regex.ts';
import { toBase64 } from '../../../../util/string.ts';
import { isHttpUrl } from '../../../../util/url.ts';
import type { HostRulesResult, YarnRcYmlFile } from './types.ts';
export function processHostRules(): HostRulesResult {
const additionalYarnRcYml: YarnRcYmlFile = { npmRegistries: {} };
// Determine the additional npmrc content to add based on host rules
const additionalNpmrcContent = [];
const npmHostRules = hostRules.findAll({
hostType: 'npm',
});
logger.debug(`Found ${npmHostRules.length} npm host rule(s)`);
// Include host rules without specific type to mimic the behavior used when determining dependencies with updates.
const noTypeHostRules = hostRules
.getAll()
.filter((rule) => isNullOrUndefined(rule.hostType));
logger.debug(
`Found ${noTypeHostRules.length} host rule(s) without host type`,
);
// Drop duplicates for the same matchHost while prefering the more specific rules with hostType npm.
const noTypeHostRulesWithoutDuplicates = noTypeHostRules.filter(
(rule) => !npmHostRules.some((elem) => elem.matchHost === rule.matchHost),
);
logger.debug(
`Found ${noTypeHostRulesWithoutDuplicates.length} host rule(s) without host type after dropping duplicates`,
);
const effectiveHostRules = npmHostRules.concat(
noTypeHostRulesWithoutDuplicates,
);
logger.trace(
`Found ${effectiveHostRules.length} effective npm host rule(s) after deduplication`,
);
for (const hostRule of effectiveHostRules) {
if (!hostRule.resolvedHost) {
logger.debug('Skipping host rule without resolved host');
continue;
}
const matchedHost = hostRule.matchHost;
// Should never be necessary as if we have a resolvedHost, there has to be a matchHost
/* v8 ignore if -- unreachable: a resolvedHost implies matchHost is set (see comment above) */
if (!matchedHost) {
logger.debug('Skipping host rule without matchHost');
continue;
}
const uri = `//${matchedHost}/`;
let cleanedUri = uri;
if (isHttpUrl(matchedHost)) {
cleanedUri = matchedHost.replace(regEx(/^https?:/), '');
}
if (hostRule.token) {
const key = hostRule.authType === 'Basic' ? '_auth' : '_authToken';
logger.debug(`Adding npmrc entry for ${cleanedUri} with key ${key}`);
additionalNpmrcContent.push(`${cleanedUri}:${key}=${hostRule.token}`);
if (hostRule.authType === 'Basic') {
const registry = {
npmAuthIdent: hostRule.token,
};
additionalYarnRcYml.npmRegistries[cleanedUri] = registry;
additionalYarnRcYml.npmRegistries[uri] = registry;
continue;
}
const registry = {
npmAuthToken: hostRule.token,
};
additionalYarnRcYml.npmRegistries[cleanedUri] = registry;
additionalYarnRcYml.npmRegistries[uri] = registry;
continue;
}
// v8 ignore else -- TODO: add test #40625
if (isString(hostRule.username) && isString(hostRule.password)) {
logger.debug(
`Adding npmrc entry for ${cleanedUri} with username/password`,
);
const password = toBase64(hostRule.password);
additionalNpmrcContent.push(
`${cleanedUri}:username=${hostRule.username}`,
);
additionalNpmrcContent.push(`${cleanedUri}:_password=${password}`);
const registries = {
npmAuthIdent: `${hostRule.username}:${hostRule.password}`,
};
additionalYarnRcYml.npmRegistries[cleanedUri] = registries;
additionalYarnRcYml.npmRegistries[uri] = registries;
}
}
const hasYarnRcNpmRegistries =
Object.keys(additionalYarnRcYml.npmRegistries).length > 0;
return {
additionalNpmrcContent,
additionalYarnRcYml: hasYarnRcNpmRegistries
? additionalYarnRcYml
: undefined,
};
}