rubocop 0.72.0 → 0.73.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +13 -0
  4. data/lib/rubocop.rb +2 -0
  5. data/lib/rubocop/ast/node.rb +8 -8
  6. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +16 -5
  7. data/lib/rubocop/ast/traversal.rb +3 -3
  8. data/lib/rubocop/cop/autocorrect_logic.rb +71 -1
  9. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -1
  10. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
  11. data/lib/rubocop/cop/commissioner.rb +3 -9
  12. data/lib/rubocop/cop/cop.rb +36 -6
  13. data/lib/rubocop/cop/corrector.rb +2 -3
  14. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +2 -2
  15. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -2
  16. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  17. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
  18. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  19. data/lib/rubocop/cop/generator.rb +1 -1
  20. data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -2
  21. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -1
  22. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -2
  23. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -1
  24. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +2 -2
  25. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -2
  26. data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
  27. data/lib/rubocop/cop/layout/class_structure.rb +1 -1
  28. data/lib/rubocop/cop/layout/indent_first_argument.rb +1 -1
  29. data/lib/rubocop/cop/layout/indent_heredoc.rb +3 -2
  30. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  31. data/lib/rubocop/cop/layout/leading_comment_space.rb +28 -0
  32. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +18 -4
  33. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +14 -2
  34. data/lib/rubocop/cop/layout/tab.rb +10 -22
  35. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -1
  36. data/lib/rubocop/cop/lint/debugger.rb +3 -3
  37. data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -3
  38. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  39. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  40. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  41. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  42. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
  43. data/lib/rubocop/cop/lint/multiple_compare.rb +1 -1
  44. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -3
  45. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  46. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +1 -1
  47. data/lib/rubocop/cop/lint/number_conversion.rb +2 -2
  48. data/lib/rubocop/cop/lint/rand_one.rb +1 -1
  49. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -1
  50. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -1
  51. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -5
  52. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
  53. data/lib/rubocop/cop/lint/unified_integer.rb +1 -1
  54. data/lib/rubocop/cop/lint/unneeded_require_statement.rb +1 -1
  55. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
  56. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  57. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -1
  58. data/lib/rubocop/cop/lint/uri_regexp.rb +2 -2
  59. data/lib/rubocop/cop/lint/useless_access_modifier.rb +6 -6
  60. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  61. data/lib/rubocop/cop/metrics/class_length.rb +1 -1
  62. data/lib/rubocop/cop/metrics/module_length.rb +1 -1
  63. data/lib/rubocop/cop/metrics/parameter_lists.rb +1 -1
  64. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  65. data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -1
  66. data/lib/rubocop/cop/mixin/enforce_superclass.rb +4 -4
  67. data/lib/rubocop/cop/mixin/method_complexity.rb +1 -1
  68. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -3
  69. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  70. data/lib/rubocop/cop/naming/constant_name.rb +2 -2
  71. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  72. data/lib/rubocop/cop/security/eval.rb +1 -1
  73. data/lib/rubocop/cop/security/json_load.rb +1 -1
  74. data/lib/rubocop/cop/security/marshal_load.rb +1 -1
  75. data/lib/rubocop/cop/security/open.rb +1 -1
  76. data/lib/rubocop/cop/security/yaml_load.rb +1 -1
  77. data/lib/rubocop/cop/style/alias.rb +1 -1
  78. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  79. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  80. data/lib/rubocop/cop/style/constant_visibility.rb +1 -1
  81. data/lib/rubocop/cop/style/date_time.rb +3 -3
  82. data/lib/rubocop/cop/style/dir.rb +1 -1
  83. data/lib/rubocop/cop/style/documentation_method.rb +1 -1
  84. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +49 -0
  85. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  86. data/lib/rubocop/cop/style/each_with_object.rb +1 -1
  87. data/lib/rubocop/cop/style/eval_with_location.rb +2 -2
  88. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  89. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
  90. data/lib/rubocop/cop/style/float_division.rb +4 -4
  91. data/lib/rubocop/cop/style/format_string.rb +7 -7
  92. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  93. data/lib/rubocop/cop/style/inverse_methods.rb +2 -2
  94. data/lib/rubocop/cop/style/min_max.rb +1 -1
  95. data/lib/rubocop/cop/style/mixin_usage.rb +1 -1
  96. data/lib/rubocop/cop/style/multiline_when_then.rb +55 -0
  97. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  98. data/lib/rubocop/cop/style/mutable_constant.rb +3 -3
  99. data/lib/rubocop/cop/style/numeric_predicate.rb +3 -3
  100. data/lib/rubocop/cop/style/option_hash.rb +1 -1
  101. data/lib/rubocop/cop/style/or_assignment.rb +2 -2
  102. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -1
  103. data/lib/rubocop/cop/style/random_with_offset.rb +6 -6
  104. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -2
  105. data/lib/rubocop/cop/style/redundant_exception.rb +2 -2
  106. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  107. data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -2
  108. data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -1
  109. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -2
  110. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  111. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  112. data/lib/rubocop/cop/style/sample.rb +1 -1
  113. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  114. data/lib/rubocop/cop/style/string_hash_keys.rb +2 -2
  115. data/lib/rubocop/cop/style/strip.rb +1 -1
  116. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -3
  117. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  118. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  119. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  120. data/lib/rubocop/cop/style/unneeded_sort.rb +1 -1
  121. data/lib/rubocop/cop/style/unpack_first.rb +1 -1
  122. data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -5
  123. data/lib/rubocop/cop/team.rb +15 -14
  124. data/lib/rubocop/error.rb +23 -0
  125. data/lib/rubocop/node_pattern.rb +2 -2
  126. data/lib/rubocop/options.rb +17 -0
  127. data/lib/rubocop/rspec/shared_contexts.rb +12 -0
  128. data/lib/rubocop/target_finder.rb +6 -4
  129. data/lib/rubocop/version.rb +1 -1
  130. metadata +4 -2
