Comments (5)
There is m.File().PkgPath
that is available for a while now.
https://go-ruleguard.github.io/by-example/file-predicates
e.g.:
m.File().PkgPath.Matches("some-regexp")
from go-ruleguard.
There are several separate issues here:
- Run some rules only for packages that meet some condition.
- gogrep can't handle import specs that great, so it's hard (impossible) to write that pattern right now. We can work around this (see below).
Conditional rules
It's probably possible to come up with the syntax to declare some rules, but I have no good ideas right now.
Should it be a per-rule filter? Should it be a per rule file constraint?
As a workaround, I can suggest having a separate rules file and run ruleguard twice: once with the normal rules set, then with the imports-related rules set:
ruleguard -rules rules.go ./src/...
ruleguard -rules import_rules.go ./src/pkg/...
Matching ImportSpec
Let's suppose that gogrep supports import
matching.
We'll assume that we want to match "foo"
import.
In Go, there are at least 2 ways to write that import on a syntax level:
// Without explicit () group
import "foo"
// Somewhere inside () group
import (
// ..
"foo"
// ..
)
This will lead to an awkward situation where you need to write 2 patterns:
m.Match(
`import "foo"`,
`import ($*_; "foo"; $*)`,
)
What you probably want to do is to match *ast.ImportSpec
which is a single import declaration unit. Like so:
m.MatchImportSpec(`"foo"`)
m.MatchImportSpec(`$alias $path`).Where(... filters over alias and path)
That's not a valid syntax as of yet, just an idea.
from go-ruleguard.
Should it be a per-rule filter? Should it be a per rule file constraint?
I believe it should be per-rule filter, because I suppose most rules are general and must be applied to all files, thus rules which should be restricted to subset of files/packages should be rare exception. So, needs to add this filter to each rule unlikely will became annoying, and at same time absence of filter with too wide scope (like per rule file) will helps to avoid restricting other rules to subset of files by mistake.
BTW, maybe it'll be easier to implement matching it in same way as for import specs - i.e. match by import path of processed file?
As a workaround, I can suggest having a separate rules file and run ruleguard twice:
I don't like the idea, because I don't see how to run it this way from .golangci.yml - it doesn't provide configuration options required for this.
from go-ruleguard.
BTW, maybe it'll be easier to implement matching it in same way as for import specs - i.e. match by import path of processed file?
Do you mean a filter that checks the package path of the file that is being processes? It does sound like a valid idea. I think it's possible to recognize such filters and build an index for them, so we don't have to run these rules for every package only to see that it doesn't match.
from go-ruleguard.
Do you mean a filter that checks the package path of the file that is being processes?
Yes.
from go-ruleguard.
Related Issues (20)
- Why does ':=' and '=' match differently? (short var declaration vs regular) HOT 2
- panic in runner.go:113 HOT 2
- Not working while importing golangci-lint under deeper packages. HOT 2
- Re-implement xtypes using the newer version of go/types
- panic with generics HOT 1
- Should not recommend to use `ignore` build tag HOT 5
- Ability to check if a match is within a method implementing interface (or method name matches) HOT 2
- Slow rule speed HOT 1
- ruleguard allows rules to be in any package
- [help needed] rule for missing ellipsis in forwarded arg HOT 11
- Ability to match on a non-deferred function call only HOT 2
- [go 1.20] Ruleguard intermittently failing with invalid compilation errors
- /x/tools dependency is too old, causes flaky interface satisfaction failures HOT 1
- Text matcher not behaving as expected HOT 2
- Support for string constexpr evaluation HOT 2
- go1.21: load rules: parse rules file: typechecker error: ...: could not import HOT 22
- Allow filtering by package name (not just path)
- Loop and Configuration HOT 1
- Go 1.22 compatibility HOT 2
- Is it possible to match implementors of generic interfaces? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-ruleguard.