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

Allow _v1 suffix in Naming/VariableNumber without having to specify every individual method #10539

Closed
henrahmagix opened this issue Apr 15, 2022 · 0 comments · Fixed by #10571

Comments

@henrahmagix
Copy link
Contributor

Is your feature request related to a problem? Please describe.

We've started suffixing some methods with _v1, _v2, etc. to indicate when we're effectively versioning them and introducing new versions, so we can do "soft" code releases where we start using the new method, and can still use the old method alongside to identify and upgrade existing things that were stored by the old method. E.g.

 def model_details_hasher(model)
+  NewHashingAlgorithm(model.to_json)
+end
+
+def model_details_hasher_v1(model)
   SomeHashingAlgorithm(model.to_s)
 end

We have Naming/VariableNumber set to EnforcedStyle: snake_case so the cop says it must be _v_1, _v_2, etc.
image
image

Describe the solution you'd like

We prefer _v1 to _v_1, so we'd like to be able to always allow these methods, whenever we add new ones. We also want to keep the rest of the rules under EnforcedStyle: snake_case the same.

Perhaps by allowing a regex to be configured, like AllowIf: /_v\d$/?

Or offering a specific option like AllowVersionSuffixInSnakeCase that does the same as the above, when the cop is set to EnforcedStyle: snake_case? This would be helpful if you want to control the speed of this feature (if allowing any regex could slow down rubocop and cause more bug reports/support issues as a result), and if other people would also want to use this combination of snake_case whilst allowing (imo) nicer-reading _v1.

Or even an option like MaxAllowedLettersBeforeNumber so you can also allow _a1, _b2, etc.

# .rubocop.yml
Naming/VariableNumber:
  EnforcedStyle: snake_case
  MaxAllowedLettersBeforeNumber: 1 # default is 0

Describe alternatives you've considered

Disable the cop inline on every method, which then feels like that particular method is "going against style", when it's actually the style we want.
image

Or add each new method name to the AllowedIdentifiers array on the cop settings, which will become tiresome as we use this method of code versioning more and more into the future.

# .rubocop.yml
Naming/VariableNumber:
  EnforcedStyle: snake_case
  AllowedIdentifiers:
    - model_details_hasher_v1
henrahmagix added a commit to henrahmagix/rubocop that referenced this issue Apr 15, 2022
Fix rubocop#10539

Adds a new integer option called `MaxAllowedLettersBeforeNumber` to
allow a positive number of non-digits before the digits in a snake_case
style that would otherwise require separation between them, e.g. `_v1`.
henrahmagix added a commit to henrahmagix/rubocop that referenced this issue Apr 22, 2022
Fix rubocop#10539

Also fix global variables not being matched correctly, discovered by
filling in the tests with global variable examples and having failures.
bbatsov pushed a commit that referenced this issue Apr 25, 2022
…me (#10571)

Also fix global variables not being matched correctly, discovered by
filling in the tests with global variable examples and having failures.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant