rubocop 0.42.0 → 0.43.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (221) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/assets/output.html.erb +21 -10
  4. data/config/default.yml +32 -2
  5. data/config/disabled.yml +8 -1
  6. data/config/enabled.yml +40 -12
  7. data/lib/rubocop.rb +14 -2
  8. data/lib/rubocop/ast_node.rb +2 -0
  9. data/lib/rubocop/cached_data.rb +13 -11
  10. data/lib/rubocop/cli.rb +5 -5
  11. data/lib/rubocop/config.rb +68 -24
  12. data/lib/rubocop/config_loader.rb +13 -11
  13. data/lib/rubocop/config_loader_resolver.rb +4 -2
  14. data/lib/rubocop/cop/cop.rb +16 -5
  15. data/lib/rubocop/cop/lint/assignment_in_condition.rb +21 -20
  16. data/lib/rubocop/cop/lint/block_alignment.rb +3 -4
  17. data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -3
  18. data/lib/rubocop/cop/lint/duplicate_methods.rb +16 -6
  19. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  20. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  21. data/lib/rubocop/cop/lint/end_alignment.rb +4 -6
  22. data/lib/rubocop/cop/lint/eval.rb +1 -1
  23. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  24. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +8 -8
  25. data/lib/rubocop/cop/lint/inherit_exception.rb +22 -7
  26. data/lib/rubocop/cop/lint/literal_in_condition.rb +5 -5
  27. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +3 -5
  28. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  29. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +9 -8
  30. data/lib/rubocop/cop/lint/percent_string_array.rb +17 -6
  31. data/lib/rubocop/cop/lint/percent_symbol_array.rb +4 -4
  32. data/lib/rubocop/cop/lint/rand_one.rb +3 -3
  33. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -3
  34. data/lib/rubocop/cop/lint/shadowed_exception.rb +39 -44
  35. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +2 -2
  36. data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -2
  37. data/lib/rubocop/cop/lint/unified_integer.rb +38 -0
  38. data/lib/rubocop/cop/lint/unneeded_disable.rb +51 -38
  39. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +114 -0
  40. data/lib/rubocop/cop/lint/useless_assignment.rb +25 -12
  41. data/lib/rubocop/cop/lint/useless_setter_call.rb +27 -28
  42. data/lib/rubocop/cop/lint/void.rb +2 -4
  43. data/lib/rubocop/cop/mixin/access_modifier_node.rb +5 -5
  44. data/lib/rubocop/cop/mixin/array_hash_indentation.rb +19 -17
  45. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +3 -5
  46. data/lib/rubocop/cop/mixin/configurable_naming.rb +4 -5
  47. data/lib/rubocop/cop/mixin/configurable_numbering.rb +52 -0
  48. data/lib/rubocop/cop/mixin/def_node.rb +28 -0
  49. data/lib/rubocop/cop/mixin/documentation_comment.rb +41 -0
  50. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +18 -13
  51. data/lib/rubocop/cop/mixin/if_node.rb +6 -0
  52. data/lib/rubocop/cop/mixin/match_range.rb +2 -5
  53. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -2
  54. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +40 -28
  55. data/lib/rubocop/cop/mixin/negative_conditional.rb +6 -6
  56. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -5
  57. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +14 -4
  58. data/lib/rubocop/cop/mixin/safe_mode.rb +23 -0
  59. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +2 -4
  60. data/lib/rubocop/cop/mixin/space_inside.rb +1 -3
  61. data/lib/rubocop/cop/mixin/statement_modifier.rb +30 -20
  62. data/lib/rubocop/cop/mixin/trailing_comma.rb +19 -17
  63. data/lib/rubocop/cop/performance/case_when_splat.rb +16 -41
  64. data/lib/rubocop/cop/performance/casecmp.rb +28 -16
  65. data/lib/rubocop/cop/performance/count.rb +58 -34
  66. data/lib/rubocop/cop/performance/detect.rb +3 -7
  67. data/lib/rubocop/cop/performance/double_start_end_with.rb +17 -13
  68. data/lib/rubocop/cop/performance/fixed_size.rb +19 -14
  69. data/lib/rubocop/cop/performance/flat_map.rb +16 -9
  70. data/lib/rubocop/cop/performance/hash_each.rb +2 -3
  71. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +4 -6
  72. data/lib/rubocop/cop/performance/redundant_match.rb +4 -1
  73. data/lib/rubocop/cop/performance/redundant_merge.rb +63 -32
  74. data/lib/rubocop/cop/performance/redundant_sort_by.rb +8 -7
  75. data/lib/rubocop/cop/performance/reverse_each.rb +1 -4
  76. data/lib/rubocop/cop/performance/size.rb +21 -8
  77. data/lib/rubocop/cop/performance/sort_with_block.rb +54 -0
  78. data/lib/rubocop/cop/performance/string_replacement.rb +3 -7
  79. data/lib/rubocop/cop/rails/delegate.rb +2 -3
  80. data/lib/rubocop/cop/rails/find_by.rb +4 -8
  81. data/lib/rubocop/cop/rails/not_null_column.rb +45 -0
  82. data/lib/rubocop/cop/rails/request_referer.rb +3 -3
  83. data/lib/rubocop/cop/rails/safe_navigation.rb +89 -0
  84. data/lib/rubocop/cop/rails/save_bang.rb +78 -9
  85. data/lib/rubocop/cop/rails/scope_args.rb +3 -1
  86. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +2 -3
  87. data/lib/rubocop/cop/rails/validation.rb +1 -1
  88. data/lib/rubocop/cop/security/json_load.rb +36 -0
  89. data/lib/rubocop/cop/style/alias.rb +1 -1
  90. data/lib/rubocop/cop/style/align_hash.rb +25 -14
  91. data/lib/rubocop/cop/style/and_or.rb +13 -3
  92. data/lib/rubocop/cop/style/array_join.rb +3 -3
  93. data/lib/rubocop/cop/style/ascii_comments.rb +1 -2
  94. data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -2
  95. data/lib/rubocop/cop/style/attr.rb +1 -3
  96. data/lib/rubocop/cop/style/block_comments.rb +2 -6
  97. data/lib/rubocop/cop/style/block_delimiters.rb +35 -21
  98. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +4 -4
  99. data/lib/rubocop/cop/style/case_indentation.rb +1 -3
  100. data/lib/rubocop/cop/style/class_methods.rb +3 -4
  101. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  102. data/lib/rubocop/cop/style/command_literal.rb +15 -8
  103. data/lib/rubocop/cop/style/comment_annotation.rb +1 -2
  104. data/lib/rubocop/cop/style/conditional_assignment.rb +68 -36
  105. data/lib/rubocop/cop/style/copyright.rb +1 -5
  106. data/lib/rubocop/cop/style/def_with_parentheses.rb +3 -5
  107. data/lib/rubocop/cop/style/documentation.rb +28 -56
  108. data/lib/rubocop/cop/style/documentation_method.rb +80 -0
  109. data/lib/rubocop/cop/style/each_for_simple_loop.rb +6 -5
  110. data/lib/rubocop/cop/style/each_with_object.rb +2 -2
  111. data/lib/rubocop/cop/style/else_alignment.rb +10 -9
  112. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -4
  113. data/lib/rubocop/cop/style/empty_else.rb +1 -4
  114. data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -3
  115. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +2 -5
  116. data/lib/rubocop/cop/style/encoding.rb +28 -14
  117. data/lib/rubocop/cop/style/even_odd.rb +28 -17
  118. data/lib/rubocop/cop/style/extra_spacing.rb +36 -25
  119. data/lib/rubocop/cop/style/file_name.rb +19 -10
  120. data/lib/rubocop/cop/style/first_parameter_indentation.rb +2 -3
  121. data/lib/rubocop/cop/style/for.rb +12 -8
  122. data/lib/rubocop/cop/style/format_string.rb +1 -1
  123. data/lib/rubocop/cop/style/guard_clause.rb +22 -56
  124. data/lib/rubocop/cop/style/hash_syntax.rb +72 -7
  125. data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -19
  126. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +3 -3
  127. data/lib/rubocop/cop/style/indentation_width.rb +30 -16
  128. data/lib/rubocop/cop/style/infinite_loop.rb +16 -13
  129. data/lib/rubocop/cop/style/initial_indentation.rb +23 -18
  130. data/lib/rubocop/cop/style/inline_comment.rb +16 -3
  131. data/lib/rubocop/cop/style/lambda.rb +22 -10
  132. data/lib/rubocop/cop/style/leading_comment_space.rb +12 -1
  133. data/lib/rubocop/cop/style/line_end_concatenation.rb +24 -6
  134. data/lib/rubocop/cop/style/method_call_parentheses.rb +18 -9
  135. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +3 -4
  136. data/lib/rubocop/cop/style/method_def_parentheses.rb +3 -4
  137. data/lib/rubocop/cop/style/method_missing.rb +10 -2
  138. data/lib/rubocop/cop/style/module_function.rb +14 -6
  139. data/lib/rubocop/cop/style/multiline_assignment_layout.rb +2 -5
  140. data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -5
  141. data/lib/rubocop/cop/style/multiline_block_layout.rb +22 -15
  142. data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +9 -0
  143. data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +41 -20
  144. data/lib/rubocop/cop/style/multiline_operation_indentation.rb +6 -6
  145. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +3 -5
  146. data/lib/rubocop/cop/style/mutable_constant.rb +21 -13
  147. data/lib/rubocop/cop/style/negated_if.rb +1 -1
  148. data/lib/rubocop/cop/style/negated_while.rb +3 -3
  149. data/lib/rubocop/cop/style/nested_modifier.rb +2 -4
  150. data/lib/rubocop/cop/style/next.rb +4 -4
  151. data/lib/rubocop/cop/style/non_nil_check.rb +18 -10
  152. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +8 -0
  153. data/lib/rubocop/cop/style/numeric_predicate.rb +9 -9
  154. data/lib/rubocop/cop/style/one_line_conditional.rb +11 -1
  155. data/lib/rubocop/cop/style/op_method.rb +1 -1
  156. data/lib/rubocop/cop/style/option_hash.rb +8 -8
  157. data/lib/rubocop/cop/style/optional_arguments.rb +21 -8
  158. data/lib/rubocop/cop/style/parallel_assignment.rb +51 -35
  159. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
  160. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  161. data/lib/rubocop/cop/style/raise_args.rb +2 -2
  162. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  163. data/lib/rubocop/cop/style/redundant_parentheses.rb +26 -15
  164. data/lib/rubocop/cop/style/redundant_return.rb +5 -5
  165. data/lib/rubocop/cop/style/redundant_self.rb +20 -11
  166. data/lib/rubocop/cop/style/regexp_literal.rb +16 -10
  167. data/lib/rubocop/cop/style/rescue_ensure_alignment.rb +8 -6
  168. data/lib/rubocop/cop/style/safe_navigation.rb +125 -0
  169. data/lib/rubocop/cop/style/self_assignment.rb +2 -2
  170. data/lib/rubocop/cop/style/semicolon.rb +9 -10
  171. data/lib/rubocop/cop/style/signal_exception.rb +2 -4
  172. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  173. data/lib/rubocop/cop/style/single_line_methods.rb +18 -11
  174. data/lib/rubocop/cop/style/space_after_method_name.rb +2 -3
  175. data/lib/rubocop/cop/style/space_after_not.rb +4 -6
  176. data/lib/rubocop/cop/style/space_around_block_parameters.rb +1 -2
  177. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -3
  178. data/lib/rubocop/cop/style/space_around_operators.rb +21 -16
  179. data/lib/rubocop/cop/style/space_before_block_braces.rb +2 -12
  180. data/lib/rubocop/cop/style/space_before_first_arg.rb +1 -3
  181. data/lib/rubocop/cop/style/space_inside_array_percent_literal.rb +1 -1
  182. data/lib/rubocop/cop/style/space_inside_block_braces.rb +33 -40
  183. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +38 -23
  184. data/lib/rubocop/cop/style/space_inside_percent_literal_delimiters.rb +1 -1
  185. data/lib/rubocop/cop/style/space_inside_string_interpolation.rb +26 -12
  186. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -4
  187. data/lib/rubocop/cop/style/symbol_array.rb +10 -10
  188. data/lib/rubocop/cop/style/symbol_proc.rb +28 -13
  189. data/lib/rubocop/cop/style/ternary_parentheses.rb +35 -5
  190. data/lib/rubocop/cop/style/trailing_blank_lines.rb +2 -4
  191. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +29 -17
  192. data/lib/rubocop/cop/style/trivial_accessors.rb +6 -6
  193. data/lib/rubocop/cop/style/unless_else.rb +2 -6
  194. data/lib/rubocop/cop/style/unneeded_capital_w.rb +8 -4
  195. data/lib/rubocop/cop/style/unneeded_interpolation.rb +4 -5
  196. data/lib/rubocop/cop/style/unneeded_percent_q.rb +13 -7
  197. data/lib/rubocop/cop/style/variable_number.rb +79 -0
  198. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  199. data/lib/rubocop/cop/style/word_array.rb +25 -15
  200. data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -0
  201. data/lib/rubocop/cop/util.rb +23 -4
  202. data/lib/rubocop/cop/variable_force.rb +59 -25
  203. data/lib/rubocop/cop/variable_force/locatable.rb +8 -6
  204. data/lib/rubocop/cop/variable_force/variable.rb +2 -2
  205. data/lib/rubocop/cop/variable_force/variable_table.rb +3 -3
  206. data/lib/rubocop/formatter/disabled_config_formatter.rb +16 -11
  207. data/lib/rubocop/formatter/formatter_set.rb +12 -10
  208. data/lib/rubocop/formatter/worst_offenders_formatter.rb +4 -4
  209. data/lib/rubocop/node_pattern.rb +79 -35
  210. data/lib/rubocop/options.rb +4 -4
  211. data/lib/rubocop/processed_source.rb +9 -5
  212. data/lib/rubocop/remote_config.rb +14 -10
  213. data/lib/rubocop/result_cache.rb +14 -6
  214. data/lib/rubocop/runner.rb +55 -34
  215. data/lib/rubocop/string_util.rb +9 -5
  216. data/lib/rubocop/target_finder.rb +1 -1
  217. data/lib/rubocop/token.rb +1 -1
  218. data/lib/rubocop/version.rb +1 -1
  219. metadata +15 -4
  220. data/lib/rubocop/cop/lint/useless_array_splat.rb +0 -56
  221. data/lib/rubocop/cop/performance/push_splat.rb +0 -47
