-
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.
dsl: add experimental API related to the custom filters
`dsl/types` is a wrapper around `go/types` that provides the minimal interface that is useful for custom filter. We may add `dsl/ast` later to mimic `go/ast` as well for node-related custom filters. See #129 (comment) to get more context on what this is about.
- Loading branch information
Showing
5 changed files
with
79 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dsl | ||
|
||
import ( | ||
"github.com/quasilyte/go-ruleguard/dsl/types" | ||
) | ||
|
||
// VarFilterContext carries Var and environment information into the filter function. | ||
// It's an input parameter type for the Var.Filter function callback. | ||
type VarFilterContext struct { | ||
// Type is mapped to Var.Type field. | ||
Type types.Type | ||
} | ||
|
||
// SizeOf returns the size of the given type. | ||
func (*VarFilterContext) SizeOf(x types.Type) int { return 0 } | ||
|
||
// GetType finds a type value by a given name. | ||
// A name can be: | ||
// - builtin type name, like `error` or `string` | ||
// - fully-qualified type name, like `github.com/username/pkgname.TypeName` | ||
func (*VarFilterContext) GetType(name string) types.Type { return nil } | ||
|
||
// GetInterface finds a type value that represents an interface by a given name. | ||
// Works like `types.AsInterface(ctx.GetType(name))`. | ||
func (*VarFilterContext) GetInterface(name string) *types.Interface { return nil } |
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,9 @@ | ||
package types | ||
|
||
// AsPointer is a type-assert like operation, x.(*Pointer), but never panics. | ||
// Returns nil if type is not a pointer. | ||
func AsPointer(x Type) *Pointer { return nil } | ||
|
||
// AsInterface is a type-assert like operation, x.(*Interface), but never panics. | ||
// Returns nil if type is not an interface. | ||
func AsInterface(x Type) *Interface { return nil } |
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,11 @@ | ||
package types | ||
|
||
// Method stubs to make various types implement Type interface. | ||
// | ||
// Nothing interesting here, hence it's moved to a separate file. | ||
|
||
func (*Pointer) String() string { return "" } | ||
func (*Interface) String() string { return "" } | ||
|
||
func (*Pointer) Underlying() Type { return nil } | ||
func (*Interface) Underlying() Type { return nil } |
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,32 @@ | ||
// Package types mimics the https://golang.org/pkg/go/types/ package. | ||
// It also contains some extra utility functions, they're defined in ext.go file. | ||
package types | ||
|
||
// Implements reports whether a given type implements the specified interface. | ||
func Implements(typ Type, iface *Interface) bool { return false } | ||
|
||
// Identical reports whether x and y are identical types. Receivers of Signature types are ignored. | ||
func Identical(x, y Type) bool { return false } | ||
|
||
// A Type represents a type of Go. All types implement the Type interface. | ||
type Type interface { | ||
// Underlying returns the underlying type of a type. | ||
Underlying() Type | ||
|
||
// String returns a string representation of a type. | ||
String() string | ||
} | ||
|
||
type ( | ||
// A Pointer represents a pointer type. | ||
Pointer struct{} | ||
|
||
// An Interface represents an interface type. | ||
Interface struct{} | ||
) | ||
|
||
// NewPointer returns a new pointer type for the given element (base) type. | ||
func NewPointer(elem Type) *Pointer { return nil } | ||
|
||
// Elem returns the element type for the given pointer. | ||
func (*Pointer) Elem() Type { return nil } |