Skip to content

Typings Intaller initial work #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions internal/project/discovertypings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"maps"
"slices"
"unicode/utf8"

"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/core"
Expand Down Expand Up @@ -287,34 +288,33 @@ func removeMinAndVersionNumbers(fileName string) string {
// We used to use the regex /[.-]((min)|(\d+(\.\d+)*))$/ and would just .replace it twice.
// Unfortunately, that regex has O(n^2) performance because v8 doesn't match from the end of the string.
// Instead, we now essentially scan the filename (backwards) ourselves.

end := len(fileName)
for pos := end - 1; pos > 0; pos-- {
ch := rune(fileName[pos])
for pos := end; pos > 0; {
ch, size := utf8.DecodeLastRuneInString(fileName[0:pos])
if ch >= '0' && ch <= '9' {
// Match a \d+ segment
for {
pos--
ch = rune(fileName[pos])
pos -= size
ch, size = utf8.DecodeLastRuneInString(fileName[0:pos])
if pos <= 0 || ch < '0' || ch > '9' {
break
}
}
} else if pos > 4 && (ch == 'n' || ch == 'N') {
// Looking for "min" or "min"
// Already matched the 'n'
pos--
ch = rune(fileName[pos])
pos -= size
ch, size = utf8.DecodeLastRuneInString(fileName[0:pos])
if ch != 'i' && ch != 'I' {
break
}
pos--
ch = rune(fileName[pos])
pos -= size
ch, size = utf8.DecodeLastRuneInString(fileName[0:pos])
if ch != 'm' && ch != 'M' {
break
}
pos--
ch = rune(fileName[pos])
pos -= size
ch, size = utf8.DecodeLastRuneInString(fileName[0:pos])
} else {
// This character is not part of either suffix pattern
break
Expand All @@ -323,12 +323,8 @@ func removeMinAndVersionNumbers(fileName string) string {
if ch != '-' && ch != '.' {
break
}
pos -= size
end = pos
}

// end might be fileName.length, in which case this should internally no-op
if end == len(fileName) {
return fileName
}
return fileName[0:end]
}
3 changes: 2 additions & 1 deletion internal/project/validatepackagename.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"strings"
"unicode/utf8"
)

type NameValidationResult int
Expand Down Expand Up @@ -36,7 +37,7 @@ func validatePackageNameWorker(packageName string, supportScopedPackage bool) (r
if packageNameLen > maxPackageNameLength {
return NameTooLong, "", false
}
firstChar := rune(packageName[0])
firstChar, _ := utf8.DecodeRuneInString(packageName)
if firstChar == '.' {
return NameStartsWithDot, "", false
}
Expand Down