rubocop 0.75.1 → 0.80.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 (199) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +4 -4
  4. data/config/default.yml +374 -335
  5. data/lib/rubocop.rb +53 -32
  6. data/lib/rubocop/ast/builder.rb +43 -41
  7. data/lib/rubocop/ast/node.rb +5 -13
  8. data/lib/rubocop/ast/node/block_node.rb +2 -0
  9. data/lib/rubocop/ast/node/def_node.rb +11 -0
  10. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  11. data/lib/rubocop/ast/node/regexp_node.rb +2 -4
  12. data/lib/rubocop/ast/node/return_node.rb +24 -0
  13. data/lib/rubocop/ast/traversal.rb +20 -3
  14. data/lib/rubocop/cli.rb +11 -227
  15. data/lib/rubocop/cli/command.rb +21 -0
  16. data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
  17. data/lib/rubocop/cli/command/base.rb +33 -0
  18. data/lib/rubocop/cli/command/execute_runner.rb +76 -0
  19. data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
  20. data/lib/rubocop/cli/command/show_cops.rb +80 -0
  21. data/lib/rubocop/cli/command/version.rb +17 -0
  22. data/lib/rubocop/cli/environment.rb +21 -0
  23. data/lib/rubocop/comment_config.rb +8 -3
  24. data/lib/rubocop/config.rb +8 -1
  25. data/lib/rubocop/config_loader.rb +20 -20
  26. data/lib/rubocop/config_loader_resolver.rb +2 -1
  27. data/lib/rubocop/config_obsoletion.rb +73 -10
  28. data/lib/rubocop/config_validator.rb +77 -110
  29. data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
  30. data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
  31. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
  32. data/lib/rubocop/cop/commissioner.rb +15 -7
  33. data/lib/rubocop/cop/cop.rb +31 -6
  34. data/lib/rubocop/cop/corrector.rb +8 -7
  35. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
  36. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  37. data/lib/rubocop/cop/generator.rb +3 -4
  38. data/lib/rubocop/cop/generator/configuration_injector.rb +2 -2
  39. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  40. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
  41. data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
  42. data/lib/rubocop/cop/layout/{align_array.rb → array_alignment.rb} +1 -1
  43. data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +1 -1
  44. data/lib/rubocop/cop/layout/comment_indentation.rb +10 -13
  45. data/lib/rubocop/cop/layout/empty_comment.rb +7 -16
  46. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
  47. data/lib/rubocop/cop/layout/end_of_line.rb +8 -3
  48. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  49. data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +14 -12
  50. data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
  51. data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +4 -4
  52. data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
  53. data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +16 -8
  54. data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +5 -5
  55. data/lib/rubocop/cop/layout/leading_comment_space.rb +33 -2
  56. data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
  57. data/lib/rubocop/cop/{metrics → layout}/line_length.rb +68 -112
  58. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
  59. data/lib/rubocop/cop/layout/multiline_block_layout.rb +14 -5
  60. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
  61. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  62. data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
  63. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -0
  64. data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
  65. data/lib/rubocop/cop/layout/space_around_operators.rb +50 -7
  66. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  67. data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
  68. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -7
  69. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -4
  70. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
  71. data/lib/rubocop/cop/layout/space_inside_parens.rb +6 -6
  72. data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
  73. data/lib/rubocop/cop/layout/trailing_whitespace.rb +18 -2
  74. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  75. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
  76. data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
  77. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  78. data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -8
  79. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  80. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +2 -2
  81. data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
  82. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  83. data/lib/rubocop/cop/lint/{unneeded_cop_disable_directive.rb → redundant_cop_disable_directive.rb} +26 -26
  84. data/lib/rubocop/cop/lint/{unneeded_cop_enable_directive.rb → redundant_cop_enable_directive.rb} +18 -15
  85. data/lib/rubocop/cop/lint/{unneeded_require_statement.rb → redundant_require_statement.rb} +1 -1
  86. data/lib/rubocop/cop/lint/{unneeded_splat_expansion.rb → redundant_splat_expansion.rb} +6 -6
  87. data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +1 -1
  88. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  89. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  90. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -6
  91. data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
  92. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
  93. data/lib/rubocop/cop/lint/useless_setter_call.rb +5 -1
  94. data/lib/rubocop/cop/lint/void.rb +4 -4
  95. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  96. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  97. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +23 -6
  98. data/lib/rubocop/cop/migration/department_name.rb +30 -2
  99. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  100. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
  101. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +7 -7
  102. data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
  103. data/lib/rubocop/cop/mixin/hash_transform_method.rb +172 -0
  104. data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
  105. data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
  106. data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
  107. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  108. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -4
  109. data/lib/rubocop/cop/mixin/trailing_comma.rb +16 -18
  110. data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
  111. data/lib/rubocop/cop/naming/file_name.rb +12 -5
  112. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
  113. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  114. data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +4 -4
  115. data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
  116. data/lib/rubocop/cop/offense.rb +11 -0
  117. data/lib/rubocop/cop/registry.rb +8 -3
  118. data/lib/rubocop/cop/style/alias.rb +1 -1
  119. data/lib/rubocop/cop/style/array_join.rb +1 -1
  120. data/lib/rubocop/cop/style/attr.rb +10 -2
  121. data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
  122. data/lib/rubocop/cop/style/comment_annotation.rb +5 -5
  123. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  124. data/lib/rubocop/cop/style/copyright.rb +11 -7
  125. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +2 -2
  126. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -2
  127. data/lib/rubocop/cop/style/empty_literal.rb +2 -2
  128. data/lib/rubocop/cop/style/empty_method.rb +5 -5
  129. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  130. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  131. data/lib/rubocop/cop/style/format_string.rb +10 -7
  132. data/lib/rubocop/cop/style/format_string_token.rb +2 -0
  133. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +99 -11
  134. data/lib/rubocop/cop/style/guard_clause.rb +3 -2
  135. data/lib/rubocop/cop/style/hash_each_methods.rb +87 -0
  136. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  137. data/lib/rubocop/cop/style/hash_transform_keys.rb +79 -0
  138. data/lib/rubocop/cop/style/hash_transform_values.rb +79 -0
  139. data/lib/rubocop/cop/style/if_unless_modifier.rb +45 -3
  140. data/lib/rubocop/cop/style/infinite_loop.rb +5 -4
  141. data/lib/rubocop/cop/style/inverse_methods.rb +19 -13
  142. data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
  143. data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -10
  144. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -205
  145. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +169 -0
  146. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  147. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -9
  148. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  149. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  150. data/lib/rubocop/cop/style/multiline_when_then.rb +6 -2
  151. data/lib/rubocop/cop/style/nested_modifier.rb +4 -2
  152. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +5 -5
  153. data/lib/rubocop/cop/style/next.rb +5 -5
  154. data/lib/rubocop/cop/style/non_nil_check.rb +21 -9
  155. data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
  156. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
  157. data/lib/rubocop/cop/style/option_hash.rb +3 -3
  158. data/lib/rubocop/cop/style/or_assignment.rb +3 -2
  159. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  160. data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +1 -1
  161. data/lib/rubocop/cop/style/{unneeded_condition.rb → redundant_condition.rb} +3 -3
  162. data/lib/rubocop/cop/style/{unneeded_interpolation.rb → redundant_interpolation.rb} +1 -1
  163. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -3
  164. data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +1 -1
  165. data/lib/rubocop/cop/style/redundant_return.rb +27 -29
  166. data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +5 -5
  167. data/lib/rubocop/cop/style/safe_navigation.rb +13 -10
  168. data/lib/rubocop/cop/style/semicolon.rb +2 -2
  169. data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
  170. data/lib/rubocop/cop/style/symbol_array.rb +2 -2
  171. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  172. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -22
  173. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  174. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
  175. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  176. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  177. data/lib/rubocop/cop/team.rb +5 -0
  178. data/lib/rubocop/cop/util.rb +1 -1
  179. data/lib/rubocop/cop/utils/format_string.rb +10 -18
  180. data/lib/rubocop/cop/variable_force.rb +11 -6
  181. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  182. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -3
  183. data/lib/rubocop/formatter/formatter_set.rb +1 -0
  184. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  185. data/lib/rubocop/formatter/junit_formatter.rb +63 -0
  186. data/lib/rubocop/formatter/tap_formatter.rb +1 -3
  187. data/lib/rubocop/node_pattern.rb +100 -12
  188. data/lib/rubocop/options.rb +17 -11
  189. data/lib/rubocop/processed_source.rb +1 -1
  190. data/lib/rubocop/rake_task.rb +1 -0
  191. data/lib/rubocop/result_cache.rb +24 -8
  192. data/lib/rubocop/rspec/shared_contexts.rb +5 -0
  193. data/lib/rubocop/runner.rb +50 -29
  194. data/lib/rubocop/target_finder.rb +12 -6
  195. data/lib/rubocop/target_ruby.rb +151 -0
  196. data/lib/rubocop/version.rb +1 -1
  197. metadata +69 -35
  198. data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
  199. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Metrics/LineLength