@@ -76,11 +76,7 @@ module RuboCop
76
76
  "match Notice /#{notice}/" unless autocorrect_notice =~ regex
77
77
 
78
78
  lambda do |corrector|
79
- range = if token.nil?
80
- Parser::Source::Range.new('', 0, 0)
81
- else
82
- token.pos
83
- end
79
+ range = token.nil? ? range_between(0, 0) : token.pos
84
80
  corrector.insert_before(range, "#{autocorrect_notice}\n")
85
81
  end
86
82
  end
@@ -14,12 +14,10 @@ module RuboCop
14
14
  'any arguments.'.freeze
15
15
 
16
16
  def on_method_def(node, _method_name, args, _body)
17
- start_line = node.loc.keyword.line
18
- end_line = node.loc.end.line
17
+ return if node.single_line?
18
+ return unless args.children.empty? && args.loc.begin
19
19
 
20
- return if start_line == end_line
21
-
22
- add_offense(args, :begin) if args.children == [] && args.loc.begin
20
+ add_offense(args, :begin)
23
21
  end
24
22
 
25
23
  def autocorrect(node)
@@ -13,94 +13,66 @@ module RuboCop
13
13
  # a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the
14
14
  # same for all its children.
15
15
  class Documentation < Cop
16
- include AnnotationComment
16
+ include DocumentationComment
17
17
 
18
18
  MSG = 'Missing top-level %s documentation comment.'.freeze
19
19
 
20
20
  def_node_matcher :constant_definition?, '{class module casgn}'
21
21
 
22
22
  def on_class(node)
23
- _name, _superclass, body = *node
23
+ _, _, body = *node
24
+
24
25
  return unless body
25
- return if namespace?(body)
26
26
 
27
- ast_with_comments = processed_source.ast_with_comments
28
- return if associated_comment?(node, ast_with_comments)
29
- return if nodoc_comment?(node, ast_with_comments)
30
- add_offense(node, :keyword, format(MSG, :class))
27
+ check(node, body, :class)
31
28
  end
32
29
 
33
30
  def on_module(node)
34
- _name, body = *node
35
- return if namespace?(body)
31
+ _, body = *node
36
32
 
37
- ast_with_comments = processed_source.ast_with_comments
38
- return if associated_comment?(node, ast_with_comments)
39
- return if nodoc_comment?(node, ast_with_comments)
40
- add_offense(node, :keyword, format(MSG, :module))
33
+ check(node, body, :module)
41
34
  end
42
35
 
43
36
  private
44
37
 
45
- def namespace?(body_node)
46
- return false unless body_node
38
+ def check(node, body, type)
39
+ return if namespace?(body)
40
+ return if documentation_comment?(node) || nodoc_comment?(node)
47
41
 
