rubocop 1.45.1 → 1.47.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 (160) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +10 -9
  4. data/lib/rubocop/cli/command/auto_generate_config.rb +7 -0
  5. data/lib/rubocop/comment_config.rb +19 -0
  6. data/lib/rubocop/cop/autocorrect_logic.rb +1 -1
  7. data/lib/rubocop/cop/base.rb +1 -1
  8. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  9. data/lib/rubocop/cop/corrector.rb +1 -1
  10. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
  11. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +3 -3
  12. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +3 -3
  13. data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
  14. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +2 -2
  15. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +1 -1
  16. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
  17. data/lib/rubocop/cop/internal_affairs/cop_description.rb +4 -4
  18. data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +1 -1
  19. data/lib/rubocop/cop/internal_affairs/location_expression.rb +37 -0
  20. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
  21. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -1
  22. data/lib/rubocop/cop/internal_affairs/processed_source_buffer_name.rb +42 -0
  23. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -1
  24. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -1
  25. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +39 -0
  26. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  27. data/lib/rubocop/cop/layout/block_end_newline.rb +4 -4
  28. data/lib/rubocop/cop/layout/class_structure.rb +5 -3
  29. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +1 -1
  30. data/lib/rubocop/cop/layout/empty_comment.rb +3 -3
  31. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
  32. data/lib/rubocop/cop/layout/end_alignment.rb +4 -0
  33. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  34. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +8 -2
  35. data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
  36. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  37. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -3
  38. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +11 -7
  39. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -2
  40. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +2 -2
  41. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  42. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
  43. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  44. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
  45. data/lib/rubocop/cop/lint/debugger.rb +3 -0
  46. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
  47. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
  48. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
  49. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -3
  50. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  51. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  52. data/lib/rubocop/cop/lint/empty_conditional_body.rb +4 -2
  53. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  54. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  55. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +46 -4
  56. data/lib/rubocop/cop/lint/missing_super.rb +31 -2
  57. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -9
  58. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +6 -10
  59. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  60. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  61. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +10 -4
  62. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -5
  63. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -1
  64. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  65. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -1
  66. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -1
  67. data/lib/rubocop/cop/lint/refinement_import_methods.rb +2 -1
  68. data/lib/rubocop/cop/lint/rescue_type.rb +3 -3
  69. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +1 -1
  70. data/lib/rubocop/cop/lint/script_permission.rb +1 -1
  71. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
  72. data/lib/rubocop/cop/lint/to_enum_arguments.rb +6 -2
  73. data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -9
  74. data/lib/rubocop/cop/lint/useless_rescue.rb +2 -1
  75. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  76. data/lib/rubocop/cop/metrics/collection_literal_length.rb +76 -0
  77. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -2
  78. data/lib/rubocop/cop/migration/department_name.rb +1 -1
  79. data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -1
  80. data/lib/rubocop/cop/mixin/code_length.rb +1 -1
  81. data/lib/rubocop/cop/mixin/comments_help.rb +2 -2
  82. data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
  83. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +1 -1
  84. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +10 -7
  85. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  86. data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +0 -3
  87. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
  88. data/lib/rubocop/cop/mixin/range_help.rb +1 -6
  89. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  90. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  91. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +1 -1
  92. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  93. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  94. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  95. data/lib/rubocop/cop/style/accessor_grouping.rb +22 -12
  96. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -3
  97. data/lib/rubocop/cop/style/array_intersect.rb +1 -1
  98. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  99. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +1 -1
  100. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  101. data/lib/rubocop/cop/style/block_delimiters.rb +2 -2
  102. data/lib/rubocop/cop/style/case_like_if.rb +1 -1
  103. data/lib/rubocop/cop/style/collection_compact.rb +1 -1
  104. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  105. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  106. data/lib/rubocop/cop/style/concat_array_literals.rb +10 -2
  107. data/lib/rubocop/cop/style/conditional_assignment.rb +6 -6
  108. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  109. data/lib/rubocop/cop/style/documentation.rb +1 -1
  110. data/lib/rubocop/cop/style/documentation_method.rb +4 -4
  111. data/lib/rubocop/cop/style/each_with_object.rb +1 -1
  112. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  113. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  114. data/lib/rubocop/cop/style/eval_with_location.rb +4 -4
  115. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  116. data/lib/rubocop/cop/style/file_read.rb +1 -1
  117. data/lib/rubocop/cop/style/file_write.rb +1 -1
  118. data/lib/rubocop/cop/style/guard_clause.rb +1 -1
  119. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  120. data/lib/rubocop/cop/style/if_unless_modifier.rb +34 -0
  121. data/lib/rubocop/cop/style/inverse_methods.rb +5 -5
  122. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +2 -2
  123. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +1 -1
  124. data/lib/rubocop/cop/style/min_max.rb +3 -3
  125. data/lib/rubocop/cop/style/mixin_grouping.rb +4 -4
  126. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  127. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  128. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -1
  129. data/lib/rubocop/cop/style/nil_lambda.rb +2 -2
  130. data/lib/rubocop/cop/style/redundant_condition.rb +2 -2
  131. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +2 -2
  132. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  133. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  134. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +5 -6
  135. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +10 -3
  136. data/lib/rubocop/cop/style/redundant_sort.rb +3 -3
  137. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -2
  138. data/lib/rubocop/cop/style/require_order.rb +1 -3
  139. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -2
  140. data/lib/rubocop/cop/style/safe_navigation.rb +2 -2
  141. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -1
  142. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
  143. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  144. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  145. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -1
  146. data/lib/rubocop/cop/style/unpack_first.rb +3 -3
  147. data/lib/rubocop/cop/style/word_array.rb +17 -5
  148. data/lib/rubocop/cop/style/yoda_condition.rb +1 -1
  149. data/lib/rubocop/cop/style/zero_length_predicate.rb +9 -5
  150. data/lib/rubocop/cop/team.rb +11 -8
  151. data/lib/rubocop/cop/util.rb +13 -4
  152. data/lib/rubocop/cop/variable_force/variable.rb +5 -3
  153. data/lib/rubocop/directive_comment.rb +3 -3
  154. data/lib/rubocop/ext/comment.rb +18 -0
  155. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  156. data/lib/rubocop/rspec/shared_contexts.rb +4 -0
  157. data/lib/rubocop/target_ruby.rb +1 -1
  158. data/lib/rubocop/version.rb +1 -1
  159. data/lib/rubocop.rb +2 -0
  160. metadata +10 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 648a8aa3156aebf0c078bc0b600f301d3aa0abaf92d6b3643c343718575f7302