3
+ # rubocop:disable Layout/LineLength
4
4
 
5
5
  module RuboCop
6
6
  module Formatter
@@ -41,7 +41,7 @@ module RuboCop
41
41
  # * `#finished`
42
42
  #
43
43
  class BaseFormatter
44
- # rubocop:enable Metrics/LineLength
44
+ # rubocop:enable Layout/LineLength
45
45
 
46
46
  # @api public
47
47
  #
@@ -24,16 +24,14 @@ module RuboCop
24
24
  message: message(offense)
25
25
  )
26
26
 
27
- # rubocop:disable Lint/HandleExceptions
28
27
  begin
29
28
  return unless valid_line?(offense)
30
29
 
31
30
  report_line(offense.location)
32
31
  report_highlighted_area(offense.highlighted_area)
33
- rescue IndexError
32
+ rescue IndexError # rubocop:disable Lint/SuppressedException
34
33
  # range is not on a valid line; perhaps the source file is empty
35
34
  end
36
- # rubocop:enable Lint/HandleExceptions
37
35
  end
38
36
 
39
37
  def valid_line?(offense)
@@ -17,6 +17,7 @@ module RuboCop
17
17
  '[fu]ubar' => FuubarStyleFormatter,
18
18
  '[h]tml' => HTMLFormatter,
19
19
  '[j]son' => JSONFormatter,
20
+ '[ju]nit' => JUnitFormatter,
20
21
  '[o]ffenses' => OffenseCountFormatter,
21
22
  '[pa]cman' => PacmanFormatter,
22
23
  '[p]rogress' => ProgressFormatter,
@@ -53,11 +53,12 @@ module RuboCop
53
53
 
54
54
  def hash_for_offense(offense)
55
55
  {
56
- severity: offense.severity.name,
57
- message: offense.message,
58
- cop_name: offense.cop_name,
59
- corrected: offense.corrected?,
60
- location: hash_for_location(offense)
56
+ severity: offense.severity.name,
57
+ message: offense.message,
58
+ cop_name: offense.cop_name,
59
+ corrected: offense.corrected?,
60
+ correctable: offense.correctable?,
61
+ location: hash_for_location(offense)
61
62
  }
62
63
  end
63
64
 
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rexml/document'
4
+
5
+ #
6
+ # This code is based on https://github.com/mikian/rubocop-junit-formatter.
7
+ #
8
+ # Copyright (c) 2015 Mikko Kokkonen
9
+ #
10
+ # MIT License
11
+ #
12
+ # https://github.com/mikian/rubocop-junit-formatter/blob/master/LICENSE.txt
13
+ #
14
+ module RuboCop
15
+ module Formatter
16
+ # This formatter formats the report data in JUnit format.
17
+ class JUnitFormatter < BaseFormatter
18
+ def initialize(output, options = {})
19
+ super
20
+
21
+ @document = REXML::Document.new.tap do |document|
22
+ document << REXML::XMLDecl.new
23
+ end
24
+ testsuites = REXML::Element.new('testsuites', @document)
25
+ testsuite = REXML::Element.new('testsuite', testsuites)
26
+ @testsuite = testsuite.tap do |element|
27
+ element.add_attributes('name' => 'rubocop')
28
+ end
29
+ end
30
+
31
+ def file_finished(file, offenses)
32
+ offenses.group_by(&:cop_name).each do |cop_name, grouped_offenses|
33
+ REXML::Element.new('testcase', @testsuite).tap do |testcase|
34
+ testcase.attributes['classname'] = file.gsub(
35
+ /\.rb\Z/, ''
36
+ ).gsub("#{Dir.pwd}/", '').tr('/', '.')
37
+ testcase.attributes['name'] = cop_name
38
+
39
+ add_failure_to(testcase, grouped_offenses, cop_name)
40
+ end
41
+ end
42
+ end
43
+
44
+ def finished(_inspected_files)
45
+ @document.write(output, 2)
46
+ end
47
+
48
+ private
49
+
50
+ def add_failure_to(testcase, offenses, cop_name)
51
+ # One failure per offense. Zero failures is a passing test case,
52
+ # for most surefire/nUnit parsers.
53
+ offenses.each do |offense|
54
+ REXML::Element.new('failure', testcase).tap do |failure|
55
+ failure.attributes['type'] = cop_name
56
+ failure.attributes['message'] = offense.message
57
+ failure.add_text(offense.location.to_s)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -51,16 +51,14 @@ module RuboCop
51
51
  message: message(offense)
