chore: update generated array type definitions in TypeScript to be readonly (#12947)

* chore: types generated handling readonly slices

* add -update flag to update goldens

* revert excess gens

* fix: update most UI types to account for readonly modifiers

* fix: remove accidental mutation from NavBarView

* fix: remove mutation warning for BatchUpdateConfirmation stories

* fix: remove mutation warning for BactchUpdateConfirmation

* fix: format ActiveUserChart

* fix: update import to make linter happy

* fix: update fmt issue

* fix: disable file write lint rule from unit test

---------

Co-authored-by: Parkreiner <throwawayclover@gmail.com>
This commit is contained in:
Steven Masley
2024-04-15 09:46:10 -04:00
committed by GitHub
co-authored by Parkreiner
parent 7cf8577f1c
commit d9da054c9d
40 changed files with 284 additions and 228 deletions
+8 -2
View File
@@ -814,11 +814,17 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
return TypescriptType{}, xerrors.Errorf("array: %w", err)
}
genValue := ""
// Always wrap in parentheses for proper scoped types.
// Running prettier on this output will remove redundant parenthesis,
// so this makes our decision-making easier.
// The example that breaks without this is:
// readonly readonly string[][]
if underlying.GenericValue != "" {
genValue = underlying.GenericValue + "[]"
genValue = "(readonly " + underlying.GenericValue + "[])"
}
return TypescriptType{
ValueType: underlying.ValueType + "[]",
ValueType: "(readonly " + underlying.ValueType + "[])",
GenericValue: genValue,
AboveTypeLine: underlying.AboveTypeLine,
GenericTypes: underlying.GenericTypes,
+11 -1
View File
@@ -7,6 +7,7 @@
package main
import (
"flag"
"os"
"path/filepath"
"strings"
@@ -15,6 +16,9 @@ import (
"github.com/stretchr/testify/require"
)
// updateGoldenFiles is a flag that can be set to update golden files.
var updateGoldenFiles = flag.Bool("update", false, "Update golden files")
func TestGeneration(t *testing.T) {
t.Parallel()
files, err := os.ReadDir("testdata")
@@ -37,7 +41,13 @@ func TestGeneration(t *testing.T) {
require.NoErrorf(t, err, "read file %s", golden)
expectedString := strings.TrimSpace(string(expected))
output = strings.TrimSpace(output)
require.Equal(t, expectedString, output, "matched output")
if *updateGoldenFiles {
// nolint:gosec
err := os.WriteFile(golden, []byte(output), 0o644)
require.NoError(t, err, "write golden file")
} else {
require.Equal(t, expectedString, output, "matched output")
}
})
}
}
+2 -2
View File
@@ -1,8 +1,8 @@
package codersdk
type (
Enum string
Enums []Enum
Enum string
EnumSliceType []Enum
)
const (
+1 -1
View File
@@ -1,5 +1,5 @@
// From codersdk/enums.go
export type Enums = Enum[]
export type EnumSliceType = (readonly Enum[])
// From codersdk/enums.go
export type Enum = "bar" | "baz" | "foo" | "qux"
+6 -6
View File
@@ -1,22 +1,22 @@
package codersdk
type Foo struct {
Bar string `json:"bar"`
}
type Buzz struct {
Foo `json:"foo"`
Bazz string `json:"bazz"`
}
type Custom interface {
Foo | Buzz
type Foo struct {
Bar string `json:"bar"`
}
type FooBuzz[R Custom] struct {
Something []R `json:"something"`
}
type Custom interface {
Foo | Buzz
}
// Not yet supported
//type FooBuzzMap[R Custom] struct {
// Something map[string]R `json:"something"`
+2 -2
View File
@@ -11,8 +11,8 @@ export interface Foo {
// From codersdk/genericmap.go
export interface FooBuzz<R extends Custom> {
readonly something: R[]
readonly something: (readonly R[])
}
// From codersdk/genericmap.go
export type Custom = Foo | Buzz
export type Custom = Foo | Buzz
+2 -2
View File
@@ -33,9 +33,9 @@ export interface Static {
}
// From codersdk/generics.go
export type Custom = string | boolean | number | string[] | null
export type Custom = string | boolean | number | (readonly string[]) | null
// From codersdk/generics.go
export type Single = string
export type comparable = boolean | number | string | any
export type comparable = boolean | number | string | any
@@ -0,0 +1,10 @@
package codersdk
type Bar struct {
Bar string
}
type Foo[R any] struct {
Slice []R
TwoD [][]R
}
@@ -0,0 +1,10 @@
// From codersdk/genericslice.go
export interface Bar {
readonly Bar: string
}
// From codersdk/genericslice.go
export interface Foo<R extends any> {
readonly Slice: (readonly R[])
readonly TwoD: (readonly (readonly R[])[])
}