4
- data.tar.gz: 21b185886d70b70a163c6e7f3ea47e04ee0a0afcf11217e0d7bcb31e25440880
3
+ metadata.gz: 3790198ce32105233a07bc15c82a62ec188e7430910b16a7ff10eefbfa89c0a2
4
+ data.tar.gz: aa440d2b6a863b774d63731cbf779cd204491f675b5ad0d4b708a1b8708dcd3b
5
5
  SHA512:
6
- metadata.gz: ff5a5e947001ba9507f3e806896559baaf0e4bccc6b2cc56316dffc3ae334f62046040be389518139d2979c4d09900600f9b233bc1f4dbd2d1d37a2f8fe99037
7
- data.tar.gz: 8f89f55198c9d206c5b8d0c32cbeb5033491046c0ea0f4311f6c1a23595bcce852ec732606ca1fd2bc499b31ecb2265dcf6d85edd352f6400d01bd9385993a44
6
+ metadata.gz: 2adcafe5b28f4c2a440d3cee024b3e2462a4b60d6ddf5136f63972255e484059780281f2be264c82bd21a8c5ebe83433c010b53b8d893f01de9d9e58de586b7f
7
+ data.tar.gz: 66eab3f5f70f3c93ee3263a982b6e7795fec658dd91462d6c5d5bc9f87f1ae99de17fb0fcd04afb060985ad1085505ff7c98d5df6d11a33b40432260ef5e91fc
data/README.md CHANGED
@@ -53,7 +53,7 @@ To prevent an unwanted RuboCop update you might want to use a conservative versi
53
53
  in your `Gemfile`:
