Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd9da054

Browse files
EmyrkParkreiner
andauthored
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>
1 parent7cf8577 commitd9da054

File tree

40 files changed

+284
-228
lines changed

40 files changed

+284
-228
lines changed

‎scripts/apitypings/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,11 +814,17 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
814814
returnTypescriptType{},xerrors.Errorf("array: %w",err)
815815
}
816816
genValue:=""
817+
818+
// Always wrap in parentheses for proper scoped types.
819+
// Running prettier on this output will remove redundant parenthesis,
820+
// so this makes our decision-making easier.
821+
// The example that breaks without this is:
822+
//readonly readonly string[][]
817823
ifunderlying.GenericValue!="" {
818-
genValue=underlying.GenericValue+"[]"
824+
genValue="(readonly "+underlying.GenericValue+"[])"
819825
}
820826
returnTypescriptType{
821-
ValueType:underlying.ValueType+"[]",
827+
ValueType:"(readonly "+underlying.ValueType+"[])",
822828
GenericValue:genValue,
823829
AboveTypeLine:underlying.AboveTypeLine,
824830
GenericTypes:underlying.GenericTypes,

‎scripts/apitypings/main_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package main
88

99
import (
10+
"flag"
1011
"os"
1112
"path/filepath"
1213
"strings"
@@ -15,6 +16,9 @@ import (
1516
"github.com/stretchr/testify/require"
1617
)
1718

19+
// updateGoldenFiles is a flag that can be set to update golden files.
20+
varupdateGoldenFiles=flag.Bool("update",false,"Update golden files")
21+
1822
funcTestGeneration(t*testing.T) {
1923
t.Parallel()
2024
files,err:=os.ReadDir("testdata")
@@ -37,7 +41,13 @@ func TestGeneration(t *testing.T) {
3741
require.NoErrorf(t,err,"read file %s",golden)
3842
expectedString:=strings.TrimSpace(string(expected))
3943
output=strings.TrimSpace(output)
40-
require.Equal(t,expectedString,output,"matched output")
44+
if*updateGoldenFiles {
45+
// nolint:gosec
46+
err:=os.WriteFile(golden, []byte(output),0o644)
47+
require.NoError(t,err,"write golden file")
48+
}else {
49+
require.Equal(t,expectedString,output,"matched output")
50+
}
4151
})
4252
}
4353
}

‎scripts/apitypings/testdata/enums/enums.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package codersdk
22

33
type (
4-
Enumstring
5-
Enums []Enum
4+
Enumstring
5+
EnumSliceType []Enum
66
)
77

88
const (

‎scripts/apitypings/testdata/enums/enums.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// From codersdk/enums.go
2-
exporttypeEnums=Enum[]
2+
exporttypeEnumSliceType=(readonlyEnum[])
33

44
// From codersdk/enums.go
55
exporttypeEnum="bar"|"baz"|"foo"|"qux"

‎scripts/apitypings/testdata/genericmap/genericmap.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package codersdk
22

3-
typeFoostruct {
4-
Barstring`json:"bar"`
5-
}
6-
73
typeBuzzstruct {
84
Foo`json:"foo"`
95
Bazzstring`json:"bazz"`
106
}
117

12-
typeCustominterface {
13-
Foo|Buzz
8+
typeFoostruct {
9+
Barstring`json:"bar"`
1410
}
1511

1612
typeFooBuzz[RCustom]struct {
1713
Something []R`json:"something"`
1814
}
1915

16+
typeCustominterface {
17+
Foo|Buzz
18+
}
19+
2020
// Not yet supported
2121
//type FooBuzzMap[R Custom] struct {
2222
//Something map[string]R `json:"something"`

‎scripts/apitypings/testdata/genericmap/genericmap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export interface Foo {
1111

1212
// From codersdk/genericmap.go
1313
exportinterfaceFooBuzz<RextendsCustom>{
14-
readonlysomething:R[]
14+
readonlysomething:(readonlyR[])
1515
}
1616

1717
// From codersdk/genericmap.go
18-
exporttypeCustom=Foo|Buzz
18+
exporttypeCustom=Foo|Buzz

‎scripts/apitypings/testdata/generics/generics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ export interface Static {
3333
}
3434

3535
// From codersdk/generics.go
36-
exporttypeCustom=string|boolean|number|string[]|null
36+
exporttypeCustom=string|boolean|number|(readonlystring[])|null
3737

3838
// From codersdk/generics.go
3939
exporttypeSingle=string
4040

41-
exporttypecomparable=boolean|number|string|any
41+
exporttypecomparable=boolean|number|string|any
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package codersdk
2+
3+
typeBarstruct {
4+
Barstring
5+
}
6+
7+
typeFoo[Rany]struct {
8+
Slice []R
9+
TwoD [][]R
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// From codersdk/genericslice.go
2+
exportinterfaceBar{
3+
readonlyBar:string
4+
}
5+
6+
// From codersdk/genericslice.go
7+
exportinterfaceFoo<Rextendsany>{
8+
readonlySlice:(readonlyR[])
9+
readonlyTwoD:(readonly(readonlyR[])[])
10+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp