Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruleguard,dsl: add Var.Node filters #124

Merged
merged 1 commit into from
Nov 7, 2020

Conversation

quasilyte
Copy link
Owner

@quasilyte quasilyte commented Nov 7, 2020

Add the ability to assert the submatch node AST type.

A new Var.Node.Is() function accepts the type name string,
like Ident (ast.Ident), Expr (ast.Expr) and so on.
For any unexpected arguments the rule compilation will fail.

We may add more node categories that are not declared in
go/ast package for our convenience.

Rationale: sometimes we need to check not only the types,
but also the "shape" of the match. For example, we may want
to enforce some func arguments to be literal strings.
Right now we can only do this by doing a regexp check
that may look like <broken markdown> but that's not reliable, nor efficient.
What we can do is to add a negated *ast.BasicLit constraint,
optionally with a type check if func signature accepts interface{}.

// Match any non-literal $s
Where(!m["s"].Node.Is(`BasicLit`))

Note that m["s"].Const is not identical here. It would accept
const expressions like "a" + "b" as well.

Not to be confused with an actual type. It's just a
syntactical check whether a matched node belongs to some
syntax category as defined by the go/ast package.

Signed-off-by: Iskander Sharipov [email protected]

Add the ability to assert the submatch node AST type.

A new `Var.Node.Is()` function accepts the type name string,
like `Ident` (ast.Ident), `Expr` (ast.Expr) and so on.
For any unexpected arguments the rule compilation will fail.

We may add more node categories that are not declared in
`go/ast` package for our convenience.

Rationale: sometimes we need to check not only the types,
but also the "shape" of the match. For example, we may want
to enforce some func arguments to be literal strings.
Right now we can only do this by doing a regexp check
that may look like `^["`].*["`]` but that's not reliable, nor efficient.
What we can do is to add a negated `*ast.BasicLit` constraint,
optionally with a type check if func signature accepts `interface{}`.

	// Match any non-literal $s
	Where(!m["s"].Node.Is(`BasicLit`))

Note that `m["s"].Const` is not identical here. It would accept
const expressions like `"a" + "b"` as well.

Not to be confused with an actual type. It's just a
syntactical check whether a matched node belongs to some
syntax category as defined by the `go/ast` package.

Signed-off-by: Iskander Sharipov <[email protected]>
@quasilyte quasilyte merged commit 4690fc9 into master Nov 7, 2020
@quasilyte quasilyte deleted the quasilyte/feature/implements_node_filters branch November 7, 2020 19:07
@quasilyte
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants