Skip to content
Snippets Groups Projects
Unverified Commit f1e3803d authored by Johannes Feichtner's avatar Johannes Feichtner Committed by GitHub
Browse files

refactor(manager/gradle): manage nesting depth separate from tokens (#20763)

parent fc2fd01a
No related branches found
No related tags found
No related merge requests found
......@@ -56,6 +56,7 @@ export function parseGradle(
registryUrls: [],
varTokens: [],
tmpNestingDepth: [],
tmpTokenStore: {},
tokenMap: {},
});
......
......@@ -4,9 +4,12 @@ import type { Ctx } from '../types';
import {
cleanupTempVars,
coalesceVariable,
increaseNestingDepth,
prependNestingDepth,
qStringValue,
qTemplateString,
qVariableAssignmentIdentifier,
reduceNestingDepth,
storeInTokenMap,
storeVarToken,
} from './common';
......@@ -65,39 +68,26 @@ const qKotlinSingleExtraVarAssignment = q
// foo: "1.2.3"
const qGroovySingleMapOfVarAssignment = q
.sym(storeVarToken)
.handler((ctx) => {
ctx.tmpTokenStore.backupVarTokens = ctx.varTokens;
return ctx;
})
.handler(prependNestingDepth)
.handler(coalesceVariable)
.handler((ctx) => storeInTokenMap(ctx, 'keyToken'))
.op(':')
.join(qTemplateString)
.handler((ctx) => storeInTokenMap(ctx, 'valToken'))
.handler(handleAssignment)
.handler((ctx) => {
ctx.varTokens = ctx.tmpTokenStore.backupVarTokens!;
ctx.varTokens.pop();
return ctx;
});
.handler(handleAssignment);
const qGroovyMapOfExpr = (
search: q.QueryBuilder<Ctx, parser.Node>
): q.QueryBuilder<Ctx, parser.Node> =>
q.alt(
q
.sym(storeVarToken)
.op(':')
.tree({
q.sym(storeVarToken).op(':').tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '[',
endsWith: ']',
preHandler: increaseNestingDepth,
search,
postHandler: (ctx: Ctx) => {
ctx.varTokens.pop();
return ctx;
},
postHandler: reduceNestingDepth,
}),
qGroovySingleMapOfVarAssignment
);
......@@ -110,45 +100,34 @@ const qGroovyMultiVarAssignment = qVariableAssignmentIdentifier
maxDepth: 1,
startsWith: '[',
endsWith: ']',
preHandler: increaseNestingDepth,
search: qGroovyMapOfExpr(qGroovyMapOfExpr(qGroovySingleMapOfVarAssignment)),
postHandler: reduceNestingDepth,
})
.handler(cleanupTempVars);
// "foo1" to "bar1"
const qKotlinSingleMapOfVarAssignment = qStringValue
.sym('to')
.handler((ctx) => {
ctx.tmpTokenStore.backupVarTokens = ctx.varTokens;
return ctx;
})
.handler(prependNestingDepth)
.handler(coalesceVariable)
.handler((ctx) => storeInTokenMap(ctx, 'keyToken'))
.join(qTemplateString)
.handler((ctx) => storeInTokenMap(ctx, 'valToken'))
.handler(handleAssignment)
.handler((ctx) => {
ctx.varTokens = ctx.tmpTokenStore.backupVarTokens!;
ctx.varTokens.pop();
return ctx;
});
.handler(handleAssignment);
const qKotlinMapOfExpr = (
search: q.QueryBuilder<Ctx, parser.Node>
): q.QueryBuilder<Ctx, parser.Node> =>
q.alt(
qStringValue
.sym('to')
.sym('mapOf')
.tree({
qStringValue.sym('to').sym('mapOf').tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '(',
endsWith: ')',
preHandler: increaseNestingDepth,
search,
postHandler: (ctx: Ctx) => {
ctx.varTokens.pop();
return ctx;
},
postHandler: reduceNestingDepth,
}),
qKotlinSingleMapOfVarAssignment
);
......@@ -162,7 +141,9 @@ const qKotlinMultiMapOfVarAssignment = qVariableAssignmentIdentifier
maxDepth: 1,
startsWith: '(',
endsWith: ')',
preHandler: increaseNestingDepth,
search: qKotlinMapOfExpr(qKotlinMapOfExpr(qKotlinSingleMapOfVarAssignment)),
postHandler: reduceNestingDepth,
})
.handler(cleanupTempVars);
......
......@@ -4,8 +4,11 @@ import type { Ctx, PackageVariables } from '../types';
import {
cleanupTempVars,
coalesceVariable,
increaseNestingDepth,
interpolateString,
loadFromTokenMap,
prependNestingDepth,
reduceNestingDepth,
storeInTokenMap,
storeVarToken,
stripReservedPrefixFromKeyTokens,
......@@ -13,7 +16,7 @@ import {
describe('modules/manager/gradle/parser/common', () => {
let ctx: Ctx;
const token = partial<lexer.Token>({});
const token = partial<lexer.Token>({ value: 'test' });
beforeEach(() => {
ctx = {
......@@ -26,6 +29,7 @@ describe('modules/manager/gradle/parser/common', () => {
registryUrls: [],
varTokens: [],
tmpNestingDepth: [],
tmpTokenStore: {},
tokenMap: {},
};
......@@ -36,6 +40,33 @@ describe('modules/manager/gradle/parser/common', () => {
expect(ctx.varTokens).toStrictEqual([token]);
});
it('increaseNestingDepth', () => {
ctx.tmpNestingDepth = ctx.varTokens = [token];
increaseNestingDepth(ctx);
expect(ctx).toMatchObject({
tmpNestingDepth: [token, token],
varTokens: [],
});
});
it('reduceNestingDepth', () => {
ctx.tmpNestingDepth = [token, token];
reduceNestingDepth(ctx);
expect(ctx.tmpNestingDepth).toHaveLength(1);
});
it('prependNestingDepth', () => {
ctx.tmpNestingDepth = ctx.varTokens = [token];
prependNestingDepth(ctx);
expect(ctx.varTokens).toStrictEqual([token, token]);
coalesceVariable(ctx);
expect(ctx).toMatchObject({
tmpNestingDepth: [{ value: 'test' }],
varTokens: [{ value: 'test.test' }],
});
});
it('storeInTokenMap', () => {
ctx.varTokens = [token];
storeInTokenMap(ctx, 'foo');
......
import { lexer, parser, query as q } from 'good-enough-parser';
import { clone } from '../../../../util/clone';
import { regEx } from '../../../../util/regex';
import type { Ctx, NonEmptyArray, PackageVariables } from '../types';
......@@ -39,6 +40,22 @@ export function storeVarToken(ctx: Ctx, node: lexer.Token): Ctx {
return ctx;
}
export function increaseNestingDepth(ctx: Ctx): Ctx {
ctx.tmpNestingDepth.push(...ctx.varTokens);
ctx.varTokens = [];
return ctx;
}
export function reduceNestingDepth(ctx: Ctx): Ctx {
ctx.tmpNestingDepth.pop();
return ctx;
}
export function prependNestingDepth(ctx: Ctx): Ctx {
ctx.varTokens = [...clone(ctx.tmpNestingDepth), ...ctx.varTokens];
return ctx;
}
export function storeInTokenMap(ctx: Ctx, tokenMapKey: string): Ctx {
ctx.tokenMap[tokenMapKey] = ctx.varTokens;
ctx.varTokens = [];
......
......@@ -82,6 +82,7 @@ export interface Ctx {
registryUrls: PackageRegistry[];
varTokens: lexer.Token[];
tmpNestingDepth: lexer.Token[];
tmpTokenStore: Record<string, lexer.Token[]>;
tokenMap: Record<string, lexer.Token[]>;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment