rubocop 0.19.1 → 0.20.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 (192) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -0
  3. data/CHANGELOG.md +60 -1
  4. data/CONTRIBUTING.md +2 -1
  5. data/README.md +9 -7
  6. data/config/default.yml +3 -3
  7. data/config/disabled.yml +4 -0
  8. data/config/enabled.yml +45 -21
  9. data/lib/rubocop.rb +30 -9
  10. data/lib/rubocop/cli.rb +1 -1
  11. data/lib/rubocop/comment_config.rb +4 -2
  12. data/lib/rubocop/config.rb +16 -22
  13. data/lib/rubocop/config_loader.rb +29 -26
  14. data/lib/rubocop/cop/commissioner.rb +1 -1
  15. data/lib/rubocop/cop/cop.rb +6 -6
  16. data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -1
  17. data/lib/rubocop/cop/lint/condition_position.rb +1 -1
  18. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  19. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +9 -7
  20. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  21. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
  22. data/lib/rubocop/cop/lint/empty_interpolation.rb +22 -0
  23. data/lib/rubocop/cop/lint/end_in_method.rb +1 -1
  24. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  25. data/lib/rubocop/cop/lint/eval.rb +1 -1
  26. data/lib/rubocop/cop/lint/literal_in_condition.rb +2 -2
  27. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +9 -0
  28. data/lib/rubocop/cop/lint/loop.rb +2 -2
  29. data/lib/rubocop/cop/lint/require_parentheses.rb +0 -4
  30. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  31. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  32. data/lib/rubocop/cop/lint/space_before_first_arg.rb +36 -0
  33. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +14 -2
  34. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -0
  35. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  36. data/lib/rubocop/cop/lint/void.rb +3 -3
  37. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  38. data/lib/rubocop/cop/offense.rb +3 -21
  39. data/lib/rubocop/cop/rails/action_filter.rb +1 -1
  40. data/lib/rubocop/cop/rails/default_scope.rb +1 -1
  41. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
  42. data/lib/rubocop/cop/rails/read_write_attribute.rb +43 -0
  43. data/lib/rubocop/cop/rails/scope_args.rb +1 -1
  44. data/lib/rubocop/cop/rails/validation.rb +1 -1
  45. data/lib/rubocop/cop/severity.rb +76 -0
  46. data/lib/rubocop/cop/style/access_modifier_indentation.rb +1 -1
  47. data/lib/rubocop/cop/style/accessor_method_name.rb +2 -2
  48. data/lib/rubocop/cop/style/alias.rb +1 -1
  49. data/lib/rubocop/cop/style/align_hash.rb +1 -1
  50. data/lib/rubocop/cop/style/and_or.rb +1 -1
  51. data/lib/rubocop/cop/style/{favor_join.rb → array_join.rb} +2 -2
  52. data/lib/rubocop/cop/style/begin_block.rb +1 -1
  53. data/lib/rubocop/cop/style/block_nesting.rb +12 -8
  54. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +1 -1
  55. data/lib/rubocop/cop/style/case_equality.rb +1 -1
  56. data/lib/rubocop/cop/style/case_indentation.rb +5 -5
  57. data/lib/rubocop/cop/style/class_methods.rb +19 -3
  58. data/lib/rubocop/cop/style/class_vars.rb +1 -1
  59. data/lib/rubocop/cop/style/collection_methods.rb +17 -7
  60. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  61. data/lib/rubocop/cop/style/{def_parentheses.rb → def_with_parentheses.rb} +0 -0
  62. data/lib/rubocop/cop/style/{hash_methods.rb → deprecated_hash_methods.rb} +5 -4
  63. data/lib/rubocop/cop/style/double_negation.rb +1 -1
  64. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +1 -1
  65. data/lib/rubocop/cop/style/end_block.rb +1 -1
  66. data/lib/rubocop/cop/style/even_odd.rb +2 -2
  67. data/lib/rubocop/cop/style/file_name.rb +1 -1
  68. data/lib/rubocop/cop/style/for.rb +2 -2
  69. data/lib/rubocop/cop/style/format_string.rb +1 -1
  70. data/lib/rubocop/cop/style/guard_clause.rb +69 -0
  71. data/lib/rubocop/cop/style/hash_syntax.rb +6 -10
  72. data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
  73. data/lib/rubocop/cop/style/lambda.rb +2 -2
  74. data/lib/rubocop/cop/style/line_end_concatenation.rb +16 -9
  75. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  76. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +2 -2
  77. data/lib/rubocop/cop/style/negated_if.rb +37 -0
  78. data/lib/rubocop/cop/style/negated_while.rb +33 -0
  79. data/lib/rubocop/cop/style/nested_ternary_operator.rb +1 -1
  80. data/lib/rubocop/cop/style/nil_comparison.rb +14 -10
  81. data/lib/rubocop/cop/style/non_nil_check.rb +70 -0
  82. data/lib/rubocop/cop/style/not.rb +25 -1
  83. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  84. data/lib/rubocop/cop/style/op_method.rb +4 -4
  85. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -1
  86. data/lib/rubocop/cop/style/predicate_name.rb +1 -1
  87. data/lib/rubocop/cop/style/proc.rb +1 -1
  88. data/lib/rubocop/cop/style/raise_args.rb +3 -2
  89. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  90. data/lib/rubocop/cop/style/self_assignment.rb +2 -2
  91. data/lib/rubocop/cop/style/signal_exception.rb +6 -3
  92. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  93. data/lib/rubocop/cop/style/single_line_methods.rb +6 -5
  94. data/lib/rubocop/cop/style/single_space_before_first_arg.rb +41 -0
  95. data/lib/rubocop/cop/style/space_around_operators.rb +1 -2
  96. data/lib/rubocop/cop/style/special_global_vars.rb +8 -8
  97. data/lib/rubocop/cop/style/trailing_comma.rb +1 -1
  98. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  99. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  100. data/lib/rubocop/cop/style/variable_interpolation.rb +10 -5
  101. data/lib/rubocop/cop/style/when_then.rb +1 -1
  102. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  103. data/lib/rubocop/cop/style/word_array.rb +1 -1
  104. data/lib/rubocop/cop/team.rb +12 -13
  105. data/lib/rubocop/cop/util.rb +4 -0
  106. data/lib/rubocop/cop/variable_inspector/locatable.rb +1 -1
  107. data/lib/rubocop/cop/variable_inspector/variable_table.rb +1 -1
  108. data/lib/rubocop/file_inspector.rb +46 -10
  109. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  110. data/lib/rubocop/formatter/disabled_lines_formatter.rb +56 -0
  111. data/lib/rubocop/formatter/emacs_style_formatter.rb +1 -1
  112. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  113. data/lib/rubocop/formatter/fuubar_style_formatter.rb +2 -4
  114. data/lib/rubocop/formatter/json_formatter.rb +3 -7
  115. data/lib/rubocop/formatter/progress_formatter.rb +1 -3
  116. data/lib/rubocop/formatter/simple_text_formatter.rb +7 -3
  117. data/lib/rubocop/options.rb +29 -10
  118. data/lib/rubocop/path_util.rb +2 -1
  119. data/lib/rubocop/processed_source.rb +8 -0
  120. data/lib/rubocop/target_finder.rb +33 -12
  121. data/lib/rubocop/version.rb +1 -1
  122. data/relnotes/v0.20.0.md +69 -0
  123. data/rubocop-todo.yml +2 -2
  124. data/spec/rubocop/cli_spec.rb +269 -94
  125. data/spec/rubocop/config_loader_spec.rb +14 -14
  126. data/spec/rubocop/config_spec.rb +8 -8
  127. data/spec/rubocop/cop/lint/deprecated_class_methods_spec.rb +11 -6
  128. data/spec/rubocop/cop/lint/empty_interpolation_spec.rb +18 -0
  129. data/spec/rubocop/cop/lint/eval_spec.rb +2 -4
  130. data/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb +10 -0
  131. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +0 -8
  132. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +4 -4
  133. data/spec/rubocop/cop/lint/space_before_first_arg_spec.rb +48 -0
  134. data/spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb +10 -0
  135. data/spec/rubocop/cop/lint/useless_access_modifier_spec.rb +154 -0
  136. data/spec/rubocop/cop/offense_spec.rb +1 -1
  137. data/spec/rubocop/cop/rails/read_write_attribute_spec.rb +19 -0
  138. data/spec/rubocop/cop/severity_spec.rb +113 -0
  139. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +10 -10
  140. data/spec/rubocop/cop/style/alias_spec.rb +2 -2
  141. data/spec/rubocop/cop/style/and_or_spec.rb +2 -2
  142. data/spec/rubocop/cop/style/{favor_join_spec.rb → array_join_spec.rb} +1 -3
  143. data/spec/rubocop/cop/style/block_nesting_spec.rb +4 -4
  144. data/spec/rubocop/cop/style/case_equality_spec.rb +1 -0
  145. data/spec/rubocop/cop/style/case_indentation_spec.rb +12 -9
  146. data/spec/rubocop/cop/style/class_methods_spec.rb +23 -0
  147. data/spec/rubocop/cop/style/collection_methods_spec.rb +2 -2
  148. data/spec/rubocop/cop/style/{hash_methods_spec.rb → deprecated_hash_methods_spec.rb} +3 -3
  149. data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +2 -2
  150. data/spec/rubocop/cop/style/even_odd_spec.rb +8 -8
  151. data/spec/rubocop/cop/style/file_name_spec.rb +55 -42
  152. data/spec/rubocop/cop/style/for_spec.rb +4 -4
  153. data/spec/rubocop/cop/style/format_string_spec.rb +10 -10
  154. data/spec/rubocop/cop/style/guard_clause_spec.rb +77 -0
  155. data/spec/rubocop/cop/style/hash_syntax_spec.rb +4 -2
  156. data/spec/rubocop/cop/style/if_unless_modifier_spec.rb +4 -4
  157. data/spec/rubocop/cop/style/lambda_spec.rb +2 -2
  158. data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +21 -0
  159. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +1 -1
  160. data/spec/rubocop/cop/style/{favor_unless_over_negated_if_spec.rb → negated_if_spec.rb} +8 -3
  161. data/spec/rubocop/cop/style/{favor_until_over_negated_while_spec.rb → negated_while_spec.rb} +8 -3
  162. data/spec/rubocop/cop/style/nil_comparison_spec.rb +7 -13
  163. data/spec/rubocop/cop/style/non_nil_check_spec.rb +35 -0
  164. data/spec/rubocop/cop/style/not_spec.rb +11 -0
  165. data/spec/rubocop/cop/style/numeric_literals_spec.rb +0 -2
  166. data/spec/rubocop/cop/style/op_method_spec.rb +10 -2
  167. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +2 -2
  168. data/spec/rubocop/cop/style/predicate_name_spec.rb +2 -1
  169. data/spec/rubocop/cop/style/raise_args_spec.rb +5 -0
  170. data/spec/rubocop/cop/style/rescue_modifier_spec.rb +2 -2
  171. data/spec/rubocop/cop/style/self_assignment_spec.rb +4 -4
  172. data/spec/rubocop/cop/style/signal_exception_spec.rb +24 -0
  173. data/spec/rubocop/cop/style/single_line_block_params_spec.rb +2 -0
  174. data/spec/rubocop/cop/style/single_space_before_first_arg_spec.rb +63 -0
  175. data/spec/rubocop/cop/style/special_global_vars_spec.rb +6 -5
  176. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +6 -3
  177. data/spec/rubocop/cop/style/unless_else_spec.rb +2 -4
  178. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +15 -6
  179. data/spec/rubocop/cop/style/when_then_spec.rb +3 -4
  180. data/spec/rubocop/cop/team_spec.rb +4 -18
  181. data/spec/rubocop/file_inspector_spec.rb +4 -0
  182. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +1 -1
  183. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +69 -0
  184. data/spec/rubocop/options_spec.rb +5 -0
  185. data/spec/rubocop/target_finder_spec.rb +42 -11
  186. data/spec/support/shared_context.rb +1 -1
  187. data/spec/support/statement_modifier_helper.rb +1 -1
  188. metadata +75 -50
  189. data/lib/rubocop/cop/rails/read_attribute.rb +0 -28
  190. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +0 -24
  191. data/lib/rubocop/cop/style/favor_until_over_negated_while.rb +0 -20
  192. data/spec/rubocop/cop/rails/read_attribute_spec.rb +0 -13
@@ -5,8 +5,8 @@ module Rubocop
5
5
  module Lint
6
6
  # This cop checks for uses of *begin...end while/until something*.
7
7
  class Loop < Cop
8
- MSG = 'Use Kernel#loop with break rather than ' \
9
- 'begin/end/until(or while).'
8
+ MSG = 'Use `Kernel#loop` with `break` rather than ' \
9
+ '`begin/end/until`(or `while`).'
10
10
 
11
11
  def on_while_post(node)
12
12
  register_offense(node)
@@ -59,10 +59,6 @@ module Rubocop
59
59
  method_name =~ /\w\?$/
60
60
  end
61
61
 
62
- def parentheses?(node)
63
- node.loc.respond_to?(:end) && node.loc.end
64
- end
65
-
66
62
  def offense?(node)
67
63
  [:and, :or].include?(node.type)
68
64
  end
@@ -5,7 +5,7 @@ module Rubocop
5
5
  module Lint
6
6
  # This cop checks for *rescue* blocks targeting the Exception class.
7
7
  class RescueException < Cop
8
- MSG = 'Avoid rescuing the Exception class.'
8
+ MSG = 'Avoid rescuing the `Exception` class.'
9
9
 
10
10
  def on_resbody(node)
11
11
  return unless node.children.first
