Skip to content

Commit 781b5de

Browse files
committed
Use strings.Cut instead of string.Index for pnp
1 parent c343dca commit 781b5de

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

internal/pnp/pnpapi.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,12 @@ func (p *PnpApi) ParseBareIdentifier(specifier string) (ident string, modulePath
272272
ident = specifier[:firstSlash+1+secondSlash]
273273
}
274274
} else {
275-
firstSlash := strings.Index(specifier, "/")
275+
beforeFirstSlash, _, found := strings.Cut(specifier, "/")
276276

277-
if firstSlash == -1 {
277+
if !found {
278278
ident = specifier
279279
} else {
280-
ident = specifier[:firstSlash]
280+
ident = beforeFirstSlash
281281
}
282282
}
283283

internal/vfs/pnpvfs/pnpvfs.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,11 @@ func getMatchingFS(pnpFS *pnpFS, path string) (vfs.FS, string, string) {
178178
// Virtual paths are used to make different paths resolve to the same real file or folder, which is necessary in some cases when PnP is enabled
179179
// See https://yarnpkg.com/advanced/lexicon#virtual-package and https://yarnpkg.com/advanced/pnpapi#resolvevirtual for more details
180180
func resolveVirtual(path string) (realPath string, hash string, basePath string) {
181-
idx := strings.Index(path, "/__virtual__/")
182-
if idx == -1 {
181+
base, rest, found := strings.Cut(path, "/__virtual__/")
182+
if !found {
183183
return path, "", ""
184184
}
185185

186-
base := path[:idx]
187-
rest := path[idx+len("/__virtual__/"):]
188186
parts := strings.SplitN(rest, "/", 3)
189187
if len(parts) < 3 {
190188
// Not enough parts to match the pattern, return as is
@@ -198,7 +196,7 @@ func resolveVirtual(path string) (realPath string, hash string, basePath string)
198196
return path, "", ""
199197
}
200198

201-
basePath = path[:idx] + "/__virtual__"
199+
basePath = base + "/__virtual__"
202200

203201
// Apply dirname n times to base
204202
for range depth {

0 commit comments

Comments
 (0)