rubocop 0.75.0 → 0.79.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 (192) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +341 -316
  4. data/lib/rubocop.rb +48 -31
  5. data/lib/rubocop/ast/builder.rb +43 -41
  6. data/lib/rubocop/ast/node.rb +5 -1
  7. data/lib/rubocop/ast/node/block_node.rb +2 -0
  8. data/lib/rubocop/ast/node/def_node.rb +11 -0
  9. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  10. data/lib/rubocop/ast/node/return_node.rb +24 -0
  11. data/lib/rubocop/ast/traversal.rb +11 -3
  12. data/lib/rubocop/cli.rb +11 -227
  13. data/lib/rubocop/cli/command.rb +21 -0
  14. data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
  15. data/lib/rubocop/cli/command/base.rb +33 -0
  16. data/lib/rubocop/cli/command/execute_runner.rb +76 -0
  17. data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
  18. data/lib/rubocop/cli/command/show_cops.rb +80 -0
  19. data/lib/rubocop/cli/command/version.rb +17 -0
  20. data/lib/rubocop/cli/environment.rb +21 -0
  21. data/lib/rubocop/comment_config.rb +2 -2
  22. data/lib/rubocop/config.rb +8 -1
  23. data/lib/rubocop/config_loader.rb +20 -20
  24. data/lib/rubocop/config_loader_resolver.rb +2 -1
  25. data/lib/rubocop/config_obsoletion.rb +73 -11
  26. data/lib/rubocop/config_validator.rb +77 -110
  27. data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
  28. data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
  29. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
  30. data/lib/rubocop/cop/commissioner.rb +15 -7
  31. data/lib/rubocop/cop/cop.rb +31 -6
  32. data/lib/rubocop/cop/corrector.rb +8 -7
  33. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  34. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
  35. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  36. data/lib/rubocop/cop/generator.rb +3 -4
  37. data/lib/rubocop/cop/generator/configuration_injector.rb +2 -2
  38. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  39. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
  40. data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
  41. data/lib/rubocop/cop/layout/{align_array.rb → array_alignment.rb} +1 -1
  42. data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +3 -2
  43. data/lib/rubocop/cop/layout/comment_indentation.rb +10 -13
  44. data/lib/rubocop/cop/layout/empty_comment.rb +7 -16
  45. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +22 -7
  46. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -2
  47. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +2 -2
  48. data/lib/rubocop/cop/layout/end_of_line.rb +8 -3
  49. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  50. data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +14 -12
  51. data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
  52. data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +4 -4
  53. data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
  54. data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +16 -8
  55. data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +5 -5
  56. data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
  57. data/lib/rubocop/cop/{metrics → layout}/line_length.rb +41 -114
  58. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
  59. data/lib/rubocop/cop/layout/multiline_block_layout.rb +14 -5
  60. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  61. data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
  62. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -0
  63. data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
  64. data/lib/rubocop/cop/layout/space_around_operators.rb +32 -7
  65. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  66. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -7
  67. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -4
  68. data/lib/rubocop/cop/layout/space_inside_parens.rb +6 -6
  69. data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
  70. data/lib/rubocop/cop/layout/trailing_whitespace.rb +18 -2
  71. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  72. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
  73. data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
  74. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  75. data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -8
  76. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  77. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +2 -2
  78. data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
  79. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  80. data/lib/rubocop/cop/lint/{unneeded_cop_disable_directive.rb → redundant_cop_disable_directive.rb} +26 -26
  81. data/lib/rubocop/cop/lint/{unneeded_cop_enable_directive.rb → redundant_cop_enable_directive.rb} +10 -12
  82. data/lib/rubocop/cop/lint/{unneeded_require_statement.rb → redundant_require_statement.rb} +1 -1
  83. data/lib/rubocop/cop/lint/{unneeded_splat_expansion.rb → redundant_splat_expansion.rb} +6 -6
  84. data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +1 -1
  85. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  86. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  87. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -6
  88. data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
  89. data/lib/rubocop/cop/lint/unused_block_argument.rb +22 -6
  90. data/lib/rubocop/cop/lint/unused_method_argument.rb +23 -5
  91. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
  92. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  93. data/lib/rubocop/cop/lint/void.rb +7 -26
  94. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  95. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  96. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +23 -6
  97. data/lib/rubocop/cop/migration/department_name.rb +16 -1
  98. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  99. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -7
  100. data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
  101. data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
  102. data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
  103. data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
  104. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  105. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -4
  106. data/lib/rubocop/cop/mixin/trailing_comma.rb +14 -9
  107. data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
  108. data/lib/rubocop/cop/naming/file_name.rb +12 -5
  109. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
  110. data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +4 -4
  111. data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
  112. data/lib/rubocop/cop/offense.rb +11 -0
  113. data/lib/rubocop/cop/registry.rb +8 -3
  114. data/lib/rubocop/cop/style/alias.rb +1 -1
  115. data/lib/rubocop/cop/style/array_join.rb +1 -1
  116. data/lib/rubocop/cop/style/attr.rb +10 -2
  117. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +6 -6
  118. data/lib/rubocop/cop/style/comment_annotation.rb +5 -5
  119. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  120. data/lib/rubocop/cop/style/copyright.rb +11 -7
  121. data/lib/rubocop/cop/style/documentation_method.rb +44 -0
  122. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +2 -2
  123. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -2
  124. data/lib/rubocop/cop/style/empty_literal.rb +2 -2
  125. data/lib/rubocop/cop/style/empty_method.rb +5 -5
  126. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  127. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  128. data/lib/rubocop/cop/style/expand_path_arguments.rb +1 -1
  129. data/lib/rubocop/cop/style/format_string.rb +10 -7
  130. data/lib/rubocop/cop/style/format_string_token.rb +15 -34
  131. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +10 -0
  132. data/lib/rubocop/cop/style/guard_clause.rb +3 -2
  133. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  134. data/lib/rubocop/cop/style/if_unless_modifier.rb +45 -3
  135. data/lib/rubocop/cop/style/infinite_loop.rb +5 -4
  136. data/lib/rubocop/cop/style/inverse_methods.rb +19 -13
  137. data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
  138. data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -10
  139. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +24 -227
  140. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +168 -0
  141. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  142. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -9
  143. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  144. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  145. data/lib/rubocop/cop/style/multiline_when_then.rb +6 -2
  146. data/lib/rubocop/cop/style/nested_modifier.rb +4 -2
  147. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +5 -5
  148. data/lib/rubocop/cop/style/next.rb +5 -5
  149. data/lib/rubocop/cop/style/non_nil_check.rb +21 -9
  150. data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
  151. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
  152. data/lib/rubocop/cop/style/option_hash.rb +3 -3
  153. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  154. data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +1 -1
  155. data/lib/rubocop/cop/style/{unneeded_condition.rb → redundant_condition.rb} +3 -3
  156. data/lib/rubocop/cop/style/{unneeded_interpolation.rb → redundant_interpolation.rb} +1 -1
  157. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -3
  158. data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +1 -1
  159. data/lib/rubocop/cop/style/redundant_return.rb +39 -29
  160. data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +5 -5
  161. data/lib/rubocop/cop/style/safe_navigation.rb +19 -8
  162. data/lib/rubocop/cop/style/semicolon.rb +13 -2
  163. data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
  164. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  165. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
  166. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  167. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  168. data/lib/rubocop/cop/team.rb +5 -0
  169. data/lib/rubocop/cop/util.rb +1 -1
  170. data/lib/rubocop/cop/utils/format_string.rb +10 -18
  171. data/lib/rubocop/cop/variable_force.rb +7 -5
  172. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  173. data/lib/rubocop/formatter/clang_style_formatter.rb +9 -6
  174. data/lib/rubocop/formatter/emacs_style_formatter.rb +22 -12
  175. data/lib/rubocop/formatter/file_list_formatter.rb +1 -1
  176. data/lib/rubocop/formatter/formatter_set.rb +16 -16
  177. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  178. data/lib/rubocop/formatter/pacman_formatter.rb +3 -3
  179. data/lib/rubocop/formatter/simple_text_formatter.rb +7 -3
  180. data/lib/rubocop/formatter/tap_formatter.rb +9 -6
  181. data/lib/rubocop/node_pattern.rb +4 -2
  182. data/lib/rubocop/options.rb +20 -26
  183. data/lib/rubocop/processed_source.rb +1 -1
  184. data/lib/rubocop/rake_task.rb +1 -0
  185. data/lib/rubocop/result_cache.rb +24 -8
  186. data/lib/rubocop/rspec/shared_contexts.rb +5 -0
  187. data/lib/rubocop/runner.rb +50 -29
  188. data/lib/rubocop/target_finder.rb +12 -6
  189. data/lib/rubocop/target_ruby.rb +151 -0
  190. data/lib/rubocop/version.rb +1 -1
  191. metadata +50 -34
  192. data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
