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

Output with patterns isn't expanded when patterns match long (> 60 character) identifiers. #70

Open
efarrer opened this issue Aug 21, 2020 · 3 comments

Comments

@efarrer
Copy link

efarrer commented Aug 21, 2020

Running ruleguard -rules rules.go -fix example.go outputs "$f should be expanded to the name" but expected the output "ZeroOneTwoThreeFourFiveSixSevenEightNineTenElevenTwelveThirte should be expanded to the name" when executed with the following files.

example.go

// +build ignore

package gorules

func ZeroOneTwoThreeFourFiveSixSevenEightNineTenElevenTwelveThirte() {
}

rules.go

// +build ignore

package gorules

import "github.com/quasilyte/go-ruleguard/dsl/fluent"

func MatchesFuncsWithLongNames(m fluent.Matcher) {
        m.Match(
                `func $f($*_) { $*_ }`,
        ).
                Report(`$f should be expanded to the name`)
}

This appears to be related to the limit placed here: https://github.com/quasilyte/go-ruleguard/blob/master/ruleguard/runner.go#L191

This limit was bumped from 40 to 60 in #63

Since many engineers use long function names for unit tests (BDD) I suggest the limit be increased to at least 120 (or removed altogether). An example of a long unit test function name could be.

func TestRuleguardOutput_CorrectlyExpandsMatchingVariablesWhenMatchingLongIdentifiers(t *testing.T) {

efarrer added a commit to efarrer/go-ruleguard that referenced this issue Aug 21, 2020
When outputing results the current behavior is to use the key in the
output if the identifier would be too long for example:
"$f != nil"

This instead truncates the value and adds elisis as follows:

"ThisIsAReallyLongIdentifierThatWillBeTruncatedInsteadOfReje... != nil"

This output is more friendly.

Addresses quasilyte#70
@efarrer
Copy link
Author

efarrer commented Aug 21, 2020

After thinking about this more perhaps a better solution would be to truncate the value instead of increasing the limit. PR for truncating the value #71

@quasilyte
Copy link
Owner

Since ruleguard is a CLI tool, it's always good to have a human-readable output that does not annoy the user.

With matcher variables, there is a risk that submatch will be huge. For example, it may occupy several source lines (100+) if you're matching the statement. Everything inside that statement will be embedded in the message. This will make a single message 1000+ characters long, which might be disappointing as it clutters the output.

The limit was a solution to filter out things that we probably don't want to interpolate. Your truncation idea is good as well, although it doesn't help with statements a lot (a more structural truncation/shortening would be better here).

@quasilyte
Copy link
Owner

I know GitHub is supposed to be almost completely a meme-free zone, but speaking of truncation...
image

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

No branches or pull requests

2 participants