mirror of
https://github.com/mgechev/revive.git
synced 2024-11-30 08:57:07 +02:00
4bb48df5d2
* refactor: extract shared code for linting if-else chains The rules "early-return", "indent-error-flow" and "superfluous-else" have a similar structure. This moves the common logic for classifying if-else chains to a common package. A few side benefits: - "early-return" now handles os.Exit/log.Panicf/etc - "superfluous-else" now handles (builtin) panic - "superfluous-else" and "indent-error-flow" now handle if/else chains with 2+ "if" branches * internal/ifelse: style fixes, renames, spelling
26 lines
448 B
Go
26 lines
448 B
Go
package ifelse
|
|
|
|
import "go/ast"
|
|
|
|
// Target decides what line/column should be indicated by the rule in question.
|
|
type Target int
|
|
|
|
const (
|
|
// TargetIf means the text refers to the "if"
|
|
TargetIf Target = iota
|
|
|
|
// TargetElse means the text refers to the "else"
|
|
TargetElse
|
|
)
|
|
|
|
func (t Target) node(ifStmt *ast.IfStmt) ast.Node {
|
|
switch t {
|
|
case TargetIf:
|
|
return ifStmt
|
|
case TargetElse:
|
|
return ifStmt.Else
|
|
default:
|
|
panic("bad target")
|
|
}
|
|
}
|