Giter Site home page Giter Site logo

Comments (5)

quasilyte avatar quasilyte commented on June 13, 2024 1

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.

quasilyte avatar quasilyte commented on June 13, 2024

There are several separate issues here:

  1. Run some rules only for packages that meet some condition.
  2. 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.

powerman avatar powerman commented on June 13, 2024

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.

quasilyte avatar quasilyte commented on June 13, 2024

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.

powerman avatar powerman commented on June 13, 2024

Do you mean a filter that checks the package path of the file that is being processes?

Yes.

from go-ruleguard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.