48
- case body_node.type
49
- when :begin
50
- body_node.children.all? { |node| constant_definition?(node) }
51
- else
52
- constant_definition?(body_node)
53
- end
42
+ add_offense(node, :keyword, format(MSG, type))
54
43
  end
55
44
 
56
- # Returns true if the node has a comment on the line above it that
57
- # isn't an annotation.
58
- def associated_comment?(node, ast_with_comments)
59
- preceding_comments = preceding_comments(node, ast_with_comments)
60
- return false if preceding_comments.empty?
61
-
62
- distance = node.loc.keyword.line - preceding_comments.last.loc.line
63
- return false if distance > 1
64
- return false unless comment_line_only?(preceding_comments.last)
45
+ def namespace?(node)
46
+ return false unless node
65
47
 
66
- # As long as there's at least one comment line that isn't an
67
- # annotation, it's OK.
68
- preceding_comments.any? do |comment|
69
- !annotation?(comment) && !interpreter_directive_comment?(comment)
48
+ if node.begin_type?
49
+ node.children.all? { |child| constant_definition?(child) }
50
+ else
51
+ constant_definition?(node)
70
52
  end
71
53
  end
72
54
 
73
- def preceding_comments(node, ast_with_comments)
74
- ast_with_comments[node].select { |c| c.loc.line < node.loc.line }
75
- end
76
-
77
- def comment_line_only?(comment)
78
- source_buffer = comment.loc.expression.source_buffer
79
- comment_line = source_buffer.source_line(comment.loc.line)
80
- comment_line =~ /^\s*#/
81
- end
82
-
83
55
  # First checks if the :nodoc: comment is associated with the
