rubocop 0.47.1 → 0.48.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.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (242) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +28 -16
  3. data/config/default.yml +203 -115
  4. data/config/disabled.yml +0 -5
  5. data/config/enabled.yml +92 -8
  6. data/lib/rubocop.rb +24 -1
  7. data/lib/rubocop/ast/builder.rb +7 -2
  8. data/lib/rubocop/ast/node.rb +23 -20
  9. data/lib/rubocop/ast/node/and_node.rb +37 -0
  10. data/lib/rubocop/ast/node/array_node.rb +4 -1
  11. data/lib/rubocop/ast/node/case_node.rb +1 -0
  12. data/lib/rubocop/ast/node/ensure_node.rb +25 -0
  13. data/lib/rubocop/ast/node/hash_node.rb +10 -3
  14. data/lib/rubocop/ast/node/if_node.rb +2 -0
  15. data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +23 -0
  16. data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +35 -0
  17. data/lib/rubocop/ast/node/or_node.rb +37 -0
  18. data/lib/rubocop/ast/node/resbody_node.rb +25 -0
  19. data/lib/rubocop/ast/node/send_node.rb +190 -0
  20. data/lib/rubocop/ast/node/when_node.rb +1 -1
  21. data/lib/rubocop/ast/traversal.rb +15 -15
  22. data/lib/rubocop/comment_config.rb +1 -1
  23. data/lib/rubocop/config.rb +39 -15
  24. data/lib/rubocop/config_loader.rb +34 -13
  25. data/lib/rubocop/cop/bundler/ordered_gems.rb +23 -4
  26. data/lib/rubocop/cop/commissioner.rb +4 -0
  27. data/lib/rubocop/cop/cop.rb +5 -0
  28. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +53 -0
  29. data/lib/rubocop/cop/lint/debugger.rb +8 -1
  30. data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -1
  31. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +2 -4
  32. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -1
  33. data/lib/rubocop/cop/lint/each_with_object_argument.rb +3 -1
  34. data/lib/rubocop/cop/lint/empty_ensure.rb +6 -2
  35. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  36. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +20 -18
  37. data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -3
  38. data/lib/rubocop/cop/lint/literal_in_condition.rb +1 -1
  39. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  40. data/lib/rubocop/cop/lint/multiple_compare.rb +5 -3
  41. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +26 -18
  42. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +7 -8
  43. data/lib/rubocop/cop/lint/require_parentheses.rb +7 -13
  44. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +7 -3
  45. data/lib/rubocop/cop/lint/shadowed_exception.rb +2 -6
  46. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +7 -8
  47. data/lib/rubocop/cop/lint/unneeded_disable.rb +35 -11
  48. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
  49. data/lib/rubocop/cop/lint/unreachable_code.rb +5 -2
  50. data/lib/rubocop/cop/lint/unused_block_argument.rb +6 -6
  51. data/lib/rubocop/cop/lint/useless_assignment.rb +2 -1
  52. data/lib/rubocop/cop/lint/useless_comparison.rb +5 -4
  53. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  54. data/lib/rubocop/cop/message_annotator.rb +7 -3
  55. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  56. data/lib/rubocop/cop/metrics/block_nesting.rb +4 -4
  57. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +2 -2
  58. data/lib/rubocop/cop/metrics/line_length.rb +2 -9
  59. data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -3
  60. data/lib/rubocop/cop/metrics/perceived_complexity.rb +2 -2
  61. data/lib/rubocop/cop/mixin/access_modifier_node.rb +1 -1
  62. data/lib/rubocop/cop/mixin/array_hash_indentation.rb +2 -2
  63. data/lib/rubocop/cop/mixin/check_assignment.rb +6 -6
  64. data/lib/rubocop/cop/mixin/duplication.rb +1 -1
  65. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
  66. data/lib/rubocop/cop/mixin/ignored_pattern.rb +27 -0
  67. data/lib/rubocop/cop/mixin/method_preference.rb +2 -0
  68. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +17 -29
  69. data/lib/rubocop/cop/mixin/on_method_def.rb +3 -3
  70. data/lib/rubocop/cop/mixin/percent_literal.rb +27 -0
  71. data/lib/rubocop/cop/mixin/rescue_node.rb +21 -0
  72. data/lib/rubocop/cop/mixin/safe_mode.rb +1 -1
  73. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  74. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -1
  75. data/lib/rubocop/cop/mixin/target_rails_version.rb +16 -0
  76. data/lib/rubocop/cop/mixin/unused_argument.rb +1 -1
  77. data/lib/rubocop/cop/offense.rb +3 -3
  78. data/lib/rubocop/cop/performance/casecmp.rb +1 -1
  79. data/lib/rubocop/cop/performance/detect.rb +2 -1
  80. data/lib/rubocop/cop/performance/double_start_end_with.rb +35 -1
  81. data/lib/rubocop/cop/performance/end_with.rb +3 -1
  82. data/lib/rubocop/cop/performance/flat_map.rb +6 -6
  83. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
  84. data/lib/rubocop/cop/performance/range_include.rb +3 -1
  85. data/lib/rubocop/cop/performance/redundant_match.rb +6 -5
  86. data/lib/rubocop/cop/performance/regexp_match.rb +10 -3
  87. data/lib/rubocop/cop/performance/reverse_each.rb +2 -1
  88. data/lib/rubocop/cop/performance/size.rb +6 -11
  89. data/lib/rubocop/cop/performance/start_with.rb +3 -1
  90. data/lib/rubocop/cop/performance/string_replacement.rb +13 -18
  91. data/lib/rubocop/cop/performance/times_map.rb +4 -4
  92. data/lib/rubocop/cop/rails/action_filter.rb +42 -42
  93. data/lib/rubocop/cop/rails/active_support_aliases.rb +68 -0
  94. data/lib/rubocop/cop/rails/blank.rb +131 -0
  95. data/lib/rubocop/cop/rails/date.rb +25 -28
  96. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +5 -7
  97. data/lib/rubocop/cop/rails/dynamic_find_by.rb +7 -3
  98. data/lib/rubocop/cop/rails/exit.rb +9 -9
  99. data/lib/rubocop/cop/rails/file_path.rb +5 -14
  100. data/lib/rubocop/cop/rails/find_by.rb +8 -10
  101. data/lib/rubocop/cop/rails/find_each.rb +6 -9
  102. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -0
  103. data/lib/rubocop/cop/rails/http_positional_arguments.rb +15 -7
  104. data/lib/rubocop/cop/rails/output.rb +3 -5
  105. data/lib/rubocop/cop/rails/output_safety.rb +4 -8
  106. data/lib/rubocop/cop/rails/pluralization_grammar.rb +25 -24
  107. data/lib/rubocop/cop/rails/present.rb +137 -0
  108. data/lib/rubocop/cop/rails/read_write_attribute.rb +9 -18
  109. data/lib/rubocop/cop/rails/relative_date_constant.rb +53 -0
  110. data/lib/rubocop/cop/rails/request_referer.rb +7 -4
  111. data/lib/rubocop/cop/rails/reversible_migration.rb +1 -2
  112. data/lib/rubocop/cop/rails/safe_navigation.rb +2 -1
  113. data/lib/rubocop/cop/rails/save_bang.rb +10 -10
  114. data/lib/rubocop/cop/rails/skips_model_validations.rb +23 -6
  115. data/lib/rubocop/cop/rails/time_zone.rb +20 -18
  116. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +3 -2
  117. data/lib/rubocop/cop/rails/validation.rb +8 -11
  118. data/lib/rubocop/cop/registry.rb +3 -3
  119. data/lib/rubocop/cop/security/json_load.rb +1 -1
  120. data/lib/rubocop/cop/security/marshal_load.rb +5 -1
  121. data/lib/rubocop/cop/security/yaml_load.rb +3 -3
  122. data/lib/rubocop/cop/severity.rb +1 -1
  123. data/lib/rubocop/cop/style/alias.rb +5 -5
  124. data/lib/rubocop/cop/style/align_hash.rb +1 -1
  125. data/lib/rubocop/cop/style/align_parameters.rb +5 -5
  126. data/lib/rubocop/cop/style/and_or.rb +16 -31
  127. data/lib/rubocop/cop/style/attr.rb +14 -8
  128. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +8 -11
  129. data/lib/rubocop/cop/style/block_delimiters.rb +11 -13
  130. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +19 -23
  131. data/lib/rubocop/cop/style/case_indentation.rb +2 -0
  132. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  133. data/lib/rubocop/cop/style/class_check.rb +5 -7
  134. data/lib/rubocop/cop/style/closing_parenthesis_indentation.rb +5 -4
  135. data/lib/rubocop/cop/style/collection_methods.rb +8 -8
  136. data/lib/rubocop/cop/style/colon_method_call.rb +2 -9
  137. data/lib/rubocop/cop/style/conditional_assignment.rb +38 -45
  138. data/lib/rubocop/cop/style/constant_name.rb +1 -1
  139. data/lib/rubocop/cop/style/documentation_method.rb +1 -0
  140. data/lib/rubocop/cop/style/dot_position.rb +3 -7
  141. data/lib/rubocop/cop/style/double_negation.rb +2 -1
  142. data/lib/rubocop/cop/style/each_with_object.rb +1 -1
  143. data/lib/rubocop/cop/style/empty_else.rb +2 -2
  144. data/lib/rubocop/cop/style/empty_line_after_magic_comment.rb +63 -0
  145. data/lib/rubocop/cop/style/empty_line_between_defs.rb +74 -4
  146. data/lib/rubocop/cop/style/empty_lines_around_begin_body.rb +42 -0
  147. data/lib/rubocop/cop/style/empty_lines_around_exception_handling_keywords.rb +127 -0
  148. data/lib/rubocop/cop/style/empty_literal.rb +17 -9
  149. data/lib/rubocop/cop/style/end_of_line.rb +25 -3
  150. data/lib/rubocop/cop/style/file_name.rb +1 -1
  151. data/lib/rubocop/cop/style/first_method_argument_line_break.rb +1 -1
  152. data/lib/rubocop/cop/style/first_parameter_indentation.rb +17 -19
  153. data/lib/rubocop/cop/style/for.rb +2 -4
  154. data/lib/rubocop/cop/style/format_string.rb +5 -4
  155. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  156. data/lib/rubocop/cop/style/identical_conditional_branches.rb +27 -1
  157. data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
  158. data/lib/rubocop/cop/style/indent_assignment.rb +2 -2
  159. data/lib/rubocop/cop/style/indent_hash.rb +2 -1
  160. data/lib/rubocop/cop/style/indent_heredoc.rb +173 -0
  161. data/lib/rubocop/cop/style/indentation_width.rb +61 -29
  162. data/lib/rubocop/cop/style/inverse_methods.rb +130 -0
  163. data/lib/rubocop/cop/style/lambda_call.rb +15 -11
  164. data/lib/rubocop/cop/style/line_end_concatenation.rb +4 -4
  165. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +26 -14
  166. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -16
  167. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
  168. data/lib/rubocop/cop/style/missing_else.rb +4 -3
  169. data/lib/rubocop/cop/style/mixin_grouping.rb +97 -0
  170. data/lib/rubocop/cop/style/multiline_memoization.rb +38 -5
  171. data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +2 -3
  172. data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +38 -19
  173. data/lib/rubocop/cop/style/mutable_constant.rb +5 -1
  174. data/lib/rubocop/cop/style/negated_if.rb +73 -1
  175. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +21 -19
  176. data/lib/rubocop/cop/style/next.rb +5 -5
  177. data/lib/rubocop/cop/style/non_nil_check.rb +7 -10
  178. data/lib/rubocop/cop/style/not.rb +3 -4
  179. data/lib/rubocop/cop/style/numeric_literals.rb +25 -3
  180. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
  181. data/lib/rubocop/cop/style/one_line_conditional.rb +2 -2
  182. data/lib/rubocop/cop/style/op_method.rb +2 -2
  183. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -3
  184. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +52 -6
  185. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  186. data/lib/rubocop/cop/style/preferred_hash_methods.rb +9 -9
  187. data/lib/rubocop/cop/style/raise_args.rb +28 -24
  188. data/lib/rubocop/cop/style/redundant_freeze.rb +5 -7
  189. data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -3
  190. data/lib/rubocop/cop/style/redundant_self.rb +17 -35
  191. data/lib/rubocop/cop/style/rescue_modifier.rb +2 -14
  192. data/lib/rubocop/cop/style/self_assignment.rb +3 -3
  193. data/lib/rubocop/cop/style/send.rb +4 -5
  194. data/lib/rubocop/cop/style/space_after_not.rb +7 -8
  195. data/lib/rubocop/cop/style/space_around_keyword.rb +8 -9
  196. data/lib/rubocop/cop/style/space_around_operators.rb +19 -15
  197. data/lib/rubocop/cop/style/space_before_first_arg.rb +17 -14
  198. data/lib/rubocop/cop/style/space_inside_brackets.rb +1 -1
  199. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +3 -3
  200. data/lib/rubocop/cop/style/space_inside_parens.rb +1 -1
  201. data/lib/rubocop/cop/style/special_global_vars.rb +14 -14
  202. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -1
  203. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  204. data/lib/rubocop/cop/style/string_methods.rb +10 -5
  205. data/lib/rubocop/cop/style/struct_inheritance.rb +4 -15
  206. data/lib/rubocop/cop/style/symbol_array.rb +31 -35
  207. data/lib/rubocop/cop/style/symbol_proc.rb +2 -2
  208. data/lib/rubocop/cop/style/ternary_parentheses.rb +41 -13
  209. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +6 -9
  210. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +4 -1
  211. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  212. data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -2
  213. data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
  214. data/lib/rubocop/cop/style/word_array.rb +12 -34
  215. data/lib/rubocop/cop/style/zero_length_predicate.rb +11 -4
  216. data/lib/rubocop/cop/team.rb +4 -1
  217. data/lib/rubocop/cop/util.rb +33 -26
  218. data/lib/rubocop/cop/variable_force.rb +13 -13
  219. data/lib/rubocop/cop/variable_force/assignment.rb +1 -8
  220. data/lib/rubocop/cop/variable_force/branch.rb +318 -0
  221. data/lib/rubocop/cop/variable_force/branchable.rb +21 -0
  222. data/lib/rubocop/cop/variable_force/reference.rb +1 -3
  223. data/lib/rubocop/cop/variable_force/scope.rb +36 -20
  224. data/lib/rubocop/cop/variable_force/variable.rb +9 -8
  225. data/lib/rubocop/formatter/colorizable.rb +10 -10
  226. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  227. data/lib/rubocop/formatter/html_formatter.rb +2 -1
  228. data/lib/rubocop/formatter/simple_text_formatter.rb +4 -2
  229. data/lib/rubocop/magic_comment.rb +20 -6
  230. data/lib/rubocop/options.rb +1 -1
  231. data/lib/rubocop/platform.rb +11 -0
  232. data/lib/rubocop/processed_source.rb +1 -1
  233. data/lib/rubocop/remote_config.rb +18 -6
  234. data/lib/rubocop/result_cache.rb +8 -8
  235. data/lib/rubocop/rspec/cop_helper.rb +2 -0
  236. data/lib/rubocop/rspec/shared_contexts.rb +20 -0
  237. data/lib/rubocop/rspec/shared_examples.rb +1 -1
  238. data/lib/rubocop/runner.rb +2 -2
  239. data/lib/rubocop/target_finder.rb +64 -6
  240. data/lib/rubocop/version.rb +2 -4
  241. metadata +27 -4
  242. data/lib/rubocop/cop/variable_force/locatable.rb +0 -200
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ class VariableForce
6
+ # Mix-in module for classes which own a node and need branch information
7
+ # of the node. The user classes must implement #node and #scope.
8
+ module Branchable
9
+ def branch
10
+ return @branch if instance_variable_defined?(:@branch)
11
+ @branch = Branch.of(node, scope: scope)
12
+ end
13
+
14
+ def run_exclusively_with?(other)
15
+ return false if !branch || !other.branch
16
+ branch.exclusive_with?(other.branch)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -5,7 +5,7 @@ module RuboCop
5
5
  class VariableForce
