Skip to content

Commit

Permalink
refactor(bazel-module): consolidate kv parsing logic (#32464)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzm0 authored Nov 12, 2024
1 parent c2117cb commit 624bda1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 31 deletions.
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

0 comments on commit 624bda1

Please sign in to comment.