Skip to content

Commit ecf6a28

Browse files
committed
refactor(yarn2): Introduce a class for the locator
Change the logic which extracts the module name, so that it splits up the given string into a module name and a remainder. This prepares for an upcoming change. Signed-off-by: Frank Viernau <[email protected]>
1 parent f019963 commit ecf6a28

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

plugins/package-managers/node/src/main/kotlin/yarn2/Yarn2DependencyHandler.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,11 @@ internal class Yarn2DependencyHandler(
8383

8484
internal val PackageInfo.isProject: Boolean get() = value.substringAfterLast("@").startsWith("workspace:")
8585

86-
internal val PackageInfo.moduleName: String get() {
86+
internal val PackageInfo.moduleName: String get() =
8787
// TODO: Handle patched packages different than non-patched ones.
8888
// Patch packages have locators as e.g. the following, where the first component ends with "@patch".
8989
// resolve@patch:resolve@npm%3A1.22.8#optional!builtin<compat/resolve>::version=1.22.8&hash=c3c19d
90-
val endIndex = value.indexOf("@", startIndex = 1)
91-
return value.take(endIndex)
92-
}
90+
Locator.parse(value).moduleName
9391

9492
internal val PackageInfo.moduleId: String get() = buildString {
9593
append(moduleName)
@@ -99,3 +97,18 @@ internal val PackageInfo.moduleId: String get() = buildString {
9997
append(children.version)
10098
}
10199
}
100+
101+
internal data class Locator(
102+
val moduleName: String,
103+
val remainder: String
104+
) {
105+
companion object {
106+
fun parse(value: String): Locator {
107+
val moduleNameEndIndex = value.indexOf("@", startIndex = 1)
108+
return Locator(
109+
moduleName = value.take(moduleNameEndIndex),
110+
remainder = value.substring(moduleNameEndIndex + 1)
111+
)
112+
}
113+
}
114+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2025 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.ossreviewtoolkit.plugins.packagemanagers.node.yarn2
21+
22+
import io.kotest.core.spec.style.WordSpec
23+
import io.kotest.matchers.shouldBe
24+
25+
class Yarn2DependencyHandlerTest : WordSpec({
26+
"Locator.parse()" should {
27+
"work for patched packages" {
28+
val locator = Locator.parse(
29+
"resolve@patch:resolve@npm%3A1.22.8#optional!builtin<compat/resolve>::version=1.22.8&hash=c3c19d"
30+
)
31+
32+
locator.moduleName shouldBe "resolve"
33+
locator.remainder shouldBe
34+
"patch:resolve@npm%3A1.22.8#optional!builtin<compat/resolve>::version=1.22.8&hash=c3c19d"
35+
}
36+
37+
"work for a package with a scope" {
38+
val locator = Locator.parse("@failing/package-with-lightningcss@workspace:packages/spark")
39+
40+
locator.moduleName shouldBe "@failing/package-with-lightningcss"
41+
locator.remainder shouldBe "workspace:packages/spark"
42+
}
43+
}
44+
})

0 commit comments

Comments
 (0)