codingjoe / relint

Write your own linting rules using regular expressions.
MIT License
57 stars 12 forks source link

Add a negate flag #15

Open petemounce opened 4 years ago

petemounce commented 4 years ago

I would like to add a rule that fails when a regex is not matched, for example a VERSION file containing a v1.0.0 and a pattern like ^v (because a v prefix on a semver is not valid).

petemounce commented 4 years ago
---
# https://semver.org/#is-v123-a-semantic-version
- name: VERSION files must contain semver.
  # LOLWAT? https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
  pattern: '^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'
  hint: "https://semver.org/#is-v123-a-semantic-version"
  filePattern: '.*VERSION'

- name: VERSION file contents must not be prefixed with a v
  pattern: '^v'
  hint: "https://semver.org/#is-v123-a-semantic-version"
  filePattern: '.*VERSION'

cmd/VERSION:

v0.1.1

I want that file to fail both rules, not just the 2nd one.

petemounce commented 4 years ago

(and the LOLWAT refers to the regex itself ;) )

codingjoe commented 4 years ago

Hi @petemounce thanks for reaching out. I see your point, how this might come in handy. However, I am not 100% sure on how to integrate this, since we currently work with matches, that have line numbers and snippets, that are being displayed. Inverse matches would mean we only had file names.

Anyhow, I still think it's possible to come up with a good solution. How about you propose a solution. Preferably as a pull-request and I will gladly review and release it.

Best -Joe

viktorkertesz commented 1 year ago

Hi @petemounce , did you try this? (I know it doesn't cover everything, just to get a hang of it)

^(?!\d+\.\d+\.\d+).*$

This won't match the correct versioning string but will trigger everything else.