52
52
  )
53
53
 
54
- # rubocop:disable Lint/HandleExceptions
55
54
  begin
56
55
  return unless valid_line?(offense)
57
56
 
58
57
  report_line(offense.location)
59
58
  report_highlighted_area(offense.highlighted_area)
60
- rescue IndexError
59
+ rescue IndexError # rubocop:disable Lint/SuppressedException
61
60
  # range is not on a valid line; perhaps the source file is empty
62
61
  end
63
- # rubocop:enable Lint/HandleExceptions
64
62
  end
65
63
 
66
64
  def annotate_message(msg)
@@ -78,6 +78,8 @@ module RuboCop
78
78
  # # matching process starts
79
79
  # '^^send' # each ^ ascends one level in the AST
80
80
  # # so this matches against the grandparent node
81
+ # '`send' # descends any number of level in the AST
82
+ # # so this matches against any descendant node
81
83
  # '#method' # we call this a 'funcall'; it calls a method in the
82
84
  # # context where a pattern-matching method is defined
83
85
  # # if that returns a truthy value, the match succeeds
@@ -112,7 +114,7 @@ module RuboCop
112
114
  SYMBOL = %r{:(?:[\w+@*/?!<>=~|%^-]+|\[\]=?)}.freeze
113
115
  IDENTIFIER = /[a-zA-Z_][a-zA-Z0-9_-]*/.freeze
114
116
  META = Regexp.union(
115
- %w"( ) { } [ ] $< < > $... $ ! ^ ... + * ?"
117
+ %w"( ) { } [ ] $< < > $... $ ! ^ ` ... + * ?"
116
118
  ).freeze
117
119
  NUMBER = /-?\d+(?:\.\d+)?/.freeze
118
120
  STRING = /".+?"/.freeze
@@ -188,7 +190,7 @@ module RuboCop
188
190
 
189
191
  @temps = 0 # avoid name clashes between temp variables
190
192
  @captures = 0 # number of captures seen
191
- @unify = {} # named wildcard -> temp variable number
193
+ @unify = {} # named wildcard -> temp variable
192
194
  @params = 0 # highest % (param) number seen
193
195
  run(node_var)
194
196
  end
@@ -223,6 +225,7 @@ module RuboCop
223
225
  when '!' then compile_negation
224
226
  when '$' then compile_capture
225
227
  when '^' then compile_ascend
228
+ when '`' then compile_descend
226
229
  when WILDCARD then compile_wildcard(token[1..-1])
227
230
  when FUNCALL then compile_funcall(token)
228
231
  when LITERAL then compile_literal(token)
@@ -380,7 +383,7 @@ module RuboCop
380
383
  def compile_seq_head
381
384
  return unless seq_head?
382
385
 
383
- fail_due_to 'sequences can not start with <' \
386
+ fail_due_to 'sequences cannot start with <' \
384
387
  if @terms[0].respond_to? :call
385
388
 
386
389
  with_seq_head_context(@terms[0])
@@ -429,12 +432,13 @@ module RuboCop
429
432
  [0..Float::INFINITY, 'true']
430
433
  end
431
434
 
435
+ # rubocop:disable Metrics/AbcSize
432
436
  # rubocop:disable Metrics/MethodLength
433
437
  def compile_any_order(capture_all = nil)
434
438
  rest = capture_rest = nil
435
439
  patterns = []
436
440
  with_temp_variables do |child, matched|
437
- tokens_until('>', 'any child').each do
441
+ tokens_until('>', 'any child') do
438
442
  fail_due_to 'ellipsis must be at the end of <>' if rest
439
443
  token = tokens.shift
440
444
  case token
