2018-06-02 10:14:21 -07:00
# Developer's Guide
This document explains how to build, test, and develop features for revive.
## Installation
2022-10-04 10:26:40 +02:00
Clone the project:
2018-06-02 10:14:21 -07:00
2025-05-21 23:00:07 +03:00
```bash
2022-10-04 10:26:40 +02:00
git clone git@github .com:mgechev/revive.git
cd revive
2018-06-02 10:14:21 -07:00
```
2025-05-21 23:00:07 +03:00
2018-06-02 10:14:21 -07:00
## Build
In order to build the project run:
2025-05-21 23:00:07 +03:00
2018-06-02 10:14:21 -07:00
```bash
make build
```
The command will produce the `revive` binary in the root of the project.
2025-03-25 09:47:59 -07:00
## Debug
To enable debug logging, set the `DEBUG` environment variable:
```sh
DEBUG=1 go run main.go
```
This will output debug information to `stderr` and to the log file `revive.log` created in the current working directory.
2025-07-25 13:11:08 +03:00
## Coding standards
Follow [the instructions ](.github/instructions/ ) which contain Go coding standards and conventions used by both humans and GitHub Copilot.
2018-06-02 10:14:21 -07:00
## Development of rules
If you want to develop a new rule, follow as an example the already existing rules in the [rule package ](https://github.com/mgechev/revive/tree/master/rule ).
2025-05-21 23:00:07 +03:00
Each rule needs to implement the `lint.Rule` interface:
2018-06-02 10:14:21 -07:00
```go
type Rule interface {
Name() string
Apply(*File, Arguments) []Failure
}
```
2025-05-21 23:00:07 +03:00
2025-01-23 09:15:44 +01:00
All rules with a configuration must implement `lint.ConfigurableRule` interface:
2025-05-21 23:00:07 +03:00
2025-01-23 09:15:44 +01:00
```go
type ConfigurableRule interface {
Configure(Arguments) error
}
```
The `Arguments` type is an alias of the type `[]any` . The arguments of the rule are passed from the configuration file.
2025-05-21 23:00:07 +03:00
### Example
2025-01-23 09:15:44 +01:00
2025-05-21 23:00:07 +03:00
Let's suppose we have developed a rule called `BanStructNameRule` which disallow us to name a structure with a given identifier.
We can set the banned identifier by using the TOML configuration file:
2025-01-23 09:15:44 +01:00
```toml
[rule.ban-struct-name]
2025-05-24 22:29:40 +03:00
arguments = ["Foo"]
2025-01-23 09:15:44 +01:00
```
With the snippet above we:
- Enable the rule with the name `ban-struct-name` . The `Name()` method of our rule should return a string that matches `ban-struct-name` .
2025-05-21 23:00:07 +03:00
- Configure the rule with the argument `Foo` .
The list of arguments will be passed to `Apply(*File, Arguments)` together with the target file we're linting currently.
2025-01-23 09:15:44 +01:00
2025-05-21 23:00:07 +03:00
A sample rule implementation can be [found here ](/rule/argument_limit.go ).
2018-06-02 10:14:21 -07:00
## Development of formatters
If you want to develop a new formatter, follow as an example the already existing formatters in the [formatter package ](https://github.com/mgechev/revive/tree/master/formatter ).
All formatters should implement the following interface:
```go
type Formatter interface {
Format(< -chan Failure , RulesConfig ) ( string , error )
Name() string
}
```
2025-05-21 23:00:07 +03:00
## Lint
### Lint Markdown files
2025-08-28 17:54:28 +03:00
We use [markdownlint ](https://github.com/DavidAnson/markdownlint ),
[markdown-toc ](https://github.com/jonschlinkert/markdown-toc ),
and [mdsf ](https://github.com/hougesen/mdsf ) to check Markdown files.
`markdownlint` verifies document formatting, such as line length and empty lines.
`markdown-toc` checks the entries in the table of contents.
`mdsf` is responsible for formatting code snippets.
2025-05-21 23:00:07 +03:00
1. Install [markdownlint-cli2 ](https://github.com/DavidAnson/markdownlint-cli2#install ).
2025-08-28 17:54:28 +03:00
2. Install [markdown-toc ](https://github.com/jonschlinkert/markdown-toc#quick-start ).
3. Install [mdsf ](https://mdsf.mhouge.dk/#installation ) and formatters:
2025-05-24 22:29:40 +03:00
- [goimports ](https://pkg.go.dev/golang.org/x/tools/cmd/goimports ) for `go` : `go install golang.org/x/tools/cmd/goimports@latest`
- [shfmt ](https://github.com/mvdan/sh#shfmt ) for `sh, shell, bash` : `go install mvdan.cc/sh/v3/cmd/shfmt@latest`
- [taplo ](https://taplo.tamasfe.dev/cli/installation/binary.html ) for `toml`
2025-08-28 17:54:28 +03:00
4. Run the following command to check formatting:
2025-05-21 23:00:07 +03:00
2025-05-24 22:29:40 +03:00
```shellsession
2025-05-21 23:00:07 +03:00
$ markdownlint-cli2 .
Finding: *.{md,markdown} * .md
Found:
CODE_OF_CONDUCT.md
CONTRIBUTING.md
DEVELOPING.md
README.md
RULES_DESCRIPTIONS.md
Linting: 5 file(s)
Summary: 0 error(s)
```
2025-05-24 22:29:40 +03:00
_The `markdownlint-cli2` tool automatically uses the config file [.markdownlint-cli2.yaml ](./.markdownlint-cli2.yaml )._
\
2025-08-28 17:54:28 +03:00
4. Run the following command to check TOC:
```sh
markdown-toc --maxdepth 4 --no-first1 --bullets "-" -i README.md & & git diff --exit-code README.md
markdown-toc --maxdepth 2 --no-first1 --bullets "-" -i RULES_DESCRIPTIONS.md & & git diff --exit-code RULES_DESCRIPTIONS.md
```
\
5. Run the following commands to verify and format code snippets:
2025-05-24 22:29:40 +03:00
```sh
mdsf verify .
```
```sh
mdsf format .
```
2025-08-27 12:31:47 +03:00
_Note: Use `golang` for Go code snippets that are intentionally non-compilable.
However, it is recommended to avoid this and use `go` whenever possible._