mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
New option: LargeNumbers#int_min_digits (default: 5)
This commit is contained in:
parent
b3a9ff2fa0
commit
3c9df13beb
3 changed files with 26 additions and 12 deletions
|
@ -21,6 +21,7 @@ EmptyExpression:
|
|||
LargeNumbers:
|
||||
# A rule that disallows usage of large numbers without underscore.
|
||||
Enabled: true
|
||||
IntMinDigits: 5 # i.e. integers higher than 9999
|
||||
|
||||
LineLength:
|
||||
# Disallows lines longer that MaxLength number of symbols.
|
||||
|
|
|
@ -17,7 +17,11 @@ module Ameba
|
|||
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
||||
16 17 18 19 20 30 40 50 60 70 80 90
|
||||
100
|
||||
999
|
||||
1000
|
||||
1_000
|
||||
9999
|
||||
9_999
|
||||
10_000
|
||||
100_000
|
||||
200_000
|
||||
|
@ -79,15 +83,13 @@ module Ameba
|
|||
|
||||
it_transforms "1_00000", "100_000"
|
||||
|
||||
it_transforms "1_23_i8", "123_i8"
|
||||
it_transforms "1000_i16", "1_000_i16"
|
||||
it_transforms "1000_i32", "1_000_i32"
|
||||
it_transforms "1000_i64", "1_000_i64"
|
||||
it_transforms "10000_i16", "10_000_i16"
|
||||
it_transforms "10000_i32", "10_000_i32"
|
||||
it_transforms "10000_i64", "10_000_i64"
|
||||
|
||||
it_transforms "1_23_u8", "123_u8"
|
||||
it_transforms "1000_u16", "1_000_u16"
|
||||
it_transforms "1000_u32", "1_000_u32"
|
||||
it_transforms "1000_u64", "1_000_u64"
|
||||
it_transforms "10000_u16", "10_000_u16"
|
||||
it_transforms "10000_u32", "10_000_u32"
|
||||
it_transforms "10000_u64", "10_000_u64"
|
||||
|
||||
it_transforms "123456_f32", "123_456_f32"
|
||||
it_transforms "123456_f64", "123_456_f64"
|
||||
|
|
|
@ -24,14 +24,19 @@ module Ameba::Rule
|
|||
# ```
|
||||
# LargeNumbers:
|
||||
# Enabled: true
|
||||
# IntMinDigits: 5 # i.e. integers higher than 9999
|
||||
# ```
|
||||
#
|
||||
struct LargeNumbers < Base
|
||||
prop int_min_digits = 5
|
||||
|
||||
def test(source)
|
||||
Tokenizer.new(source).run do |token|
|
||||
next unless token.type == :NUMBER && decimal?(token.raw)
|
||||
|
||||
if (expected = underscored token.raw) != token.raw
|
||||
parsed = parse_number token.raw
|
||||
|
||||
if allowed?(*parsed) && (expected = underscored *parsed) != token.raw
|
||||
source.error self, token.location,
|
||||
"Large numbers should be written with underscores: #{expected}"
|
||||
end
|
||||
|
@ -42,8 +47,14 @@ module Ameba::Rule
|
|||
value !~ /^0(x|b|o)/
|
||||
end
|
||||
|
||||
private def underscored(raw_number)
|
||||
sign, value, fraction, suffix = parse_number raw_number
|
||||
private def allowed?(_sign, value, fraction, _suffix)
|
||||
return true unless fraction.nil?
|
||||
|
||||
digits = value.chars.select &.to_s.=~ /[0-9]/
|
||||
digits.size >= int_min_digits
|
||||
end
|
||||
|
||||
private def underscored(sign, value, fraction, suffix)
|
||||
value = slice_digits(value.reverse) { |slice| slice }.reverse
|
||||
fraction = "." + slice_digits(fraction) { |slice| slice } if fraction
|
||||
|
||||
|
@ -75,7 +86,7 @@ module Ameba::Rule
|
|||
end
|
||||
|
||||
private def parse_suffix(value)
|
||||
if pos = (value =~ /e/ || value =~ /_(i|u|f)/)
|
||||
if pos = (value =~ /e/ || value =~ /_?(i|u|f)/)
|
||||
suffix = value[pos..-1]
|
||||
value = value[0..pos - 1]
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue