-
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: implement dsl Do() function (#379)
- Loading branch information
Showing
16 changed files
with
305 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//go:build ignore | ||
// +build ignore | ||
|
||
package gorules | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/quasilyte/go-ruleguard/dsl" | ||
"github.com/quasilyte/go-ruleguard/dsl/types" | ||
) | ||
|
||
func reportHello(ctx *dsl.DoContext) { | ||
ctx.SetReport("Hello, World!") | ||
} | ||
|
||
func suggestHello(ctx *dsl.DoContext) { | ||
ctx.SetSuggest("Hello, World!") | ||
} | ||
|
||
func reportX(ctx *dsl.DoContext) { | ||
ctx.SetReport(ctx.Var("x").Text()) | ||
} | ||
|
||
func unquote(s string) string { | ||
return s[1 : len(s)-1] | ||
} | ||
|
||
func reportTrimPrefix(ctx *dsl.DoContext) { | ||
s := unquote(ctx.Var("x").Text()) | ||
prefix := unquote(ctx.Var("y").Text()) | ||
ctx.SetReport(strings.TrimPrefix(s, prefix)) | ||
} | ||
|
||
func reportEmptyString(ctx *dsl.DoContext) { | ||
x := ctx.Var("x") | ||
if x.Text() == `""` { | ||
ctx.SetReport("empty string") | ||
} else { | ||
ctx.SetReport("non-empty string") | ||
} | ||
} | ||
|
||
func reportType(ctx *dsl.DoContext) { | ||
ctx.SetReport(ctx.Var("x").Type().String()) | ||
} | ||
|
||
func reportTypesIdentical(ctx *dsl.DoContext) { | ||
xtype := ctx.Var("x").Type() | ||
ytype := ctx.Var("y").Type() | ||
ctx.SetReport(fmt.Sprintf("%v", types.Identical(xtype, ytype))) | ||
} | ||
|
||
func testRules(m dsl.Matcher) { | ||
m.Match(`test("custom report")`). | ||
Do(reportHello) | ||
|
||
m.Match(`test("custom suggest")`). | ||
Do(suggestHello) | ||
|
||
m.Match(`test("var text", $x)`). | ||
Do(reportX) | ||
|
||
m.Match(`test("trim prefix", $x, $y)`). | ||
Do(reportTrimPrefix) | ||
|
||
m.Match(`test("report empty string", $x)`). | ||
Do(reportEmptyString) | ||
|
||
m.Match(`test("report type", $x)`). | ||
Do(reportType) | ||
|
||
m.Match(`test("types identical", $x, $y)`). | ||
Do(reportTypesIdentical) | ||
} |
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,30 @@ | ||
package do | ||
|
||
func Example() { | ||
test("custom report") // want `\QHello, World!` | ||
test("custom suggest") // want `\Qsuggestion: Hello, World!` | ||
|
||
var x int | ||
test("var text", "str") // want `\Q"str"` | ||
test("var text", x+1) // want `\Qx+1` | ||
|
||
test("trim prefix", "hello, world", "hello") // want `\Q, world` | ||
test("trim prefix", "hello, world", "hello, ") // want `\Qworld` | ||
test("trim prefix", "hello, world", "???") // want `\Qhello, world` | ||
|
||
test("report empty string", "") // want `\Qempty string` | ||
test("report empty string", "example") // want `\Qnon-empty string` | ||
|
||
test("report type", 13) // want `\Qint` | ||
test("report type", "str") // want `\Qstring` | ||
test("report type", []int{1}) // want `\Q[]int` | ||
test("report type", x) // want `\Qint` | ||
test("report type", &x) // want `\Q*int` | ||
|
||
test("types identical", 1, 1) // want `true` | ||
test("types identical", x, x) // want `true` | ||
test("types identical", x, &x) // want `false` | ||
test("types identical", 1, 1.5) // want `false` | ||
} | ||
|
||
func test(args ...interface{}) {} |
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
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.