@@ -448,6 +452,7 @@ module RuboCop
448
452
  end
449
453
  end
450
454
  # rubocop:enable Metrics/MethodLength
455
+ # rubocop:enable Metrics/AbcSize
451
456
 
452
457
  def insure_same_captures(enum, what)
453
458
  return to_enum __method__, enum, what unless block_given?
@@ -464,12 +469,67 @@ module RuboCop
464
469
  end
465
470
  end
466
471
 
472
+ def access_unify(name)
473
+ var = @unify[name]
474
+
475
+ if var == :forbidden_unification
476
+ fail_due_to "Wildcard #{name} was first seen in a subset of a" \
477
+ " union and can't be used outside that union"
478
+ end
479
+ var
480
+ end
481
+
482
+ def forbid_unification(*names)
483
+ names.each do |name|
484
+ @unify[name] = :forbidden_unification
485
+ end
486
+ end
487
+
488
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
489
+ def unify_in_union(enum)
490
+ # We need to reset @unify before each branch is processed.
491
+ # Moreover we need to keep track of newly encountered wildcards.
492
+ # Var `new_unify_intersection` will hold those that are encountered
493
+ # in all branches; these are not a problem.
494
+ # Var `partial_unify` will hold those encountered in only a subset
495
+ # of the branches; these can't be used outside of the union.
496
+
497
+ return to_enum __method__, enum unless block_given?
498
+
499
+ new_unify_intersection = nil
500
+ partial_unify = []
501
+ unify_before = @unify.dup
502
+
503
+ result = enum.each do |e|
504
+ @unify = unify_before.dup if new_unify_intersection
505
+ yield e
506
+ new_unify = @unify.keys - unify_before.keys
507
+ if new_unify_intersection.nil?
508
+ # First iteration
509
+ new_unify_intersection = new_unify
510
+ else
511
+ union = new_unify_intersection | new_unify
512
+ new_unify_intersection &= new_unify
513
+ partial_unify |= union - new_unify_intersection
514
+ end
515
+ end
516
+
517
+ # At this point, all members of `new_unify_intersection` can be used
518
+ # for unification outside of the union, but partial_unify may not
519
+
520
+ forbid_unification(*partial_unify)
521
+
522
+ result
523
+ end
524
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
525
+
467
526
  def compile_union
468
527
  # we need to ensure that each branch of the {} contains the same
469
528
  # number of captures (since only one branch of the {} can actually
470
529
  # match, the same variables are used to hold the captures for each
471
530
  # branch)
472
531
  enum = tokens_until('}', 'union')
532
+ enum = unify_in_union(enum)
473
533
  terms = insure_same_captures(enum, 'branch of {}')
474
534
  .map { compile_expr }
475
535
 
@@ -494,6 +554,19 @@ module RuboCop
494
554
  with_context("#{CUR_NODE} && #{compile_expr}", "#{CUR_NODE}.parent")
495
555
  end
496
556
 
557
+ def compile_descend
558
+ with_temp_variables do |descendant|
559
+ pattern = with_context(compile_expr, descendant,
560
+ use_temp_node: false)
561
+ [
562
+ "RuboCop::NodePattern.descend(#{CUR_ELEMENT}).",
563
+ "any? do |#{descendant}|",
564
+ " #{pattern}",
565
+ 'end'
566
+ ].join("\n")
567
+ end
568
+ end
569
+
497
570
  def compile_wildcard(name)
498
571
  if name.empty?
499
572
  'true'
@@ -501,12 +574,12 @@ module RuboCop
501
574
  # we have already seen a wildcard with this name before
502
575
  # so the value it matched the first time will already be stored
503
576
  # in a temp. check if this value matches the one stored in the temp
504
- "#{CUR_ELEMENT} == temp#{@unify[name]}"
577
+ "#{CUR_ELEMENT} == #{access_unify(name)}"
505
578
  else
506
- n = @unify[name] = next_temp_value
507
- # double assign to temp#{n} to avoid "assigned but unused variable"
508
- "(temp#{n} = #{CUR_ELEMENT}; " \
509
- "temp#{n} = temp#{n}; true)"
579
+ n = @unify[name] = "unify_#{name.gsub('-', '__')}"
580
+ # double assign to avoid "assigned but unused variable"
581
+ "(#{n} = #{CUR_ELEMENT}; " \
582
+ "#{n} = #{n}; true)"
510
583
  end