6
6
  # This class represents each reference of a variable.
7
7
  class Reference
8
- include Locatable
8
+ include Branchable
9
9
 
10
10
  VARIABLE_REFERENCE_TYPES = (
11
11
  [VARIABLE_REFERENCE_TYPE] +
@@ -24,8 +24,6 @@ module RuboCop
24
24
 
25
25
  @node = node
26
26
  @scope = scope
27
-
28
- super
29
27
  end
30
28
 
31
29
  # There's an implicit variable reference by the zero-arity `super`:
@@ -15,14 +15,18 @@ module RuboCop
15
15
  block: 0..0
16
16
  }.freeze
17
17
 
18
- attr_reader :node, :variables
18
+ attr_reader :node, :variables, :naked_top_level
19
+ alias naked_top_level? naked_top_level
19
20
 
20
21
  def initialize(node)
21
- # Accept any node type for top level scope
22
- unless SCOPE_TYPES.include?(node.type) || !node.parent
23
- raise ArgumentError,
24
- "Node type must be any of #{SCOPE_TYPES}, " \
25
- "passed #{node.type}"
22
+ unless SCOPE_TYPES.include?(node.type)
23
+ # Accept any node type for top level scope
24
+ if node.parent
25
+ raise ArgumentError,
26
+ "Node type must be any of #{SCOPE_TYPES}, " \
27
+ "passed #{node.type}"
28
+ end
29
+ @naked_top_level = true
26
30
  end
