rubocop 0.78.0 → 0.82.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +4 -4
  4. data/config/default.yml +158 -48
  5. data/lib/rubocop.rb +19 -4
  6. data/lib/rubocop/ast/builder.rb +45 -42
  7. data/lib/rubocop/ast/node.rb +12 -19
  8. data/lib/rubocop/ast/node/array_node.rb +13 -0
  9. data/lib/rubocop/ast/node/block_node.rb +5 -1
  10. data/lib/rubocop/ast/node/case_match_node.rb +56 -0
  11. data/lib/rubocop/ast/node/def_node.rb +11 -0
  12. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  13. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +8 -0
  14. data/lib/rubocop/ast/node/regexp_node.rb +2 -4
  15. data/lib/rubocop/ast/traversal.rb +29 -10
  16. data/lib/rubocop/cli.rb +10 -4
  17. data/lib/rubocop/cli/command/show_cops.rb +11 -4
  18. data/lib/rubocop/comment_config.rb +6 -1
  19. data/lib/rubocop/config.rb +36 -10
  20. data/lib/rubocop/config_loader.rb +42 -33
  21. data/lib/rubocop/config_loader_resolver.rb +1 -1
  22. data/lib/rubocop/config_obsoletion.rb +4 -1
  23. data/lib/rubocop/config_validator.rb +66 -92
  24. data/lib/rubocop/cop/autocorrect_logic.rb +6 -3
  25. data/lib/rubocop/cop/badge.rb +5 -5
  26. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -1
  27. data/lib/rubocop/cop/corrector.rb +48 -24
  28. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
  29. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -2
  30. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  31. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +3 -3
  32. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
  33. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  34. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +2 -2
  35. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  36. data/lib/rubocop/cop/generator.rb +3 -4
  37. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  38. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -1
  39. data/lib/rubocop/cop/layout/array_alignment.rb +53 -10
  40. data/lib/rubocop/cop/layout/block_end_newline.rb +5 -3
  41. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  42. data/lib/rubocop/cop/layout/else_alignment.rb +8 -0
  43. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
  44. data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
  45. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +16 -10
  46. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
  47. data/lib/rubocop/cop/layout/hash_alignment.rb +8 -4
  48. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  49. data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
  50. data/lib/rubocop/cop/layout/{tab.rb → indentation_style.rb} +48 -6
  51. data/lib/rubocop/cop/layout/leading_comment_space.rb +34 -3
  52. data/lib/rubocop/cop/layout/line_length.rb +32 -3
  53. data/lib/rubocop/cop/layout/multiline_block_layout.rb +15 -6
  54. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
  55. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  56. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +3 -3
  57. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +133 -0
  58. data/lib/rubocop/cop/layout/space_around_operators.rb +19 -1
  59. data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -2
  60. data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
  61. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
  62. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -2
  63. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
  64. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +2 -2
  65. data/lib/rubocop/cop/lint/boolean_symbol.rb +12 -0
  66. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  67. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  68. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  69. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
  70. data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
  71. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  72. data/lib/rubocop/cop/lint/loop.rb +6 -4
  73. data/lib/rubocop/cop/lint/multiple_comparison.rb +1 -1
  74. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  75. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
  76. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  77. data/lib/rubocop/cop/lint/percent_string_array.rb +2 -2
  78. data/lib/rubocop/cop/lint/raise_exception.rb +75 -0
  79. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -1
  80. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +12 -7
  81. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
  82. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -1
  83. data/lib/rubocop/cop/lint/struct_new_override.rb +58 -0
  84. data/lib/rubocop/cop/lint/suppressed_exception.rb +12 -22
  85. data/lib/rubocop/cop/lint/unified_integer.rb +0 -2
  86. data/lib/rubocop/cop/lint/unused_method_argument.rb +32 -6
  87. data/lib/rubocop/cop/lint/uri_regexp.rb +4 -4
  88. data/lib/rubocop/cop/lint/useless_setter_call.rb +4 -0
  89. data/lib/rubocop/cop/migration/department_name.rb +47 -6
  90. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
  91. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +6 -1
  92. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +7 -7
  93. data/lib/rubocop/cop/mixin/hash_transform_method.rb +171 -0
  94. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
  95. data/lib/rubocop/cop/mixin/method_complexity.rb +5 -0
  96. data/lib/rubocop/cop/mixin/statement_modifier.rb +4 -3
  97. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -10
  98. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  99. data/lib/rubocop/cop/naming/constant_name.rb +2 -1
  100. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  101. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  102. data/lib/rubocop/cop/naming/method_name.rb +30 -0
  103. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  104. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  105. data/lib/rubocop/cop/registry.rb +15 -3
  106. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -6
  107. data/lib/rubocop/cop/style/alias.rb +4 -4
  108. data/lib/rubocop/cop/style/and_or.rb +5 -6
  109. data/lib/rubocop/cop/style/array_join.rb +1 -1
  110. data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
  111. data/lib/rubocop/cop/style/case_equality.rb +24 -1
  112. data/lib/rubocop/cop/style/character_literal.rb +2 -2
  113. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  114. data/lib/rubocop/cop/style/conditional_assignment.rb +8 -8
  115. data/lib/rubocop/cop/style/copyright.rb +1 -1
  116. data/lib/rubocop/cop/style/dir.rb +1 -1
  117. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -0
  118. data/lib/rubocop/cop/style/documentation.rb +43 -5
  119. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -1
  120. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  121. data/lib/rubocop/cop/style/each_with_object.rb +3 -3
  122. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  123. data/lib/rubocop/cop/style/end_block.rb +6 -0
  124. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  125. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
  126. data/lib/rubocop/cop/style/exponential_notation.rb +119 -0
  127. data/lib/rubocop/cop/style/format_string.rb +2 -2
  128. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +89 -11
  129. data/lib/rubocop/cop/style/hash_each_methods.rb +89 -0
  130. data/lib/rubocop/cop/style/hash_syntax.rb +3 -5
  131. data/lib/rubocop/cop/style/hash_transform_keys.rb +83 -0
  132. data/lib/rubocop/cop/style/hash_transform_values.rb +80 -0
  133. data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -3
  134. data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
  135. data/lib/rubocop/cop/style/lambda.rb +3 -2
  136. data/lib/rubocop/cop/style/lambda_call.rb +2 -2
  137. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -205
  138. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +169 -0
  139. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  140. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  141. data/lib/rubocop/cop/style/module_function.rb +58 -12
  142. data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
  143. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  144. data/lib/rubocop/cop/style/multiline_when_then.rb +5 -1
  145. data/lib/rubocop/cop/style/mutable_constant.rb +2 -4
  146. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -4
  147. data/lib/rubocop/cop/style/next.rb +2 -2
  148. data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
  149. data/lib/rubocop/cop/style/non_nil_check.rb +4 -4
  150. data/lib/rubocop/cop/style/not.rb +1 -1
  151. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  152. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  153. data/lib/rubocop/cop/style/numeric_predicate.rb +5 -4
  154. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
  155. data/lib/rubocop/cop/style/or_assignment.rb +4 -3
  156. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  157. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  158. data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
  159. data/lib/rubocop/cop/style/proc.rb +1 -1
  160. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  161. data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
  162. data/lib/rubocop/cop/style/redundant_condition.rb +18 -6
  163. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
  164. data/lib/rubocop/cop/style/redundant_exception.rb +3 -3
  165. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  166. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  167. data/lib/rubocop/cop/style/redundant_return.rb +5 -7
  168. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  169. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  170. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  171. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  172. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  173. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  174. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  175. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -4
  176. data/lib/rubocop/cop/style/string_hash_keys.rb +1 -1
  177. data/lib/rubocop/cop/style/symbol_array.rb +3 -3
  178. data/lib/rubocop/cop/style/symbol_literal.rb +2 -2
  179. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -3
  180. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +35 -22
  181. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +41 -0
  182. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +88 -0
  183. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +44 -0
  184. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  185. data/lib/rubocop/cop/style/unpack_first.rb +0 -4
  186. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
  187. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  188. data/lib/rubocop/cop/style/word_array.rb +1 -1
  189. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  190. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
  191. data/lib/rubocop/cop/variable_force.rb +4 -1
  192. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  193. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  194. data/lib/rubocop/formatter/junit_formatter.rb +74 -0
  195. data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
  196. data/lib/rubocop/formatter/tap_formatter.rb +1 -1
  197. data/lib/rubocop/node_pattern.rb +96 -10
  198. data/lib/rubocop/options.rb +7 -1
  199. data/lib/rubocop/processed_source.rb +1 -4
  200. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  201. data/lib/rubocop/rspec/expect_offense.rb +1 -1
  202. data/lib/rubocop/rspec/shared_contexts.rb +5 -4
  203. data/lib/rubocop/runner.rb +1 -1
  204. data/lib/rubocop/target_ruby.rb +151 -0
  205. data/lib/rubocop/version.rb +1 -1
  206. metadata +39 -12
  207. data/lib/rubocop/cop/lint/end_in_method.rb +0 -40
  208. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
  209. data/lib/rubocop/formatter/disabled_lines_formatter.rb +0 -57
