Skip to content

Commit b3100dc

Browse files
committed
feat: add the gosmopolitan linter
1 parent 271a55d commit b3100dc

16 files changed

+218
-0
lines changed

.golangci.reference.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,38 @@ linters-settings:
940940
# Default: "0600"
941941
G306: "0600"
942942

943+
gosmopolitan:
944+
# Allow and ignore `time.Local` usages.
945+
#
946+
# Default: false
947+
allow-time-local: false
948+
# List of fully qualified names in the `(full/pkg/path).name` form, to act
949+
# as "i18n escape hatches".
950+
#
951+
# String literals inside call-like expressions to, or struct literals of
952+
# those names, are exempt from the writing system check.
953+
#
954+
# Default: []
955+
escape-hatches:
956+
- '(github.com/nicksnyder/go-i18n/v2/i18n).Message'
957+
- '(example.com/your/project/i18n/markers).Raw'
958+
- '(example.com/your/project/i18n/markers).OK'
959+
- '(example.com/your/project/i18n/markers).TODO'
960+
# Ignore test files.
961+
#
962+
# Default: true
963+
ignore-tests: true
964+
# List of Unicode scripts to watch for any usage in string literals.
965+
# If empty the default of ["Han"] will be used.
966+
#
967+
# Default: ["Han"]
968+
watch-for-scripts:
969+
- Devanagari
970+
- Han
971+
- Hangul
972+
- Hiragana
973+
- Katakana
974+
943975
govet:
944976
# Report about shadowed variables.
945977
# Default: false
@@ -2023,6 +2055,7 @@ linters:
20232055
- goprintffuncname
20242056
- gosec
20252057
- gosimple
2058+
- gosmopolitan
20262059
- govet
20272060
- grouper
20282061
- ifshort
@@ -2131,6 +2164,7 @@ linters:
21312164
- goprintffuncname
21322165
- gosec
21332166
- gosimple
2167+
- gosmopolitan
21342168
- govet
21352169
- grouper
21362170
- ifshort

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ require (
105105
github.com/ultraware/whitespace v0.0.5
106106
github.com/uudashr/gocognit v1.0.6
107107
github.com/valyala/quicktemplate v1.7.0
108+
github.com/xen0n/gosmopolitan v1.1.1
108109
github.com/yagipy/maintidx v1.0.0
109110
github.com/yeya24/promlinter v0.2.0
110111
gitlab.com/bosi/decorder v0.2.3

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ var defaultLintersSettings = LintersSettings{
6161
Gosec: GoSecSettings{
6262
Concurrency: runtime.NumCPU(),
6363
},
64+
Gosmopolitan: GosmopolitanSettings{
65+
AllowTimeLocal: false,
66+
EscapeHatches: []string{},
67+
IgnoreTests: true,
68+
WatchForScripts: []string{"Han"},
69+
},
6470
Ifshort: IfshortSettings{
6571
MaxDeclLines: 1,
6672
MaxDeclChars: 30,
@@ -167,6 +173,7 @@ type LintersSettings struct {
167173
Gomodguard GoModGuardSettings
168174
Gosec GoSecSettings
169175
Gosimple StaticCheckSettings
176+
Gosmopolitan GosmopolitanSettings
170177
Govet GovetSettings
171178
Grouper GrouperSettings
172179
Ifshort IfshortSettings
@@ -446,6 +453,13 @@ type GoSecSettings struct {
446453
Concurrency int `mapstructure:"concurrency"`
447454
}
448455

456+
type GosmopolitanSettings struct {
457+
AllowTimeLocal bool `mapstructure:"allow-time-local"`
458+
EscapeHatches []string `mapstructure:"escape-hatches"`
459+
IgnoreTests bool `mapstructure:"ignore-tests"`
460+
WatchForScripts []string `mapstructure:"watch-for-scripts"`
461+
}
462+
449463
type GovetSettings struct {
450464
Go string `mapstructure:"-"`
451465
CheckShadowing bool `mapstructure:"check-shadowing"`

pkg/golinters/gosmopolitan.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package golinters
2+
3+
import (
4+
"strings"
5+
6+
"github.com/xen0n/gosmopolitan"
7+
"golang.org/x/tools/go/analysis"
8+
9+
"github.com/golangci/golangci-lint/pkg/config"
10+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
11+
)
12+
13+
func NewGosmopolitan(s *config.GosmopolitanSettings) *goanalysis.Linter {
14+
a := gosmopolitan.NewAnalyzer()
15+
16+
cfgMap := map[string]map[string]interface{}{}
17+
if s != nil {
18+
cfgMap[a.Name] = map[string]interface{}{
19+
"allowtimelocal": s.AllowTimeLocal,
20+
"escapehatches": strings.Join(s.EscapeHatches, ","),
21+
"lookattests": !s.IgnoreTests,
22+
"watchforscripts": strings.Join(s.WatchForScripts, ","),
23+
}
24+
}
25+
26+
return goanalysis.NewLinter(
27+
a.Name,
28+
a.Doc,
29+
[]*analysis.Analyzer{a},
30+
cfgMap,
31+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
32+
}

pkg/lint/lintersdb/manager.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
135135
gomodguardCfg *config.GoModGuardSettings
136136
gosecCfg *config.GoSecSettings
137137
gosimpleCfg *config.StaticCheckSettings
138+
gosmopolitanCfg *config.GosmopolitanSettings
138139
govetCfg *config.GovetSettings
139140
grouperCfg *config.GrouperSettings
140141
ifshortCfg *config.IfshortSettings
@@ -212,6 +213,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
212213
gomodguardCfg = &m.cfg.LintersSettings.Gomodguard
213214
gosecCfg = &m.cfg.LintersSettings.Gosec
214215
gosimpleCfg = &m.cfg.LintersSettings.Gosimple
216+
gosmopolitanCfg = &m.cfg.LintersSettings.Gosmopolitan
215217
govetCfg = &m.cfg.LintersSettings.Govet
216218
grouperCfg = &m.cfg.LintersSettings.Grouper
217219
ifshortCfg = &m.cfg.LintersSettings.Ifshort
@@ -550,6 +552,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
550552
WithAlternativeNames(megacheckName).
551553
WithURL("https://github.com/dominikh/go-tools/tree/master/simple"),
552554

555+
linter.NewConfig(golinters.NewGosmopolitan(gosmopolitanCfg)).
556+
WithSince("v1.51.0").
557+
WithLoadForGoAnalysis().
558+
WithPresets(linter.PresetBugs).
559+
WithURL("https://github.com/xen0n/gosmopolitan"),
560+
553561
linter.NewConfig(golinters.NewGovet(govetCfg)).
554562
WithSince("v1.0.0").
555563
WithLoadForGoAnalysis().
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
gosmopolitan:
3+
allow-time-local: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
gosmopolitan:
3+
ignore-tests: false
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
linters-settings:
2+
gosmopolitan:
3+
escape-hatches:
4+
- '(command-line-arguments).A'
5+
- '(command-line-arguments).B'
6+
- '(command-line-arguments).C'
7+
- '(command-line-arguments).D'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
linters-settings:
2+
gosmopolitan:
3+
watch-for-scripts:
4+
- Hiragana
5+
- Katakana
6+
- Latin

0 commit comments

Comments
 (0)