@@ -10,7 +10,7 @@ module Rubocop
10
10
  class ShadowingOuterLocalVariable < Cop
11
11
  include VariableInspector
12
12
 
13
- MSG = 'Shadowing outer local variable - %s'
13
+ MSG = 'Shadowing outer local variable - `%s`'
14
14
 
15
15
  def investigate(processed_source)
16
16
  inspect_variables(processed_source.ast)
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Lint
6
+ # Checks for space between a method name and the first argument for
7
+ # method calls without parentheses.
8
+ #
9
+ # @example
10
+ #
11
+ # something?x
12
+ # something!x
13
+ #
14
+ class SpaceBeforeFirstArg < Cop
15
+ MSG = 'Put space between the method name and the first argument.'
16
+
17
+ def on_send(node)
18
+ return if parentheses?(node)
19
+
20
+ _receiver, method_name, *args = *node
21
+ return if args.empty?
22
+ return if operator?(method_name)
23
+
24
+ # Setter calls with parentheses are parsed this way. The parentheses
25
+ # belong to the argument, not the send node.
26
+ return if args.first.type == :begin
27
+
28
+ arg1 = args.first.loc.expression
29
+ arg1_with_space = range_with_surrounding_space(arg1, :left)
30
+
31
+ add_offense(nil, arg1) if arg1_with_space.source =~ /^\S/
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -10,12 +10,12 @@ module Rubocop
10
10
  #
11
11
  # "result is #{something.to_s}"
12
12
  class StringConversionInInterpolation < Cop
13
- MSG = 'Redundant use of Object#to_s in interpolation.'
13
+ MSG = 'Redundant use of `Object#to_s` in interpolation.'
14
14
 
15
15
  def on_dstr(node)
16
16
  node.children.select { |n| n.type == :begin }.each do |begin_node|
17
17
  final_node = begin_node.children.last
18
- next unless final_node.type == :send
18
+ next unless final_node && final_node.type == :send
19
19
 
20
20
  _receiver, method_name, *args = *final_node
21
21
 
@@ -24,6 +24,18 @@ module Rubocop
24
24
  end
25
25
  end
26
26
  end
27
+
28
+ private
29
+
30
+ def autocorrect(node)
31
+ @corrections << lambda do |corrector|
32
+ receiver, _method_name, *_args = *node
33
+ corrector.replace(
34
+ node.loc.expression,
35
+ receiver.loc.expression.source
36
+ )
37
+ end
38
+ end
27
39
  end
28
40
  end
29
41
  end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+ module Rubocop
3
+ module Cop
4
+ module Lint
5
+ # This cop checks for access modifiers without any code.
6
+ #
7
+ # @example
8
+ # class Foo
9
+ # private # This is useless
10
+ #
11
+ # def self.some_method
12
+ # end
13
+ # end
14
+ class UselessAccessModifier < Cop
15
+ MSG = 'Useless `%s` access modifier.'
16
+
17
+ def on_class(node)
18
+ _name, _base_class, body = *node
19
+ return unless body
20
+
21
+ body_nodes = body.type == :begin ? body.children : [body]
22
+
23
+ body_nodes.each do |child_node|
24
+ check_for_access_modifier(child_node)
25
+ check_for_instance_method(child_node)
26
+ end
27
+
28
+ add_offense_for_access_modifier
29
+ end
30
+
31
+ private
32
+
33
+ def add_offense_for_access_modifier
34
+ return unless @access_modifier_node
35
+
36
+ _, modifier = *@access_modifier_node
37
+ message = format(MSG, modifier)
38
+ add_offense(@access_modifier_node, :expression, message)
39
+ end
40
+
41
+ def check_for_instance_method(node)
42
+ return unless node.type == :def
43
+
44
+ @access_modifier_node = nil
45
+ end
46
+
47
+ def check_for_access_modifier(node)
48
+ return unless Style::AccessModifierIndentation
49
+ .modifier_node?(node)
50
+
51
+ add_offense_for_access_modifier
52
+ @access_modifier_node = node
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -40,7 +40,7 @@ module Rubocop
40
40
 