84
56
  # class/module. Unless the element is tagged with :nodoc:, the search
85
57
  # proceeds to check its ancestors for :nodoc: all.
86
58
  # Note: How end-of-line comments are associated with code changed in
87
59
  # parser-2.2.0.4.
88
- def nodoc_comment?(node, ast_with_comments, require_all = false)
89
- return false unless node
90
- nodoc_node = node.children.first
91
- return false unless nodoc_node
92
- comment = ast_with_comments[nodoc_node].first
60
+ def nodoc_comment?(node, require_all = false)
61
+ return false unless node && node.children.first
93
62
 
94
- if comment && comment.loc.line == node.loc.line
95
- regex = /^#\s*:nodoc:#{"\s+all\s*$" if require_all}/
96
- return true if comment.text =~ regex
97
- end
63
+ nodoc = nodoc(node)
64
+
65
+ return true if same_line?(nodoc, node) && nodoc?(nodoc, require_all)
66
+
67
+ nodoc_comment?(node.parent, true)
68
+ end
98
69
 
99
- nodoc_comment?(node.ancestors.first, ast_with_comments, true)
70
+ def nodoc?(comment, require_all = false)
71
+ comment.text =~ /^#\s*:nodoc:#{"\s+all\s*$" if require_all}/
100
72
  end
