diff --git a/.docs-deploy-key.pem.enc b/.docs-deploy-key.pem.enc new file mode 100644 index 0000000..17ff416 Binary files /dev/null and b/.docs-deploy-key.pem.enc differ diff --git a/.travis.yml b/.travis.yml index 52aad3f..63135e4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,9 @@ language: go go: 1.11.x node_js: 11.15.0 +before_install: +- openssl aes-256-cbc -K $encrypted_ff954c97a679_key -iv $encrypted_ff954c97a679_iv -in .docs-deploy-key.pem.enc -out .docs-deploy-key -d +- export GA_SERVICE_ACCOUNT_KEY=$(cat .docs-deploy-key) install: make install script: - make build @@ -17,4 +20,4 @@ env: global: - GH_REF: github.com/mgechev/revive.run.git - secure: Bp2/fJOVEor5aj3rNwA3+4/wecCmX2mVQ2pQt1AJ1n+pT6MjKYywswTDT6kzK/Cu1bPcfEGh3a7XKieAhIWVKvchoyYVV8J80F76HGyqgSBuzFXvV0c32zFn2LtxQxvmtCNynjmGAV57dHtsxGmHxkX9u8JIJ4J06E2Eq9nuuflTCf5o5gHtaE7P7hQT2WL/JRJVABHUMa0XzsMuUdRNO0OBXGMm+SqiWEcZetf2Vq3tfo2LL4ula99oTKKspg0iRKiauCZZaRxyZG/V3QiR0Rl9nhTVnb6hx6/RFrxru63Pm1FUaK1gIqEq9EUMpZRTddGW77UPp9GSB81/GaUm+e0GNFjUAL2e59t72wMxCQEOT+835hVbeCjgdksg0IDbn7sR/S+rYbiCyxTuCA/4YNlDoEajl9RMxK4culsq35LnibE1x7L4Q/5blD7HwVSMhA33HSDCC5MINwTdWwsdHYiAvFo0RCi5B0GngMzE6/pJxvYhWV3YhKWrgSmhafV1QO3Qu9dCn6P+7KsEVDbUeA8Yxnugd60kQNh2vG7bdTYKaZ6GhfU12zAM15xd2SSrKl6szSAo64CYOTznNFMBMskpm05SubTW1w+xDQy8vGjIHqb6zntiqUhFhTDd326iRYfrQuhAK53XbU1NUFFOyZa8kCTlSsPWDoSOX68oH9Q= - + - secure: WhbUu6HA4S8quk2bLuU/cuxZPRBAi47iFYuPZOd2zKnPkd8j+FAApeNwdGcYReUJUmmYIJHbSAH8MmlaD2TwMpiGXHm83EXT5TtSL612T0fG8IZ3LNdQ2j+q736IDDt3Z024QixfB4uPnO+Q8no6/nAR4m9kdrhNVOQFIEMPtunrWLeD5v7snF3Nhzr01Djl9G7GJnSPenwpBdIVQwpcYTR9qIUWjeXI/Cosvbz/fzLHla1QNKilV2l7PiKWt6zYsTnDX7Sqfh7WGVpmDganG5LITOmKoJsg13rx52/4cNhOTIuvSikpAC3opfrMW7Q+Lif6GWkVFtgVHRTNY2xBjBGRImFvYhKz5vbv04YrbG5GhRoTjJoJsyoyKlQUg6fKIk19eNRpSirL5Z6xUldquQk9WpeSP9ODfYwReKcGx2g8vUEExcaawmGLvKRjxKjqDPbfB/7DHic198fJwJdeYeyF8TfikuT82o59ppaMiEPVtD2FsMQAccXHUi13pu2uLifqHXqVpWpKNr3SMT8zh6KG96QNr+Wvz7ZBm4cYLrTx2AUvpN/bIsHiwrgENVLHn4a9x+r7DJi3tKLl6EN+ndc8netH7qn7GgvJdI1m+enolQbpdhL/U0yVWhFfMLZAqkZqh5hi1cE9dmuBCJO9cq8R+6OxUWN3N9glY+wcxYk= diff --git a/README.md b/README.md index 4bc968b..77268ab 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ Here's how `revive` is different from `golint`: - [`lorawan-stack`](https://github.com/TheThingsNetwork/lorawan-stack) - The Things Network Stack for LoRaWAN V3 - [`gofight`](https://github.com/appleboy/gofight) - Testing API Handler written in Golang. - [`ggz`](https://github.com/go-ggz/ggz) - An URL shortener service written in Golang +- [`Codeac.io`](https://www.codeac.io?ref=revive) - Automated code review service integrates with GitHub, Bitbucket and GitLab (even self-hosted) and helps you fight technical debt. *Open a PR to add your project*. @@ -53,6 +54,7 @@ Here's how `revive` is different from `golint`: - [revive](#revive) - [Usage](#usage) - [Text Editors](#text-editors) + - [Continuous Integration](#continuous-integration) - [Bazel](#bazel) - [Installation](#installation) - [Command Line Flags](#command-line-flags) @@ -87,6 +89,8 @@ Here's how `revive` is different from `golint`: Since the default behavior of `revive` is compatible with `golint`, without providing any additional flags, the only difference you'd notice is faster execution. +`revive` supports a `-config` flag whose value should correspond to a TOML file describing which rules to use for `revive`'s linting. If not provided, `revive` will try to use a global config file (assumed to be located at `$HOME/revive.toml`). Otherwise, if no configuration TOML file is found then `revive` uses a built-in set of default linting rules. + ### Bazel If you want to use revive with Bazel, take a look at the [rules](https://github.com/atlassian/bazel-tools/tree/master/gorevive) that Atlassian maintains. @@ -108,6 +112,14 @@ call ale#linter#Define('go', { \}) ``` +### GitHub Actions + +- [Revive Action](https://github.com/marketplace/actions/revive-action) with annotation support + +### Continuous Integration + +[Codeac.io](https://www.codeac.io?ref=revive) - Automated code review service integrates with GitHub, Bitbucket and GitLab (even self-hosted) and helps you fight technical debt. Check your [pull-requests](https://www.codeac.io/documentation/pull-requests.html?ref=revive) with [revive](https://www.codeac.io/documentation/revive-configuration.html?ref=revive) automatically. (free for open-source projects) + ### Installation ```bash @@ -118,7 +130,7 @@ go get -u github.com/mgechev/revive `revive` accepts three command line parameters: -- `-config [PATH]` - path to config file in TOML format. +- `-config [PATH]` - path to config file in TOML format, defaults to `$HOME/revive.toml` if present. - `-exclude [PATTERN]` - pattern for files/directories/packages to be excluded for linting. You can specify the files you want to exclude for linting either as package name (i.e. `github.com/mgechev/revive`), list them as individual files (i.e. `file.go`), directories (i.e. `./foo/...`), or any combination of the three. - `-formatter [NAME]` - formatter to be used for the output. The currently available formatters are: @@ -470,17 +482,21 @@ Currently, type checking is enabled by default. If you want to run the linter wi ## Contributors -[mgechev](https://github.com/mgechev) |[chavacava](https://github.com/chavacava) |[xuri](https://github.com/xuri) |[gsamokovarov](https://github.com/gsamokovarov) |[morphy2k](https://github.com/morphy2k) |[tamird](https://github.com/tamird) | +[mgechev](https://github.com/mgechev) |[chavacava](https://github.com/chavacava) |[xuri](https://github.com/xuri) |[gsamokovarov](https://github.com/gsamokovarov) |[morphy2k](https://github.com/morphy2k) |[markelog](https://github.com/markelog) | :---: |:---: |:---: |:---: |:---: |:---: | -[mgechev](https://github.com/mgechev) |[chavacava](https://github.com/chavacava) |[xuri](https://github.com/xuri) |[gsamokovarov](https://github.com/gsamokovarov) |[morphy2k](https://github.com/morphy2k) |[tamird](https://github.com/tamird) | +[mgechev](https://github.com/mgechev) |[chavacava](https://github.com/chavacava) |[xuri](https://github.com/xuri) |[gsamokovarov](https://github.com/gsamokovarov) |[morphy2k](https://github.com/morphy2k) |[markelog](https://github.com/markelog) | -[AragurDEV](https://github.com/AragurDEV) |[yangdiangzb](https://github.com/yangdiangzb) |[jamesmaidment](https://github.com/jamesmaidment) |[mapreal19](https://github.com/mapreal19) |[markelog](https://github.com/markelog) |[pa-m](https://github.com/pa-m) | +[tamird](https://github.com/tamird) |[mapreal19](https://github.com/mapreal19) |[Clivern](https://github.com/Clivern) |[AragurDEV](https://github.com/AragurDEV) |[yangdiangzb](https://github.com/yangdiangzb) |[jamesmaidment](https://github.com/jamesmaidment) | :---: |:---: |:---: |:---: |:---: |:---: | -[AragurDEV](https://github.com/AragurDEV) |[yangdiangzb](https://github.com/yangdiangzb) |[jamesmaidment](https://github.com/jamesmaidment) |[mapreal19](https://github.com/mapreal19) |[markelog](https://github.com/markelog) |[pa-m](https://github.com/pa-m) | +[tamird](https://github.com/tamird) |[mapreal19](https://github.com/mapreal19) |[Clivern](https://github.com/Clivern) |[AragurDEV](https://github.com/AragurDEV) |[yangdiangzb](https://github.com/yangdiangzb) |[jamesmaidment](https://github.com/jamesmaidment) | -[paul-at-start](https://github.com/paul-at-start) |[psapezhko](https://github.com/psapezhko) |[ridvansumset](https://github.com/ridvansumset) |[Jarema](https://github.com/Jarema) |[vkrol](https://github.com/vkrol) |[haya14busa](https://github.com/haya14busa) | +[johnrichardrinehart](https://github.com/johnrichardrinehart) |[michalhisim](https://github.com/michalhisim) |[pa-m](https://github.com/pa-m) |[paul-at-start](https://github.com/paul-at-start) |[weastur](https://github.com/weastur) |[ridvansumset](https://github.com/ridvansumset) | :---: |:---: |:---: |:---: |:---: |:---: | -[paul-at-start](https://github.com/paul-at-start) |[psapezhko](https://github.com/psapezhko) |[ridvansumset](https://github.com/ridvansumset) |[Jarema](https://github.com/Jarema) |[vkrol](https://github.com/vkrol) |[haya14busa](https://github.com/haya14busa) | +[johnrichardrinehart](https://github.com/johnrichardrinehart) |[michalhisim](https://github.com/michalhisim) |[pa-m](https://github.com/pa-m) |[paul-at-start](https://github.com/paul-at-start) |[weastur](https://github.com/weastur) |[ridvansumset](https://github.com/ridvansumset) | + +[Jarema](https://github.com/Jarema) |[vkrol](https://github.com/vkrol) |[haya14busa](https://github.com/haya14busa) | +:---: |:---: |:---: | +[Jarema](https://github.com/Jarema) |[vkrol](https://github.com/vkrol) |[haya14busa](https://github.com/haya14busa) | ## License diff --git a/config.go b/config.go index e6bb1af..508ba6e 100644 --- a/config.go +++ b/config.go @@ -5,9 +5,11 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" "strings" "github.com/mgechev/dots" + "github.com/mitchellh/go-homedir" "github.com/mgechev/revive/formatter" @@ -173,6 +175,18 @@ func getFormatter() lint.Formatter { return formatter } +func buildDefaultConfigPath() string { + var result string + if homeDir, err := homedir.Dir(); err == nil { + result = filepath.Join(homeDir, "revive.toml") + if _, err := os.Stat(result); err != nil { + result = "" + } + } + + return result +} + func defaultConfig() *lint.Config { defaultConfig := lint.Config{ Confidence: 0.0, @@ -233,12 +247,16 @@ func init() { fmt.Println(banner) originalUsage() } + // command line help strings const ( - configUsage = "path to the configuration TOML file (i.e. -config myconf.toml)" + configUsage = "path to the configuration TOML file, defaults to $HOME/revive.toml, if present (i.e. -config myconf.toml)" excludeUsage = "list of globs which specify files to be excluded (i.e. -exclude foo/...)" formatterUsage = "formatter to be used for the output (i.e. -formatter stylish)" ) - flag.StringVar(&configPath, "config", "", configUsage) + + defaultConfigPath := buildDefaultConfigPath() + + flag.StringVar(&configPath, "config", defaultConfigPath, configUsage) flag.Var(&excludePaths, "exclude", excludeUsage) flag.StringVar(&formatterName, "formatter", "", formatterUsage) flag.Parse() diff --git a/fixtures/golint/exported.go b/fixtures/golint/exported.go index fb2880e..1ed5002 100644 --- a/fixtures/golint/exported.go +++ b/fixtures/golint/exported.go @@ -13,3 +13,23 @@ type ( ) type Foo struct{} // MATCH /exported type Foo should have comment or be unexported/ + +// The following cases are no-regression tests for issue 229 + +/* Bar something */ +type Bar struct{} + +/* Toto something */ +func Toto() {} + +/* FirstLetter something */ +const FirstLetter = "A" + +/*Bar2 something */ +type Bar2 struct{} + +/*Toto2 something */ +func Toto2() {} + +/*SecondLetter something */ +const SecondLetter = "B" diff --git a/fixtures/lint-file-header6.go b/fixtures/lint-file-header6.go new file mode 100644 index 0000000..32acceb --- /dev/null +++ b/fixtures/lint-file-header6.go @@ -0,0 +1 @@ +package fixtures // MATCH /the file doesn't have an appropriate header/ diff --git a/fixtures/struct-tag.go b/fixtures/struct-tag.go index b53c883..e4aa074 100644 --- a/fixtures/struct-tag.go +++ b/fixtures/struct-tag.go @@ -20,7 +20,9 @@ type decodeAndValidateRequest struct { OptionalStruct *optionalStruct `json:"optionalStruct,omitempty"` OptionalQuery string `json:"-" querystring:"queryfoo"` optionalQuery string `json:"-" querystring:"queryfoo"` // MATCH /tag on not-exported field optionalQuery/ - + // No-reg test for bug https://github.com/mgechev/revive/issues/208 + Tiret string `json:"-,"` + BadTiret string `json:"other,"` // MATCH /option can not be empty in JSON tag/ } type RangeAllocation struct { diff --git a/go.mod b/go.mod index a069d28..1576033 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,17 @@ module github.com/mgechev/revive +go 1.12 + require ( github.com/BurntSushi/toml v0.3.1 github.com/fatih/color v1.7.0 github.com/fatih/structtag v1.0.0 github.com/mattn/go-colorable v0.1.2 // indirect + github.com/mattn/go-isatty v0.0.9 // indirect github.com/mattn/go-runewidth v0.0.4 // indirect - github.com/mgechev/dots v0.0.0-20190603122614-18fa4c4b71cc + github.com/mgechev/dots v0.0.0-20181228164730-18fa4c4b71cc + github.com/mitchellh/go-homedir v1.1.0 github.com/olekukonko/tablewriter v0.0.1 github.com/pkg/errors v0.8.1 - golang.org/x/sys v0.0.0-20190801053355-cbf593c0f2f3 // indirect - golang.org/x/tools v0.0.0-20190802005412-e9bb7d36c060 + golang.org/x/tools v0.0.0-20190816200558-6889da9d5479 ) diff --git a/go.sum b/go.sum index 9519d8b..dd56e66 100644 --- a/go.sum +++ b/go.sum @@ -1,152 +1,32 @@ -github.com/BurntSushi/toml v0.3.0 h1:e1/Ivsx3Z0FVTV0NSOv/aVgbUWyQuzj7DDnFblkRvsY= -github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/structtag v1.0.0 h1:pTHj65+u3RKWYPSGaU290FpI/dXxTaHdVwVwbcPKmEc= github.com/fatih/structtag v1.0.0/go.mod h1:IKitwq45uXL/yqi5mYghiD3w9H6eTOvI9vnk8tXMphA= -github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= -github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= -github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= -github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4= -github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= -github.com/mgechev/dots v0.0.0-20180605013149-8e09d8ea2757 h1:KTwJ7Lo3KDKMknRYN5JEFRGIM4IkG59QjFFM2mxsMEU= -github.com/mgechev/dots v0.0.0-20180605013149-8e09d8ea2757/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg= -github.com/mgechev/dots v0.0.0-20190603122614-18fa4c4b71cc h1:ErGdrZWM/CrAz0FVwcznlAScsmr2pdSMMPMwFL9TNmw= -github.com/mgechev/dots v0.0.0-20190603122614-18fa4c4b71cc/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg= -github.com/olekukonko/tablewriter v0.0.0-20180912035003-be2c049b30cc h1:rQ1O4ZLYR2xXHXgBCCfIIGnuZ0lidMQw2S5n1oOv+Wg= -github.com/olekukonko/tablewriter v0.0.0-20180912035003-be2c049b30cc/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= +github.com/mgechev/dots v0.0.0-20181228164730-18fa4c4b71cc h1:SQHr6jXnsY5YmRoO7RWDcZjmC3PgwPW/xQ9TYJ/SiRY= +github.com/mgechev/dots v0.0.0-20181228164730-18fa4c4b71cc/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88= github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= -github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= -github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs= -golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190603122648-4c4f7f33c9ed h1:Lf5SX+bXEwoj3Y6Nfu5qtffzOXhPQA9REb2R5W4nGP8= -golang.org/x/sys v0.0.0-20190603122648-4c4f7f33c9ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190606125941-79a91cf218c4 h1:HWs74PINelUuEfUbwnBO+1N52oVhkjuLqpbUmyrFA1s= -golang.org/x/sys v0.0.0-20190606125941-79a91cf218c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190606203914-7fc4e5ec1444 h1:U3H/lfsEYy6ld4rCevWA3QgxHlZLiFUfuBFIEc/Ifyo= -golang.org/x/sys v0.0.0-20190606203914-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190608055321-5b15430b70e3 h1:vzXBGBUiVyR0q7D4t89HsaY0TYhmviZBVOVUyvyMBjU= -golang.org/x/sys v0.0.0-20190608055321-5b15430b70e3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190614215434-b47fdc937951 h1:pGVEuw9fHbMpaZlMbLwssX14J35+8blxkNxbyMcO/qE= -golang.org/x/sys v0.0.0-20190614215434-b47fdc937951/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190616132722-15dcb6c0061f h1:bWC5mWiwVGXbr7P6ugM+hu6QytMFEjbwt+SO2r1M2+o= -golang.org/x/sys v0.0.0-20190616132722-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190619193031-17bc6164aac4 h1:wlKUlj5/boPTVWT2ysl97FAXePcoaV9FNqFOzsNuwpk= -golang.org/x/sys v0.0.0-20190619193031-17bc6164aac4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190621215844-d432491b9138 h1:mOtS9UBYWoZzbGWv3/XJz0cxMw8MTBbNKy3pRvP2Mjk= -golang.org/x/sys v0.0.0-20190621215844-d432491b9138/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190624145334-c5567b49c5d0 h1:z1lZzI4A4GjnM80wHx8Yrjtcr56DRJ6VJnipc0bbG3o= -golang.org/x/sys v0.0.0-20190624145334-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190629205408-04f50cda93cb h1:IeU57h/r0+/v829dDR8bskefuF1Cx4KAxce1Cn0LFvE= -golang.org/x/sys v0.0.0-20190629205408-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190710165549-6ec70d6a5542 h1:PNYRbnC8XRk0bAINQCfprVc169PXbLRG2HI1HXbUEg8= -golang.org/x/sys v0.0.0-20190710165549-6ec70d6a5542/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190712063909-fae7ac547cb7 h1:kj9oKJYzWQH5MvoBhxb9WQn/LqYor42sKTvsrUCCu7g= -golang.org/x/sys v0.0.0-20190712063909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190726004620-94b544f455ef h1:pKfIZaaN+ipVf4Vz//s+8MRZkgZvBsds3BOAAYmXV/Y= -golang.org/x/sys v0.0.0-20190726004620-94b544f455ef/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190726093751-fc99dfbffb4e h1:AhhcJML1FGXN10f3s4/WyoO5/8u7Rfb1ZyB47O8qp+s= -golang.org/x/sys v0.0.0-20190726093751-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190731002446-1393eb018365 h1:skmasgLtLOsII4VBh/TRzNotE2cn3rMyPB9/A67wLDU= -golang.org/x/sys v0.0.0-20190731002446-1393eb018365/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190801053355-cbf593c0f2f3 h1:sL449jA3eITt86CGRvhic5g9AuQEnaqaasKTT+ieDng= -golang.org/x/sys v0.0.0-20190801053355-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20180911133044-677d2ff680c1 h1:dzEuQYa6+a3gROnSlgly5ERUm4SZKJt+dh+4iSbO+bI= -golang.org/x/tools v0.0.0-20180911133044-677d2ff680c1/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190604003109-8aaa1484dc10 h1:VULJh5DQg4Inr1Xiypf4pj72xB4lPJdiUgV2Ra5M8og= -golang.org/x/tools v0.0.0-20190604003109-8aaa1484dc10/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190606052759-4d9ae51c2468 h1:ousb8Vkhj77qYml3n9BuPuYaR6LnZyLh5LXB2qq8wzI= -golang.org/x/tools v0.0.0-20190606052759-4d9ae51c2468/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190614153353-1edc8e83c897 h1:Tcg4xXCU7bSKi8WaBg3N7lJTHwLv45zG+LXHG6WxorM= -golang.org/x/tools v0.0.0-20190614153353-1edc8e83c897/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190614185444-d1d6cdd8a67e h1:bZRjbS2OsoCz4Whw4o7b1Il1obBuaKSM0FXMfFrRut4= -golang.org/x/tools v0.0.0-20190614185444-d1d6cdd8a67e/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190614215004-5aca471b1d59 h1:rBGSbLsDaYExWFDBn79o7jc1xpwfxSofsWJNihAfWto= -golang.org/x/tools v0.0.0-20190614215004-5aca471b1d59/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190617174016-6fea9ef05e7a h1:gdHr3XuwwcWiVcNwFFdaMAXkLC9SGW3+yn63Pol+wL0= -golang.org/x/tools v0.0.0-20190617174016-6fea9ef05e7a/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190617192825-da514acc4774 h1:hL3a3nRZsZm7FQGehfRB1w5y8mbrLyMSAgki4j0z3I0= -golang.org/x/tools v0.0.0-20190617192825-da514acc4774/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190618173119-fdf1049a943a h1:UYXkp2zG6yfcsx4WdMshPebv30sUsSXURy1nWiwy0oc= -golang.org/x/tools v0.0.0-20190618173119-fdf1049a943a/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190623014052-6e04913cbbac h1:p4rf0zoHymrHSlKS+xG2PhJOtxOEWC9nu4NamWffoY0= -golang.org/x/tools v0.0.0-20190623014052-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190624225754-a101b041ded4 h1:3Yme/SSFE6mHkvIIhjjiTrlmOsy6m0aNnyTDZPYJIy4= -golang.org/x/tools v0.0.0-20190624225754-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190625183255-252024b82959 h1:icB3Kx/bolKriEovlxmFvvEBA1b24q6lQkwGLiO7Kgo= -golang.org/x/tools v0.0.0-20190625183255-252024b82959/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190628234000-fb37f6ba8261 h1:D/pylchHpGxucPHRiQsXXfyk9R4ywTUoH6FaZx0R0Q0= -golang.org/x/tools v0.0.0-20190628234000-fb37f6ba8261/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190701204812-38ae2c8f6412 h1:eiOZ97iLROr9dsoElIcgpaBTnypaUM6FklHaTH1WCUQ= -golang.org/x/tools v0.0.0-20190701204812-38ae2c8f6412/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190702152726-7e72c71c505f h1:XEee/4s6OPmxKNwnku3HeR9yvHS+DfiMUBnZRvGYKsc= -golang.org/x/tools v0.0.0-20190702152726-7e72c71c505f/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190702203059-44aeb8b7c377 h1:ImzDLDLqRaKKO5JeKsrJF73YTtC/UGJYXQfnhCykd1U= -golang.org/x/tools v0.0.0-20190702203059-44aeb8b7c377/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190703183741-063514c48b26 h1:RSQtTb58BibjAu/zvYqkvmSd3hCVrxUHtxtJgdmlo8U= -golang.org/x/tools v0.0.0-20190703183741-063514c48b26/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190708153032-60762fc531e6 h1:+09ssZLX5m87DAU4ovA5R2dT8+rxQJOSrnvJ08dNOvI= -golang.org/x/tools v0.0.0-20190708153032-60762fc531e6/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190708212629-c8855242db9c h1:61VlnXlxJ8ZQ0ScNJLbv0lMvy2zxMOMtmqgczqTjoSw= -golang.org/x/tools v0.0.0-20190708212629-c8855242db9c/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190709205837-f82b303b69d7 h1:POOqS8nIAlVjiggJspEagLTIm7FecjZ3zjL+UXqxaeo= -golang.org/x/tools v0.0.0-20190709205837-f82b303b69d7/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190709213823-7b25e351ac0e h1:KLE9RrAWtdmIWsb/E0ljAvXMM5lZf4Gtr3HYCEE4FIE= -golang.org/x/tools v0.0.0-20190709213823-7b25e351ac0e/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190710195507-286818132824 h1:2492BvYnJkin59SmTIFBH3mlkrNxeIKPJuM3GrK/gYo= -golang.org/x/tools v0.0.0-20190710195507-286818132824/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190712214651-8b927904ee0d h1:KlQeLUDWg+cWfoNyo2obro62ANg9Ain//HXb8aKT2jg= -golang.org/x/tools v0.0.0-20190712214651-8b927904ee0d/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190715053316-607ca053a137 h1:JD/hG/8Cy1uTpUou5cTdh88WzXO985+ykk+/kD2B6Og= -golang.org/x/tools v0.0.0-20190715053316-607ca053a137/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190715174000-9e48ab1d90cd h1:WtY90UqL9QgrhtGZXkSuv3VgN9O2zchNuw7mCsy5EAo= -golang.org/x/tools v0.0.0-20190715174000-9e48ab1d90cd/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190715222530-9b2cb0e5f602 h1:aPQ4URfBa5qpEffv7JQWB643/X9MIooR7AmSM4PhjbQ= -golang.org/x/tools v0.0.0-20190715222530-9b2cb0e5f602/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190716023037-fefcef05abb1 h1:XTh+ATqTLtxlslz1/eewT2lc16Tn566b6TuHLllcamo= -golang.org/x/tools v0.0.0-20190716023037-fefcef05abb1/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190716155013-919acb9f1ffd h1:awSISWvdxEIWPMBRrxodeas3r6hNs3R7fSCAKsN+2eg= -golang.org/x/tools v0.0.0-20190716155013-919acb9f1ffd/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190716192744-0b5a7f81db50 h1:MjNb67gYViBJS2G0qlnUyUvz01HpvT0hWbRK6+CVeB4= -golang.org/x/tools v0.0.0-20190716192744-0b5a7f81db50/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190719014327-e377ae9d6386 h1:XYHge1yfBqxAoAUYFNfgQBUM/pLA4cVgBCxSVP4SFM8= -golang.org/x/tools v0.0.0-20190719014327-e377ae9d6386/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190723023520-8bb11ff117ca h1:jxUITcp9Zck5mo94NnK9nxKhcrCHHu3Uxn3zT/Ab6GQ= -golang.org/x/tools v0.0.0-20190723023520-8bb11ff117ca/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190724192812-8aa4eac1a7c1 h1:fUhbXHMxzVzne8fm8LoGbgb29YxdSR6sXVOA6M1+79c= -golang.org/x/tools v0.0.0-20190724192812-8aa4eac1a7c1/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190725162026-2e34cfcb95cb h1:nCidYN5rpBq6eOoKgsg8OPyARFROWoopPyENOA1AADU= -golang.org/x/tools v0.0.0-20190725162026-2e34cfcb95cb/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190727014933-1bd56024c620 h1:V0R1y3ny4Qty3oJpgixfc7BF1GqcBtG492UJDH4XNi4= -golang.org/x/tools v0.0.0-20190727014933-1bd56024c620/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190727174842-db2fa46ec33c h1:jOixaG4f/xPYap0HSnwlewCOTxw7qq5hqxvCyw5Kfvg= -golang.org/x/tools v0.0.0-20190727174842-db2fa46ec33c/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190728085240-fc6e2057e7f6 h1:HwL5zE73Lj6ca2l3QvmH+z+mMQJp6w6/7zcxRJcGNRw= -golang.org/x/tools v0.0.0-20190728085240-fc6e2057e7f6/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190729094940-ff9f1409240a h1:s5iVUvQeQogtT5N2UsrWTExwB/vujdgo2oRIoKqGHP0= -golang.org/x/tools v0.0.0-20190729094940-ff9f1409240a/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190730215743-ed3277de2799 h1:QDcqPiPMwhOSeNS/65AYJQxAYb2HIYLFw6NRbGQUtSU= -golang.org/x/tools v0.0.0-20190730215743-ed3277de2799/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190731224408-1e85ed8060aa h1:crSxYKs/4ckOznRS/ORDRJikUnqR5pcExM2Q9fNghbw= -golang.org/x/tools v0.0.0-20190731224408-1e85ed8060aa/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= -golang.org/x/tools v0.0.0-20190802005412-e9bb7d36c060 h1:ixdNsWPlaysKGPShUO+OF5eF/dSQOGTB86mRds+STrY= -golang.org/x/tools v0.0.0-20190802005412-e9bb7d36c060/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= +golang.org/x/tools v0.0.0-20190816200558-6889da9d5479 h1:lfN2PY/jymfnxkNHlbBF5DwPsUvhqUnrdgfK01iH2s0= +golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/rule/exported.go b/rule/exported.go index 714acf6..b68f2ba 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -85,7 +85,7 @@ func (w *lintExported) lintFuncDoc(fn *ast.FuncDecl) { }) return } - s := fn.Doc.Text() + s := normalizeText(fn.Doc.Text()) prefix := fn.Name.Name + " " if !strings.HasPrefix(s, prefix) { w.onFailure(lint.Failure{ @@ -141,7 +141,7 @@ func (w *lintExported) lintTypeDoc(t *ast.TypeSpec, doc *ast.CommentGroup) { return } - s := doc.Text() + s := normalizeText(doc.Text()) articles := [...]string{"A", "An", "The", "This"} for _, a := range articles { if t.Name.Name == a { @@ -217,7 +217,8 @@ func (w *lintExported) lintValueSpecDoc(vs *ast.ValueSpec, gd *ast.GenDecl, genD doc = gd.Doc } prefix := name + " " - if !strings.HasPrefix(doc.Text(), prefix) { + s := normalizeText(doc.Text()) + if !strings.HasPrefix(s, prefix) { w.onFailure(lint.Failure{ Confidence: 1, Node: doc, @@ -227,6 +228,14 @@ func (w *lintExported) lintValueSpecDoc(vs *ast.ValueSpec, gd *ast.GenDecl, genD } } +// normalizeText is a helper function that normalizes comment strings by: +// * removing one leading space +// +// This function is needed because ast.CommentGroup.Text() does not handle //-style and /*-style comments uniformly +func normalizeText(t string) string { + return strings.TrimPrefix(t, " ") +} + func (w *lintExported) Visit(n ast.Node) ast.Visitor { switch v := n.(type) { case *ast.GenDecl: diff --git a/rule/file-header.go b/rule/file-header.go index 9b4fbc3..6df974e 100644 --- a/rule/file-header.go +++ b/rule/file-header.go @@ -1,7 +1,6 @@ package rule import ( - "go/ast" "regexp" "github.com/mgechev/revive/lint" @@ -10,13 +9,47 @@ import ( // FileHeaderRule lints given else constructs. type FileHeaderRule struct{} +var ( + multiRegexp = regexp.MustCompile("^/\\*") + singleRegexp = regexp.MustCompile("^//") +) + // Apply applies the rule to given file. func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - var failures []lint.Failure + if len(arguments) != 1 { + panic(`invalid configuration for "file-header" rule`) + } header, ok := arguments[0].(string) if !ok { - panic("Invalid argument to the FileHeaderRule") + panic(`invalid argument for "file-header" rule: first argument should be a string`) + } + + failure := []lint.Failure{ + { + Node: file.AST, + Confidence: 1, + Failure: "the file doesn't have an appropriate header", + }, + } + + if len(file.AST.Comments) == 0 { + return failure + } + + g := file.AST.Comments[0] + if g == nil { + return failure + } + comment := "" + for _, c := range g.List { + text := c.Text + if multiRegexp.Match([]byte(text)) { + text = text[2 : len(text)-2] + } else if singleRegexp.Match([]byte(text)) { + text = text[2:] + } + comment += text } regex, err := regexp.Compile(header) @@ -24,59 +57,13 @@ func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint panic(err.Error()) } - fileAst := file.AST - walker := lintFileHeader{ - file: file, - fileAst: fileAst, - regex: regex, - onFailure: func(failure lint.Failure) { - failures = append(failures, failure) - }, + if !regex.Match([]byte(comment)) { + return failure } - - ast.Walk(walker, fileAst) - - return failures + return nil } // Name returns the rule name. func (r *FileHeaderRule) Name() string { return "file-header" } - -type lintFileHeader struct { - file *lint.File - fileAst *ast.File - regex *regexp.Regexp - onFailure func(lint.Failure) -} - -func (w lintFileHeader) Visit(_ ast.Node) ast.Visitor { - g := w.fileAst.Comments[0] - failure := lint.Failure{ - Node: w.fileAst, - Confidence: 1, - Failure: "the file doesn't have an appropriate header", - } - if g == nil { - w.onFailure(failure) - return nil - } - multi := regexp.MustCompile("^/\\*") - single := regexp.MustCompile("^//") - comment := "" - for _, c := range g.List { - text := c.Text - if multi.Match([]byte(text)) { - text = text[2 : len(text)-2] - } else if single.Match([]byte(text)) { - text = text[2:] - } - comment += text - } - - if !w.regex.Match([]byte(comment)) { - w.onFailure(failure) - } - return nil -} diff --git a/rule/struct-tag.go b/rule/struct-tag.go index 8335e0d..57cf810 100644 --- a/rule/struct-tag.go +++ b/rule/struct-tag.go @@ -86,7 +86,7 @@ func (w lintStructTagRule) checkTaggedField(f *ast.Field) { w.addFailure(f.Tag, "field's type and default value's type mismatch") } case "json": - msg, ok := w.checkJSONTag(tag.Options) + msg, ok := w.checkJSONTag(tag.Name, tag.Options) if !ok { w.addFailure(f.Tag, msg) } @@ -161,10 +161,15 @@ func (w lintStructTagRule) checkBSONTag(options []string) (string, bool) { return "", true } -func (w lintStructTagRule) checkJSONTag(options []string) (string, bool) { +func (w lintStructTagRule) checkJSONTag(name string, options []string) (string, bool) { for _, opt := range options { switch opt { case "omitempty", "string": + case "": + // special case for JSON key "-" + if name != "-" { + return "option can not be empty in JSON tag", false + } default: return fmt.Sprintf("unknown option '%s' in JSON tag", opt), false } diff --git a/test/file-header_test.go b/test/file-header_test.go index b92d05a..a8cbcba 100644 --- a/test/file-header_test.go +++ b/test/file-header_test.go @@ -27,4 +27,17 @@ func TestLintFileHeader(t *testing.T) { testRule(t, "lint-file-header5", &rule.FileHeaderRule{}, &lint.RuleConfig{ Arguments: []interface{}{"^\\sfoo.*bar$"}, }) + + testRule(t, "lint-file-header6", &rule.FileHeaderRule{}, &lint.RuleConfig{ + Arguments: []interface{}{"foobar"}, + }) +} + +func BenchmarkLintFileHeader(b *testing.B) { + var t *testing.T + for i := 0; i <= b.N; i++ { + testRule(t, "lint-file-header1", &rule.FileHeaderRule{}, &lint.RuleConfig{ + Arguments: []interface{}{"foobar"}, + }) + } }