@@ -33,13 +33,7 @@ module RuboCop
33
33
  end
34
34
 
35
35
  def autocorrect(token)
36
- raise Warning, AUTOCORRECT_EMPTY_WARNING if autocorrect_notice.empty?
37
-
38
- regex = Regexp.new(notice)
39
- unless autocorrect_notice =~ regex
40
- raise Warning, "AutocorrectNotice '#{autocorrect_notice}' must " \
41
- "match Notice /#{notice}/"
42
- end
36
+ verify_autocorrect_notice!
43
37
 
44
38
  lambda do |corrector|
45
39
  range = token.nil? ? range_between(0, 0) : token.pos
@@ -57,6 +51,16 @@ module RuboCop
57
51
  cop_config['AutocorrectNotice']
58
52
  end
59
53
 
54
+ def verify_autocorrect_notice!
55
+ raise Warning, AUTOCORRECT_EMPTY_WARNING if autocorrect_notice.empty?
56
+
57
+ regex = Regexp.new(notice)
58
+ return if autocorrect_notice =~ regex
59
+
60
+ raise Warning, "AutocorrectNotice '#{autocorrect_notice}' must " \
61
+ "match Notice /#{notice}/"
62
+ end
63
+
60
64
  def insert_notice_before(processed_source)
61
65
  token_index = 0
