MCPcopy Create free account
hub / github.com/microsoft/typescript-go / ModuleSpecifierToValidIdentifier

Function ModuleSpecifierToValidIdentifier

internal/ls/lsutil/utilities.go:120–153  ·  view source on GitHub ↗
(moduleSpecifier string, forceCapitalize bool)

Source from the content-addressed store, hash-verified

118}
119
120func ModuleSpecifierToValidIdentifier(moduleSpecifier string, forceCapitalize bool) string {
121 baseName := tspath.GetBaseFileName(strings.TrimSuffix(tspath.RemoveFileExtension(moduleSpecifier), "/index"))
122 res := []rune{}
123 lastCharWasValid := true
124 baseNameRunes := []rune(baseName)
125 if len(baseNameRunes) > 0 && scanner.IsIdentifierStart(baseNameRunes[0]) {
126 if forceCapitalize {
127 res = append(res, unicode.ToUpper(baseNameRunes[0]))
128 } else {
129 res = append(res, baseNameRunes[0])
130 }
131 } else {
132 lastCharWasValid = false
133 }
134
135 for i := 1; i < len(baseNameRunes); i++ {
136 isValid := scanner.IsIdentifierPart(baseNameRunes[i])
137 if isValid {
138 if !lastCharWasValid {
139 res = append(res, unicode.ToUpper(baseNameRunes[i]))
140 } else {
141 res = append(res, baseNameRunes[i])
142 }
143 }
144 lastCharWasValid = isValid
145 }
146
147 // Need `"_"` to ensure result isn't empty.
148 resString := string(res)
149 if resString != "" && !IsNonContextualKeyword(scanner.StringToToken(resString)) {
150 return resString
151 }
152 return "_" + resString
153}
154
155func IsNonContextualKeyword(token ast.Kind) bool {
156 return ast.IsKeywordKind(token) && !ast.IsContextualKeyword(token)

Callers 2

createExportMethod · 0.92

Calls 9

GetBaseFileNameFunction · 0.92
RemoveFileExtensionFunction · 0.92
IsIdentifierStartFunction · 0.92
IsIdentifierPartFunction · 0.92
StringToTokenFunction · 0.92
lenFunction · 0.85
IsNonContextualKeywordFunction · 0.70
appendFunction · 0.50
stringEnum · 0.50

Tested by

no test coverage detected