|
| 1 | +package migrate |
| 2 | + |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/golangci/golangci-lint/pkg/commands/internal/migrate/one" |
| 8 | + "github.com/golangci/golangci-lint/pkg/commands/internal/migrate/ptr" |
| 9 | + "github.com/golangci/golangci-lint/pkg/commands/internal/migrate/two" |
| 10 | +) |
| 11 | + |
| 12 | +func toFormatters(old *one.Config) two.Formatters { |
| 13 | + enable, _ := ProcessEffectiveLinters(old.Linters) |
| 14 | + |
| 15 | + formatterNames := onlyFormatterNames(enable) |
| 16 | + |
| 17 | + var paths []string |
| 18 | + if len(formatterNames) != 0 { |
| 19 | + paths = slices.Concat(old.Issues.ExcludeFiles, old.Issues.ExcludeDirs) |
| 20 | + } |
| 21 | + |
| 22 | + return two.Formatters{ |
| 23 | + Enable: formatterNames, |
| 24 | + Settings: two.FormatterSettings{ |
| 25 | + Gci: toGciSettings(old.LintersSettings.Gci), |
| 26 | + GoFmt: toGoFmtSettings(old.LintersSettings.GoFmt), |
| 27 | + GoFumpt: toGoFumptSettings(old.LintersSettings.GoFumpt), |
| 28 | + GoImports: toGoImportsSettings(old.LintersSettings.GoImports), |
| 29 | + }, |
| 30 | + Exclusions: two.FormatterExclusions{ |
| 31 | + Generated: toExclusionGenerated(old.Issues.ExcludeGenerated), |
| 32 | + Paths: paths, |
| 33 | + }, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func toGciSettings(old one.GciSettings) two.GciSettings { |
| 38 | + return two.GciSettings{ |
| 39 | + Sections: old.Sections, |
| 40 | + NoInlineComments: old.NoInlineComments, |
| 41 | + NoPrefixComments: old.NoPrefixComments, |
| 42 | + CustomOrder: old.CustomOrder, |
| 43 | + NoLexOrder: old.CustomOrder, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func toGoFmtSettings(old one.GoFmtSettings) two.GoFmtSettings { |
| 48 | + settings := two.GoFmtSettings{ |
| 49 | + Simplify: old.Simplify, |
| 50 | + } |
| 51 | + |
| 52 | + for _, rule := range old.RewriteRules { |
| 53 | + settings.RewriteRules = append(settings.RewriteRules, two.GoFmtRewriteRule{ |
| 54 | + Pattern: rule.Pattern, |
| 55 | + Replacement: rule.Replacement, |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + return settings |
| 60 | +} |
| 61 | + |
| 62 | +func toGoFumptSettings(old one.GoFumptSettings) two.GoFumptSettings { |
| 63 | + return two.GoFumptSettings{ |
| 64 | + ModulePath: old.ModulePath, |
| 65 | + ExtraRules: old.ExtraRules, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func toGoImportsSettings(old one.GoImportsSettings) two.GoImportsSettings { |
| 70 | + var localPrefixes []string |
| 71 | + |
| 72 | + if ptr.Deref(old.LocalPrefixes) != "" { |
| 73 | + localPrefixes = strings.Split(ptr.Deref(old.LocalPrefixes), ",") |
| 74 | + } |
| 75 | + |
| 76 | + return two.GoImportsSettings{ |
| 77 | + LocalPrefixes: localPrefixes, |
| 78 | + } |
| 79 | +} |
0 commit comments