511
584
  end
512
585
 
@@ -558,9 +631,8 @@ module RuboCop
558
631
  def compile_arg(token)
559
632
  case token
560
633
  when WILDCARD then
561
- name = token[1..-1]
562
- number = @unify[name] || fail_due_to('invalid in arglist: ' + token)
563
- "temp#{number}"
634
+ name = token[1..-1]
635
+ access_unify(name) || fail_due_to('invalid in arglist: ' + token)
564
636
  when LITERAL then token
565
637
  when PARAM then get_param(token[1..-1])
566
638
  when CLOSING then fail_due_to("#{token} in invalid position")
@@ -794,6 +866,22 @@ module RuboCop
794
866
  def to_s
795
867
  "#<#{self.class} #{pattern}>"
796
868
  end
869
+
870
+ # Yields its argument and any descendants, depth-first.
871
+ #
872
+ def self.descend(element, &block)
873
+ return to_enum(__method__, element) unless block_given?
874
+
875
+ yield element
876
+
877
+ if element.is_a?(::RuboCop::AST::Node)
878
+ element.children.each do |child|
879
+ descend(child, &block)
880
+ end
881
+ end
882
+
883
+ nil
884
+ end
797
885
  end
798
886
  end
799
887
  # rubocop:enable Metrics/ClassLength, Metrics/CyclomaticComplexity
@@ -255,21 +255,27 @@ module RuboCop
255
255
  @options = options
256
256
  end
257
257
 
258
+ def validate_cop_options
259
+ %i[only except].each do |opt|
260
+ OptionsValidator.validate_cop_list(@options[opt])
261
+ end
262
+ end
263
+
258
264
  # rubocop:disable Metrics/AbcSize
259
265
  def validate_compatibility # rubocop:disable Metrics/MethodLength
260
- if only_includes_unneeded_disable?
261
- raise OptionArgumentError, 'Lint/UnneededCopDisableDirective can not ' \
266
+ if only_includes_redundant_disable?
267
+ raise OptionArgumentError, 'Lint/RedundantCopDisableDirective cannot ' \
262
268
  'be used with --only.'
263
269
  end
264
270
  if except_syntax?
265
- raise OptionArgumentError, 'Syntax checking can not be turned off.'
271
+ raise OptionArgumentError, 'Syntax checking cannot be turned off.'
266
272
  end
267
273
  unless boolean_or_empty_cache?
268
274
  raise OptionArgumentError, '-C/--cache argument must be true or false'
269
275
  end
270
276
 
271
277
  if display_only_fail_level_offenses_with_autocorrect?
272
- raise OptionArgumentError, '--autocorrect can not be used with ' \
278
+ raise OptionArgumentError, '--autocorrect cannot be used with ' \
273
279
  '--display-only-fail-level-offenses'
274
280
  end
275
281
  validate_auto_gen_config
@@ -323,8 +329,8 @@ module RuboCop
323
329
  auto_gen_config: '-P/--parallel uses caching to speed up execution, ' \
324
330
  'while --auto-gen-config needs a non-cached run, ' \
325
331
  'so they cannot be combined.',
326
- fail_fast: '-P/--parallel can not be combined with -F/--fail-fast.',
327
- auto_correct: '-P/--parallel can not be combined with --auto-correct.'
332
+ fail_fast: '-P/--parallel cannot be combined with -F/--fail-fast.',
333
+ auto_correct: '-P/--parallel cannot be combined with --auto-correct.'
328
334
  }
329
335
 
330
336
  combos.each do |key, msg|
@@ -332,10 +338,10 @@ module RuboCop
332
338
  end
333
339
  end
334
340
 
335
- def only_includes_unneeded_disable?
341
+ def only_includes_redundant_disable?
336
342
  @options.key?(:only) &&
337
- (@options[:only] & %w[Lint/UnneededCopDisableDirective
338
- UnneededCopDisableDirective]).any?
343
+ (@options[:only] & %w[Lint/RedundantCopDisableDirective
344
+ RedundantCopDisableDirective]).any?
339
345
  end
340
346
 
341
347
  def display_only_fail_level_offenses_with_autocorrect?
@@ -367,7 +373,7 @@ module RuboCop
367
373
  # This module contains help texts for command line options.
368
374
  module OptionsHelp