27
31
  @node = node
28
32
  @variables = {}
@@ -41,37 +45,43 @@ module RuboCop
41
45
  end
42
46
 
43
47
  def body_node
44
- child_index = case @node.type
45
- when :module, :sclass then 1
46
- when :def, :class, :block then 2
47
- when :defs then 3
48
- end
48
+ if naked_top_level?
49
+ node
50
+ else
51
+ child_index = case node.type
52
+ when :module, :sclass then 1
53
+ when :def, :class, :block then 2
54
+ when :defs then 3
55
+ end
49
56
 
50
- child_index ? @node.children[child_index] : @node
57
+ node.children[child_index]
58
+ end
59
+ end
60
+
61
+ def include?(target_node)
62
+ !belong_to_outer_scope?(target_node) &&
63
+ !belong_to_inner_scope?(target_node)
51
64
  end
52
65
 
53
66
  def each_node(&block)
54
67
  return to_enum(__method__) unless block_given?
68
+ yield node if naked_top_level?
55
69
  scan_node(node, &block)
56
70
  end
57
71
 
58
72
  private
59
73
 
60
74
  def scan_node(node, &block)
61
- yield node unless node.parent
62
-
63
75
  node.each_child_node do |child_node|
64
- next if belong_to_another_scope?(child_node)
76
+ next unless include?(child_node)
65
77
  yield child_node
