Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(bazel-module): consolidate kv parsing logic #32464

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/modules/manager/bazel-module/parser/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { query as q } from 'good-enough-parser';
import { regEx } from '../../../../util/regex';
import type { Ctx } from '../context';
import * as starlark from '../starlark';

const booleanValuesRegex = regEx(`^${starlark.booleanStringValues.join('|')}$`);

/**
* Matches key-value pairs:
* - `name = "foobar"`
* - `name = True`
* - `name = ["string"]`
**/
export const kvParams = q
.sym<Ctx>((ctx, token) => ctx.startAttribute(token.value))
.op('=')
.alt(
q.str((ctx, token) => ctx.addString(token.value)),
q.sym<Ctx>(booleanValuesRegex, (ctx, token) => ctx.addBoolean(token.value)),
q.tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '[',
endsWith: ']',
postHandler: (ctx) => ctx.endArray(),
preHandler: (ctx) => ctx.startArray(),
search: q.many(q.str<Ctx>((ctx, token) => ctx.addString(token.value))),
}),
);
8 changes: 8 additions & 0 deletions lib/modules/manager/bazel-module/parser/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ describe('modules/manager/bazel-module/parser/index', () => {
{
rule: fragments.string('git_override'),
module_name: fragments.string('rules_foo'),
patches: fragments.array(
[fragments.string('//:rules_foo.patch')],
true,
),
commit: fragments.string(
'6a2c2e22849b3e6b33d5ea9aa72222d4803a986a',
),
Expand Down Expand Up @@ -102,6 +106,10 @@ describe('modules/manager/bazel-module/parser/index', () => {
{
rule: fragments.string('archive_override'),
module_name: fragments.string('rules_foo'),
urls: fragments.array(
[fragments.string('https://example.com/archive.tar.gz')],
true,
),
},
true,
),
Expand Down
17 changes: 1 addition & 16 deletions lib/modules/manager/bazel-module/parser/maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
StringArrayFragmentSchema,
StringFragmentSchema,
} from '../fragments';
import { kvParams } from './common';

const artifactMethod = 'artifact';
const installMethod = 'install';
Expand Down Expand Up @@ -115,22 +116,6 @@ export function fillRegistryUrls(
return result;
}

const kvParams = q
.sym<Ctx>((ctx, token) => ctx.startAttribute(token.value))
.op('=')
.alt(
q.str((ctx, token) => ctx.addString(token.value)),
q.tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '[',
endsWith: ']',
postHandler: (ctx) => ctx.endArray(),
preHandler: (ctx) => ctx.startArray(),
search: q.many(q.str<Ctx>((ctx, token) => ctx.addString(token.value))),
}),
);

export const mavenRules = q
.sym<Ctx>(mavenVariableRegex, (ctx, token) => {
return ctx.startRule(token.value);
Expand Down
16 changes: 1 addition & 15 deletions lib/modules/manager/bazel-module/parser/module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { query as q } from 'good-enough-parser';
import { regEx } from '../../../../util/regex';
import type { Ctx } from '../context';
import * as starlark from '../starlark';
import { kvParams } from './common';

const booleanValuesRegex = regEx(`^${starlark.booleanStringValues.join('|')}$`);
const supportedRules = [
'archive_override',
'bazel_dep',
Expand All @@ -13,19 +12,6 @@ const supportedRules = [
];
const supportedRulesRegex = regEx(`^${supportedRules.join('|')}$`);

/**
* Matches key-value pairs:
* - `name = "foobar"`
* - `name = True`
**/
const kvParams = q
.sym<Ctx>((ctx, token) => ctx.startAttribute(token.value))
.op('=')
.alt(
q.str((ctx, token) => ctx.addString(token.value)),
q.sym<Ctx>(booleanValuesRegex, (ctx, token) => ctx.addBoolean(token.value)),
);

export const moduleRules = q
.sym<Ctx>(supportedRulesRegex, (ctx, token) => ctx.startRule(token.value))
.join(
Expand Down