diff --git a/src/java/MavenGradleMetadataParser.ts b/src/java/MavenGradleMetadataParser.ts index 60b0ccbfab..508ecb1886 100644 --- a/src/java/MavenGradleMetadataParser.ts +++ b/src/java/MavenGradleMetadataParser.ts @@ -246,69 +246,70 @@ const mavenRows = ( : xmlChildrenNamed(container, rowName, container.namespaceURI ?? ""); }; -const parseMavenDependency = < +const parseMavenDependencies = < Origin extends "dependencies" | "dependencyManagement", >( - element: Element, + elements: readonly Element[], origin: Origin, - index: number, unresolved: MavenGradleUnresolvedCollector, maven4Model: boolean, -): MavenGradleDependency => { - const group_id = childText(element, "groupId"); - const artifact_id = childText(element, "artifactId"); - const version = childText(element, "version"); - const type = childText(element, "type"); - const scope = childText(element, "scope"); - // Maven 4 also imports a managed `bom` type, whatever its scope. Only a - // 4.1.0 model POM is certain to be built by Maven 4 rather than Maven 3. - const bom_import = - origin === "dependencyManagement" && - ((type === "pom" && scope === "import") || (maven4Model && type === "bom")); - const exclusions = mavenRows(element, "exclusions", "exclusion").map( - (exclusion) => ({ - group_id: childText(exclusion, "groupId"), - artifact_id: childText(exclusion, "artifactId"), - }), - ); - const owner = - origin === "dependencies" ? "dependency" : "dependency_management"; - unresolved.coordinates( - { group_id, artifact_id, version }, - owner, - index, - false, - ); - const optional_spelling = childText(element, "optional"); - const classifier = childText(element, "classifier"); - unresolved.properties( - [ - ["scope", scope], - ["type", type], - ["classifier", classifier], - ["optional", optional_spelling], - ], - owner, - index, - ); - return { - group_id, - artifact_id, - version, - version_kind: requestedVersionKind(version, "maven-pom"), - gradle_version: null, - scope, - type, - optional: parseTrueFalseText(optional_spelling), - optional_spelling, - classifier, - exclusions, - origin, - variant: null, - bom_import, - platform: false, - }; -}; +): MavenGradleDependency[] => + elements.map((element, index) => { + const group_id = childText(element, "groupId"); + const artifact_id = childText(element, "artifactId"); + const version = childText(element, "version"); + const type = childText(element, "type"); + const scope = childText(element, "scope"); + // Maven 4 also imports a managed `bom` type, whatever its scope. Only a + // 4.1.0 model POM is certain to be built by Maven 4 rather than Maven 3. + const bom_import = + origin === "dependencyManagement" && + ((type === "pom" && scope === "import") || + (maven4Model && type === "bom")); + const exclusions = mavenRows(element, "exclusions", "exclusion").map( + (exclusion) => ({ + group_id: childText(exclusion, "groupId"), + artifact_id: childText(exclusion, "artifactId"), + }), + ); + const owner = + origin === "dependencies" ? "dependency" : "dependency_management"; + unresolved.coordinates( + { group_id, artifact_id, version }, + owner, + index, + false, + ); + const optional_spelling = childText(element, "optional"); + const classifier = childText(element, "classifier"); + unresolved.properties( + [ + ["scope", scope], + ["type", type], + ["classifier", classifier], + ["optional", optional_spelling], + ], + owner, + index, + ); + return { + group_id, + artifact_id, + version, + version_kind: requestedVersionKind(version, "maven-pom"), + gradle_version: null, + scope, + type, + optional: parseTrueFalseText(optional_spelling), + optional_spelling, + classifier, + exclusions, + origin, + variant: null, + bom_import, + platform: false, + }; + }); const parsePom = ( xml: string, @@ -408,23 +409,17 @@ const parsePom = ( 0, ), ); - const dependency_management = managedElements.map((element, index) => - parseMavenDependency( - element, - "dependencyManagement", - index, - unresolved, - maven4Model, - ), + const dependency_management = parseMavenDependencies( + managedElements, + "dependencyManagement", + unresolved, + maven4Model, ); - const dependencies = dependencyElements.map((element, index) => - parseMavenDependency( - element, - "dependencies", - index, - unresolved, - maven4Model, - ), + const dependencies = parseMavenDependencies( + dependencyElements, + "dependencies", + unresolved, + maven4Model, ); if (parent !== null) limitations.push(