Files
renovate/lib/modules/manager/gradle/parser/assignments.ts
Sebastian Poxhofer 58771bb183 refactor(linters): enabled oxlints func-style (#44691)
* refactor(oxlint): enable func-style

Enable the built-in func-style rule as ["warn", "declaration"] so that
named functions are written as function declarations instead of arrow or
function expressions assigned to variables, matching the convention in
docs/development/best-practices.md (Functions section). Inline arrow
callbacks are unaffected; prefer-arrow-callback continues to cover the
callback direction.

Severity is "warn" instead of "error" as a fallback: a repo-wide run
found 196 pre-existing violations (70 in spec files, 126 in production
code), exceeding the 80-violation fix threshold for this change, so no
violations were fixed here. Follow-up work can convert the offenders
(largest clusters: lib/modules/versioning/ruby, lib/util/cache/package,
lib/modules/versioning/maven, lib/modules/versioning/gradle) and then
raise the rule to "error". Note oxlint 1.72 also supports an
allowTypeAnnotation option if typed consts should remain arrows.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* ci(linter): enforce func-style at error

* refactor(func-style): convert const-arrow functions to declarations in with-cache spec and ruby versioning

* refactor(func-style): convert const-arrow functions to declarations in maven/gradle modules

* refactor(func-style): convert const-arrow functions to declarations in remaining versioning modules

* refactor(func-style): convert const-arrow functions to declarations in platform modules

Discriminated-union closures (ensureCommentRemoval byTopic/byContent
helpers in github, gitlab, bitbucket, bitbucket-server) needed the
narrowed field passed explicitly as a parameter, since function
declarations are hoisted and lose the outer if-narrowing that inline
arrow-function closures previously relied on.

* refactor(func-style): convert const-arrow functions to declarations across util/manager/datasource modules

One case (logger/renovate-logger.ts logOnceFn) is kept as an arrow
function with a documented oxlint-disable: it is stored on
this.logger.once[level] and relies on lexically-bound this, which a
hoisted function declaration would not preserve.

* refactor(func-style): convert remaining const-arrow test helpers to declarations

get-updated.spec.ts: hoisted expectPipCompilePackageAndLockFile to
module scope (it only closes over the module-level pipCompile mock)
and added a documented oxlint-disable for vitest/expect-expect on its
caller test, since oxlint's expect-expect check does not trace
assertions through an externally-defined helper function.

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-19 11:14:22 +00:00

170 lines
4.6 KiB
TypeScript

import type { parser } from '@renovatebot/good-enough-parser';
import { query as q } from '@renovatebot/good-enough-parser';
import { regEx } from '../../../../util/regex.ts';
import type { Ctx } from '../types.ts';
import {
cleanupTempVars,
coalesceVariable,
increaseNestingDepth,
prependNestingDepth,
qStringValue,
qValueMatcher,
qVariableAssignmentIdentifier,
reduceNestingDepth,
storeInTokenMap,
storeVarToken,
} from './common.ts';
import {
qDependencyStrings,
qGroovyMapNotationDependencies,
} from './dependencies.ts';
import { handleAssignment } from './handlers.ts';
// foo = "1.2.3"
const qGroovySingleVarAssignment = qVariableAssignmentIdentifier
.op('=')
.handler(coalesceVariable)
.handler((ctx) => storeInTokenMap(ctx, 'keyToken'))
.join(qStringValue)
.handler((ctx) => storeInTokenMap(ctx, 'valToken'))
.handler(handleAssignment)
.handler(cleanupTempVars);
// set("foo", "1.2.3")
const qKotlinSingleVarAssignment = q
.sym<Ctx>(regEx(/^(?:set|version)$/))
.tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '(',
endsWith: ')',
search: q
.begin<Ctx>()
.join(qStringValue)
.handler((ctx) => storeInTokenMap(ctx, 'keyToken'))
.op(',')
.join(qStringValue)
.handler((ctx) => storeInTokenMap(ctx, 'valToken'))
.handler(handleAssignment)
.end(),
})
.handler(cleanupTempVars);
// val foo by extra { "1.2.3" }
const qKotlinSingleExtraVarAssignment = q
.sym<Ctx>('val')
.sym(storeVarToken)
.handler((ctx) => storeInTokenMap(ctx, 'keyToken'))
.opt(q.op<Ctx>(':').sym('String'))
.sym('by')
.sym('extra')
.tree({
type: 'wrapped-tree',
maxDepth: 1,
search: q
.begin<Ctx>()
.join(qStringValue)
.handler((ctx) => storeInTokenMap(ctx, 'valToken'))
.handler(handleAssignment)
.end(),
})
.handler(cleanupTempVars);
const qGroovySingleMapOfVarAssignment = q.alt(
// foo: [group: "foo", name: "bar", version: "1.2.3"]
q.begin<Ctx>().join(qGroovyMapNotationDependencies).end(),
// foo: "1.2.3"
q
.alt(q.sym(storeVarToken), q.str(storeVarToken))
.handler(prependNestingDepth)
.handler(coalesceVariable)
.handler((ctx) => storeInTokenMap(ctx, 'keyToken'))
.op(':')
.join(qValueMatcher)
.handler((ctx) => storeInTokenMap(ctx, 'valToken'))
.handler(handleAssignment),
// ["foo:bar:1.2.3", "foo:baz:$qux"]
qDependencyStrings,
);
function qGroovyMapOfExpr(
search: q.QueryBuilder<Ctx, parser.Node>,
): q.QueryBuilder<Ctx, parser.Node> {
return q.alt(
q.alt(q.sym(storeVarToken), q.str(storeVarToken)).op(':').tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '[',
endsWith: ']',
preHandler: increaseNestingDepth,
search,
postHandler: reduceNestingDepth,
}),
qGroovySingleMapOfVarAssignment,
);
}
// versions = [ android: [ buildTools: '30.0.3' ], kotlin: '1.4.30' ]
const qGroovyMultiVarAssignment = qVariableAssignmentIdentifier
.alt(q.op('='), q.op('+='))
.tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '[',
endsWith: ']',
preHandler: increaseNestingDepth,
search: qGroovyMapOfExpr(qGroovyMapOfExpr(qGroovySingleMapOfVarAssignment)),
postHandler: reduceNestingDepth,
})
.handler(cleanupTempVars);
// "foo1" to "bar1"
const qKotlinSingleMapOfVarAssignment = qStringValue
.sym('to')
.handler(prependNestingDepth)
.handler(coalesceVariable)
.handler((ctx) => storeInTokenMap(ctx, 'keyToken'))
.join(qValueMatcher)
.handler((ctx) => storeInTokenMap(ctx, 'valToken'))
.handler(handleAssignment);
function qKotlinMapOfExpr(
search: q.QueryBuilder<Ctx, parser.Node>,
): q.QueryBuilder<Ctx, parser.Node> {
return q.alt(
qStringValue.sym('to').sym('mapOf').tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '(',
endsWith: ')',
preHandler: increaseNestingDepth,
search,
postHandler: reduceNestingDepth,
}),
qKotlinSingleMapOfVarAssignment,
);
}
// val versions = mapOf("foo1" to "bar1", "foo2" to "bar2", "foo3" to "bar3")
export const qKotlinMultiMapOfVarAssignment = qVariableAssignmentIdentifier
.op('=')
.sym('mapOf')
.tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '(',
endsWith: ')',
preHandler: increaseNestingDepth,
search: qKotlinMapOfExpr(qKotlinMapOfExpr(qKotlinSingleMapOfVarAssignment)),
postHandler: reduceNestingDepth,
})
.handler(cleanupTempVars);
export const qAssignments = q.alt(
qGroovySingleVarAssignment,
qGroovyMultiVarAssignment,
qKotlinSingleVarAssignment,
qKotlinSingleExtraVarAssignment,
qKotlinMultiMapOfVarAssignment,
);