101
73
 
102
- def interpreter_directive_comment?(comment)
103
- comment.text =~ /^#\s*(frozen_string_literal|encoding):/
74
+ def nodoc(node)
75
+ processed_source.ast_with_comments[node.children.first].first
104
76
  end
105
77
  end
106
78
  end
@@ -0,0 +1,80 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # This cop checks for missing documentation comment for public methods.
7
+ # It can optionally be configured to also require documentation for
8
+ # non-public methods.
9
+ #
10
+ # @example
11
+ #
12
+ # # bad
13
+ #
14
+ # class Foo
15
+ # def bar
16
+ # puts baz
17
+ # end
18
+ # end
19
+ #
20
+ # module Foo
21
+ # def bar
22
+ # puts baz
23
+ # end
24
+ # end
25
+ #
26
+ # def foo.bar
27
+ # puts baz
28
+ # end
29
+ #
30
+ # # good
31
+ #
32
+ # class Foo
33
+ # # Documentation
34
+ # def bar
35
+ # puts baz
36
+ # end
37
+ # end
38
+ #
39
+ # module Foo
40
+ # # Documentation
41
+ # def bar
42
+ # puts baz
43
+ # end
44
+ # end
45
+ #
46
+ # # Documenation
47
+ # def foo.bar
48
+ # puts baz
49
+ # end
50
+ class DocumentationMethod < Cop
51
+ include DocumentationComment
52
+ include OnMethodDef
53
+ include DefNode
54
+
55
+ MSG = 'Missing method documentation comment.'.freeze
56
+
57
+ def on_def(node)
58
+ check(node)
59
+ end
60
+
61
+ def on_method_def(node, *)
62
+ check(node)
63
+ end
64
+
65
+ private
66
+
67
+ def check(node)
68
+ return if non_public?(node) && !require_for_non_public_methods?
69
+ return if documentation_comment?(node)
70
+
71
+ add_offense(node, :expression, MSG)
72
+ end
73
+
74
+ def require_for_non_public_methods?
75
+ cop_config['RequireForNonPublicMethods']
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -28,11 +28,12 @@ module RuboCop
28
28
  'number of times.'.freeze
29
29
 
30
30
  def on_block(node)
31
- if offending_each_range(node)
32
- send_node, = *node
33
- range = send_node.receiver.source_range.join(send_node.loc.selector)
34
- add_offense(node, range)
35
- end
31
+ return unless offending_each_range(node)
32
+
33
+ send_node, = *node
34
+ range = send_node.receiver.source_range.join(send_node.loc.selector)
35
+
36
+ add_offense(node, range)
36
37
  end
37
38
 
38
39
  private
@@ -76,8 +76,8 @@ module RuboCop
76
76
  def return_value(body)
77
77
  return unless body
