(moduleSpecifier string, forceCapitalize bool)
| 118 | } |
| 119 | |
| 120 | func 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 | |
| 155 | func IsNonContextualKeyword(token ast.Kind) bool { |
| 156 | return ast.IsKeywordKind(token) && !ast.IsContextualKeyword(token) |
no test coverage detected