62
66
  token_index += 1 if shebang_token?(processed_source, token_index)
@@ -47,6 +47,50 @@ module RuboCop
47
47
  # def foo.bar
48
48
  # puts baz
49
49
  # end
50
+ #
51
+ # @example RequireForNonPublicMethods: false (default)
52
+ # # good
53
+ # class Foo
54
+ # protected
55
+ # def do_something
56
+ # end
57
+ # end
58
+ #
59
+ # class Foo
60
+ # private
61
+ # def do_something
62
+ # end
63
+ # end
64
+ #
65
+ # @example RequireForNonPublicMethods: true
66
+ # # bad
67
+ # class Foo
68
+ # protected
69
+ # def do_something
70
+ # end
71
+ # end
72
+ #
73
+ # class Foo
74
+ # private
75
+ # def do_something
76
+ # end
77
+ # end
78
+ #
79
+ # # good
80
+ # class Foo
81
+ # protected
82
+ # # Documentation
83
+ # def do_something
84
+ # end
85
+ # end
86
+ #
87
+ # class Foo
88
+ # private
89
+ # # Documentation
90
+ # def do_something
91
+ # end
92
+ # end
93
+ #
50
94
  class DocumentationMethod < Cop
51
95
  include DocumentationComment
52
96
  include DefNode
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Lint/UnneededCopDisableDirective
3
+ # rubocop:disable Lint/RedundantCopDisableDirective
4
4
  # rubocop:disable Style/DoubleCopDisableDirective
5
5
 
6
6
  module RuboCop
@@ -26,7 +26,7 @@ module RuboCop
26
26
  #
27
27
  class DoubleCopDisableDirective < Cop
28
28
  # rubocop:enable Style/For, Style/DoubleCopDisableDirective
29
- # rubocop:enable Lint/UnneededCopDisableDirective, Metrics/AbcSize
29
+ # rubocop:enable Lint/RedundantCopDisableDirective, Metrics/AbcSize
30
30
  MSG = 'More than one disable comment on one line.'
31
31
 
32
32
  def investigate(processed_source)
@@ -84,8 +84,8 @@ module RuboCop
84
84
 
85
85
  next unless conditions.size > 1
86
86
 
87
- range = range_between(conditions.first.loc.expression.begin_pos,
88
- conditions.last.loc.expression.end_pos)
87
+ range = range_between(conditions.first.source_range.begin_pos,
88
+ conditions.last.source_range.end_pos)
89
89
 
90
90
  corrector.replace(range, conditions.map(&:source).join(' || '))
91
91
  end
@@ -84,8 +84,8 @@ module RuboCop
84
84
  # to rewrite the arguments to wrap them in parenthesis.
85
85
  args = node.parent.arguments
86
86
 
