Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: Explain usage of eslint comments
  • Loading branch information
Emyrk committed Mar 16, 2023
commit 0ba9c0e81e960bb60c28b89708844ee9603450d8
33 changes: 28 additions & 5 deletions scripts/apitypings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,8 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
}
case *types.Struct:
// This handles anonymous structs. This should never happen really.
// If you require this, either change your datastructures, or implement
// anonymous structs here.
// Such as:
// type Name struct {
// Embedded struct {
Expand All @@ -643,7 +645,9 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
ValueType: "any",
AboveTypeLine: fmt.Sprintf("%s\n%s",
indentedComment("Embedded anonymous struct, please fix by naming it"),
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed"),
// Must include an eslint exception, otherwise the typescript linter will complain about
// the usage of an "any" type.
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any"),
),
}, nil
case *types.Map:
Expand Down Expand Up @@ -766,9 +770,14 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {

// If it's a struct, just use the name of the struct type
if _, ok := n.Underlying().(*types.Struct); ok {
// External structs cannot be introspected, as we only parse the codersdk package.
// You can handle your type manually in the switch list above, otherwise "any" will be used.
// An easy way to fix this is to pull your external type into `codersdk` package, then it will
// be known by the generator.
return TypescriptType{ValueType: "any", AboveTypeLine: fmt.Sprintf("%s\n%s",
indentedComment(fmt.Sprintf("Named type %q unknown, using \"any\"", n.String())),
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed"),
// Linter needs to be disabled here, or else it will complain about the "any" type.
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any"),
)}, nil
}

Expand All @@ -789,14 +798,28 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
resp.Optional = true
return resp, nil
case *types.Interface:
// only handle the empty interface for now
// only handle the empty interface (interface{}) for now
intf := ty
if intf.Empty() {
// This field is 'interface{}'. We can't infer any type from 'interface{}'
// so just use "any" as the type.
return TypescriptType{
ValueType: "any",
AboveTypeLine: indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed"),
ValueType: "any",
AboveTypeLine: fmt.Sprintf("%s\n%s",
indentedComment("Empty interface{} type, cannot resolve any type."),
// Linter needs to be disabled here, or else it will complain about the "any" type.
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any"),
),
}, nil
}
// All complex interfaces should be named. So if we get here, that means
// we are using anonymous interfaces. Which is just weird and not supported.
// Example:
// type Foo struct {
// Bar interface {
// Baz() string
// }
// }
return TypescriptType{}, xerrors.New("only empty interface types are supported")
case *types.TypeParam:
_, ok := ty.Underlying().(*types.Interface)
Expand Down
26 changes: 15 additions & 11 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ export type AuditDiff = Record<string, AuditDiffField>

// From codersdk/audit.go
export interface AuditDiffField {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
// Empty interface{} type, cannot resolve any type.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly old?: any
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
// Empty interface{} type, cannot resolve any type.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly new?: any
readonly secret: boolean
}
Expand All @@ -67,7 +69,7 @@ export interface AuditLog {
readonly time: string
readonly organization_id: string
// Named type "net/netip.Addr" unknown, using "any"
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly ip: any
readonly user_agent: string
readonly resource_type: ResourceType
Expand Down Expand Up @@ -357,12 +359,12 @@ export interface DeploymentValues {
readonly disable_password_auth?: boolean
readonly support?: SupportConfig
// Named type "github.com/coder/coder/cli/clibase.Struct[[]github.com/coder/coder/codersdk.GitAuthConfig]" unknown, using "any"
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly git_auth?: any
readonly config?: string
readonly write_config?: boolean
// Named type "github.com/coder/coder/cli/clibase.HostPort" unknown, using "any"
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly address?: any
}

Expand Down Expand Up @@ -441,7 +443,8 @@ export interface License {
readonly id: number
readonly uuid: string
readonly uploaded_at: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
// Empty interface{} type, cannot resolve any type.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly claims: Record<string, any>
}

Expand Down Expand Up @@ -577,15 +580,15 @@ export interface PatchGroupRequest {
export interface PprofConfig {
readonly enable: boolean
// Named type "github.com/coder/coder/cli/clibase.HostPort" unknown, using "any"
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly address: any
}

// From codersdk/deployment.go
export interface PrometheusConfig {
readonly enable: boolean
// Named type "github.com/coder/coder/cli/clibase.HostPort" unknown, using "any"
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly address: any
}

Expand Down Expand Up @@ -670,7 +673,8 @@ export interface Role {
// From codersdk/serversentevents.go
export interface ServerSentEvent {
readonly type: ServerSentEventType
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
// Empty interface{} type, cannot resolve any type.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly data: any
}

Expand All @@ -692,7 +696,7 @@ export interface SessionCountDeploymentStats {
// From codersdk/deployment.go
export interface SupportConfig {
// Named type "github.com/coder/coder/cli/clibase.Struct[[]github.com/coder/coder/codersdk.LinkConfig]" unknown, using "any"
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly links: any
}

Expand All @@ -705,7 +709,7 @@ export interface SwaggerConfig {
export interface TLSConfig {
readonly enable: boolean
// Named type "github.com/coder/coder/cli/clibase.HostPort" unknown, using "any"
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO explain why this is needed
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly address: any
readonly redirect_http: boolean
readonly cert_file: string[]
Expand Down