Skip to content

Commit 2379c21

Browse files
committed
Go linting
1 parent 0d943f1 commit 2379c21

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

scripts/apitypings/main.go

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func main() {
3333
}
3434

3535
// Just cat the output to a file to capture it
36-
fmt.Println(codeBlocks.String())
36+
_, _ = fmt.Println(codeBlocks.String())
3737
}
3838

3939
// TypescriptTypes holds all the code blocks created.
@@ -46,7 +46,7 @@ type TypescriptTypes struct {
4646
// String just combines all the codeblocks.
4747
func (t TypescriptTypes) String() string {
4848
var s strings.Builder
49-
s.WriteString("// Code generated by 'make coder/scripts/apitypings/main.go'. DO NOT EDIT.\n\n")
49+
_, _ = s.WriteString("// Code generated by 'make coder/scripts/apitypings/main.go'. DO NOT EDIT.\n\n")
5050

5151
sortedTypes := make([]string, 0, len(t.Types))
5252
sortedEnums := make([]string, 0, len(t.Types))
@@ -63,14 +63,14 @@ func (t TypescriptTypes) String() string {
6363

6464
for _, k := range sortedTypes {
6565
v := t.Types[k]
66-
s.WriteString(v)
67-
s.WriteRune('\n')
66+
_, _ = s.WriteString(v)
67+
_, _ = s.WriteRune('\n')
6868
}
6969

7070
for _, k := range sortedEnums {
7171
v := t.Enums[k]
72-
s.WriteString(v)
73-
s.WriteRune('\n')
72+
_, _ = s.WriteString(v)
73+
_, _ = s.WriteRune('\n')
7474
}
7575

7676
return strings.TrimRight(s.String(), "\n")
@@ -147,7 +147,6 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
147147
ignoredTypes[strings.TrimSpace(s)] = struct{}{}
148148
}
149149
}
150-
151150
}
152151
}
153152
}
@@ -164,7 +163,7 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
164163
continue
165164
}
166165

167-
switch obj.(type) {
166+
switch obj := obj.(type) {
168167
// All named types are type declarations
169168
case *types.TypeName:
170169
named, ok := obj.Type().(*types.Named)
@@ -175,7 +174,7 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
175174
case *types.Struct:
176175
// type <Name> struct
177176
// Structs are obvious.
178-
st := obj.Type().Underlying().(*types.Struct)
177+
st, _ := obj.Type().Underlying().(*types.Struct)
179178
codeBlock, err := g.buildStruct(obj, st)
180179
if err != nil {
181180
return nil, xerrors.Errorf("generate %q: %w", obj.Name())
@@ -188,14 +187,11 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
188187
}
189188
case *types.Var:
190189
// TODO: Are any enums var declarations? This is also codersdk.Me.
191-
v := obj.(*types.Var)
192-
var _ = v
193190
case *types.Const:
194-
c := obj.(*types.Const)
195191
// We only care about named constant types, since they are enums
196-
if named, ok := c.Type().(*types.Named); ok {
192+
if named, ok := obj.Type().(*types.Named); ok {
197193
name := named.Obj().Name()
198-
enumConsts[name] = append(enumConsts[name], c)
194+
enumConsts[name] = append(enumConsts[name], obj)
199195
}
200196
case *types.Func:
201197
// Noop
@@ -214,8 +210,8 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
214210
values = append(values, elem.Val().String())
215211
}
216212
var s strings.Builder
217-
s.WriteString(g.posLine(v))
218-
s.WriteString(fmt.Sprintf("export type %s = %s\n",
213+
_, _ = s.WriteString(g.posLine(v))
214+
_, _ = s.WriteString(fmt.Sprintf("export type %s = %s\n",
219215
name, strings.Join(values, " | "),
220216
))
221217

@@ -240,9 +236,9 @@ func (g *Generator) posLine(obj types.Object) string {
240236
// buildStruct just prints the typescript def for a type.
241237
func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, error) {
242238
var s strings.Builder
243-
s.WriteString(g.posLine(obj))
239+
_, _ = s.WriteString(g.posLine(obj))
244240

245-
s.WriteString(fmt.Sprintf("export interface %s {\n", obj.Name()))
241+
_, _ = s.WriteString(fmt.Sprintf("export interface %s {\n", obj.Name()))
246242
// For each field in the struct, we print 1 line of the typescript interface
247243
for i := 0; i < st.NumFields(); i++ {
248244
field := st.Field(i)
@@ -273,15 +269,15 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
273269
}
274270

275271
if tsType.Comment != "" {
276-
s.WriteString(fmt.Sprintf("%s// %s\n", indent, tsType.Comment))
272+
_, _ = s.WriteString(fmt.Sprintf("%s// %s\n", indent, tsType.Comment))
277273
}
278274
optional := ""
279275
if tsType.Optional {
280276
optional = "?"
281277
}
282-
s.WriteString(fmt.Sprintf("%sreadonly %s%s: %s\n", indent, jsonName, optional, tsType.ValueType))
278+
_, _ = s.WriteString(fmt.Sprintf("%sreadonly %s%s: %s\n", indent, jsonName, optional, tsType.ValueType))
283279
}
284-
s.WriteString("}\n")
280+
_, _ = s.WriteString("}\n")
285281
return s.String(), nil
286282
}
287283

@@ -297,9 +293,9 @@ type TypescriptType struct {
297293
// Eg:
298294
// []byte returns "string"
299295
func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
300-
switch ty.(type) {
296+
switch ty := ty.(type) {
301297
case *types.Basic:
302-
bs := ty.(*types.Basic)
298+
bs := ty
303299
// All basic literals (string, bool, int, etc).
304300
switch {
305301
case bs.Info()&types.IsNumeric > 0:
@@ -323,7 +319,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
323319
return TypescriptType{ValueType: "any", Comment: "Embedded struct, please fix by naming it"}, nil
324320
case *types.Map:
325321
// map[string][string] -> Record<string, string>
326-
m := ty.(*types.Map)
322+
m := ty
327323
keyType, err := g.typescriptType(m.Key())
328324
if err != nil {
329325
return TypescriptType{}, xerrors.Errorf("map key: %w", err)
@@ -342,7 +338,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
342338
Elem() types.Type
343339
}
344340

345-
arr := ty.(hasElem)
341+
arr, _ := ty.(hasElem)
346342
switch {
347343
// When type checking here, just use the string. You can cast it
348344
// to a types.Basic and get the kind if you want too :shrug:
@@ -359,7 +355,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
359355
return TypescriptType{ValueType: underlying.ValueType + "[]", Comment: underlying.Comment}, nil
360356
}
361357
case *types.Named:
362-
n := ty.(*types.Named)
358+
n := ty
363359
// First see if the type is defined elsewhere. If it is, we can just
364360
// put the name as it will be defined in the typescript codeblock
365361
// we generate.
@@ -397,7 +393,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
397393
return ts, nil
398394
case *types.Pointer:
399395
// Dereference pointers.
400-
pt := ty.(*types.Pointer)
396+
pt := ty
401397
resp, err := g.typescriptType(pt.Elem())
402398
if err != nil {
403399
return TypescriptType{}, xerrors.Errorf("pointer: %w", err)

0 commit comments

Comments
 (0)