87
- range_between(args[0].loc.expression.begin_pos - 1,
88
- args[-1].loc.expression.end_pos)
87
+ range_between(args[0].source_range.begin_pos - 1,
88
+ args[-1].source_range.end_pos)
89
89
  else
90
90
  node.source_range
91
91
  end
@@ -73,11 +73,11 @@ module RuboCop
73
73
  end
74
74
 
75
75
  def corrected(node)
76
- has_parentheses = parentheses?(node.arguments)
77
-
78
- arguments = node.arguments? ? node.arguments.source : ''
79
- extra_space = node.arguments? && !has_parentheses ? ' ' : ''
80
- scope = node.receiver ? "#{node.receiver.source}." : ''
76
+ if node.arguments?
77
+ arguments = node.arguments.source
78
+ extra_space = ' ' unless parentheses?(node.arguments)
79
+ end
80
+ scope = node.receiver ? "#{node.receiver.source}." : ''
81
81
 
82
82
  signature = [scope, node.method_name, extra_space, arguments].join
83
83
 
@@ -85,7 +85,7 @@ module RuboCop
85
85
  # FIXME: It's a Style/ConditionalAssignment's false positive.
86
86
  # rubocop:disable Style/ConditionalAssignment
87
87
  def with_lineno?(node)
88
- if node.method_name == :eval
88
+ if node.method?(:eval)
89
89
  node.arguments.size == 4
90
90
  else
91
91
  node.arguments.size == 3
@@ -23,7 +23,7 @@ module RuboCop
23
23
  {(send $_ :% (int 2))
24
24
  (begin (send $_ :% (int 2)))}
25
25
  ${:== :!=}
26
- (int ${0 1 2}))
26
+ (int ${0 1}))
27
27
  PATTERN
28
28
 
29
29
  def on_send(node)
@@ -160,7 +160,7 @@ module RuboCop
160
160
  def depth(current_path)
161
161
  paths = current_path.split(File::SEPARATOR)
162
162
 
163
- paths.reject { |path| path == '.' }.count
163
+ paths.count { |path| path != '.' }
164
164
  end
165
165
 
166
166
  def parent_path(current_path)
@@ -94,10 +94,12 @@ module RuboCop
94
94
  private
95
95
 
96
96
  def autocorrect_from_percent(corrector, node)
97
- args = if %i[array hash].include?(node.first_argument.type)
98
- node.first_argument.children.map(&:source).join(', ')
97
+ percent_rhs = node.first_argument
98
+ args = case percent_rhs.type
99
+ when :array, :hash
100
+ percent_rhs.children.map(&:source).join(', ')
99
101
  else
100
- node.first_argument.source
102
+ percent_rhs.source
101
103
  end
102
104
 
103
105
  corrected = "#{style}(#{node.receiver.source}, #{args})"
@@ -106,14 +108,15 @@ module RuboCop
106
108
  end
107
109
 
108
110
  def autocorrect_to_percent(corrector, node)
109
- format = node.first_argument.source
111
+ format_arg, *param_args = node.arguments
112
+ format = format_arg.source
110
113
 
111
- args = if node.arguments.size == 2
112
- arg = node.arguments.last
114
+ args = if param_args.one?
115
+ arg = param_args.last
113
116
 
114
117
  arg.hash_type? ? "{ #{arg.source} }" : arg.source
115
118
  else
116
- "[#{node.arguments[1..-1].map(&:source).join(', ')}]"
119
+ "[#{param_args.map(&:source).join(', ')}]"
117
120
  end
118
121
 
119
122
  corrector.replace(node.loc.expression, "#{format} % #{args}")
@@ -8,7 +8,7 @@ module RuboCop
8
8
  # **Note:**
9
9
  # `unannotated` style cop only works for strings
10
10
  # which are passed as arguments to those methods:
11
- # `sprintf`, `format`, `%`.
11
+ # `printf`, `sprintf`, `format`, `%`.
12
12
  # The reason is that *unannotated* format is very similar
13
13
  # to encoded URLs or Date/Time formatting strings.
14
14
  #
@@ -41,10 +41,7 @@ module RuboCop
41
41
  class FormatStringToken < Cop
42
42
  include ConfigurableEnforcedStyle
43
43
 
44
- FORMAT_STRING_METHODS = %i[sprintf format %].freeze
45
-
46
44
  def on_str(node)
47
- return if placeholder_argument?(node)
48
45
  return if node.each_ancestor(:xstr, :regexp).any?
