Files
renovate/lib/modules/manager/tflint-plugin/plugins.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

90 lines
2.7 KiB
TypeScript

import { isString } from '@sindresorhus/is';
import { logger } from '../../../logger/index.ts';
import { coerceArray } from '../../../util/array.ts';
import { regEx } from '../../../util/regex.ts';
import { GithubReleasesDatasource } from '../../datasource/github-releases/index.ts';
import type { PackageDependency } from '../types.ts';
import type { ExtractionResult } from './types.ts';
import { keyValueExtractionRegex } from './util.ts';
export function extractTFLintPlugin(
startingLine: number,
lines: string[],
// TODO: remove unused parameter
_pluginName: string,
): ExtractionResult {
let lineNumber = startingLine;
const deps: PackageDependency[] = [];
let pluginSource: string | null = null;
let currentVersion: string | null = null;
let braceCounter = 0;
do {
// istanbul ignore if
if (lineNumber > lines.length - 1) {
logger.debug(`Malformed TFLint configuration file detected.`);
}
const line = lines[lineNumber];
// istanbul ignore else
if (isString(line)) {
// `{` will be counted with +1 and `}` with -1.
// Therefore if we reach braceCounter == 0 then we found the end of the tflint configuration block.
const openBrackets = coerceArray(line.match(regEx(/\{/g))).length;
const closedBrackets = coerceArray(line.match(regEx(/\}/g))).length;
braceCounter = braceCounter + openBrackets - closedBrackets;
// only update fields inside the root block
if (braceCounter === 1) {
const kvMatch = keyValueExtractionRegex.exec(line);
if (kvMatch?.groups) {
if (kvMatch.groups.key === 'version') {
currentVersion = kvMatch.groups.value;
} else if (kvMatch.groups.key === 'source') {
pluginSource = kvMatch.groups.value;
}
}
}
} else {
// stop - something went wrong
braceCounter = 0;
}
lineNumber += 1;
} while (braceCounter !== 0);
const dep = analyseTFLintPlugin(pluginSource, currentVersion);
deps.push(dep);
// remove last lineNumber addition to not skip a line after the last bracket
lineNumber -= 1;
return { lineNumber, dependencies: deps };
}
function analyseTFLintPlugin(
source: string | null,
version: string | null,
): PackageDependency {
const dep: PackageDependency = {};
if (source) {
dep.depType = 'plugin';
const sourceParts = source.split('/');
if (sourceParts[0] === 'github.com') {
dep.currentValue = version;
dep.datasource = GithubReleasesDatasource.id;
dep.depName = sourceParts.slice(1).join('/');
} else {
dep.skipReason = 'unsupported-datasource';
dep.depName = source;
}
} else {
logger.debug({ dep }, 'tflint plugin has no source');
dep.skipReason = 'no-source';
}
return dep;
}