66
78
  scan_node(child_node, &block)
67
79
  end
68
80
  end
69
81
 
70
- def belong_to_another_scope?(node)
71
- belong_to_outer_scope?(node) || belong_to_inner_scope?(node)
72
- end
73
-
74
82
  def belong_to_outer_scope?(target_node)
83
+ return true if !naked_top_level? && target_node.equal?(node)
84
+ return true if ancestor_node?(target_node)
75
85
  return false unless target_node.parent.equal?(node)
76
86
  indices = OUTER_SCOPE_CHILD_INDICES[target_node.parent.type]
77
87
  return false unless indices
@@ -79,12 +89,18 @@ module RuboCop
79
89
  end
80
90
 
81
91
  def belong_to_inner_scope?(target_node)
82
- return false if target_node.parent.equal?(node)
92
+ return false if !target_node.parent || target_node.parent.equal?(node)
83
93
  return false unless SCOPE_TYPES.include?(target_node.parent.type)
84
94
  indices = OUTER_SCOPE_CHILD_INDICES[target_node.parent.type]
85
95
  return true unless indices
86
96
  !indices.include?(target_node.sibling_index)
87
97
  end
98
+
99
+ def ancestor_node?(target_node)
100
+ node.each_ancestor.any? do |ancestor_node|
101
+ ancestor_node.equal?(target_node)
102
+ end
103
+ end
88
104
  end