49
46
 
50
47
  tokens(node) do |detected_style, token_range|
@@ -61,14 +58,16 @@ module RuboCop
61
58
 
62
59
  private
63
60
 
64
- def includes_format_methods?(node)
65
- node.each_ancestor(:send).any? do |ancestor|
66
- FORMAT_STRING_METHODS.include?(ancestor.method_name)
67
- end
68
- end
61
+ def_node_matcher :format_string_in_typical_context?, <<~PATTERN
62
+ {
63
+ ^(send _ {:format :sprintf :printf} %0 ...)
64
+ ^(send %0 :% _)
65
+ }
66
+ PATTERN
69
67
 
70
68
  def unannotated_format?(node, detected_style)
71
- detected_style == :unannotated && !includes_format_methods?(node)
69
+ detected_style == :unannotated &&
70
+ !format_string_in_typical_context?(node)
72
71
  end
73
72
 
74
73
  def message(detected_style)
@@ -95,11 +94,7 @@ module RuboCop
95
94
  if source_map.is_a?(Parser::Source::Map::Heredoc)
96
95
  source_map.heredoc_body
97
96
  elsif source_map.begin
98
- slice_source(
99
- source_map.expression,
100
- source_map.expression.begin_pos + 1,
101
- source_map.expression.end_pos - 1
102
- )
97
+ source_map.expression.adjust(begin_pos: +1, end_pos: -1)
103
98
  else
104
99
  source_map.expression
105
100
  end
@@ -109,31 +104,17 @@ module RuboCop
109
104
  format_string = RuboCop::Cop::Utils::FormatString.new(contents.source)
110
105
 
111
106
  format_string.format_sequences.each do |seq|
107
+ next if seq.percent?
108
+
112
109
  detected_style = seq.style