78
78
 
79
- return_value = body.type == :begin ? body.children.last : body
80
- return_value if return_value && return_value.type == :lvar
79
+ return_value = body.begin_type? ? body.children.last : body
80
+ return_value if return_value && return_value.lvar_type?
81
81
  end
82
82
 
83
83
  def first_argument_returned?(args, return_value)
@@ -12,25 +12,26 @@ module RuboCop
12
12
  include EndKeywordAlignment
13
13
  include AutocorrectAlignment
14
14
  include CheckAssignment
15
+ include IfNode
15
16
 
16
17
  MSG = 'Align `%s` with `%s`.'.freeze
17
18
 
18
19
  def on_if(node, base = nil)
19
20
  return if ignored_node?(node)
20
- return unless node.loc.respond_to?(:else)
21
- return if node.loc.else.nil?
21
+ return unless if_else?(node)
22
22
 
23
23
  else_range = node.loc.else
24
24
  return unless begins_its_line?(else_range)
25
25
 
26
26
  check_alignment(base_range(node, base), else_range)
27
27
 
28
- return if else_range.source != 'elsif'
28
+ _, _, else_body = *node
29
+
30
+ return unless else_body && elsif?(else_body)
29
31
 
30
32
  # If the `else` part is actually an `elsif`, we check the `elsif`
31
33
  # node in case it contains an `else` within, because that `else`
32
34
  # should have the same alignment (base).
33
- _condition, _if_body, else_body = *node
34
35
  on_if(else_body, base)
35
36
  # The `elsif` node will get an `on_if` call from the framework later,
36
37
  # but we're done here, so we set it to ignored.
@@ -38,10 +39,10 @@ module RuboCop
38
39
  end
39
40
 
40
41
  def on_rescue(node)
41
- return unless node.loc.else
42
+ return unless if_else?(node)
42
43
 
43
44
  parent = node.parent
44
- parent = parent.parent if parent.type == :ensure
45
+ parent = parent.parent if parent.ensure_type?
45
46
  base = case parent.type
46
47
  when :def, :defs then base_for_method_definition(parent)
47
48
  when :kwbegin then parent.loc.begin
@@ -52,7 +53,7 @@ module RuboCop
52
53
 
53
54
  def on_case(node)
54
55
  _cond, *whens, _else = *node
55
- return unless node.loc.else
56
+ return unless if_else?(node)
56
57
  check_alignment(whens.last.loc.keyword, node.loc.else)
57
58
  end
58
59
 
@@ -72,7 +73,7 @@ module RuboCop
72
73
 
73
74
  def base_for_method_definition(node)
74
75
  parent = node.parent
75
- if parent && parent.type == :send
76
+ if parent && parent.send_type?
76
77
  parent.loc.selector # For example "private def ..."
77
78
  else
78
79
  node.loc.keyword
@@ -90,7 +91,7 @@ module RuboCop
90
91
  style = end_config['AlignWith'] || 'keyword'
91
92
  base = variable_alignment?(node.loc, rhs, style.to_sym) ? node : rhs
92
93
 
93
- return if rhs.type != :if
94
+ return unless rhs.if_type?
94
95
 
95
96
  on_if(rhs, base)
96
97
  ignore_node(rhs)
@@ -82,10 +82,8 @@ module RuboCop
82
82
 
83
83
  first = children.first
84
84
  last = children.last
85
- range =
86
- Parser::Source::Range.new(when_node.loc.expression.source_buffer,
87
- first.loc.expression.begin_pos,
88
- last.loc.expression.end_pos)
85
+ range = range_between(first.loc.expression.begin_pos,
86
+ last.loc.expression.end_pos)
89
87
 
90
88
  corrector.replace(range, children.map(&:source).join(' || '))
91
89
  end
@@ -117,10 +117,7 @@ module RuboCop
117
117
  node.parent.loc.end.begin_pos