89
105
  end
90
106
  end
@@ -41,19 +41,20 @@ module RuboCop
41
41
  def reference!(node)
42
42
  reference = Reference.new(node, @scope)
43
43
  @references << reference
44
- consumed_branch_ids = Set.new
44
+ consumed_branches = Set.new
45
45
 
46
46
  @assignments.reverse_each do |assignment|
47
- next if consumed_branch_ids.include?(assignment.branch_id)
47
+ next if consumed_branches.include?(assignment.branch)
48
48
 
49
49
  unless assignment.run_exclusively_with?(reference)
50
50
  assignment.reference!
51
51
  end
52
52
 
53
- break unless assignment.inside_of_branch?
54
- break if assignment.branch_id == reference.branch_id
55
- next if assignment.reference_penetrable?
56
- consumed_branch_ids << assignment.branch_id
53
+ break if !assignment.branch || assignment.branch == reference.branch
54
+
55
+ unless assignment.branch.may_run_incompletely?
56
+ consumed_branches << assignment.branch
57
+ end
57
58
  end
58
59
  end
59
60
 
@@ -82,7 +83,7 @@ module RuboCop
82
83
  end
83
84
 
84
85
  def method_argument?
85
- argument? && [:def, :defs].include?(@scope.node.type)
86
+ argument? && %i(def defs).include?(@scope.node.type)
86
87
  end
