mirror of
https://github.com/mgechev/revive.git
synced 2025-11-27 22:18:41 +02:00
committed by
GitHub
parent
77616f0d30
commit
111737201e
@@ -10,17 +10,9 @@ Clone the project:
|
|||||||
git clone git@github.com:mgechev/revive.git
|
git clone git@github.com:mgechev/revive.git
|
||||||
cd revive
|
cd revive
|
||||||
```
|
```
|
||||||
|
|
||||||
In order to fetch all the dependencies run:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
make install
|
|
||||||
```
|
|
||||||
|
|
||||||
## Build
|
## Build
|
||||||
|
|
||||||
In order to build the project run:
|
In order to build the project run:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
make build
|
make build
|
||||||
```
|
```
|
||||||
@@ -31,14 +23,38 @@ The command will produce the `revive` binary in the root of the project.
|
|||||||
|
|
||||||
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).
|
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).
|
||||||
|
|
||||||
All rules should implement the following interface:
|
Each rule needs to implement the `lint.Rule` interface:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type Rule interface {
|
type Rule interface {
|
||||||
Name() string
|
Name() string
|
||||||
Apply(*File, Arguments) []Failure
|
Apply(*File, Arguments) []Failure
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
All rules with a configuration must implement `lint.ConfigurableRule` interface:
|
||||||
|
```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.
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[rule.ban-struct-name]
|
||||||
|
arguments = ["Foo"]
|
||||||
|
```
|
||||||
|
|
||||||
|
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`.
|
||||||
|
- 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.
|
||||||
|
|
||||||
|
A sample rule implementation can be found [here](/rule/argument_limit.go).
|
||||||
|
|
||||||
|
|
||||||
## Development of formatters
|
## Development of formatters
|
||||||
|
|
||||||
|
|||||||
27
README.md
27
README.md
@@ -636,32 +636,7 @@ To add a custom formatter you'll have to push it to this repository or fork it.
|
|||||||
|
|
||||||
### Writing a Custom Rule
|
### Writing a Custom Rule
|
||||||
|
|
||||||
Each rule needs to implement the `lint.Rule` interface:
|
See [DEVELOPING.md](./DEVELOPING.md) for instructions on how to write a custom rule.
|
||||||
|
|
||||||
```go
|
|
||||||
type Rule interface {
|
|
||||||
Name() string
|
|
||||||
Apply(*File, Arguments) []Failure
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
The `Arguments` type is an alias of the type `[]interface{}`. The arguments of the rule are passed from the configuration file.
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
|
|
||||||
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:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
[rule.ban-struct-name]
|
|
||||||
arguments = ["Foo"]
|
|
||||||
```
|
|
||||||
|
|
||||||
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`.
|
|
||||||
- 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.
|
|
||||||
|
|
||||||
A sample rule implementation can be found [here](/rule/argument_limit.go).
|
|
||||||
|
|
||||||
#### Using `revive` as a library
|
#### Using `revive` as a library
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user