@@ -3,10 +3,11 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # This cop checks for missing top-level documentation of
7
- # classes and modules. Classes with no body are exempt from the
8
- # check and so are namespace modules - modules that have nothing in
9
- # their bodies except classes, other modules, or constant definitions.
6
+ # This cop checks for missing top-level documentation of classes and
7
+ # modules. Classes with no body are exempt from the check and so are
8
+ # namespace modules - modules that have nothing in their bodies except
9
+ # classes, other modules, constant definitions or constant visibility
10
+ # declarations.
10
11
  #
11
12
  # The documentation requirement is annulled if the class or module has
12
13
  # a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the
@@ -18,12 +19,42 @@ module RuboCop
18
19
  # # ...
19
20
  # end
20
21
  #
22
+ # module Math
23
+ # end
24
+ #
21
25
  # # good
22
26
  # # Description/Explanation of Person class
23
27
  # class Person
24
28
  # # ...
25
29
  # end
26
30
  #
31
+ # # allowed
32
+ # # Class without body
33
+ # class Person
34
+ # end
35
+ #
36
+ # # Namespace - A namespace can be a class or a module
37
+ # # Containing a class
38
+ # module Namespace
39
+ # # Description/Explanation of Person class
40
+ # class Person
41
+ # # ...
42
+ # end
43
+ # end
44
+ #
45
+ # # Containing constant visibility declaration
46
+ # module Namespace
47
+ # class Private
48
+ # end
49
+ #
50
+ # private_constant :Private
51
+ # end
52
+ #
53
+ # # Containing constant definition
54
+ # module Namespace
55
+ # Public = Class.new
56
+ # end
57
+ #
27
58
  class Documentation < Cop