87
88
 
88
89
  def block_argument?
@@ -90,7 +91,7 @@ module RuboCop
90
91
  end
91
92
 
92
93
  def keyword_argument?
93
- [:kwarg, :kwoptarg].include?(@declaration_node.type)
94
+ %i(kwarg kwoptarg).include?(@declaration_node.type)
94
95
  end
95
96
 
96
97
  def explicit_block_local_variable?
@@ -22,16 +22,16 @@ module RuboCop
22
22
  rainbow.wrap(string).color(*args)
23
23
  end
24
24
 
25
- [
26
- :black,
27
- :red,
28
- :green,
29
- :yellow,
30
- :blue,
31
- :magenta,
32
- :cyan,
33
- :white
34
- ].each do |color|
25
+ %i(
26
+ black
27
+ red
28
+ green
29
+ yellow
30
+ blue
31
+ magenta
32
+ cyan
33
+ white
34
+ ).each do |color|
35
35
  define_method(color) do |string|
36
36
  colorize(string, color)
37
37
  end
@@ -22,7 +22,7 @@ module RuboCop
22
22
  'worst' => WorstOffendersFormatter
23
23
  }.freeze
24
24
 
25
- FORMATTER_APIS = [:started, :finished].freeze
25
+ FORMATTER_APIS = %i(started finished).freeze
26
26
 
27
27
  FORMATTER_APIS.each do |method_name|
28
28
  define_method(method_name) do |*args|
@@ -61,7 +61,8 @@ module RuboCop
61
61
 
62
62
  # This class provides helper methods used in the ERB template.
63
63
  class ERBContext
64
- include PathUtil, TextUtil
64
+ include PathUtil
65
+ include TextUtil
65
66
 