41
41
  add_offense(receiver,
42
42
  :name,
43
- MSG.format(receiver.loc.name.source))
43
+ format(MSG, receiver.loc.name.source))
44
44
  end
45
45
 
46
46
  def setter_call_to_local_variable?(node)
@@ -6,9 +6,9 @@ module Rubocop
6
6
  # This cop checks for operators, variables and literals used
7
7
  # in void context.
8
8
  class Void < Cop
9
- OP_MSG = 'Operator %s used in void context.'
10
- VAR_MSG = 'Variable %s used in void context.'
11
- LIT_MSG = 'Literal %s used in void context'
9
+ OP_MSG = 'Operator `%s` used in void context.'
10
+ VAR_MSG = 'Variable `%s` used in void context.'
11
+ LIT_MSG = 'Literal `%s` used in void context'
12
12
 
13
13
  OPS = %w(* / % + - == === != < > <= >= <=>)
14
14
  VARS = [:ivar, :lvar, :cvar, :const]
@@ -11,7 +11,7 @@ module Rubocop
11
11
  processed_source.tokens.each_cons(2) do |t1, t2|
12
12
  if kind(t1) && t1.pos.line == t2.pos.line &&
13
13
  t2.pos.column == t1.pos.column + offset(t1) &&
14
- t2.type != :tRPAREN
14
+ ![:tRPAREN, :tRBRACK].include?(t2.type)
15
15
  add_offense(t1, t1.pos, format(MSG, kind(t1)))
16
16
  end
17
17
  end
