Skip to content

Commit

Permalink
fix(gradle): remove redundant checks and parser statements (#33526)
Browse files Browse the repository at this point in the history
  • Loading branch information
Churro authored Jan 11, 2025
1 parent 44fc39b commit 6ff935e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 58 deletions.
29 changes: 14 additions & 15 deletions lib/modules/manager/gradle/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
PackageVariables,
ParseGradleResult,
} from './types';
import { isDependencyString, parseDependencyString } from './utils';
import { parseDependencyString } from './utils';

const groovy = lang.createLang('groovy');
const ctx: Ctx = {
Expand Down Expand Up @@ -127,28 +127,27 @@ export function parseProps(
): { vars: PackageVariables; deps: PackageDependency<GradleManagerData>[] } {
let offset = 0;
const vars: PackageVariables = {};
const deps: PackageDependency[] = [];
const deps: PackageDependency<GradleManagerData>[] = [];

for (const line of input.split(newlineRegex)) {
const lineMatch = propRegex.exec(line);
if (lineMatch?.groups) {
const { key, value, leftPart } = lineMatch.groups;
if (isDependencyString(value)) {
const dep = parseDependencyString(value);
if (dep) {
deps.push({
...dep,
managerData: {
fileReplacePosition:
offset + leftPart.length + dep.depName!.length + 1,
packageFile,
},
});
}
const replacePosition = offset + leftPart.length;
const dep = parseDependencyString(value);
if (dep) {
deps.push({
...dep,
managerData: {
fileReplacePosition: replacePosition + dep.depName!.length + 1,
packageFile,
},
});
} else {
vars[key] = {
key,
value,
fileReplacePosition: offset + leftPart.length,
fileReplacePosition: replacePosition,
packageFile,
};
}
Expand Down
16 changes: 3 additions & 13 deletions lib/modules/manager/gradle/parser/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,23 +267,13 @@ export const qTemplateString = q
ctx.tmpTokenStore.templateTokens = [];
return ctx;
},
search: q.alt(
qStringValue.handler((ctx) => {
search: q
.alt(qStringValue, qPropertyAccessIdentifier, qVariableAccessIdentifier)
.handler((ctx) => {
ctx.tmpTokenStore.templateTokens?.push(...ctx.varTokens);
ctx.varTokens = [];
return ctx;
}),
qPropertyAccessIdentifier.handler((ctx) => {
ctx.tmpTokenStore.templateTokens?.push(...ctx.varTokens);
ctx.varTokens = [];
return ctx;
}),
qVariableAccessIdentifier.handler((ctx) => {
ctx.tmpTokenStore.templateTokens?.push(...ctx.varTokens);
ctx.varTokens = [];
return ctx;
}),
),
})
.handler((ctx) => {
ctx.varTokens = ctx.tmpTokenStore.templateTokens!;
Expand Down
47 changes: 17 additions & 30 deletions lib/modules/manager/gradle/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const artifactRegex = regEx(

const versionLikeRegex = regEx('^(?<version>[-_.\\[\\](),a-zA-Z0-9+]+)');

// Extracts version-like and range-like strings
// from the beginning of input
// Extracts version-like and range-like strings from the beginning of input
export function versionLikeSubstring(
input: string | null | undefined,
): string | null {
Expand All @@ -32,40 +31,32 @@ export function versionLikeSubstring(
}

export function isDependencyString(input: string): boolean {
const split = input?.split(':');
if (split?.length !== 3 && split?.length !== 4) {
const parts = input.split(':');
if (parts.length !== 3 && parts.length !== 4) {
return false;
}
// eslint-disable-next-line prefer-const
let [tempGroupId, tempArtifactId, tempVersionPart, optionalClassifier] =
split;
const [groupId, artifactId, versionPart, optionalClassifier] = parts;

if (optionalClassifier && !artifactRegex.test(optionalClassifier)) {
return false;
}

if (
tempVersionPart !== versionLikeSubstring(tempVersionPart) &&
tempVersionPart.includes('@')
) {
const versionSplit = tempVersionPart?.split('@');
if (versionSplit?.length !== 2) {
let version = versionPart;
if (versionPart.includes('@')) {
const [actualVersion, ...rest] = versionPart.split('@');
if (rest.length !== 1) {
return false;
}
[tempVersionPart] = versionSplit;
version = actualVersion;
}
const [groupId, artifactId, versionPart] = [
tempGroupId,
tempArtifactId,
tempVersionPart,
];

return !!(
groupId &&
artifactId &&
versionPart &&
version &&
artifactRegex.test(groupId) &&
artifactRegex.test(artifactId) &&
versionPart === versionLikeSubstring(versionPart)
version === versionLikeSubstring(version)
);
}

Expand All @@ -75,18 +66,14 @@ export function parseDependencyString(
if (!isDependencyString(input)) {
return null;
}
const [groupId, artifactId, FullValue] = input.split(':');
if (FullValue === versionLikeSubstring(FullValue)) {
return {
depName: `${groupId}:${artifactId}`,
currentValue: FullValue,
};
}
const [currentValue, dataType] = FullValue.split('@');

const [groupId, artifactId, fullValue] = input.split(':');
const [currentValue, dataType] = fullValue.split('@');

return {
depName: `${groupId}:${artifactId}`,
currentValue,
dataType,
...(dataType && { dataType }),
};
}

Expand Down

0 comments on commit 6ff935e

Please sign in to comment.