28
59
  include DocumentationComment
29
60
 
@@ -31,6 +62,9 @@ module RuboCop
31
62
 
32
63
  def_node_matcher :constant_definition?, '{class module casgn}'
33
64
  def_node_search :outer_module, '(const (const nil? _) _)'
65
+ def_node_matcher :constant_visibility_declaration?, <<~PATTERN
66
+ (send nil? {:public_constant :private_constant} ({sym str} _))
67
+ PATTERN
34
68
 
35
69
  def on_class(node)
36
70
  return unless node.body
@@ -59,12 +93,16 @@ module RuboCop
59
93
  return false unless node
60
94
 
61
95
  if node.begin_type?
62
- node.children.all? { |child| constant_definition?(child) }
96
+ node.children.all?(&method(:constant_declaration?))
63
97
  else
64
98
  constant_definition?(node)
65
99
  end
66
100
  end
67
101
 
102
+ def constant_declaration?(node)
103
+ constant_definition?(node) || constant_visibility_declaration?(node)
104
+ end
105
+
68
106
  def compact_namespace?(node)
69
107
  node.loc.name.source =~ /::/
70
108
  end
@@ -45,7 +45,7 @@ module RuboCop
45
45
  end
46
46
 
47
47
  lambda do |corrector|
48
- corrector.replace(comment.loc.expression,
48
+ corrector.replace(comment,
49
49
  comment.text[/#{prefix} \S+/])
50
50
  end
51
51
  end
@@ -42,7 +42,7 @@ module RuboCop
42
42
 
43
43
  max += 1 if range_type == :irange
44
44
 
45
- corrector.replace(node.send_node.source_range,
45
+ corrector.replace(node.send_node,
46
46
  "#{max - min}.times")
47
47
  end
48
48
  end
@@ -48,15 +48,15 @@ module RuboCop
48
48
 
49
49
  first_arg, second_arg = *node.arguments
50
50
 
51
- corrector.replace(first_arg.loc.expression, second_arg.source)
52
- corrector.replace(second_arg.loc.expression, first_arg.source)
51
+ corrector.replace(first_arg, second_arg.source)
52
+ corrector.replace(second_arg, first_arg.source)
53
53
 
54
54
  return_value = return_value(node.body)
55
55
 
56
56
  if return_value_occupies_whole_line?(return_value)
57
57
  corrector.remove(whole_line_expression(return_value))
58
58
  else
59
- corrector.remove(return_value.loc.expression)
59
+ corrector.remove(return_value)
60
60
  end
61
61
  end
62
62
  end
@@ -57,7 +57,7 @@ module RuboCop
57
57
 
58
58
  def autocorrect(node)
59
59
  lambda do |corrector|
60
- corrector.replace(node.source_range, corrected(node))
60
+ corrector.replace(node, corrected(node))
61
61
  end
62
62
  end
63
63
 
@@ -19,6 +19,12 @@ module RuboCop
19
19
  def on_postexe(node)
20
20
  add_offense(node, location: :keyword)
21
21
  end
22
+
23
+ def autocorrect(node)
24
+ lambda do |corrector|
25
+ corrector.replace(node.loc.keyword, 'at_exit')
26
+ end
27
+ end
22
28
  end
23
29
  end
24
30
  end
@@ -38,7 +38,7 @@ module RuboCop
38
38
  replacement_method = replacement_method(arg, method)
39
39
 
40
40
  correction = "#{base_number.source}.#{replacement_method}?"
41
- ->(corrector) { corrector.replace(node.source_range, correction) }
41
+ ->(corrector) { corrector.replace(node, correction) }
42
42
  end
43
43
  end
44
44
 
@@ -97,7 +97,7 @@ module RuboCop
97
97
  autocorrect_expand_path(corrector, current_path, default_dir)
98
98
  elsif (default_dir = pathname_parent_expand_path(node)) ||
99
99
  (default_dir = pathname_new_parent_expand_path(node))
100
- corrector.replace(default_dir.loc.expression, '__dir__')
100
+ corrector.replace(default_dir, '__dir__')
101
101
  remove_parent_method(corrector, default_dir)
102
102
  end
103
103
  end
@@ -145,8 +145,8 @@ module RuboCop
145
145
  else
146
146
  new_path = "'#{parent_path(stripped_current_path)}'"
147
147
 
148
- corrector.replace(current_path.loc.expression, new_path)
149
- corrector.replace(default_dir.loc.expression, '__dir__')
148
+ corrector.replace(current_path, new_path)
149
+ corrector.replace(default_dir, '__dir__')
150
150
  end
151
151
  end
152
152
 
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # This cop enforces consistency when using exponential notation
7
+ # for numbers in the code (eg 1.2e4). Different styles are supported:
8
+ # - `scientific` which enforces a mantissa between 1 (inclusive)
9
+ # and 10 (exclusive).
10
+ # - `engineering` which enforces the exponent to be a multiple of 3
11
+ # and the mantissa to be between 0.1 (inclusive)
12
+ # and 10 (exclusive).
13
+ # - `integral` which enforces the mantissa to always be a whole number
14
+ # without trailing zeroes.
15
+ #
16
+ # @example EnforcedStyle: scientific (default)
17
+ # # Enforces a mantissa between 1 (inclusive) and 10 (exclusive).
18
+ #
19
+ # # bad
20
+ # 10e6
21
+ # 0.3e4
22
+ # 11.7e5
23
+ # 3.14e0
24
+ #
25
+ # # good
26
+ # 1e7
27
+ # 3e3
28
+ # 1.17e6
29
+ # 3.14
30
+ #
31
+ # @example EnforcedStyle: engineering
32
+ # # Enforces using multiple of 3 exponents,
33
+ # # mantissa should be between 0.1 (inclusive) and 1000 (exclusive)
34
+ #
35
+ # # bad
36
+ # 3.2e7
37
+ # 0.1e5
38
+ # 12e5
39
+ # 1232e6
40
+ #
41
+ # # good
42
+ # 32e6
43
+ # 10e3
44
+ # 1.2e6
45
+ # 1.232e9
46
+ #
47
+ # @example EnforcedStyle: integral
48
+ # # Enforces the mantissa to have no decimal part and no
49
+ # # trailing zeroes.
50
+ #
51
+ # # bad
52
+ # 3.2e7
53
+ # 0.1e5
54
+ # 120e4
55
+ #
56
+ # # good
57
+ # 32e6
58
+ # 1e4
59
+ # 12e5
60
+ #
61
+ class ExponentialNotation < Cop
62
+ include ConfigurableEnforcedStyle
63
+
64
+ def on_float(node)
65
+ add_offense(node) if offense?(node)
66
+ end
67
+
68
+ private
69
+
70
+ def scientific?(node)
71
+ mantissa, = node.source.split('e')
72
+ mantissa =~ /^-?[1-9](\.\d*[0-9])?$/
73
+ end
74
+
75
+ def engineering?(node)
76
+ mantissa, exponent = node.source.split('e')
77
+ return false unless /^-?\d+$/.match?(exponent)
78
+ return false unless (exponent.to_i % 3).zero?
79
+ return false if /^-?\d{4}/.match?(mantissa)
80
+ return false if /^-?0\d/.match?(mantissa)
81
+ return false if /^-?0.0/.match?(mantissa)
82
+
83
+ true
84
+ end
85
+
86
+ def integral(node)
87
+ mantissa, = node.source.split('e')
88
+ mantissa =~ /^-?[1-9](\d*[1-9])?$/
89
+ end
90
+
91
+ def offense?(node)
92
+ return false unless node.source['e']
93
+
94
+ case style
95
+ when :scientific
96
+ !scientific?(node)
97
+ when :engineering
98
+ !engineering?(node)
99
+ when :integral
100
+ !integral(node)
101
+ else
102
+ false
103
+ end
104
+ end
105
+
106
+ def message(_node)
107
+ case style
108
+ when :scientific
109
+ 'Use a mantissa in [1, 10[.'
110
+ when :engineering
111
+ 'Use an exponent divisible by 3 and a mantissa in [0.1, 1000[.'
112
+ when :integral
113
+ 'Use an integer as mantissa, without trailing zero.'
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -104,7 +104,7 @@ module RuboCop
104
104
 
105
105
  corrected = "#{style}(#{node.receiver.source}, #{args})"
106
106
 
107
- corrector.replace(node.loc.expression, corrected)
107
+ corrector.replace(node, corrected)
108
108
  end
109
109
 
110
110
  def autocorrect_to_percent(corrector, node)
@@ -119,7 +119,7 @@ module RuboCop
119
119
  "[#{param_args.map(&:source).join(', ')}]"
120
120
  end
121
121
 
122
- corrector.replace(node.loc.expression, "#{format} % #{args}")
122
+ corrector.replace(node, "#{format} % #{args}")
123
123
  end
124
124
  end
125
125
  end
@@ -51,29 +51,65 @@ module RuboCop
51
51
  # module Baz
52
52
  # # ...
53
53
  # end
54
+ #
55
+ # @example EnforcedStyle: always_true
56
+ # # The `always_true` style enforces that the frozen string literal
57
+ # # comment is set to `true`. This is a stricter option than `always`
58
+ # # and forces projects to use frozen string literals.
59
+ # # bad
60
+ # # frozen_string_literal: false
61
+ #
62
+ # module Baz
63
+ # # ...
64
+ # end
65
+ #
66
+ # # bad
67
+ # module Baz
68
+ # # ...
69
+ # end
70
+ #
71
+ # # good
72
+ # # frozen_string_literal: true
73
+ #
74
+ # module Bar
75
+ # # ...
76
+ # end
54
77
  class FrozenStringLiteralComment < Cop
55
78
  include ConfigurableEnforcedStyle
56
79
  include FrozenStringLiteral
57
80
  include RangeHelp
58
81
 
59
- MSG = 'Missing magic comment `# frozen_string_literal: true`.'
82
+ MSG_MISSING_TRUE = 'Missing magic comment `# frozen_string_literal: '\
83
+ 'true`.'
84
+ MSG_MISSING = 'Missing frozen string literal comment.'
60
85
  MSG_UNNECESSARY = 'Unnecessary frozen string literal comment.'
86
+ MSG_DISABLED = 'Frozen string literal comment must be set to `true`.'
61
87
  SHEBANG = '#!'
62
88
 
63
89
  def investigate(processed_source)
64
90
  return if processed_source.tokens.empty?
65
91
 
66
- if frozen_string_literal_comment_exists?
67
- check_for_no_comment(processed_source)
92
+ case style
93
+ when :never
94
+ ensure_no_comment(processed_source)
95
+ when :always_true
96
+ ensure_enabled_comment(processed_source)
68
97
  else
69
- check_for_comment(processed_source)
98
+ ensure_comment(processed_source)
70
99
  end
71
100
  end
72
101
 
73
102
  def autocorrect(node)
74
103
  lambda do |corrector|
75
- if style == :never
104
+ case style
105
+ when :never
76
106
  remove_comment(corrector, node)
107
+ when :always_true
108
+ if frozen_string_literal_specified?
109
+ enable_comment(corrector)
110
+ else
111
+ insert_comment(corrector)
112
+ end
77
113
  else
78
114
  insert_comment(corrector)
79
115
  end
@@ -82,12 +118,27 @@ module RuboCop
82
118
 
83
119
  private
84
120
 
85
- def check_for_no_comment(processed_source)
86
- unnecessary_comment_offense(processed_source) if style == :never
121
+ def ensure_no_comment(processed_source)
122
+ return unless frozen_string_literal_comment_exists?
123
+
124
+ unnecessary_comment_offense(processed_source)
87
125
  end
88
126
 
89
- def check_for_comment(processed_source)
90
- offense(processed_source) unless style == :never
127
+ def ensure_comment(processed_source)
128
+ return if frozen_string_literal_comment_exists?
129
+
130
+ missing_offense(processed_source)
131
+ end
132
+
133
+ def ensure_enabled_comment(processed_source)
134
+ if frozen_string_literal_specified?
135
+ return if frozen_string_literals_enabled?
136
+
137
+ # The comment exists, but is not enabled.
138
+ disabled_offense(processed_source)
139
+ else # The comment doesn't exist at all.
140
+ missing_true_offense(processed_source)
141
+ end
91
142
  end
92
143
 
93
144
  def last_special_comment(processed_source)
@@ -111,11 +162,22 @@ module RuboCop
111
162
  end
112
163
  end
113
164
 
114
- def offense(processed_source)
165
+ def missing_offense(processed_source)
166
+ last_special_comment = last_special_comment(processed_source)
167
+ range = source_range(processed_source.buffer, 0, 0)
168
+
169
+ add_offense(last_special_comment,
170
+ location: range,
171
+ message: MSG_MISSING)
172
+ end
173
+
174
+ def missing_true_offense(processed_source)
115
175
  last_special_comment = last_special_comment(processed_source)
116
176
  range = source_range(processed_source.buffer, 0, 0)
117
177
 
118
- add_offense(last_special_comment, location: range)
178
+ add_offense(last_special_comment,
179
+ location: range,
180
+ message: MSG_MISSING_TRUE)
119
181
  end
120
182
 
121
183
  def unnecessary_comment_offense(processed_source)
@@ -127,11 +189,27 @@ module RuboCop
127
189
  message: MSG_UNNECESSARY)
128
190
  end
129
191
 
192
+ def disabled_offense(processed_source)
193
+ frozen_string_literal_comment =
194
+ frozen_string_literal_comment(processed_source)
195
+
196
+ add_offense(frozen_string_literal_comment,
197
+ location: frozen_string_literal_comment.pos,
198
+ message: MSG_DISABLED)
199
+ end
200
+
130
201
  def remove_comment(corrector, node)
131
202
  corrector.remove(range_with_surrounding_space(range: node.pos,
132
203
  side: :right))
133
204
  end
134
205
 
206
+ def enable_comment(corrector)
207
+ comment = frozen_string_literal_comment(processed_source)
208
+
209
+ corrector.replace(line_range(comment.line),
210
+ FROZEN_STRING_LITERAL_ENABLED)
211
+ end
212
+
135
213
  def insert_comment(corrector)
136
214
  comment = last_special_comment(processed_source)
137
215