@@ -6,16 +6,11 @@ module Rubocop
6
6
  class Offense
7
7
  include Comparable
8
8
 
9
- # @api private
10
- SEVERITIES = [:refactor, :convention, :warning, :error, :fatal]
11
-
12
9
  # @api public
13
10
  #
14
11
  # @!attribute [r] severity
15
12
  #
16
- # @return [Symbol]
17
- # severity.
18
- # any of `:refactor`, `:convention`, `:warning`, `:error` or `:fatal`.
13
+ # @return [Rubocop::Cop::Severity]
19
14
  attr_reader :severity
20
15
 
21
16
  # @api public
@@ -69,10 +64,7 @@ module Rubocop
69
64
 
70
65
  # @api private
71
66
  def initialize(severity, location, message, cop_name, corrected = false)
72
- unless SEVERITIES.include?(severity)
73
- fail ArgumentError, "Unknown severity: #{severity}"
74
- end
75
- @severity = severity.freeze
67
+ @severity = Rubocop::Cop::Severity.new(severity)
76
68
  @location = location.freeze
77
69
  @line = location.line.freeze
78
70
  @column = location.column.freeze
@@ -86,17 +78,7 @@ module Rubocop
86
78
  # This is just for debugging purpose.
87
79
  def to_s
88
80
  format('%s:%3d:%3d: %s',
89
- severity_code, line, real_column, message)
90
- end
91
-
92
- # @api private
93
- def severity_code
94
- @severity.to_s[0].upcase
95
- end
96
-
97
- # @api private
98
- def severity_level
99
- SEVERITIES.index(severity) + 1
81
+ severity.code, line, real_column, message)
100
82
  end
101
83
 
102
84
  # @api private
@@ -10,7 +10,7 @@ module Rubocop
10
10
  class ActionFilter < Cop
11
11
  include ConfigurableEnforcedStyle
12
12
 
13
- MSG = 'Prefer %s over %s.'
13
+ MSG = 'Prefer `%s` over `%s`.'
14
14
 
15
15
  FILTER_METHODS = [:before_filter, :skip_before_filter,
16
16
  :after_filter, :around_filter]
@@ -14,7 +14,7 @@ module Rubocop
14
14
  # # correct
15
15
  # default_scope { something }
16
16
  class DefaultScope < Cop
17
- MSG = 'default_scope expects a block as its sole argument.'
17
+ MSG = '`default_scope` expects a block as its sole argument.'
18
18
 
19
19
  def on_send(node)
20
20
  return unless command?(:default_scope, node)
@@ -5,7 +5,7 @@ module Rubocop
5
5
  module Rails
6
6
  # This cop checks for the use of the has_and_belongs_to_many macro.
7
7
  class HasAndBelongsToMany < Cop
8
- MSG = 'Prefer has_many :through to has_and_belongs_to_many.'
8
+ MSG = 'Prefer `has_many :through` to `has_and_belongs_to_many`.'
9
9
 
10
10
  def on_send(node)
11
11
  if command?(:has_and_belongs_to_many, node)
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Rails
6
+ # This cop checks for the use of the read_attribute or
7
+ # write_attribute methods.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # x = read_attributed(:attr)
13
+ # write_attribute(:attr, val)
14
+ #
15
+ # # good
16
+ # x = self[:attr]
17
+ # self[:attr] = val
18
+ class ReadWriteAttribute < Cop
19
+ MSG = 'Prefer `%s` over `%s`.'
20
+
21
+ def on_send(node)
22
+ receiver, method_name, *_args = *node
23
+
24
+ return if receiver
25
+
26
+ if [:read_attribute, :write_attribute].include?(method_name)
27
+ add_offense(node, :selector)
28
+ end
29
+ end
30
+
31
+ def message(node)
32
+ _receiver, method_name, *_args = *node
33
+
34
+ if method_name == :read_attribute
35
+ format(MSG, 'self[:attr]', 'read_attribute(:attr)')
36
+ else
37
+ format(MSG, 'self[:attr] = val', 'write_attribute(:attr, var)')
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -14,7 +14,7 @@ module Rubocop
14
14
  # # good