118
118
  end
119
119
 
120
- range = Parser::Source::Range.new(node.source_range.source_buffer,
121
- node.loc.else.begin_pos,
122
- end_pos)
123
- corrector.remove(range)
120
+ corrector.remove(range_between(node.loc.else.begin_pos, end_pos))
124
121
  end
125
122
  end
126
123
 
@@ -65,9 +65,7 @@ module RuboCop
65
65
  end_pos = prev_def.loc.end.end_pos
66
66
  source_buffer = prev_def.loc.end.source_buffer
67
67
  newline_pos = source_buffer.source.index("\n", end_pos)
68
- newline = Parser::Source::Range.new(source_buffer,
69
- newline_pos,
70
- newline_pos + 1)
68
+ newline = range_between(newline_pos, newline_pos + 1)
71
69
  ->(corrector) { corrector.insert_after(newline, "\n") }
72
70
  end
73
71
  end
@@ -38,11 +38,8 @@ module RuboCop
38
38
  private
39
39
 
40
40
  def line_range(node)
41
- Parser::Source::Range.new(
42
- processed_source.buffer,
43
- node.source_range.begin_pos - node.loc.column,
44
- node.source_range.end_pos
45
- )
41
+ range_between(node.source_range.begin_pos - node.loc.column,
42
+ node.source_range.end_pos)
46
43
  end
47
44
 
48
45
  def previous_line_ignoring_comments(processed_source, send_line)
@@ -35,13 +35,10 @@ module RuboCop
35
35
 
36
36
  def autocorrect(range)
37
37
  if @message == MSG_MISSING
38
- encoding = cop_config[AUTO_CORRECT_ENCODING_COMMENT]
39
- if encoding && encoding =~ ENCODING_PATTERN
40
- lambda do |corrector|
41
- corrector.insert_before(range, "#{encoding}\n")
42
- end
43
- else
44
- raise "#{encoding} does not match #{ENCODING_PATTERN}"
38
+ raise encoding_mismatch_message unless matching_encoding?
39
+
40
+ lambda do |corrector|
41
+ corrector.insert_before(range, "#{encoding}\n")
45
42
  end
46
43
  else
47
44
  # Need to remove unnecessary encoding comment
@@ -53,21 +50,38 @@ module RuboCop
53
50
 
54
51
  private
55
52
 
53
+ def encoding
54
+ cop_config[AUTO_CORRECT_ENCODING_COMMENT]
55
+ end
56
+
57
+ def matching_encoding?
58
+ encoding =~ ENCODING_PATTERN
59
+ end
60
+
61
+ def encoding_mismatch_message
62
+ "#{encoding} does not match #{ENCODING_PATTERN}"
63
+ end
64
+
56
65
  def offense(processed_source, line_number)
57
66
  line = processed_source[line_number]
58
- encoding_present = line =~ ENCODING_PATTERN
59
- ascii_only = processed_source.buffer.source.ascii_only?
60
- always_encode = style == :always
61
- never_encode = style == :never
62
- encoding_omitable = never_encode || (!always_encode && ascii_only)
63
67
 
64
- if !encoding_present && !encoding_omitable
68
+ if !encoding_present?(line) && !encoding_omitable?
65
69
  MSG_MISSING
66
- elsif encoding_present && encoding_omitable
70
+ elsif encoding_present?(line) && encoding_omitable?
67
71
  MSG_UNNECESSARY
68
72
  end
69
73
  end
70
74
 
75
+ def encoding_present?(line)
76
+ line =~ ENCODING_PATTERN
77
+ end
78
+
79
+ def encoding_omitable?
80
+ return true if style == :never
81
+
82
+ style != :always && processed_source.buffer.source.ascii_only?
83
+ end
84
+
71
85
  def encoding_line_number(processed_source)
72
86
  line_number = 0
73
87
  line_number += 1 if processed_source[line_number].start_with?(SHEBANG)