rubocop 1.46.0 → 1.47.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +7 -0
  4. data/lib/rubocop/cli/command/auto_generate_config.rb +7 -0
  5. data/lib/rubocop/comment_config.rb +2 -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 +1 -1
  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 +2 -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/first_argument_indentation.rb +1 -1
  33. data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
  34. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  35. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -3
  36. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -2
  37. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +2 -2
  38. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  39. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
  40. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  41. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
  42. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
  43. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
  44. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
  45. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -3
  46. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  47. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  48. data/lib/rubocop/cop/lint/empty_conditional_body.rb +4 -2
  49. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  50. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  51. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +46 -4
  52. data/lib/rubocop/cop/lint/missing_super.rb +31 -2
  53. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  54. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  55. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +3 -3
  56. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -5
  57. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -1
  58. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  59. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -1
  60. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -1
  61. data/lib/rubocop/cop/lint/rescue_type.rb +3 -3
  62. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +1 -1
  63. data/lib/rubocop/cop/lint/script_permission.rb +1 -1
  64. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
  65. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  66. data/lib/rubocop/cop/metrics/collection_literal_length.rb +76 -0
  67. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -2
  68. data/lib/rubocop/cop/migration/department_name.rb +1 -1
  69. data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -1
  70. data/lib/rubocop/cop/mixin/code_length.rb +1 -1
  71. data/lib/rubocop/cop/mixin/comments_help.rb +2 -2
  72. data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
  73. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +1 -1
  74. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  75. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
  76. data/lib/rubocop/cop/mixin/range_help.rb +1 -6
  77. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  78. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  79. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +1 -1
  80. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  81. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  82. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  83. data/lib/rubocop/cop/style/accessor_grouping.rb +10 -1
  84. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -3
  85. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  86. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +1 -1
  87. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  88. data/lib/rubocop/cop/style/block_delimiters.rb +2 -2
  89. data/lib/rubocop/cop/style/case_like_if.rb +1 -1
  90. data/lib/rubocop/cop/style/collection_compact.rb +1 -1
  91. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  92. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  93. data/lib/rubocop/cop/style/concat_array_literals.rb +10 -2
  94. data/lib/rubocop/cop/style/conditional_assignment.rb +6 -6
  95. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  96. data/lib/rubocop/cop/style/documentation.rb +1 -1
  97. data/lib/rubocop/cop/style/each_with_object.rb +1 -1
  98. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  99. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  100. data/lib/rubocop/cop/style/eval_with_location.rb +4 -4
  101. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  102. data/lib/rubocop/cop/style/file_read.rb +1 -1
  103. data/lib/rubocop/cop/style/file_write.rb +1 -1
  104. data/lib/rubocop/cop/style/guard_clause.rb +1 -1
  105. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  106. data/lib/rubocop/cop/style/if_unless_modifier.rb +34 -0
  107. data/lib/rubocop/cop/style/inverse_methods.rb +5 -5
  108. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +2 -2
  109. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +1 -1
  110. data/lib/rubocop/cop/style/min_max.rb +3 -3
  111. data/lib/rubocop/cop/style/mixin_grouping.rb +4 -4
  112. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  113. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  114. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -1
  115. data/lib/rubocop/cop/style/nil_lambda.rb +2 -2
  116. data/lib/rubocop/cop/style/redundant_condition.rb +2 -2
  117. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +2 -2
  118. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  119. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +10 -3
  120. data/lib/rubocop/cop/style/redundant_sort.rb +3 -3
  121. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -2
  122. data/lib/rubocop/cop/style/require_order.rb +1 -3
  123. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -2
  124. data/lib/rubocop/cop/style/safe_navigation.rb +2 -2
  125. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
  126. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  127. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  128. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -1
  129. data/lib/rubocop/cop/style/unpack_first.rb +3 -3
  130. data/lib/rubocop/cop/style/yoda_condition.rb +1 -1
  131. data/lib/rubocop/cop/style/zero_length_predicate.rb +9 -5
  132. data/lib/rubocop/cop/util.rb +1 -1
  133. data/lib/rubocop/cop/variable_force/variable.rb +5 -3
  134. data/lib/rubocop/directive_comment.rb +3 -3
  135. data/lib/rubocop/ext/comment.rb +18 -0
  136. data/lib/rubocop/version.rb +1 -1
  137. data/lib/rubocop.rb +2 -0
  138. metadata +7 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a2c58d5ebb1a39e8a8f66fe34861ea52ae7c7d601c4f64e5f39c9b3f335a04b4