369
375
  MAX_EXCL = RuboCop::Options::DEFAULT_MAXIMUM_EXCLUSION_ITEMS.to_s
370
- # rubocop:disable Metrics/LineLength
376
+ # rubocop:disable Layout/LineLength
371
377
  FORMATTER_OPTION_LIST = RuboCop::Formatter::FormatterSet::BUILTIN_FORMATTERS_FOR_KEYS.keys
372
378
 
373
379
  TEXT = {
@@ -449,6 +455,6 @@ module RuboCop
449
455
  'reports. This is useful for editor integration.'],
450
456
  init: 'Generate a .rubocop.yml file in the current directory.'
451
457
  }.freeze
452
- # rubocop:enable Metrics/LineLength
458
+ # rubocop:enable Layout/LineLength
453
459
  end
454
460
  end
@@ -163,7 +163,7 @@ module RuboCop
163
163
  ast, comments, tokens = parser.tokenize(@buffer)
164
164
 
165
165
  ast.respond_to?(:complete!) && ast.complete!
166
- rescue Parser::SyntaxError # rubocop:disable Lint/HandleExceptions
166
+ rescue Parser::SyntaxError # rubocop:disable Lint/SuppressedException
167
167
  # All errors are in diagnostics. No need to handle exception.
168
168
  end
169
169
 
@@ -70,6 +70,7 @@ module RuboCop
70
70
  RakeFileUtils.verbose(verbose) do
71
71
  yield(*[self, task_args].slice(0, task_block.arity)) if block_given?
72
72
  options = full_options.unshift('--auto-correct')
73
+ options.delete('--parallel')
73
74
  run_cli(verbose, options)
74
75
  end
75
76
  end
@@ -77,12 +77,13 @@ module RuboCop
77
77
  config_store.for('.').for_all_cops['AllowSymlinksInCacheRootDirectory']
78
78
  end
79
79
 
80
- def initialize(file, options, config_store, cache_root = nil)
80
+ def initialize(file, team, options, config_store, cache_root = nil)
81
81
  cache_root ||= ResultCache.cache_root(config_store)
82
82
  @allow_symlinks_in_cache_location =
83
83
  ResultCache.allow_symlinks_in_cache_location?(config_store)
84
- @path = File.join(cache_root, rubocop_checksum,
85
- relevant_options_digest(options),
84
+ @path = File.join(cache_root,
85
+ rubocop_checksum,
86
+ context_checksum(team, options),
86
87
  file_checksum(file, config_store))
87
88
  @cached_data = CachedData.new(file)
88
89
  end
@@ -100,7 +101,7 @@ module RuboCop
100
101
 
101
102
  begin
102
103
  FileUtils.mkdir_p(dir)
103
- rescue Errno::EACCES => e
104
+ rescue Errno::EACCES, Errno::EROFS => e
104
105
  warn "Couldn't create cache directory. Continuing without cache."\
105
106
  "\n #{e.message}"
106
107
  return
@@ -182,10 +183,25 @@ module RuboCop
182
183
  # don't affect caching.
183
184
  def relevant_options_digest(options)
184
185
  options = options.reject { |key, _| NON_CHANGING.include?(key) }
185
- options = options.to_s.gsub(/[^a-z]+/i, '_')
186
- # We must avoid making file names too long for some filesystems to handle
187
- # If they are short, we can leave them human-readable
188
- options.length <= 32 ? options : Digest::SHA1.hexdigest(options)
186
+ options.to_s.gsub(/[^a-z]+/i, '_')
187
+ end
188
+
189
+ # The external dependency checksums are cached per RuboCop team so that
190
+ # the checksums don't need to be recomputed for each file.
191
+ def team_checksum(team)
192
+ @checksum_by_team ||= {}
193
+ @checksum_by_team[team.object_id] ||= team.external_dependency_checksum
194
+ end
195
+
196
+ # We combine team and options into a single "context" checksum to avoid
197
+ # making file names that are too long for some filesystems to handle.
198
+ # This context is for anything that's not (1) the RuboCop executable
199
+ # checksum or (2) the inspected file checksum.
200
+ def context_checksum(team, options)
201
+ Digest::SHA1.hexdigest([
202
+ team_checksum(team),
203
+ relevant_options_digest(options)
204
+ ].join)
189
205
  end
190
206
  end
191
207
  end