Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement new Style/ExponentialNotation cop
This cop implements the rule describe in the "exponential notation" section of the ruby style guide. It allows for an additional style not mentionned in the guide, `integral` which allows only integer mantissa, without any trailing zero.
- Loading branch information
Showing
7 changed files
with
368 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop enforces consistency when using exponential notation | ||
# for numbers in the code (eg 1.2e4). Different styles are supported: | ||
# - `scientific` which enforces a mantissa between 1 (inclusive) | ||
# and 10 (exclusive). | ||
# - `engineering` which enforces the exponent to be a multiple of 3 | ||
# and the mantissa to be between 0.1 (inclusive) | ||
# and 10 (exclusive). | ||
# - `integral` which enforces the mantissa to always be a whole number | ||
# without trailing zeroes. | ||
# | ||
# @example EnforcedStyle: scientific (default) | ||
# # Enforces a mantissa between 1 (inclusive) and 10 (exclusive). | ||
# | ||
# # bad | ||
# 10e6 | ||
# 0.3e4 | ||
# 11.7e5 | ||
# 3.14e0 | ||
# | ||
# # good | ||
# 1e7 | ||
# 3e3 | ||
# 1.17e6 | ||
# 3.14 | ||
# | ||
# @example EnforcedStyle: engineering | ||
# # Enforces using multiple of 3 exponents, | ||
# # mantissa should be between 0.1 (inclusive) and 1000 (exclusive) | ||
# | ||
# # bad | ||
# 3.2e7 | ||
# 0.1e5 | ||
# 12e5 | ||
# 1232e6 | ||
# | ||
# # good | ||
# 32e6 | ||
# 10e3 | ||
# 1.2e6 | ||
# 1.232e9 | ||
# | ||
# @example EnforcedStyle: integral | ||
# # Enforces the mantissa to have no decimal part and no | ||
# # trailing zeroes. | ||
# | ||
# # bad | ||
# 3.2e7 | ||
# 0.1e5 | ||
# 120e4 | ||
# | ||
# # good | ||
# 32e6 | ||
# 1e4 | ||
# 12e5 | ||
# | ||
class ExponentialNotation < Cop | ||
include ConfigurableEnforcedStyle | ||
|
||
def on_float(node) | ||
add_offense(node) if offense?(node) | ||
end | ||
|
||
private | ||
|
||
def scientific?(node) | ||
mantissa, = node.source.split('e') | ||
mantissa =~ /^-?[1-9](\.\d*[0-9])?$/ | ||
end | ||
|
||
def engineering?(node) | ||
mantissa, exponent = node.source.split('e') | ||
return false unless exponent =~ /^-?\d+$/ | ||
return false unless (exponent.to_i % 3).zero? | ||
return false if mantissa =~ /^-?\d{4}/ | ||
return false if mantissa =~ /^-?0\d/ | ||
return false if mantissa =~ /^-?0.0/ | ||
|
||
true | ||
end | ||
|
||
def integral(node) | ||
mantissa, = node.source.split('e') | ||
mantissa =~ /^-?[1-9](\d*[1-9])?$/ | ||
end | ||
|
||
def offense?(node) | ||
return false unless node.source['e'] | ||
|
||
case style | ||
when :scientific | ||
!scientific?(node) | ||
when :engineering | ||
!engineering?(node) | ||
when :integral | ||
!integral(node) | ||
else | ||
false | ||
end | ||
end | ||
|
||
def message(_node) | ||
case style | ||
when :scientific | ||
'Use a mantissa in [1, 10[.' | ||
when :engineering | ||
'Use an exponent divisible by 3 and a mantissa in [0.1, 1000[.' | ||
when :integral | ||
'Use an integer as mantissa, without trailing zero.' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.