4
- data.tar.gz: b0eab2e29b9875b27aa3eb069147ec82b3fc108fe15c81dcb43c68265b6757d9
3
+ metadata.gz: 3790198ce32105233a07bc15c82a62ec188e7430910b16a7ff10eefbfa89c0a2
4
+ data.tar.gz: aa440d2b6a863b774d63731cbf779cd204491f675b5ad0d4b708a1b8708dcd3b
5
5
  SHA512:
6
- metadata.gz: 63da1401d75b3245df06193f264339cf62b180102c584b5b708ed1ba7191787bd71ae33636eb703406ab6ea87127854ba38c8348336f521113fee265d74da9cc
7
- data.tar.gz: 7155f94dd7b68d2e103003bbfa95b74e2e4a9d9a8efd78c210adc227bc0a2b6cd399a7e7e285acd6934b070036ca0362d3b9560b9e92693bd9a9bc0497b71af6
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.46', 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
 
@@ -2543,6 +2544,12 @@ Metrics/ClassLength:
2543
2544
  Max: 100
2544
2545
  CountAsOne: []
2545
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
+
2546
2553
  # Avoid complex methods.
2547
2554
  Metrics/CyclomaticComplexity:
2548
2555
  Description: >-
@@ -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)
@@ -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
@@ -30,7 +30,7 @@ module RuboCop
30
30
  def on_send(node)
31
31
  return unless processed_source_buffer_name?(node)
32
32
 
33
- offense_range = node.children.first.loc.selector.begin.join(node.loc.expression.end)
33
+ offense_range = node.children.first.loc.selector.begin.join(node.source_range.end)
34
34
 
35
35
  add_offense(offense_range) do |corrector|
36
36
  corrector.replace(offense_range, 'file_path')
@@ -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'
@@ -22,6 +23,7 @@ require_relative 'internal_affairs/redundant_let_rubocop_config_new'
22
23
  require_relative 'internal_affairs/redundant_location_argument'
23
24
  require_relative 'internal_affairs/redundant_message_argument'
24
25
  require_relative 'internal_affairs/redundant_method_dispatch_node'
26
+ require_relative 'internal_affairs/redundant_source_range'
25
27
  require_relative 'internal_affairs/single_line_comparison'
26
28
  require_relative 'internal_affairs/style_detected_api_use'
27
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
@@ -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
 
@@ -115,7 +115,7 @@ module RuboCop
115
115
  end
116
116
 
117
117
  def adjust_minus(corrector, node)
118
- heredoc_beginning = node.loc.expression.source
118
+ heredoc_beginning = node.source
119
119
  corrected = heredoc_beginning.sub(/<<-?/, '<<~')
120
120
  corrector.replace(node, corrected)
121
121
  end
@@ -139,7 +139,7 @@ module RuboCop
139
139
  end
140
140
 
141
141
  def base_indent_level(node)
142
- base_line_num = node.loc.expression.line
142
+ base_line_num = node.source_range.line
143
143
  base_line = processed_source.lines[base_line_num - 1]
144
144
  indent_level(base_line)
145
145
  end
@@ -63,7 +63,7 @@ module RuboCop
63
63
  next if gemfile_ruby_comment?(comment)
64
64
 
65
65
  add_offense(comment) do |corrector|
66
- expr = comment.loc.expression
66
+ expr = comment.source_range
67
67
 
68
68
  corrector.insert_after(hash_mark(expr), ' ')