@@ -40,7 +40,7 @@ module RuboCop
40
40
  MSG = '`%<assignment>s` method calls already given on line '\
41
41
  '%<line_of_first_occurrence>d of the gemspec.'
42
42
 
43
- def_node_search :gem_specification, <<-PATTERN
43
+ def_node_search :gem_specification, <<~PATTERN
44
44
  (block
45
45
  (send
46
46
  (const
@@ -49,7 +49,7 @@ module RuboCop
49
49
  (arg $_)) ...)
50
50
  PATTERN
51
51
 
52
- def_node_search :assignment_method_declarations, <<-PATTERN
52
+ def_node_search :assignment_method_declarations, <<~PATTERN
53
53
  (send
54
54
  (lvar #match_block_variable_name?) #assignment_method? ...)
55
55
  PATTERN
@@ -97,7 +97,7 @@ module RuboCop
97
97
  node.method_name
98
98
  end
99
99
 
100
- def_node_search :dependency_declarations, <<-PATTERN
100
+ def_node_search :dependency_declarations, <<~PATTERN
101
101
  (send (lvar _) {:add_dependency :add_runtime_dependency :add_development_dependency} ...)
102
102
  PATTERN
103
103
  end
@@ -41,7 +41,7 @@ module RuboCop
41
41
  '(%<target_ruby_version>s, which may be specified in ' \
42
42
  '.rubocop.yml) should be equal.'
43
43
 
44
- def_node_search :required_ruby_version, <<-PATTERN
44
+ def_node_search :required_ruby_version, <<~PATTERN
45
45
  (send _ :required_ruby_version= ${(str _) (array (str _))})
46
46
  PATTERN
47
47
 
@@ -30,7 +30,7 @@ module RuboCop
30
30
 
31
31
  def_node_matcher :ruby_version?, '(const nil? :RUBY_VERSION)'
32
32
 
33
- def_node_search :gem_specification?, <<-PATTERN
33
+ def_node_search :gem_specification?, <<~PATTERN
34
34
  (block
35
35
  (send
36
36
  (const
@@ -63,7 +63,7 @@ module RuboCop
63
63
  # For example
64
64
  MSG = 'Use `#good_method` instead of `#bad_method`.'.freeze
65
65
 
66
- def_node_matcher :bad_method?, <<-PATTERN
66
+ def_node_matcher :bad_method?, <<~PATTERN
67
67
  (send nil? :bad_method ...)
68
68
  PATTERN
69
69
 
@@ -19,11 +19,11 @@ module RuboCop
19
19
  MSG = 'Use the methods provided with the node extensions instead ' \
20
20
  'of manually destructuring nodes.'
21
21
 
22
- def_node_matcher :node_variable?, <<-PATTERN
22
+ def_node_matcher :node_variable?, <<~PATTERN
23
23
  {(lvar [#node_suffix? _]) (send nil? [#node_suffix? _])}
24
24
  PATTERN
25
25
 
26
- def_node_matcher :node_destructuring?, <<-PATTERN
26
+ def_node_matcher :node_destructuring?, <<~PATTERN
27
27
  {(masgn (mlhs ...) {(send #node_variable? :children) (array (splat #node_variable?))})}
28
28
  PATTERN
29
29
 
@@ -16,7 +16,7 @@ module RuboCop
16
16
  class NodeTypePredicate < Cop
17
17
  MSG = 'Use `#%<type>s_type?` to check node type.'
18
18
 
19
- def_node_matcher :node_type_check, <<-PATTERN
19
+ def_node_matcher :node_type_check, <<~PATTERN
20
20
  (send (send $_ :type) :== (sym $_))
21
21
  PATTERN
22
22
 
@@ -33,11 +33,11 @@ module RuboCop
33
33
 
34
34
  private
35
35
 
36
- def_node_matcher :node_type_check, <<-PATTERN
36
+ def_node_matcher :node_type_check, <<~PATTERN
37
37
  (send nil? :add_offense $_node $hash)
38
38
  PATTERN
39
39
 
40
- def_node_matcher :offending_location_argument, <<-PATTERN
40
+ def_node_matcher :offending_location_argument, <<~PATTERN
41
41
  (pair (sym :location) $(send (send $_node :loc) $_keyword))
42
42
  PATTERN
43
43
 
@@ -21,7 +21,7 @@ module RuboCop
21
21
 
22
22
  MSG = 'Redundant location argument to `#add_offense`.'
23
23
 
24
- def_node_matcher :redundant_location_argument, <<-PATTERN
24
+ def_node_matcher :redundant_location_argument, <<~PATTERN
25
25
  (send nil? :add_offense _
26
26
  (hash <$(pair (sym :location) (sym :expression)) ...>)
27
27
  )
@@ -24,11 +24,11 @@ module RuboCop
24
24
 
25
25
  MSG = 'Redundant message argument to `#add_offense`.'
26
26
 
27
- def_node_matcher :node_type_check, <<-PATTERN
27
+ def_node_matcher :node_type_check, <<~PATTERN
28
28
  (send nil? :add_offense $_node $hash)
29
29
  PATTERN
30
30
 
31
- def_node_matcher :redundant_message_argument, <<-PATTERN
31
+ def_node_matcher :redundant_message_argument, <<~PATTERN
32
32
  (pair
33
33
  (sym :message)
34
34
  ${(const nil? :MSG) (send nil? :message) (send nil? :message _)})
@@ -16,11 +16,11 @@ module RuboCop
16
16
  class UselessMessageAssertion < Cop
17
17
  MSG = 'Do not specify cop behavior using `described_class::MSG`.'
18
18
 
19
- def_node_search :described_class_msg, <<-PATTERN
19
+ def_node_search :described_class_msg, <<~PATTERN
20
20
  (const (send nil? :described_class) :MSG)
21
21
  PATTERN
22
22
 
23
- def_node_matcher :rspec_expectation_on_msg?, <<-PATTERN
23
+ def_node_matcher :rspec_expectation_on_msg?, <<~PATTERN
24
24
  (send (send nil? :expect #contains_described_class_msg?) :to ...)
25
25
  PATTERN
26
26
 
@@ -67,7 +67,7 @@ module RuboCop
67
67
 
68
68
  MSG = '%<current>s is not aligned with %<prefer>s%<alt_prefer>s.'
69
69
 
70
- def_node_matcher :block_end_align_target?, <<-PATTERN
70
+ def_node_matcher :block_end_align_target?, <<~PATTERN
71
71
  {assignment?
72
72
  splat
73
73
  and
@@ -142,7 +142,7 @@ module RuboCop
142
142
  MSG = '`%<category>s` is supposed to appear before ' \
143
143
  '`%<previous>s`.'
144
144
 
145
- def_node_matcher :visibility_block?, <<-PATTERN
145
+ def_node_matcher :visibility_block?, <<~PATTERN
146
146
  (send nil? { :private :protected :public })
147
147
  PATTERN
148
148
 
@@ -204,7 +204,7 @@ module RuboCop
204
204
  node.source_range.begin_pos > parent.source_range.begin_pos
205
205
  end
206
206
 
207
- def_node_matcher :eligible_method_call?, <<-PATTERN
207
+ def_node_matcher :eligible_method_call?, <<~PATTERN
208
208
  (send _ !:[]= ...)
209
209
  PATTERN
210
210
 
@@ -37,7 +37,7 @@ module RuboCop
37
37
  # # good
38
38
  # # When EnforcedStyle is powerpack, bad code is auto-corrected to
39
39
  # # the following code.
40
- # <<~RUBY
40
+ # <<-RUBY.strip_indent
41
41
  # something
42
42
  # RUBY
43
43
  #
@@ -206,7 +206,8 @@ module RuboCop
206
206
  body = heredoc_body(node)
207
207
  body_indent_level = indent_level(body)
208
208
  correct_indent_level = base_indent_level(node) + indentation_width
209
- body.gsub(/^\s{#{body_indent_level}}/, ' ' * correct_indent_level)
209
+ body.gsub(/^[^\S\r\n]{#{body_indent_level}}/,
210
+ ' ' * correct_indent_level)
210
211
  end
211
212
 
212
213
  def indented_end(node)
@@ -52,7 +52,7 @@ module RuboCop
52
52
  MSG = 'Use %<configured_indentation_width>d (not %<indentation>d) ' \
53
53
  'spaces for%<name>s indentation.'
54
54
 
55
- def_node_matcher :access_modifier?, <<-PATTERN
55
+ def_node_matcher :access_modifier?, <<~PATTERN
56
56
  [(send ...) access_modifier?]
57
57
  PATTERN
58
58
 
@@ -16,6 +16,25 @@ module RuboCop
16
16
  #
17
17
  # # good
18
18
  # # Some comment
19
+ #
20
+ # @example AllowDoxygenCommentStyle: false (default)
21
+ #
22
+ # # bad
23
+ #
24
+ # #**
25
+ # # Some comment
26
+ # # Another line of comment
27
+ # #*
28
+ #
29
+ # @example AllowDoxygenCommentStyle: true
30
+ #
31
+ # # good
32
+ #
33
+ # #**
34
+ # # Some comment
35
+ # # Another line of comment
36
+ # #*
37
+ #
19
38
  class LeadingCommentSpace < Cop
20
39
  include RangeHelp
21
40
 
@@ -25,6 +44,7 @@ module RuboCop
25
44
  processed_source.each_comment do |comment|
26
45
  next unless comment.text =~ /\A#+[^#\s=:+-]/
27
46
  next if comment.loc.line == 1 && allowed_on_first_line?(comment)
47
+ next if allow_doxygen_comment? && doxygen_comment_style?(comment)
28
48
 
29
49
  add_offense(comment)
30
50
  end
@@ -54,6 +74,14 @@ module RuboCop
54
74
  def rackup_config_file?
55
75
  File.basename(processed_source.file_path).eql?('config.ru')
56
76
  end
77
+
78
+ def allow_doxygen_comment?
79
+ cop_config['AllowDoxygenCommentStyle']
80
+ end
81
+
82
+ def doxygen_comment_style?(comment)
83
+ comment.text.start_with?('#*')
84
+ end
57
85
  end
58
86
  end
59
87
  end
@@ -30,6 +30,8 @@ module RuboCop
30
30
  ANCESTOR_TYPES = %i[kwbegin def defs class module].freeze
31
31
  RUBY_2_5_ANCESTOR_TYPES = (ANCESTOR_TYPES + %i[block]).freeze
32
32
  ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS = %i[def defs].freeze
33
+ ALTERNATIVE_ACCESS_MODIFIERS = %i[public_class_method
34
+ private_class_method].freeze
33
35
 
34
36
  def on_resbody(node)
35
37
  check(node) unless modifier?(node)
@@ -99,8 +101,12 @@ module RuboCop
99
101
  case node.type
100
102
  when :block, :kwbegin
101
103
  node.loc.begin
102
- when :def, :defs, :class, :module
104
+ when :def, :defs, :class, :module,
105
+ :lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn
103
106
  node.loc.name
107
+ when :masgn
108
+ mlhs_node, = *node
109
+ mlhs_node.loc.expression
104
110
  else
105
111
  # It is a wrapper with access modifier.
106
112
  node.child_nodes.first.loc.name
@@ -150,9 +156,7 @@ module RuboCop
150
156
  ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS.include?(node.type)
151
157
 
152
158
  access_modifier_node = node.ancestors.first
153
- return nil unless
154
- access_modifier_node.respond_to?(:access_modifier?) &&
155
- access_modifier_node.access_modifier?
159
+ return nil unless access_modifier?(access_modifier_node)
156
160
 
157
161
  access_modifier_node
158
162
  end
@@ -169,6 +173,16 @@ module RuboCop
169
173
 
170
174
  range_between(begin_pos - current_column, begin_pos)
171
175
  end
176
+
177
+ def access_modifier?(node)
178
+ return true if node.respond_to?(:access_modifier?) &&
179
+ node.access_modifier?
180
+
181
+ return true if node.respond_to?(:method_name) &&
182
+ ALTERNATIVE_ACCESS_MODIFIERS.include?(node.method_name)
183
+
184
+ false
185
+ end
172
186
  end
173
187
  end
174
188
  end
@@ -130,7 +130,8 @@ module RuboCop
130
130
  args_delimiter = node.arguments.loc.begin # Can be ( | or nil.
131
131
 
132
132
  check_left_brace(inner, node.loc.begin, args_delimiter)
133
- check_right_brace(inner, node.loc.end, node.single_line?)
133
+ check_right_brace(inner, node.loc.begin, node.loc.end,
134
+ node.single_line?)
134
135
  end
135
136
 
136
137
  def check_left_brace(inner, left_brace, args_delimiter)
@@ -141,15 +142,26 @@ module RuboCop
141
142
  end
142
143
  end
143
144
 
144
- def check_right_brace(inner, right_brace, single_line)
145
+ def check_right_brace(inner, left_brace, right_brace, single_line)
145
146
  if single_line && inner =~ /\S$/
146
147
  no_space(right_brace.begin_pos, right_brace.end_pos,
147
148
  'Space missing inside }.')
148
149
  else
150
+ return if multiline_block?(left_brace, right_brace) &&
151
+ aligned_braces?(left_brace, right_brace)
152
+
149
153
  space_inside_right_brace(right_brace)
150
154
  end
151
155
  end
152
156
 
157
+ def multiline_block?(left_brace, right_brace)
158
+ left_brace.first_line != right_brace.first_line
159
+ end
160
+
161
+ def aligned_braces?(left_brace, right_brace)
162
+ left_brace.first_line == right_brace.last_column
163
+ end
164
+
153
165
  def no_space_inside_left_brace(left_brace, args_delimiter)
154
166
  if pipe?(args_delimiter)
155
167
  if left_brace.end_pos == args_delimiter.begin_pos &&
@@ -30,16 +30,13 @@ module RuboCop
30
30
  str_ranges = string_literal_ranges(processed_source.ast)
31
31
 
32
32
  processed_source.lines.each.with_index(1) do |line, lineno|
33
- match = line.match(/^([^\t]*)\t+/)
33
+ match = line.match(/\t+/)
34
34
  next unless match
35
35
 
36
- prefix = match.captures[0]
37
- col = prefix.length
38
- next if in_string_literal?(str_ranges, lineno, col)
39
-
40
36
  range = source_range(processed_source.buffer,
41
37
  lineno,
42
- col...match.end(0))
38
+ match.begin(0)...match.end(0))
39
+ next if in_string_literal?(str_ranges, range)
43
40
 
44
41
  add_offense(range, location: range)
45
42
  end
@@ -54,16 +51,9 @@ module RuboCop
54
51
 
55
52
  private
56
53
 
57
- # rubocop:disable Metrics/CyclomaticComplexity
58
- def in_string_literal?(ranges, line, col)
59
- ranges.any? do |range|
60
- (range.line == line && range.column <= col) ||
61
- (range.line < line && line < range.last_line) ||
62
- (range.line != line && range.last_line == line &&
63
- range.last_column >= col)
64
- end
54
+ def in_string_literal?(ranges, tabs_range)
55
+ ranges.any? { |range| range.contains?(tabs_range) }
65
56
  end
66
- # rubocop:enable Metrics/CyclomaticComplexity
67
57
 
68
58
  def string_literal_ranges(ast)
69
59
  # which lines start inside a string literal?
@@ -72,13 +62,11 @@ module RuboCop
72
62
  ast.each_node(:str, :dstr).each_with_object(Set.new) do |str, ranges|
73
63
  loc = str.location
74
64
 
75
- range = if str.heredoc?
76
- loc.heredoc_body
77
- else
78
- loc.expression
79
- end
80
-
81
- ranges << range
65
+ if str.heredoc?
66
+ ranges << loc.heredoc_body
67
+ elsif loc.respond_to?(:begin) && loc.begin
68
+ ranges << loc.expression
69
+ end
82
70
  end
83
71
  end
84
72
  end
@@ -18,7 +18,7 @@ module RuboCop
18
18
  MSG = '`%<double_colon>sBigDecimal.new()` is deprecated. ' \
19
19
  'Use `%<double_colon>sBigDecimal()` instead.'
20
20
 
21
- def_node_matcher :big_decimal_new, <<-PATTERN
21
+ def_node_matcher :big_decimal_new, <<~PATTERN
22
22
  (send
23
23
  (const ${nil? cbase} :BigDecimal) :new ...)
24
24
  PATTERN
@@ -35,14 +35,14 @@ module RuboCop
35
35
  class Debugger < Cop
36
36
  MSG = 'Remove debugger entry point `%<source>s`.'
37
37
 
38
- def_node_matcher :kernel?, <<-PATTERN
38
+ def_node_matcher :kernel?, <<~PATTERN
39
39
  {
40
40
  (const nil? :Kernel)
41
41
  (const (cbase) :Kernel)
42
42
  }
43
43
  PATTERN
44
44
 
45
- def_node_matcher :debugger_call?, <<-PATTERN
45
+ def_node_matcher :debugger_call?, <<~PATTERN
46
46
  {(send {nil? #kernel?} {:debugger :byebug} ...)
47
47
  (send (send {#kernel? nil?} :binding)
48
48
  {:pry :remote_pry :pry_remote} ...)
@@ -52,7 +52,7 @@ module RuboCop
52
52
  :save_screenshot} ...)}
53
53
  PATTERN
54
54
 
55
- def_node_matcher :binding_irb_call?, <<-PATTERN
55
+ def_node_matcher :binding_irb_call?, <<~PATTERN
56
56
  (send (send {#kernel? nil?} :binding) :irb ...)
57
57
  PATTERN
58
58
 
@@ -79,7 +79,7 @@ module RuboCop
79
79
  end
80
80
  end
81
81
 
82
- def_node_matcher :method_alias?, <<-PATTERN
82
+ def_node_matcher :method_alias?, <<~PATTERN
83
83
  (alias (sym $_name) sym)
84
84
  PATTERN
85
85
 
@@ -91,11 +91,11 @@ module RuboCop
91
91
  found_instance_method(node, name)
92
92
  end
93
93
 
94
- def_node_matcher :alias_method?, <<-PATTERN
94
+ def_node_matcher :alias_method?, <<~PATTERN
95
95
  (send nil? :alias_method (sym $_name) _)
96
96
  PATTERN
97
97
 
98
- def_node_matcher :attr?, <<-PATTERN
98
+ def_node_matcher :attr?, <<~PATTERN
99
99
  (send nil? ${:attr_reader :attr_writer :attr_accessor :attr} $...)
100
100
  PATTERN
101
101