-
Notifications
You must be signed in to change notification settings - Fork 8
/
loopbreak.go
47 lines (39 loc) · 874 Bytes
/
loopbreak.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package loopbreak
import (
"go/ast"
"go/token"
"go/types"
"github.com/gtramontina/ooze/viruses"
)
type LoopBreak struct {
mutations map[token.Token]token.Token
}
// New returns a new LoopBreak virus.
//
// It replaces loop `break` with `continue` and vice versa.
func New() *LoopBreak {
return &LoopBreak{
mutations: map[token.Token]token.Token{
token.CONTINUE: token.BREAK,
token.BREAK: token.CONTINUE,
},
}
}
func (v *LoopBreak) Incubate(node ast.Node, _ *types.Info) []*viruses.Infection {
statement, matches := node.(*ast.BranchStmt)
if !matches {
return nil
}
originalToken := statement.Tok
mutatedToken, ok := v.mutations[statement.Tok]
if !ok {
return nil
}
return []*viruses.Infection{
viruses.NewInfection(
"Loop Break",
func() { statement.Tok = mutatedToken },
func() { statement.Tok = originalToken },
),
}
}