69
69
  end
@@ -51,12 +51,11 @@ module RuboCop
51
51
  private_constant :LINE_1_ENDING, :LINE_2_BEGINNING,
52
52
  :LEADING_STYLE_OFFENSE, :TRAILING_STYLE_OFFENSE
53
53
 
54
- # rubocop:disable Metrics/AbcSize
55
54
  def on_dstr(node)
56
55
  # Quick check if we possibly have line continuations.
57
56
  return unless node.source.include?('\\')
58
57
 
59
- end_of_first_line = node.loc.expression.begin_pos - node.loc.expression.column
58
+ end_of_first_line = node.source_range.begin_pos - node.source_range.column
60
59
 
61
60
  raw_lines(node).each_cons(2) do |raw_line_one, raw_line_two|
62
61
  end_of_first_line += raw_line_one.length
@@ -70,7 +69,6 @@ module RuboCop
70
69
  end
71
70
  end
72
71
  end
73
- # rubocop:enable Metrics/AbcSize
74
72
 
75
73
  private
76
74
 
@@ -102,7 +102,7 @@ module RuboCop
102
102
  node.loc.name
103
103
  when :masgn
104
104
  mlhs_node, = *node
105
- mlhs_node.loc.expression
105
+ mlhs_node.source_range
106
106
  else
107
107
  # It is a wrapper with access modifier.
108
108
  node.child_nodes.first.loc.name
@@ -196,7 +196,7 @@ module RuboCop
196
196
  if begin_end_alignment_style == 'start_of_line'
197
197
  start_line_range(alignment_node)
198
198
  else
199
- alignment_node.loc.expression
199
+ alignment_node.source_range
200
200
  end
201
201
  end
202
202
 
@@ -64,8 +64,8 @@ module RuboCop
64
64
 
65
65
  def range_of_offense(node)
66
66
  range_between(
67
- node.parent.loc.expression.begin_pos,
68
- node.parent.arguments.loc.expression.end_pos
67
+ node.parent.source_range.begin_pos,
68
+ node.parent.arguments.source_range.end_pos
69
69
  )
70
70
  end
71
71
 
@@ -146,7 +146,7 @@ module RuboCop
146
146
  if single_line && /\S$/.match?(inner)
147
147
  no_space(right_brace.begin_pos, right_brace.end_pos, 'Space missing inside }.')
148
148
  else
149
- column = node.loc.expression.column
149
+ column = node.source_range.column
150
150
  return if multiline_block?(left_brace, right_brace) &&
151
151
  aligned_braces?(inner, right_brace, column)
152
152
 
@@ -83,7 +83,7 @@ module RuboCop
83
83
  end
84
84
 
85
85
  def body_range(node)
86
- node.location.expression.with(
86
+ node.source_range.with(
87
87
  begin_pos: node.location.begin.end_pos,
88
88
  end_pos: node.location.end.begin_pos
89
89
  )
@@ -93,7 +93,7 @@ module RuboCop
93
93
  end
94
94
 
95
95
  def static?(heredoc)
96
- heredoc.loc.expression.source.end_with? "'"
96
+ heredoc.source.end_with? "'"
97
97
  end
98
98
 
99
99
  def skip_heredoc?
@@ -68,7 +68,7 @@ module RuboCop
68
68
  PATTERN
69
69
 
70
70
  def on_const(node)
71
- return if !unqualified_const?(node) || node.parent&.defined_module
71
+ return if !unqualified_const?(node) || node.parent&.defined_module || node.loc.nil?
72
72
 
73
73
  add_offense(node)
74
74
  end
@@ -82,7 +82,7 @@ module RuboCop
82
82
 
83
83
  def offense_range(node)
84
84
  if socket_const?(node.receiver) || dir_env_file_const?(node.receiver)
85
- node.loc.expression.begin.join(node.loc.selector.end)
85
+ node.source_range.begin.join(node.loc.selector.end)
86
86
  elsif node.method?(:attr)
87
87
  node
88
88
  else