15
15
  # scope :something, -> { where(something: true) }
16
16
  class ScopeArgs < Cop
17
- MSG = 'Use lambda/proc instead of a plain method call.'
17
+ MSG = 'Use `lambda`/`proc` instead of a plain method call.'
18
18
 
19
19
  def on_send(node)
20
20
  return unless command?(:scope, node)
@@ -5,7 +5,7 @@ module Rubocop
5
5
  module Rails
6
6
  # This cop checks for the use of old-style attribute validation macros.
7
7
  class Validation < Cop
8
- MSG = 'Use the new "sexy" validations (validates ...).'
8
+ MSG = 'Use the new "sexy" validations (`validates` ...).'
9
9
 
10
10
  BLACKLIST = [:validates_acceptance_of,
11
11
  :validates_confirmation_of,
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ # Severity class is simple value object about severity
6
+ class Severity
7
+ include Comparable
8
+
9
+ # @api private
10
+ NAMES = [:refactor, :convention, :warning, :error, :fatal]
11
+
12
+ # @api private
13
+ CODE_TABLE = { R: :refactor, C: :convention,
14
+ W: :warning, E: :error, F: :fatal }
15
+
16
+ # @api public
17
+ #
18
+ # @!attribute [r] name
19
+ #
20
+ # @return [Symbol]
21
+ # severity.
22
+ # any of `:refactor`, `:convention`, `:warning`, `:error` or `:fatal`.
23
+ attr_reader :name
24
+
25
+ # @api private
26
+ def self.name_from_code(code)
27
+ name = code.to_sym
28
+ CODE_TABLE[name] || name
29
+ end
30
+
31
+ # @api private
32
+ def initialize(name_or_code)
33
+ name = Severity.name_from_code(name_or_code)
34
+ unless NAMES.include?(name)
35
+ fail ArgumentError, "Unknown severity: #{name}"
36
+ end
37
+ @name = name.freeze
38
+ freeze
39
+ end
40
+
41
+ # @api private
42
+ def to_s
43
+ @name.to_s
44
+ end
45
+
46
+ # @api private
47
+ def code
48
+ @name.to_s[0].upcase
49
+ end
50
+
51
+ # @api private
52
+ def level
53
+ NAMES.index(name) + 1
54
+ end
55
+
56
+ # @api private
57
+ def ==(other)
58
+ if other.is_a?(Symbol)
59
+ @name == other
60
+ else
61
+ @name == other.name
62
+ end
63
+ end
64
+
65
+ # @api private
66
+ def hash
67
+ @name.hash
68
+ end
69
+
70
+ # @api private
71
+ def <=>(other)
72
+ level <=> other.level
73
+ end
74
+ end
75
+ end
76
+ end
@@ -9,7 +9,7 @@ module Rubocop
9
9
  include AutocorrectAlignment
10
10
  include ConfigurableEnforcedStyle
11
11
 
12
- MSG = '%s access modifiers like %s.'
12
+ MSG = '%s access modifiers like `%s`.'
13
13
 
14
14
  PRIVATE_NODE = s(:send, nil, :private)
15
15
  PROTECTED_NODE = s(:send, nil, :protected)
@@ -25,10 +25,10 @@ module Rubocop
25
25
  def check(node, method_name, args, _body)
26
26
  if bad_reader_name?(method_name.to_s, args)
27
27
  add_offense(node, :name,
28
- 'Do not prefix reader method names with get_.')
28
+ 'Do not prefix reader method names with `get_`.')
29
29
  elsif bad_writer_name?(method_name.to_s, args)
30
30
  add_offense(node, :name,
31
- 'Do not prefix writer method names with set_.')
31
+ 'Do not prefix writer method names with `set_`.')
32
32
  end
33
33
  end
34
34