113
- token = slice_source(
114
- contents,
115
- contents.begin_pos + seq.begin_pos,
116
- contents.begin_pos + seq.end_pos
110
+ token = contents.begin.adjust(
111
+ begin_pos: seq.begin_pos,
112
+ end_pos: seq.end_pos
117
113
  )
118
114
 
119
115
  yield(detected_style, token)
120
116
  end
121
117
  end
122
-
123
- def slice_source(source_range, new_begin, new_end)
124
- Parser::Source::Range.new(
125
- source_range.source_buffer,
126
- new_begin,
127
- new_end
128
- )
129
- end
130
-
131
- def placeholder_argument?(node)
132
- return false unless node.parent
133
- return true if node.parent.pair_type?
134
-
135
- placeholder_argument?(node.parent)
136
- end
137
118
  end
138
119
  end
139
120
  end
@@ -11,6 +11,9 @@ module RuboCop
11
11
  # encoding comment. The frozen string literal comment is only valid in
12
12
  # Ruby 2.3+.
13
13
  #
14
+ # Note that the cop will ignore files where the comment exists but is set
15
+ # to `false` instead of `true`.
16
+ #
14
17
  # @example EnforcedStyle: always (default)
15
18
  # # The `always` style will always add the frozen string literal comment
16
19
  # # to a file, regardless of the Ruby version or if `freeze` or `<<` are
@@ -27,6 +30,13 @@ module RuboCop
27
30
  # # ...
28
31
  # end
29
32
  #
33
+ # # good
34
+ # # frozen_string_literal: false
35
+ #
36
+ # module Bar
37
+ # # ...
38
+ # end
39
+ #
30
40
  # @example EnforcedStyle: never
31
41
  # # The `never` will enforce that the frozen string literal comment does
32
42
  # # not exist in a file.
@@ -49,8 +49,9 @@ module RuboCop
49
49
 
50
50
  if body.if_type?
51
51
  check_ending_if(body)
52
- elsif body.begin_type? && body.children.last.if_type?
53
- check_ending_if(body.children.last)
52
+ elsif body.begin_type?
53
+ final_expression = body.children.last
54
+ check_ending_if(final_expression) if final_expression&.if_type?
54
55
  end
55
56
  end
56
57
  alias on_defs on_def
@@ -168,10 +168,10 @@ module RuboCop
168
168
  end
169
169
 
170
170
  def autocorrect_ruby19(corrector, pair_node)
171
- key = pair_node.key
171
+ key = pair_node.key.source_range
172
172
  op = pair_node.loc.operator
173
173
 
174
- range = range_between(key.source_range.begin_pos, op.end_pos)
174
+ range = key.join(op)
175
175
  range = range_with_surrounding_space(range: range, side: :right)
176
176
 
177
177
  space = argument_without_space?(pair_node.parent) ? ' ' : ''
@@ -7,7 +7,7 @@ module RuboCop
7
7
  # written as modifier `if`/`unless`. The cop also checks for modifier
8
8
  # `if`/`unless` lines that exceed the maximum line length.
9
9
  #
10
- # The maximum line length is configured in the `Metrics/LineLength`
10
+ # The maximum line length is configured in the `Layout/LineLength`
11
11
  # cop. The tab size is configured in the `IndentationWidth` of the
12
12
  # `Layout/Tab` cop.
13
13
  #
@@ -32,6 +32,8 @@ module RuboCop
32
32
  # end
33
33
  class IfUnlessModifier < Cop
34
34
  include StatementModifier
35
+ include LineLengthHelp
36
+ include IgnoredPattern
35
37
 
36
38
  MSG_USE_MODIFIER = 'Favor modifier `%<keyword>s` usage when having a ' \
37
39
  'single-line body. Another good alternative is ' \
@@ -66,12 +68,52 @@ module RuboCop
66
68
 
67
69
  private
68
70
 
71
+ def ignored_patterns
72
+ config.for_cop('Layout/LineLength')['IgnoredPatterns'] || []
73
+ end
74
+
69
75
  def too_long_single_line?(node)
70
76
  return false unless max_line_length
71
77
 
72
78
  range = node.source_range
73
- range.first_line == range.last_line &&
74
- range.last_column > max_line_length
79
+ return false unless range.first_line == range.last_line
80
+ return false unless line_length_enabled_at_line?(range.first_line)
81
+
82
+ line = range.source_line
83
+ return false if line_length(line) <= max_line_length
84
+
85
+ too_long_line_based_on_config?(range, line)
86
+ end
87
+
88
+ def too_long_line_based_on_config?(range, line)
89
+ return false if matches_ignored_pattern?(line)
90
+
91
+ too_long = too_long_line_based_on_ignore_cop_directives?(range, line)
92
+ return too_long unless too_long == :undetermined
93
+
94
+ too_long_line_based_on_allow_uri?(line)
95
+ end
96
+
97
+ def too_long_line_based_on_ignore_cop_directives?(range, line)
98
+ if ignore_cop_directives? && directive_on_source_line?(range.line - 1)
99
+ return line_length_without_directive(line) > max_line_length
100
+ end
101
+
102
+ :undetermined
103
+ end
104
+
105
+ def too_long_line_based_on_allow_uri?(line)
106
+ if allow_uri?
107
+ uri_range = find_excessive_uri_range(line)
108
+ return false if uri_range && allowed_uri_position?(line, uri_range)
109
+ end
110
+
111
+ true
112
+ end
113
+
114
+ def line_length_enabled_at_line?(line)
115
+ processed_source.comment_config
116
+ .cop_enabled_at_line?('Layout/LineLength', line)
75
117
  end
76
118
 
77
119
  def named_capture_in_condition?(node)
@@ -58,7 +58,7 @@ module RuboCop
58
58
  # `loop do` without further modification. The reason is that a
59
59
  # variable that's introduced inside a while/until loop is in scope
60
60
  # outside of that loop too, but a variable that's assigned for the
61
- # first time inside a block can not be accessed after the block. In
61
+ # first time inside a block cannot be accessed after the block. In
62
62
  # those more complicated cases we don't report an offense.
63
63
  return if @variables.any? do |var|
64
64
  assigned_inside_loop?(var, range) &&
@@ -97,12 +97,13 @@ module RuboCop
97
97
  end
98
98
 
99
99
  def modifier_replacement(node)
100
+ body = node.body
100
101
  if node.single_line?
101
- 'loop { ' + node.body.source + ' }'
102
+ 'loop { ' + body.source + ' }'
102
103
  else
103
- indentation = node.body.loc.expression.source_line[LEADING_SPACE]
104
+ indentation = body.source_range.source_line[LEADING_SPACE]
104
105
 
105
- ['loop do', node.body.source.gsub(/^/, configured_indent),
106
+ ['loop do', body.source.gsub(/^/, configured_indent),
106
107
  'end'].join("\n#{indentation}")
107
108
  end
108
109
  end