rubocop 1.46.0 → 1.48.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +24 -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/or_assignment_to_constant.rb +2 -0
  54. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  55. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  56. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +3 -3
  57. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -5
  58. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -1
  59. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  60. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -1
  61. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -1
  62. data/lib/rubocop/cop/lint/rescue_type.rb +3 -3
  63. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +1 -1
  64. data/lib/rubocop/cop/lint/script_permission.rb +1 -1
  65. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
  66. data/lib/rubocop/cop/lint/useless_access_modifier.rb +9 -1
  67. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  68. data/lib/rubocop/cop/metrics/collection_literal_length.rb +76 -0
  69. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -2
  70. data/lib/rubocop/cop/migration/department_name.rb +1 -1
  71. data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -1
  72. data/lib/rubocop/cop/mixin/code_length.rb +1 -1
  73. data/lib/rubocop/cop/mixin/comments_help.rb +2 -2
  74. data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
  75. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +1 -1
  76. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +3 -2
  77. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  78. data/lib/rubocop/cop/mixin/min_branches_count.rb +40 -0
  79. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
  80. data/lib/rubocop/cop/mixin/range_help.rb +1 -6
  81. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  82. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  83. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +1 -1
  84. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  85. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  86. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  87. data/lib/rubocop/cop/registry.rb +3 -1
  88. data/lib/rubocop/cop/style/accessor_grouping.rb +23 -2
  89. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -3
  90. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  91. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +1 -1
  92. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  93. data/lib/rubocop/cop/style/block_delimiters.rb +11 -2
  94. data/lib/rubocop/cop/style/case_like_if.rb +20 -3
  95. data/lib/rubocop/cop/style/collection_compact.rb +1 -1
  96. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  97. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  98. data/lib/rubocop/cop/style/concat_array_literals.rb +10 -2
  99. data/lib/rubocop/cop/style/conditional_assignment.rb +6 -6
  100. data/lib/rubocop/cop/style/dir_empty.rb +60 -0
  101. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  102. data/lib/rubocop/cop/style/documentation.rb +10 -4
  103. data/lib/rubocop/cop/style/each_with_object.rb +1 -1
  104. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  105. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  106. data/lib/rubocop/cop/style/eval_with_location.rb +4 -4
  107. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  108. data/lib/rubocop/cop/style/file_empty.rb +71 -0
  109. data/lib/rubocop/cop/style/file_read.rb +1 -1
  110. data/lib/rubocop/cop/style/file_write.rb +1 -1
  111. data/lib/rubocop/cop/style/guard_clause.rb +1 -1
  112. data/lib/rubocop/cop/style/hash_like_case.rb +3 -9
  113. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  114. data/lib/rubocop/cop/style/if_unless_modifier.rb +76 -9
  115. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +2 -0
  116. data/lib/rubocop/cop/style/inverse_methods.rb +5 -5
  117. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +2 -2
  118. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +1 -1
  119. data/lib/rubocop/cop/style/min_max.rb +3 -3
  120. data/lib/rubocop/cop/style/mixin_grouping.rb +4 -4
  121. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  122. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  123. data/lib/rubocop/cop/style/negated_if_else_condition.rb +12 -7
  124. data/lib/rubocop/cop/style/nil_lambda.rb +2 -2
  125. data/lib/rubocop/cop/style/redundant_condition.rb +2 -2
  126. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +2 -2
  127. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  128. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +10 -3
  129. data/lib/rubocop/cop/style/redundant_sort.rb +3 -3
  130. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -2
  131. data/lib/rubocop/cop/style/require_order.rb +1 -3
  132. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -2
  133. data/lib/rubocop/cop/style/safe_navigation.rb +2 -2
  134. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
  135. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  136. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  137. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -1
  138. data/lib/rubocop/cop/style/unpack_first.rb +3 -3
  139. data/lib/rubocop/cop/style/yoda_condition.rb +1 -1
  140. data/lib/rubocop/cop/style/zero_length_predicate.rb +9 -5
  141. data/lib/rubocop/cop/team.rb +1 -1
  142. data/lib/rubocop/cop/util.rb +1 -1
  143. data/lib/rubocop/cop/variable_force/variable.rb +5 -3
  144. data/lib/rubocop/directive_comment.rb +3 -3
  145. data/lib/rubocop/ext/comment.rb +18 -0
  146. data/lib/rubocop/formatter/junit_formatter.rb +4 -1
  147. data/lib/rubocop/server/core.rb +1 -1
  148. data/lib/rubocop/version.rb +1 -1
  149. data/lib/rubocop.rb +5 -0
  150. metadata +10 -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: fb25d50ecbd2f942993d245b3302d53cf3605fa362e0addf1ee088caaa22bfff
4
+ data.tar.gz: 62df82b33ce3ae669b7fd23ca69f1a53f9dbbcd480d44cfff763df3c84c39585
5
5
  SHA512:
6
- metadata.gz: 63da1401d75b3245df06193f264339cf62b180102c584b5b708ed1ba7191787bd71ae33636eb703406ab6ea87127854ba38c8348336f521113fee265d74da9cc
7
- data.tar.gz: 7155f94dd7b68d2e103003bbfa95b74e2e4a9d9a8efd78c210adc227bc0a2b6cd399a7e7e285acd6934b070036ca0362d3b9560b9e92693bd9a9bc0497b71af6
6
+ metadata.gz: 563e301ae73973fd6ccb3679ce82fe15a02d6634c7993bfab9f0c15ae29687982ac63ec07f656be22273e6b5c63cd2686b678cdc3319536b51d086103ec60fcd
7
+ data.tar.gz: eb24bb4636f91c76715a81896852d6620306ac45e52755cc5c2148ff0f85d1012ebe7b043076a802259bf1d130fc4296d55b70f4d99816aba39056fa05205b16
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.48', 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: >-
@@ -3254,6 +3261,9 @@ Style/CaseLikeIf:
3254
3261
  Enabled: true
3255
3262
  Safe: false
3256
3263
  VersionAdded: '0.88'
3264
+ VersionChanged: '1.48'
3265
+ # `MinBranchesCount` defines the number of branches `if` needs to have to trigger this cop.
3266
+ MinBranchesCount: 3
3257
3267
 
3258
3268
  Style/CharacterLiteral:
3259
3269
  Description: 'Checks for uses of character literals.'
@@ -3520,6 +3530,12 @@ Style/Dir:
3520
3530
  Enabled: true
3521
3531
  VersionAdded: '0.50'
3522
3532
 
3533
+ Style/DirEmpty:
3534
+ Description: >-
3535
+ Prefer to use `Dir.empty?('path/to/dir')` when checking if a directory is empty.
3536
+ Enabled: pending
3537
+ VersionAdded: '1.48'
3538
+
3523
3539
  Style/DisableCopsWithinSourceCodeDirective:
3524
3540
  Description: >-
3525
3541
  Forbids disabling/enabling cops within source code.
@@ -3715,6 +3731,14 @@ Style/FetchEnvVar:
3715
3731
  # Environment variables to be excluded from the inspection.
3716
3732
  AllowedVars: []
3717
3733
 
3734
+ Style/FileEmpty:
3735
+ Description: >-
3736
+ Prefer to use `File.empty?('path/to/file')` when checking if a file is empty.
3737
+ Enabled: pending
3738
+ Safe: false
3739
+ SafeAutoCorrect: false
3740
+ VersionAdded: '1.48'
3741
+
3718
3742
  Style/FileRead:
3719
3743
  Description: 'Favor `File.(bin)read` convenience methods.'
3720
3744
  StyleGuide: '#file-read'
@@ -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
  )