54
54
 
55
55
  ```rb
56
- gem 'rubocop', '~> 1.45', require: false
56
+ gem 'rubocop', '~> 1.47', require: false
57
57
  ```
58
58
 
59
59
  See [our versioning policy](https://docs.rubocop.org/rubocop/versioning.html) for further details.
data/config/default.yml CHANGED
@@ -153,6 +153,7 @@ AllCops:
153
153
  rubocop-sequel: [sequel]
154
154
  rubocop-rake: [rake]
155
155
  rubocop-graphql: [graphql]
156
+ rubocop-capybara: [capybara]
156
157
  # Enable/Disable checking the methods extended by Active Support.
157
158
  ActiveSupportExtensionsEnabled: false
158
159
 
@@ -1630,13 +1631,12 @@ Lint/Debugger:
1630
1631
  Description: 'Check for debugger calls.'
1631
1632
  Enabled: true
1632
1633
  VersionAdded: '0.14'
1633
- VersionChanged: '1.10'
1634
+ VersionChanged: '1.46'
1634
1635
  DebuggerMethods:
1635
1636
  # Groups are available so that a specific group can be disabled in
1636
1637
  # a user's configuration, but are otherwise not significant.
1637
1638
  Kernel:
1638
1639
  - binding.irb
1639
- - p
1640
1640
  - Kernel.binding.irb
1641
1641
  Byebug:
1642
1642
  - byebug
@@ -1646,9 +1646,6 @@ Lint/Debugger:
1646
1646
  Capybara:
1647
1647
  - save_and_open_page
1648
1648
  - save_and_open_screenshot
1649
- PP:
1650
- - PP.pp
1651
- - pp
1652
1649
  debug.rb:
1653
1650
  - binding.b
1654
1651
  - binding.break
@@ -2547,6 +2544,12 @@ Metrics/ClassLength:
2547
2544
  Max: 100
2548
2545
  CountAsOne: []
2549
2546
 
2547
+ Metrics/CollectionLiteralLength:
2548
+ Description: 'Checks for `Array` or `Hash` literals with many entries.'
2549
+ Enabled: pending
2550
+ VersionAdded: '1.47'
2551
+ LengthThreshold: 250
2552
+
2550
2553
  # Avoid complex methods.
2551
2554
  Metrics/CyclomaticComplexity:
2552
2555
  Description: >-
@@ -2927,6 +2930,7 @@ Naming/VariableNumber:
2927
2930
  - rfc822 # Time#rfc822
2928
2931
  - rfc2822 # Time#rfc2822
2929
2932
  - rfc3339 # DateTime.rfc3339
2933
+ - x86_64 # Allowed by default as an underscore separated CPU architecture name
2930
2934
  AllowedPatterns: []
2931
2935
 
2932
2936
  #################### Security ##############################
@@ -3054,6 +3058,7 @@ Style/ArrayCoercion:
3054
3058
  Style/ArrayIntersect:
3055
3059
  Description: 'Use `array1.intersect?(array2)` instead of `(array1 & array2).any?`.'
3056
3060
  Enabled: 'pending'
3061
+ Safe: false
3057
3062
  VersionAdded: '1.40'
3058
3063
 
3059
3064
  Style/ArrayJoin:
@@ -4027,10 +4032,6 @@ Style/InverseMethods:
4027
4032
  :=~: :!~
4028
4033
  :<: :>=
4029
4034
  :>: :<=
4030
- # `ActiveSupport` defines some common inverse methods. They are listed below,
4031
- # and not enabled by default.
4032
- # :present?: :blank?,
4033
- # :include?: :exclude?
4034
4035
  # `InverseBlocks` are methods that are inverted by inverting the return
4035
4036
  # of the block that is passed to the method
4036
4037
  InverseBlocks:
@@ -16,6 +16,7 @@ module RuboCop
16
16
 
17
17
  PHASE_1_OVERRIDDEN = '(skipped because the default Layout/LineLength:Max is overridden)'
18
18
  PHASE_1_DISABLED = '(skipped because Layout/LineLength is disabled)'
19
+ PHASE_1_SKIPPED = '(skipped because a list of cops is passed to the `--only` flag)'
19
20
 
20
21
  def run
21
22
  add_formatter
@@ -31,6 +32,8 @@ module RuboCop
31
32
  skip_line_length_cop(PHASE_1_DISABLED)
32
33
  elsif !same_max_line_length?(@config_store.for_pwd, ConfigLoader.default_configuration)
33
34
  skip_line_length_cop(PHASE_1_OVERRIDDEN)
35
+ elsif options_has_only_flag?
36
+ skip_line_length_cop(PHASE_1_SKIPPED)
34
37
  else
35
38
  run_line_length_cop
36
39
  end
@@ -57,6 +60,10 @@ module RuboCop
57
60
  config.for_cop('Layout/LineLength')
58
61
  end
59
62
 
63
+ def options_has_only_flag?
64
+ @options[:only]
65
+ end
66
+
60
67
  # Do an initial run with only Layout/LineLength so that cops that
61
68
  # depend on Layout/LineLength:Max get the correct value for that
62
69
  # parameter.
@@ -11,6 +11,8 @@ module RuboCop
11
11
  # This class provides an API compatible with RuboCop::DirectiveComment
12
12
  # to be used for cops that are disabled in the config file
13
13
  class ConfigDisabledCopDirectiveComment
14
+ include RuboCop::Ext::Comment
15
+
14
16
  attr_reader :text, :loc, :line_number
15
17
 
16
18
  Loc = Struct.new(:expression)
@@ -42,6 +44,10 @@ module RuboCop
42
44
  disabled_line_ranges.none? { |range| range.include?(line_number) }
43
45
  end
44
46
 
47
+ def cop_opted_in?(cop)
48
+ opt_in_cops.include?(cop.cop_name)
49
+ end
50
+
45
51
  def cop_disabled_line_ranges
46
52
  @cop_disabled_line_ranges ||= analyze
47
53
  end
@@ -74,6 +80,19 @@ module RuboCop
74
80
  extras
75
81
  end
76
82
 
83
+ def opt_in_cops
84
+ @opt_in_cops ||= begin
85
+ cops = Set.new
86
+ each_directive do |directive|
87
+ next unless directive.enabled?
88
+ next if directive.all_cops?
89
+
90
+ cops.merge(directive.cop_names)
91
+ end
92
+ cops
93
+ end
94
+ end
95
+
77
96
  def analyze # rubocop:todo Metrics/AbcSize
78
97
  return {} if @no_directives
79
98
 
@@ -65,7 +65,7 @@ module RuboCop
65
65
  heredoc_nodes = processed_source.ast.each_descendant.select do |node|
66
66
  node.respond_to?(:heredoc?) && node.heredoc?
67
67
  end
68
- heredoc_nodes.map { |node| node.loc.expression.join(node.loc.heredoc_end) }
68
+ heredoc_nodes.map { |node| node.source_range.join(node.loc.heredoc_end) }
69
69
  .find { |range| range.contains?(offense_range) }
70
70
  end
71
71
 
@@ -416,7 +416,7 @@ module RuboCop
416
416
 
417
417
  def range_from_node_or_range(node_or_range)
418
418
  if node_or_range.respond_to?(:loc)
419
- node_or_range.loc.expression
419
+ node_or_range.source_range
420
420
  elsif node_or_range.is_a?(::Parser::Source::Range)
421
421
  node_or_range
422
422
  else
@@ -124,7 +124,7 @@ module RuboCop
124
124
  end
125
125
 
126
126
  def preceding_comment?(node1, node2)
127
- node1 && node2 && precede?(node2, node1) && comment_line?(node2.loc.expression.source)
127
+ node1 && node2 && precede?(node2, node1) && comment_line?(node2.source)
128
128
  end
129
129
 
130
130
  def ignored_gem?(node)
@@ -104,7 +104,7 @@ module RuboCop
104
104
  def to_range(node_or_range)
105
105
  range = case node_or_range
106
106
  when ::RuboCop::AST::Node, ::Parser::Source::Comment
107
- node_or_range.loc.expression
107
+ node_or_range.source_range
108
108
  when ::Parser::Source::Range
109
109
  node_or_range
110
110
  else
@@ -16,7 +16,7 @@ module RuboCop
16
16
  return unless node
17
17
 
18
18
  @processed_source = processed_source
19
- expr = node.respond_to?(:loc) ? node.loc.expression : node
19
+ expr = node.respond_to?(:loc) ? node.source_range : node
20
20
  return if block_comment_within?(expr)
21
21
 
22
22
  taboo_ranges = inside_string_ranges(node)
@@ -80,7 +80,7 @@ module RuboCop
80
80
 
81
81
  def block_comment_within?(expr)
82
82
  processed_source.comments.select(&:document?).any? do |c|
83
- within?(c.loc.expression, expr)
83
+ within?(c.source_range, expr)
84
84
  end
85
85
  end
86
86
 
@@ -35,15 +35,15 @@ module RuboCop
35
35
 
36
36
  def offending_range
37
37
  if block_node.arguments?
38
- replacement_range(argument_node.loc.expression.end_pos)
38
+ replacement_range(argument_node.source_range.end_pos)
39
39
  else
40
40
  replacement_range(block_node.loc.begin.end_pos)
41
41
  end
42
42
  end
43
43
 
44
44
  def replacement_range(end_pos)
45
- Parser::Source::Range.new(block_node.loc.expression.source_buffer,
46
- block_node.loc.expression.begin_pos,
45
+ Parser::Source::Range.new(block_node.source_range.source_buffer,
46
+ block_node.source_range.begin_pos,
47
47
  end_pos)
48
48
  end
49
49
  end
@@ -56,7 +56,7 @@ module RuboCop
56
56
  if collection_node.begin_type?
57
57
  collection_node.loc.end
58
58
  else
59
- collection_node.loc.expression
59
+ collection_node.source_range
60
60
  end
61
61
  end
62
62
 
@@ -65,8 +65,8 @@ module RuboCop
65
65
  end
66
66
 
67
67
  def replacement_range(end_pos)
68
- Parser::Source::Range.new(for_node.loc.expression.source_buffer,
69
- for_node.loc.expression.begin_pos,
68
+ Parser::Source::Range.new(for_node.source_range.source_buffer,
69
+ for_node.source_range.begin_pos,
70
70
  end_pos)
71
71
  end
72
72
  end
@@ -35,7 +35,7 @@ module RuboCop
35
35
  def move_comment(eol_comment:, node:, corrector:)
36
36
  return unless eol_comment
37
37
 
38
- text = eol_comment.loc.expression.source
38
+ text = eol_comment.source
39
39
  corrector.insert_before(node, "#{text}\n#{' ' * node.loc.keyword.column}")
40
40
  corrector.remove(eol_comment)
41
41
  end
@@ -52,7 +52,7 @@ module RuboCop
52
52
  return unless (parent = node.parent)
53
53
  return unless use_heredoc_argument_method_chain?(parent)
54
54
 
55
- chained_method = range_between(parent.loc.dot.begin_pos, parent.loc.expression.end_pos)
55
+ chained_method = range_between(parent.loc.dot.begin_pos, parent.source_range.end_pos)
56
56
 
57
57
  corrector.remove(chained_method)
58
58
  corrector.insert_after(end_range, chained_method.source)
@@ -80,7 +80,7 @@ module RuboCop
80
80
  def select_content_to_be_inserted_after_last_element(corrector, node)
81
81
  range = range_between(
82
82
  node.loc.end.begin_pos,
83
- range_by_whole_lines(node.loc.expression).end.end_pos
83
+ range_by_whole_lines(node.source_range).end.end_pos
84
84
  )
85
85
 
86
86
  remove_trailing_content_of_comment(corrector, range)
@@ -26,7 +26,7 @@ module RuboCop
26
26
  def declaration_with_comment(node)
27
27
  buffer = processed_source.buffer
28
28
  begin_pos = range_by_whole_lines(get_source_range(node, comments_as_separators)).begin_pos
29
- end_line = buffer.line_for_position(node.loc.expression.end_pos)
29
+ end_line = buffer.line_for_position(node.source_range.end_pos)
30
30
  end_pos = range_by_whole_lines(buffer.line_range(end_line),
31
31
  include_final_newline: true).end_pos
32
32
 
@@ -74,7 +74,7 @@ module RuboCop
74
74
  def add_heredoc_comma(corrector, node)
75
75
  return unless heredoc?(node)
76
76
 
77
- corrector.insert_after(node.child_nodes.last.loc.expression, ',')
77
+ corrector.insert_after(node.child_nodes.last.source_range, ',')
78
78
  end
79
79
 
80
80
  def heredoc?(node)
@@ -59,9 +59,9 @@ module RuboCop
59
59
  end
60
60
 
61
61
  def range(node, comment_line)
62
- source_buffer = node.loc.expression.source_buffer
62
+ source_buffer = node.source_range.source_buffer
63
63
 
64
- begin_pos = node.loc.expression.begin_pos
64
+ begin_pos = node.source_range.begin_pos
65
65
  begin_pos += comment_index(node, comment_line)
66
66
  end_pos = begin_pos + comment_body(comment_line).length
67
67
 
@@ -77,7 +77,7 @@ module RuboCop
77
77
  end
78
78
 
79
79
  def first_comment_line(node)
80
- node.loc.expression.source.lines.find { |line| comment_line?(line) }
80
+ node.source.lines.find { |line| comment_line?(line) }
81
81
  end
82
82
 
83
83
  def comment_body(comment_line)
@@ -86,7 +86,7 @@ module RuboCop
86
86
 
87
87
  def comment_index(node, comment_line)
88
88
  body = comment_body(comment_line)
89
- node.loc.expression.source.index(body)
89
+ node.source.index(body)
90
90
  end
91
91
 
92
92
  def relevant_file?(file)
@@ -92,7 +92,7 @@ module RuboCop
92
92
  # @return [Parser::Source::Range]
93
93
  def heredoc_openning_delimiter_range_from(node)
94
94
  match_data = node.source.match(Heredoc::OPENING_DELIMITER)
95
- node.location.expression.begin.adjust(
95
+ node.source_range.begin.adjust(
96
96
  begin_pos: match_data.begin(2),
97
97
  end_pos: match_data.end(2)
98
98
  )
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Enforces the use of `node.source_range` instead of `node.location.expression`.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ # node.location.expression
12
+ # node.loc.expression
13
+ #
14
+ # # good
15
+ # node.source_range
16
+ #
17
+ class LocationExpression < Base
18
+ extend AutoCorrector
19
+
20
+ MSG = 'Use `source_range` instead.'
21
+ RESTRICT_ON_SEND = %i[loc location].freeze
22
+
23
+ def on_send(node)
24
+ return unless (parent = node.parent)
25
+ return unless parent.send_type? && parent.method?(:expression)
26
+ return unless parent.receiver.receiver
27
+
28
+ offense = node.loc.selector.join(parent.source_range.end)
29
+
30
+ add_offense(offense) do |corrector|
31
+ corrector.replace(offense, 'source_range')
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -99,7 +99,7 @@ module RuboCop
99
99
  # If the pattern matcher uses arguments (`%1`, `%2`, etc.), include them in the directive
100
100
  arguments = pattern_arguments(node.arguments[1].source)
101
101
 
102
- range = range_with_surrounding_space(node.loc.expression, side: :left, newlines: false)
102
+ range = range_with_surrounding_space(node.source_range, side: :left, newlines: false)
103
103
  indentation = range.source.match(/^\s*/)[0]
104
104
  directive = "#{indentation}# @!method #{actual_name}(#{arguments.join(', ')})\n"
105
105
  directive = "\n#{directive}" if add_newline?(node)
@@ -30,7 +30,7 @@ module RuboCop
30
30
 
31
31
  message = format(MSG, type: node_type)
32
32
  add_offense(node, message: message) do |corrector|
33
- range = node.loc.expression.with(begin_pos: receiver.loc.expression.end_pos + 1)
33
+ range = node.source_range.with(begin_pos: receiver.source_range.end_pos + 1)
34
34
  corrector.replace(range, "#{node_type}_type?")
35
35
  end
36
36
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Enforces the use of `processed_source.file_path` instead of `processed_source.buffer.name`.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ # processed_source.buffer.name
12
+ #
13
+ # # good
14
+ # processed_source.file_path
15
+ #
16
+ class ProcessedSourceBufferName < Base
17
+ extend AutoCorrector
18
+
19
+ MSG = 'Use `file_path` instead.'
20
+
21
+ RESTRICT_ON_SEND = %i[name].freeze
22
+
23
+ # @!method processed_source_buffer_name?(node)
24
+ def_node_matcher :processed_source_buffer_name?, <<~PATTERN
25
+ (send
26
+ (send
27
+ {(lvar :processed_source) (send nil? :processed_source)} :buffer) :name)
28
+ PATTERN
29
+
30
+ def on_send(node)
31
+ return unless processed_source_buffer_name?(node)
32
+
33
+ offense_range = node.children.first.loc.selector.begin.join(node.source_range.end)
34
+
35
+ add_offense(offense_range) do |corrector|
36
+ corrector.replace(offense_range, 'file_path')
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -43,7 +43,7 @@ module RuboCop
43
43
  private
44
44
 
45
45
  def offending_range(node)
46
- with_space = range_with_surrounding_space(node.loc.expression)
46
+ with_space = range_with_surrounding_space(node.source_range)
47
47
 
48
48
  range_with_surrounding_comma(with_space, :left)
49
49
  end
@@ -56,7 +56,7 @@ module RuboCop
56
56
  private
57
57
 
58
58
  def offending_range(node)
59
- with_space = range_with_surrounding_space(node.loc.expression)
59
+ with_space = range_with_surrounding_space(node.source_range)
60
60
 
61
61
  range_with_surrounding_comma(with_space, :left)
62
62
  end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Checks for redundant `source_range`.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ # node.source_range.source
12
+ #
13
+ # # good
14
+ # node.source
15
+ #
16
+ class RedundantSourceRange < Base
17
+ extend AutoCorrector
18
+
19
+ MSG = 'Remove the redundant `source_range`.'
20
+ RESTRICT_ON_SEND = %i[source].freeze
21
+
22
+ # @!method redundant_source_range(node)
23
+ def_node_matcher :redundant_source_range, <<~PATTERN
24
+ (send $(send _ :source_range) :source)
25
+ PATTERN
26
+
27
+ def on_send(node)
28
+ return unless (source_range = redundant_source_range(node))
29
+
30
+ selector = source_range.loc.selector
31
+
32
+ add_offense(selector) do |corrector|
33
+ corrector.remove(source_range.loc.dot.join(selector))
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -7,6 +7,7 @@ require_relative 'internal_affairs/example_description'
7
7
  require_relative 'internal_affairs/example_heredoc_delimiter'
8
8
  require_relative 'internal_affairs/inherit_deprecated_cop_class'
9
9
  require_relative 'internal_affairs/lambda_or_proc'
10
+ require_relative 'internal_affairs/location_expression'
10
11
  require_relative 'internal_affairs/location_line_equality_comparison'
11
12
  require_relative 'internal_affairs/method_name_end_with'
12
13
  require_relative 'internal_affairs/method_name_equal'
@@ -15,12 +16,14 @@ require_relative 'internal_affairs/node_matcher_directive'
15
16
  require_relative 'internal_affairs/node_type_predicate'
16
17
  require_relative 'internal_affairs/numblock_handler'
17
18
  require_relative 'internal_affairs/offense_location_keyword'
19
+ require_relative 'internal_affairs/processed_source_buffer_name'
18
20
  require_relative 'internal_affairs/redundant_context_config_parameter'
19
21
  require_relative 'internal_affairs/redundant_described_class_as_subject'
20
22
  require_relative 'internal_affairs/redundant_let_rubocop_config_new'
21
23
  require_relative 'internal_affairs/redundant_location_argument'
22
24
  require_relative 'internal_affairs/redundant_message_argument'
23
25
  require_relative 'internal_affairs/redundant_method_dispatch_node'
26
+ require_relative 'internal_affairs/redundant_source_range'
24
27
  require_relative 'internal_affairs/single_line_comparison'
25
28
  require_relative 'internal_affairs/style_detected_api_use'
26
29
  require_relative 'internal_affairs/undefined_config'
@@ -73,14 +73,14 @@ module RuboCop
73
73
 
74
74
  def offense_range(node)
75
75
  Parser::Source::Range.new(
76
- node.loc.expression.source_buffer,
77
- node.children.compact.last.loc.expression.end_pos,
78
- end_of_method_chain(node).loc.expression.end_pos
76
+ node.source_range.source_buffer,
77
+ node.children.compact.last.source_range.end_pos,
78
+ end_of_method_chain(node).source_range.end_pos
79
79
  )
80
80
  end
81
81
 
82
82
  def replacement(node)
83
- end_with_method_chain = node.loc.end.join(end_of_method_chain(node).loc.expression.end)
83
+ end_with_method_chain = node.loc.end.join(end_of_method_chain(node).source_range.end)
84
84
 
85
85
  "\n#{end_with_method_chain.source.strip}"
86
86
  end
@@ -285,10 +285,12 @@ module RuboCop
285
285
  end
286
286
 
287
287
  def end_position_for(node)
288
- heredoc = find_heredoc(node)
289
- return heredoc.location.heredoc_end.end_pos + 1 if heredoc
288
+ if node.casgn_type?
289
+ heredoc = find_heredoc(node)
290
+ return heredoc.location.heredoc_end.end_pos + 1 if heredoc
291
+ end
290
292
 
291
- end_line = buffer.line_for_position(node.loc.expression.end_pos)
293
+ end_line = buffer.line_for_position(node.source_range.end_pos)
292
294
  buffer.line_range(end_line).end_pos
293
295
  end
294
296
 
@@ -85,7 +85,7 @@ module RuboCop
85
85
  end
86
86
 
87
87
  def heredoc_opening(node)
88
- node.loc.expression.source_line
88
+ node.source_range.source_line
89
89
  end
90
90
 
91
91
  def heredoc_closing(node)
@@ -97,9 +97,9 @@ module RuboCop
97
97
  def autocorrect(corrector, node)
98
98
  previous_token = previous_token(node)
99
99
  range = if previous_token && same_line?(node, previous_token)
100
- range_with_surrounding_space(node.loc.expression, newlines: false)
100
+ range_with_surrounding_space(node.source_range, newlines: false)
101
101
  else
102
- range_by_whole_lines(node.loc.expression, include_final_newline: true)
102
+ range_by_whole_lines(node.source_range, include_final_newline: true)
103
103
  end
104
104
 
105
105
  corrector.remove(range)
@@ -137,7 +137,7 @@ module RuboCop
137
137
  end
138
138
 
139
139
  def current_token(comment)
140
- processed_source.find_token { |token| token.pos == comment.loc.expression }
140
+ processed_source.find_token { |token| token.pos == comment.source_range }
141
141
  end
142
142
 
143
143
  def previous_token(node)
@@ -235,7 +235,7 @@ module RuboCop
235
235
 
236
236
  def end_loc(node)
237
237
  if (node.def_type? || node.defs_type?) && node.endless?
238
- node.loc.expression.end
238
+ node.source_range.end
239
239
  else
240
240
  node.loc.end
241
241
  end
@@ -82,6 +82,10 @@ module RuboCop
82
82
  check_other_alignment(node)
83
83
  end
84
84
 
85
+ def on_sclass(node)
86
+ check_other_alignment(node)
87
+ end
88
+
85
89
  def on_module(node)
86
90
  check_other_alignment(node)
87
91
  end
@@ -255,7 +255,7 @@ module RuboCop
255
255
  @comment_lines ||=
256
256
  processed_source
257
257
  .comments
258
- .select { |c| begins_its_line?(c.loc.expression) }
258
+ .select { |c| begins_its_line?(c.source_range) }
259
259
  .map { |c| c.loc.line }
260
260
  end
261
261