-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the gosmopolitan linter (#3458)
- Loading branch information
Showing
16 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package golinters | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/xen0n/gosmopolitan" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
) | ||
|
||
func NewGosmopolitan(s *config.GosmopolitanSettings) *goanalysis.Linter { | ||
a := gosmopolitan.NewAnalyzer() | ||
|
||
cfgMap := map[string]map[string]interface{}{} | ||
if s != nil { | ||
cfgMap[a.Name] = map[string]interface{}{ | ||
"allowtimelocal": s.AllowTimeLocal, | ||
"escapehatches": strings.Join(s.EscapeHatches, ","), | ||
"lookattests": !s.IgnoreTests, | ||
"watchforscripts": strings.Join(s.WatchForScripts, ","), | ||
} | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
a.Name, | ||
a.Doc, | ||
[]*analysis.Analyzer{a}, | ||
cfgMap, | ||
).WithLoadMode(goanalysis.LoadModeTypesInfo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
linters-settings: | ||
gosmopolitan: | ||
allow-time-local: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
linters-settings: | ||
gosmopolitan: | ||
ignore-tests: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
linters-settings: | ||
gosmopolitan: | ||
escape-hatches: | ||
- 'command-line-arguments.A' | ||
- 'command-line-arguments.B' | ||
- 'command-line-arguments.C' | ||
- 'command-line-arguments.D' | ||
- 'fmt.Println' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
linters-settings: | ||
gosmopolitan: | ||
watch-for-scripts: | ||
- Hiragana | ||
- Katakana | ||
- Latin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//golangcitest:args -Egosmopolitan | ||
package testdata | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type col struct { | ||
// struct tag should not get reported | ||
Foo string `gorm:"column:bar;not null;comment:'不应该报告这一行'"` | ||
} | ||
|
||
func main() { | ||
fmt.Println("hello world") | ||
fmt.Println("你好,世界") // want `string literal contains rune in Han script` | ||
fmt.Println("こんにちは、セカイ") | ||
|
||
_ = col{Foo: "hello"} | ||
_ = col{Foo: "你好"} // want `string literal contains rune in Han script` | ||
|
||
x := time.Local // want `usage of time.Local` | ||
_ = time.Now().In(x) | ||
_ = time.Date(2023, 1, 2, 3, 4, 5, 678901234, time.Local) // want `usage of time.Local` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//golangcitest:args -Egosmopolitan | ||
//golangcitest:config_path testdata/configs/gosmopolitan_allow_time_local.yml | ||
//golangcitest:expected_exitcode 0 | ||
package testdata | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
func main() { | ||
_ = time.Local | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//golangcitest:args -Egosmopolitan | ||
//golangcitest:config_path testdata/configs/gosmopolitan_dont_ignore_tests.yml | ||
package testdata | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
func main() { | ||
_ = "开启检查测试文件" // want `string literal contains rune in Han script` | ||
_ = time.Local // want `usage of time.Local` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//golangcitest:args -Egosmopolitan | ||
//golangcitest:config_path testdata/configs/gosmopolitan_escape_hatches.yml | ||
package testdata | ||
|
||
import ( | ||
myAlias "fmt" | ||
) | ||
|
||
type A string | ||
type B = string | ||
type C struct { | ||
foo string | ||
Bar string | ||
} | ||
|
||
func D(fmt string) string { | ||
myAlias.Println(fmt, "测试") | ||
return myAlias.Sprintf("%s 测试", fmt) // want `string literal contains rune in Han script` | ||
} | ||
|
||
type X struct { | ||
baz string | ||
} | ||
|
||
func main() { | ||
_ = A("测试") | ||
_ = string(A(string("测试"))) | ||
_ = B("测试") | ||
_ = C{ | ||
foo: "测试", | ||
Bar: "测试", | ||
} | ||
_ = D("测试") | ||
|
||
_ = &X{ | ||
baz: "测试", // want `string literal contains rune in Han script` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//golangcitest:args -Egosmopolitan | ||
//golangcitest:expected_exitcode 0 | ||
package testdata | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
func main() { | ||
_ = "默认不检查测试文件" | ||
_ = time.Local | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//golangcitest:args -Egosmopolitan | ||
//golangcitest:config_path testdata/configs/gosmopolitan_scripts.yml | ||
package testdata | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
fmt.Println("hello world") // want `string literal contains rune in Latin script` | ||
fmt.Println("should not report this line") //nolint:gosmopolitan | ||
fmt.Println("你好,世界") | ||
fmt.Println("こんにちは、セカイ") // want `string literal contains rune in Hiragana script` | ||
} |