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

string-format: Missing possibility to consider matches as linting errors #762

Closed
nachtjasmin opened this issue Oct 20, 2022 · 2 comments · Fixed by #765
Closed

string-format: Missing possibility to consider matches as linting errors #762

nachtjasmin opened this issue Oct 20, 2022 · 2 comments · Fixed by #765
Assignees

Comments

@nachtjasmin
Copy link

Is your feature request related to a problem? Please describe.
Since Go (or at least the RE2 parsing engine) does not support negative lookaheads in regular expressions, it's hard for me to exclude certain strings in the string format.

Right now, I want to check for the things inside the fmt.Errorf call:

  1. Does not contain phrases like "could not", "error" or similar.
  2. Verbs are using the present tense instead of the continuous tense (e.g. "do" instead of "doing").

Both solutions are fairly easy to implement with regular expressions.

  1. /(could not|error|failed)/
  2. /(\w+ing)/

Examples:

  • valid: create API client: %w
  • invalid: creating API client failed: %w:

Unfortunately, the current revive rule asserts: If the pattern matches, this is valid code.
But I need: If the pattern matches, this is a linting error.

Describe the solution you'd like
There should be a way of defining expression rules that should not appear in certain strings. So, if the pattern matches, this is considered a failure.

Describe alternatives you've considered
"Invert" the above regular expressions. In my opinion that's way too complicated for such a simple case.

@nachtjasmin nachtjasmin changed the title [feature request] Add inverted type of string-format string-format: Missing possibility to consider matches as linting errors Oct 20, 2022
@chavacava
Copy link
Collaborator

chavacava commented Oct 22, 2022

Hi @chloe-the-catgirl, thanks for filling the issue.

Does negating the regular expression in the configuration could work for you? For example by adding a ! in front of the regular expression:

[rule.string-format]
  arguments = [
    ["fmt.Errorf[0]", "/^([^A-Z]|$)/",  "must not start with a capital letter"],
    ["panic",         "!/^([^A-Z]|$)/", "must     start with a capital letter"]]

Then your example could be written as:

[rule.string-format]
  arguments = [
    ["fmt.Errorf[0]", "!/(\w+ing)/",  "must not use the present tense instead of the continuous tense"],

You can test by checking-out https://github.com/mgechev/revive/tree/feature/762

@chavacava chavacava self-assigned this Oct 22, 2022
@nachtjasmin
Copy link
Author

Hi @chavacava, thanks for the quick response ☺️
That would be perfectly fine as it covers my usecases perfectly with a clean syntax. Really like the approach! 😄

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

Successfully merging a pull request may close this issue.

2 participants