-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ruleguard: add support for local functions
This feature is useful for rule filters readability improvements. Instead of copying a complex `Where()` expression several times, one can now use a local function literal to define that filter operation and use it inside `Where()` expressions. Here is an example: ```go func preferFprint(m dsl.Matcher) { isFmtPackage := func(v dsl.Var) bool { return v.Text == "fmt" && v.Object.Is(`PkgName`) } m.Match(`$w.Write([]byte($fmt.Sprint($*args)))`). Where(m["w"].Type.Implements("io.Writer") && isFmtPackage(m["fmt"])). Suggest("fmt.Fprint($w, $args)"). Report(`fmt.Fprint($w, $args) should be preferred to the $$`) m.Match(`$w.Write([]byte($fmt.Sprintf($*args)))`). Where(m["w"].Type.Implements("io.Writer") && isFmtPackage(m["fmt"])). Suggest("fmt.Fprintf($w, $args)"). Report(`fmt.Fprintf($w, $args) should be preferred to the $$`) // ...etc } ``` Note that we used `isFmtPackage` in more than 1 rule. Functions can accept almost arbitrary params, but there are some restrictions on what kinds of arguments they can receive right now. These arguments work: * Matcher var expressions like `m["varname"]` * Basic literals like `"foo"`, `104`, `5.2` * Constants
- Loading branch information
Showing
11 changed files
with
352 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// +build ignore | ||
|
||
package gorules | ||
|
||
import "github.com/quasilyte/go-ruleguard/dsl" | ||
|
||
func testRules(m dsl.Matcher) { | ||
bothConst := func(x, y dsl.Var) bool { | ||
return x.Const && y.Const | ||
} | ||
m.Match(`test("both const", $x, $y)`). | ||
Where(bothConst(m["x"], m["y"])). | ||
Report(`true`) | ||
|
||
intValue := func(x dsl.Var, val int) bool { | ||
return x.Value.Int() == val | ||
} | ||
m.Match(`test("== 10", $x)`). | ||
Where(intValue(m["x"], 10)). | ||
Report(`true`) | ||
|
||
isZero := func(x dsl.Var) bool { return x.Value.Int() == 0 } | ||
m.Match(`test("== 0", $x)`). | ||
Where(isZero(m["x"])). | ||
Report(`true`) | ||
|
||
// Testing closure-captured m variable. | ||
fmtIsImported := func() bool { | ||
return m.File().Imports(`fmt`) | ||
} | ||
m.Match(`test("fmt is imported")`). | ||
Where(fmtIsImported()). | ||
Report(`true`) | ||
|
||
// Testing explicitly passed matcher. | ||
ioutilIsImported := func(m2 dsl.Matcher) bool { | ||
return m2.File().Imports(`io/ioutil`) | ||
} | ||
m.Match(`test("ioutil is imported")`). | ||
Where(ioutilIsImported(m)). | ||
Report(`true`) | ||
|
||
// Test precedence after the macro expansion. | ||
isSimpleExpr := func(v dsl.Var) bool { | ||
return v.Const || v.Node.Is(`Ident`) | ||
} | ||
m.Match(`test("check precedence", $x, $y)`). | ||
Where(isSimpleExpr(m["x"]) && m["y"].Text == "err"). | ||
Report(`true`) | ||
|
||
// Test variables referenced through captured m. | ||
isStringLit := func() bool { | ||
return m["x"].Node.Is(`BasicLit`) && m["x"].Type.Is(`string`) | ||
} | ||
m.Match(`test("is string", $x)`). | ||
Where(isStringLit()). | ||
Report(`true`) | ||
|
||
// Test predicate applied to different matcher vars. | ||
isPureCall := func(v dsl.Var) bool { | ||
return v.Node.Is(`CallExpr`) && v.Pure | ||
} | ||
m.Match(`test("is pure call", $x, $y)`). | ||
Where(isPureCall(m["x"]) && isPureCall(m["y"])). | ||
Report(`true`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package localfunc | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
) | ||
|
||
func test(args ...interface{}) {} | ||
|
||
func f() interface{} { return nil } | ||
|
||
func _() { | ||
fmt.Println("ok") | ||
_ = ioutil.Discard | ||
|
||
var i int | ||
var err error | ||
|
||
test("both const", 1, 2) // want `true` | ||
test("both const", 1, 2+2) // want `true` | ||
test("both const", i, 2) | ||
test("both const", 1, i) | ||
test("both const", i, i) | ||
|
||
test("== 10", 10) // want `true` | ||
test("== 10", 9+1) // want `true` | ||
test("== 10", 11) | ||
test("== 10", i) | ||
|
||
test("== 0", 0) // want `true` | ||
test("== 0", 1-1) // want `true` | ||
test("== 0", 11) | ||
test("== 0", i) | ||
|
||
test("fmt is imported") // want `true` | ||
|
||
test("ioutil is imported") // want `true` | ||
|
||
test("check precedence", 1, err) // want `true` | ||
test("check precedence", 1+2, err) // want `true` | ||
test("check precedence", i, err) // want `true` | ||
test("check precedence", err, err) // want `true` | ||
test("check precedence", f(), err) | ||
test("check precedence", 1) | ||
test("check precedence", 1+2) | ||
test("check precedence", i) | ||
test("check precedence", err) | ||
test("check precedence", f()) | ||
test("check precedence", 1, nil) | ||
test("check precedence", 1+2, nil) | ||
test("check precedence", i, nil) | ||
test("check precedence", err, nil) | ||
test("check precedence", f(), nil) | ||
|
||
test("is string", "yes") // want `true` | ||
test("is string", `yes`) // want `true` | ||
test("is string", 1) | ||
test("is string", i) | ||
|
||
test("is pure call", int(0), int(1)) // want `true` | ||
test("is pure call", string("f"), int(1)) // want `true` | ||
test("is pure call", f(), f()) | ||
test("is pure call", int(0), 1) | ||
test("is pure call", 0, int(1)) | ||
test("is pure call", f(), int(1)) | ||
test("is pure call", 1, 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package localfunc | ||
|
||
func _() { | ||
test("fmt is imported") | ||
|
||
test("ioutil is imported") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.