refactor: deduplicate / type license feature code (#5734)

This commit is contained in:
Ammar Bandukwala
2023-01-17 17:04:29 -06:00
committed by GitHub
parent ea1b03f7c9
commit 501cfa9e8d
39 changed files with 648 additions and 396 deletions
+18 -1
View File
@@ -63,7 +63,11 @@ type TypescriptTypes struct {
// String just combines all the codeblocks.
func (t TypescriptTypes) String() string {
var s strings.Builder
_, _ = s.WriteString("// Code generated by 'make site/src/api/typesGenerated.ts'. DO NOT EDIT.\n\n")
const prelude = `
// Code generated by 'make site/src/api/typesGenerated.ts'. DO NOT EDIT.
`
_, _ = s.WriteString(prelude)
sortedTypes := make([]string, 0, len(t.Types))
sortedEnums := make([]string, 0, len(t.Enums))
@@ -223,6 +227,18 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
name, strings.Join(values, " | "),
))
var pluralName string
if strings.HasSuffix(name, "s") {
pluralName = name + "es"
} else {
pluralName = name + "s"
}
// Generate array used for enumerating all possible values.
_, _ = s.WriteString(fmt.Sprintf("export const %s: %s[] = [%s]\n",
pluralName, name, strings.Join(values, ", "),
))
enumCodeBlocks[name] = s.String()
}
@@ -644,6 +660,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
aboveTypeLine = aboveTypeLine + "\n"
}
aboveTypeLine = aboveTypeLine + valueType.AboveTypeLine
return TypescriptType{
ValueType: fmt.Sprintf("Record<%s, %s>", keyType.ValueType, valueType.ValueType),
AboveTypeLine: aboveTypeLine,
+1
View File
@@ -2,3 +2,4 @@
// From codersdk/enums.go
export type Enum = "bar" | "baz" | "foo" | "qux"
export const Enums: Enum[] = ["bar", "baz", "foo", "qux"]