66
67
  SEVERITY_COLORS = {
67
68
  refactor: Color.new(0xED, 0x9C, 0x28, 1.0),
@@ -9,7 +9,8 @@ module RuboCop
9
9
  # Offenses are displayed at compact form - just the
10
10
  # location of the problem and the associated message.
11
11
  class SimpleTextFormatter < BaseFormatter
12
- include Colorizable, PathUtil
12
+ include Colorizable
13
+ include PathUtil
13
14
 
14
15
  COLOR_FOR_SEVERITY = {
15
16
  refactor: :yellow,
@@ -90,7 +91,8 @@ module RuboCop
90
91
 
91
92
  # A helper class for building the report summary text.
92
93
  class Report
93
- include Colorizable, TextUtil
94
+ include Colorizable
95
+ include TextUtil
94
96
 
95
97
  def initialize(file_count, offense_count, correction_count, rainbow)
96
98
  @file_count = file_count
@@ -26,6 +26,10 @@ module RuboCop
26
26
  @comment = comment
27
27
  end
28
28
 
29
+ def any?
30
+ frozen_string_literal_specified? || encoding_specified?
31
+ end
32
+
29
33
  # Does the magic comment enable the frozen string literal feature.
30
34
  #
31
35
  # Test whether the frozen string literal value is `true`. Cannot
@@ -42,7 +46,7 @@ module RuboCop
42
46
  #
43
47
  # @return [Boolean]
44
48
  def frozen_string_literal_specified?
45
- !frozen_string_literal.nil?
49
+ specified?(frozen_string_literal)
46
50
  end
47
51
 
48
52
  # Expose the `frozen_string_literal` value coerced to a boolean if possible.
@@ -61,8 +65,16 @@ module RuboCop
61
65
  end
62
66
  end
63
67
 
68
+ def encoding_specified?
69
+ specified?(encoding)
70
+ end
71
+
64
72
  private
65
73
 
74
+ def specified?(value)
75
+ !value.nil?
76
+ end
77
+
66
78
  # Match the entire comment string with a pattern and take the first capture.
67
79
  #
68
80
  # @param pattern [Regexp]
@@ -109,11 +121,10 @@ module RuboCop
109
121
  #
110
122
  # @example Emacs style comment
111
123
  # comment = RuboCop::MagicComment.parse(
112
- # '# -*- encoding: ASCII-8BIT; frozen_string_literal: true -*-'
124
+ # '# -*- encoding: ASCII-8BIT -*-'
113
125
  # )
114
126
  #
115
- # comment.encoding # => 'ascii-8bit'
116
- # comment.frozen_string_literal # => true
127
+ # comment.encoding # => 'ascii-8bit'
117
128
  #
118
129
  # @see https://www.gnu.org/software/emacs/manual/html_node/emacs/Specify-Coding.html
119
130
  # @see https://git.io/vMCXh Emacs handling in Ruby's parse.y
@@ -179,7 +190,7 @@ module RuboCop
179
190
  class SimpleComment < MagicComment
180
191
  # Match `encoding` or `coding`
181
192
  def encoding
182
- extract(/\b(?:en)?coding: (#{TOKEN})/)
193
+ extract(/\b(?:en)?coding: (#{TOKEN})/i)
183
194
  end
184
195
 
185
196
  private
@@ -188,8 +199,11 @@ module RuboCop
188
199
  #
189
200
  # The `frozen_string_literal` magic comment only works if it
190
201
  # is the only text in the comment.
202
+ #
203
+ # Case-insensitive and dashes/underscores are acceptable.
204
+ # @see https://git.io/vM7Mg
191
205
  def extract_frozen_string_literal
192
- extract(/^#\s*frozen_string_literal:\s*(#{TOKEN})\s*$/)
206
+ extract(/\A#\s*frozen[_-]string[_-]literal:\s*(#{TOKEN})\s*\z/i)
193
207
  end
194
208
  end
195
209
  end
@@ -6,7 +6,7 @@ require 'shellwords'
6
6
  module RuboCop
7
7
  # This class handles command line options.
8
8
  class Options
9
- EXITING_OPTIONS = [:version, :verbose_version, :show_cops].freeze
9
+ EXITING_OPTIONS = %i(version verbose_version show_cops).freeze
10
10
  DEFAULT_MAXIMUM_EXCLUSION_ITEMS = 15
11
11
 
12
12
  def initialize
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # This module provides information on the platform that RuboCop is being run
5
+ # on.
6
+ module Platform
7
+ def self.windows?
8
+ RUBY_PLATFORM =~ /cygwin|mswin|mingw|bccwin|wince|emx/
9
+ end
10
+ end
11
+ end
@@ -70,7 +70,7 @@ module RuboCop
70
70
 
71
71
  def valid_syntax?
72
72
  return false if @parser_error
73
- @diagnostics.none? { |d| [:error, :fatal].include?(d.level) }
73
+ @diagnostics.none? { |d| %i(error fatal).include?(d.level) }
74
74
  end
75
75
 
76
76
  # Raw source checksum for tracking infinite loops.
@@ -27,17 +27,29 @@ module RuboCop
27
27
 
28
28
  private
29
29
 
30
- def request
31
- http = Net::HTTP.new(@uri.hostname, @uri.port)
32
- http.use_ssl = true if @uri.instance_of? URI::HTTPS
30
+ def request(uri = @uri, limit = 10, &block)
31
+ raise ArgumentError, 'HTTP redirect too deep' if limit.zero?
33
32
 
34
- request = Net::HTTP::Get.new(@uri.request_uri)
33
+ http = Net::HTTP.new(uri.hostname, uri.port)
34
+ http.use_ssl = true if uri.instance_of? URI::HTTPS
35
+
36
+ request = Net::HTTP::Get.new(uri.request_uri)
35
37
  if cache_path_exists?
36
38
  request['If-Modified-Since'] = File.stat(cache_path).mtime.rfc2822
37
39
  end
38
40
 
39
- response = http.request(request)
40
- yield response if response.is_a?(Net::HTTPSuccess)
41
+ handle_response(http.request(request), limit, &block)
42
+ end
43
+
44
+ def handle_response(response, limit, &block)
45
+ case response
46
+ when Net::HTTPSuccess
47
+ yield response
48
+ when Net::HTTPRedirection
49
+ request(URI.parse(response['location']), limit - 1, &block)
50
+ else
51
+ response.error!
52
+ end
41
53
  end
42
54
 
43
55
  def cache_path
@@ -8,8 +8,8 @@ require 'etc'
8
8
  module RuboCop
9
9
  # Provides functionality for caching rubocop runs.
10
10
  class ResultCache
11
- NON_CHANGING = [:color, :format, :formatters, :out, :debug, :fail_level,
12
- :cache, :fail_fast, :stdin].freeze
11
+ NON_CHANGING = %i(color format formatters out debug fail_level
12
+ cache fail_fast stdin).freeze
13
13
 
14
14
  # Remove old files so that the cache doesn't grow too big. When the
15
15
  # threshold MaxFilesInCache has been exceeded, the oldest 50% of all the
@@ -44,20 +44,20 @@ module RuboCop
44
44
  puts "Removing the #{remove_count} oldest files from #{cache_root}"
45
45
  end
46
46
  sorted = files.sort_by { |path| File.mtime(path) }
47
- remove_files(sorted, dirs, remove_count, verbose)
47
+ remove_files(sorted, dirs, remove_count)
48
+ rescue Errno::ENOENT
49
+ # This can happen if parallel RuboCop invocations try to remove the
50
+ # same files. No problem.
51
+ puts $ERROR_INFO if verbose
48
52
  end
49
53
 
50
- def remove_files(files, dirs, remove_count, verbose)
54
+ def remove_files(files, dirs, remove_count)
51
55
  # Batch file deletions, deleting over 130,000+ files will crash
52
56
  # File.delete.
53
57
  files[0, remove_count].each_slice(10_000).each do |files_slice|
54
58
  File.delete(*files_slice)
55
59
  end
56
60
  dirs.each { |dir| Dir.rmdir(dir) if Dir["#{dir}/*"].empty? }
57
- rescue Errno::ENOENT
58
- # This can happen if parallel RuboCop invocations try to remove the
59
- # same files. No problem.
60
- puts $ERROR_INFO if verbose
61
61
  end
62
62
  end
63
63
 
@@ -7,6 +7,8 @@ module CopHelper
7
7
  extend RSpec::SharedContext
8
8
 
9
9
  let(:ruby_version) { 2.2 }
10
+ let(:enabled_rails) { false }
11
+ let(:rails_version) { false }
10
12
 
11
13
  def inspect_source_file(cop, source)
12
14
  Tempfile.open('tmp') { |f| inspect_source(cop, source, f) }