Skip to content

Commit

Permalink
chore: duplicate linter structure to avoid side-effect of the seriali…
Browse files Browse the repository at this point in the history
…zation
  • Loading branch information
ldez committed Mar 5, 2024
1 parent 4370aa6 commit 0a4585f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 22 deletions.
30 changes: 15 additions & 15 deletions pkg/lint/linter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ const (
const LastLinter = "nolintlint"

type Deprecation struct {
Since string `json:"since,omitempty"`
Message string `json:"message,omitempty"`
Replacement string `json:"replacement,omitempty"`
Since string
Message string
Replacement string
}

type Config struct {
Linter Linter `json:"-"`
EnabledByDefault bool `json:"enabledByDefault,omitempty"`
Linter Linter
EnabledByDefault bool

LoadMode packages.LoadMode `json:"loadMode,omitempty"`
LoadMode packages.LoadMode

InPresets []string `json:"inPresets,omitempty"`
AlternativeNames []string `json:"alternativeNames,omitempty"`
InPresets []string
AlternativeNames []string

OriginalURL string `json:"originalURL,omitempty"` // URL of original (not forked) repo, needed for autogenerated README
Internal bool `json:"internal"` // Internal linters cannot be disabled (ex: typecheck).
CanAutoFix bool `json:"canAutoFix,omitempty"`
IsSlow bool `json:"isSlow"`
DoesChangeTypes bool `json:"doesChangeTypes,omitempty"`
OriginalURL string // URL of original (not forked) repo, needed for autogenerated README
Internal bool // Internal linters cannot be disabled (ex: typecheck).
CanAutoFix bool
IsSlow bool
DoesChangeTypes bool

Since string `json:"since,omitempty"`
Deprecation *Deprecation `json:"deprecation,omitempty"`
Since string
Deprecation *Deprecation
}

func (lc *Config) WithEnabledByDefault() *Config {
Expand Down
25 changes: 24 additions & 1 deletion scripts/website/dump_info/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,30 @@ func main() {

var wraps []types.LinterWrapper
for _, l := range linters {
wraps = append(wraps, types.LinterWrapper{Config: l, Name: l.Linter.Name(), Desc: l.Linter.Desc()})
wrapper := types.LinterWrapper{
Name: l.Linter.Name(),
Desc: l.Linter.Desc(),
EnabledByDefault: l.EnabledByDefault,
LoadMode: l.LoadMode,
InPresets: l.InPresets,
AlternativeNames: l.AlternativeNames,
OriginalURL: l.OriginalURL,
Internal: l.Internal,
CanAutoFix: l.CanAutoFix,
IsSlow: l.IsSlow,
DoesChangeTypes: l.DoesChangeTypes,
Since: l.Since,
}

if l.Deprecation != nil {
wrapper.Deprecation = &types.Deprecation{
Since: l.Deprecation.Since,
Message: l.Deprecation.Message,
Replacement: l.Deprecation.Replacement,
}
}

wraps = append(wraps, wrapper)
}

err := saveToJSONFile(filepath.Join("assets", "linters-info.json"), wraps)
Expand Down
4 changes: 2 additions & 2 deletions scripts/website/expand_templates/linters.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func getName(lc *types.LinterWrapper) string {
name = fmt.Sprintf("%s [%s](#%s)", name, spanWithID(listItemPrefix+lc.Name, "Configuration", "⚙️"), lc.Name)
}

if !lc.IsDeprecated() {
if lc.Deprecation == nil {
return name
}

Expand All @@ -89,7 +89,7 @@ func check(b bool, title string) string {

func getDesc(lc *types.LinterWrapper) string {
desc := lc.Desc
if lc.IsDeprecated() {
if lc.Deprecation == nil {
desc = lc.Deprecation.Message
if lc.Deprecation.Replacement != "" {
desc += fmt.Sprintf(" Replaced by %s.", lc.Deprecation.Replacement)
Expand Down
33 changes: 29 additions & 4 deletions scripts/website/types/types.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
package types

import "github.com/golangci/golangci-lint/pkg/lint/linter"
import (
"golang.org/x/tools/go/packages"
)

type CLIHelp struct {
Enable string `json:"enable"`
Disable string `json:"disable"`
Help string `json:"help"`
}

type Deprecation struct {
Since string `json:"since,omitempty"`
Message string `json:"message,omitempty"`
Replacement string `json:"replacement,omitempty"`
}

// LinterWrapper same fields but with struct tags.
// The field Name and Desc are added to have the information about the linter.
// The field Linter is removed (not serializable).
type LinterWrapper struct {
*linter.Config
Name string `json:"name"` // From linter.
Desc string `json:"desc"` // From linter.

EnabledByDefault bool `json:"enabledByDefault,omitempty"`

LoadMode packages.LoadMode `json:"loadMode,omitempty"`

InPresets []string `json:"inPresets,omitempty"`
AlternativeNames []string `json:"alternativeNames,omitempty"`

OriginalURL string `json:"originalURL,omitempty"` // URL of original (not forked) repo, needed for autogenerated README
Internal bool `json:"internal"` // Internal linters cannot be disabled (ex: typecheck).
CanAutoFix bool `json:"canAutoFix,omitempty"`
IsSlow bool `json:"isSlow"`
DoesChangeTypes bool `json:"doesChangeTypes,omitempty"`

Name string `json:"name"`
Desc string `json:"desc"`
Since string `json:"since,omitempty"`
Deprecation *Deprecation `json:"deprecation,omitempty"`
}

0 comments on commit 0a4585f

Please sign in to comment.