-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Add NumericLiteralPrefix cop #3177
Conversation
P.S. This should also be a rule in the style guide. |
# encoding: utf-8 | ||
# frozen_string_literal: true | ||
|
||
require 'pry' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remember to remove this line.
f58f143
to
deb3855
Compare
@bbatsov Added a rule to the styleguide and updated this PR |
Btw, ideally this cop should be configurable. While few people would prefer |
@bbatsov Right. I will make this configurable. Also regarding the hex numbers and similarly binary numbers, should we have separate cops for them or one cop for all three (octal, hex and binary)? Looks like functionality would be more or less the same. |
Guess one cop should be fine. |
@tejasbubane ping :-) |
deb3855
to
c1206bd
Compare
c1206bd
to
82852c0
Compare
@bbatsov Sorry for the delay. Made the cop generic for all numeric literals (octal, hex, binary and decimal). Added configuration option for octal literals to use just Corresponding PR for style-guide is here: rubocop/ruby-style-guide#579 Decimal prefixes came up in the discussion in above style-guide PR. I have made the cop to discourage the use of decimal prefixes |
expect(cop.messages).to be_empty | ||
end | ||
|
||
it 'autocorrects a octal literal starting with 0O or 0o' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an octal literal
Looks good! I had some small comments. |
Closes rubocop#3123 This cop checks for numeric literals using uppercase prefixes and corrects them to use lowercase of no prefix (in case of decimals).
Can use `EnforcedOctalStyle` => `zero_only` to use only `0` for octal literals instead of `0o` or `0O`.
2be973e
to
9ffa30d
Compare
Thanks @jonas054 👍 Those were some really good comments. I have fixed them. |
I have kept two commits separate - one for creating the cop, and other for making it configurable. Let me know if you want them squashed. |
👍 LGTM |
Closes rubocop#3123 This cop checks for numeric literals using uppercase prefixes and corrects them to use lowercase of no prefix (in case of decimals). Can use `EnforcedOctalStyle` => `zero_only` to use only `0` for octal literals instead of `0o` or `0O`.
Closes #3123
This cop checks for octal numbers starting with
0
and0O
andcorrects them to
0o
.Before submitting the PR make sure the following are checked:
master
(if not - rebase it)and description in grammatically correct, complete sentences.