rubocop-rails 2.11.2 → 2.19.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +23 -2
  4. data/config/default.yml +316 -20
  5. data/config/obsoletion.yml +10 -0
  6. data/lib/rubocop/cop/mixin/active_record_helper.rb +17 -8
  7. data/lib/rubocop/cop/mixin/active_record_migrations_helper.rb +32 -0
  8. data/lib/rubocop/cop/mixin/class_send_node_helper.rb +20 -0
  9. data/lib/rubocop/cop/mixin/enforce_superclass.rb +1 -1
  10. data/lib/rubocop/cop/mixin/index_method.rb +7 -17
  11. data/lib/rubocop/cop/mixin/migrations_helper.rb +26 -0
  12. data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +112 -0
  13. data/lib/rubocop/cop/rails/action_controller_test_case.rb +47 -0
  14. data/lib/rubocop/cop/rails/action_filter.rb +2 -2
  15. data/lib/rubocop/cop/rails/action_order.rb +116 -0
  16. data/lib/rubocop/cop/rails/active_record_aliases.rb +9 -6
  17. data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +8 -13
  18. data/lib/rubocop/cop/rails/active_record_override.rb +2 -5
  19. data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -1
  20. data/lib/rubocop/cop/rails/active_support_on_load.rb +70 -0
  21. data/lib/rubocop/cop/rails/add_column_index.rb +3 -6
  22. data/lib/rubocop/cop/rails/after_commit_override.rb +3 -13
  23. data/lib/rubocop/cop/rails/application_controller.rb +6 -2
  24. data/lib/rubocop/cop/rails/application_job.rb +7 -3
  25. data/lib/rubocop/cop/rails/application_mailer.rb +6 -2
  26. data/lib/rubocop/cop/rails/application_record.rb +7 -2
  27. data/lib/rubocop/cop/rails/arel_star.rb +8 -2
  28. data/lib/rubocop/cop/rails/assert_not.rb +1 -1
  29. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +1 -1
  30. data/lib/rubocop/cop/rails/belongs_to.rb +2 -5
  31. data/lib/rubocop/cop/rails/blank.rb +13 -13
  32. data/lib/rubocop/cop/rails/bulk_change_table.rb +28 -31
  33. data/lib/rubocop/cop/rails/compact_blank.rb +111 -0
  34. data/lib/rubocop/cop/rails/content_tag.rb +42 -33
  35. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +16 -8
  36. data/lib/rubocop/cop/rails/date.rb +12 -17
  37. data/lib/rubocop/cop/rails/default_scope.rb +1 -1
  38. data/lib/rubocop/cop/rails/delegate.rb +24 -18
  39. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +2 -2
  40. data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +168 -0
  41. data/lib/rubocop/cop/rails/dot_separated_keys.rb +71 -0
  42. data/lib/rubocop/cop/rails/duplicate_association.rb +56 -0
  43. data/lib/rubocop/cop/rails/duplicate_scope.rb +46 -0
  44. data/lib/rubocop/cop/rails/duration_arithmetic.rb +98 -0
  45. data/lib/rubocop/cop/rails/dynamic_find_by.rb +31 -15
  46. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +6 -2
  47. data/lib/rubocop/cop/rails/enum_hash.rb +3 -4
  48. data/lib/rubocop/cop/rails/enum_uniqueness.rb +3 -6
  49. data/lib/rubocop/cop/rails/environment_comparison.rb +3 -4
  50. data/lib/rubocop/cop/rails/environment_variable_access.rb +1 -1
  51. data/lib/rubocop/cop/rails/exit.rb +1 -1
  52. data/lib/rubocop/cop/rails/expanded_date_range.rb +39 -23
  53. data/lib/rubocop/cop/rails/file_path.rb +41 -24
  54. data/lib/rubocop/cop/rails/find_by.rb +1 -1
  55. data/lib/rubocop/cop/rails/find_by_id.rb +3 -3
  56. data/lib/rubocop/cop/rails/find_each.rb +27 -4
  57. data/lib/rubocop/cop/rails/freeze_time.rb +79 -0
  58. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
  59. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +19 -9
  60. data/lib/rubocop/cop/rails/helper_instance_variable.rb +3 -3
  61. data/lib/rubocop/cop/rails/http_positional_arguments.rb +29 -11
  62. data/lib/rubocop/cop/rails/http_status.rb +6 -11
  63. data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +96 -0
  64. data/lib/rubocop/cop/rails/i18n_locale_assignment.rb +1 -1
  65. data/lib/rubocop/cop/rails/i18n_locale_texts.rb +110 -0
  66. data/lib/rubocop/cop/rails/ignored_columns_assignment.rb +50 -0
  67. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +5 -14
  68. data/lib/rubocop/cop/rails/index_by.rb +8 -8
  69. data/lib/rubocop/cop/rails/index_with.rb +8 -8
  70. data/lib/rubocop/cop/rails/inquiry.rb +1 -1
  71. data/lib/rubocop/cop/rails/inverse_of.rb +20 -10
  72. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +44 -16
  73. data/lib/rubocop/cop/rails/link_to_blank.rb +4 -7
  74. data/lib/rubocop/cop/rails/mailer_name.rb +9 -5
  75. data/lib/rubocop/cop/rails/match_route.rb +1 -1
  76. data/lib/rubocop/cop/rails/migration_class_name.rb +63 -0
  77. data/lib/rubocop/cop/rails/negate_include.rb +5 -4
  78. data/lib/rubocop/cop/rails/not_null_column.rb +10 -7
  79. data/lib/rubocop/cop/rails/order_by_id.rb +2 -3
  80. data/lib/rubocop/cop/rails/output.rb +26 -10
  81. data/lib/rubocop/cop/rails/output_safety.rb +6 -2
  82. data/lib/rubocop/cop/rails/pick.rb +8 -1
  83. data/lib/rubocop/cop/rails/pluck.rb +51 -11
  84. data/lib/rubocop/cop/rails/pluck_id.rb +5 -2
  85. data/lib/rubocop/cop/rails/pluck_in_where.rb +8 -7
  86. data/lib/rubocop/cop/rails/pluralization_grammar.rb +4 -5
  87. data/lib/rubocop/cop/rails/presence.rb +22 -13
  88. data/lib/rubocop/cop/rails/present.rb +10 -13
  89. data/lib/rubocop/cop/rails/rake_environment.rb +8 -3
  90. data/lib/rubocop/cop/rails/read_write_attribute.rb +52 -15
  91. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +7 -17
  92. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +3 -3
  93. data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +257 -0
  94. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +31 -27
  95. data/lib/rubocop/cop/rails/redundant_travel_back.rb +57 -0
  96. data/lib/rubocop/cop/rails/reflection_class_name.rb +39 -4
  97. data/lib/rubocop/cop/rails/refute_methods.rb +1 -5
  98. data/lib/rubocop/cop/rails/relative_date_constant.rb +9 -9
  99. data/lib/rubocop/cop/rails/render_inline.rb +1 -1
  100. data/lib/rubocop/cop/rails/render_plain_text.rb +1 -1
  101. data/lib/rubocop/cop/rails/request_referer.rb +2 -3
  102. data/lib/rubocop/cop/rails/require_dependency.rb +2 -2
  103. data/lib/rubocop/cop/rails/response_parsed_body.rb +57 -0
  104. data/lib/rubocop/cop/rails/reversible_migration.rb +29 -67
  105. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +6 -15
  106. data/lib/rubocop/cop/rails/root_join_chain.rb +72 -0
  107. data/lib/rubocop/cop/rails/root_pathname_methods.rb +238 -0
  108. data/lib/rubocop/cop/rails/root_public_path.rb +59 -0
  109. data/lib/rubocop/cop/rails/safe_navigation.rb +8 -13
  110. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +14 -7
  111. data/lib/rubocop/cop/rails/save_bang.rb +30 -23
  112. data/lib/rubocop/cop/rails/schema_comment.rb +104 -0
  113. data/lib/rubocop/cop/rails/scope_args.rb +6 -2
  114. data/lib/rubocop/cop/rails/short_i18n.rb +3 -6
  115. data/lib/rubocop/cop/rails/skips_model_validations.rb +3 -4
  116. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +13 -8
  117. data/lib/rubocop/cop/rails/strip_heredoc.rb +56 -0
  118. data/lib/rubocop/cop/rails/table_name_assignment.rb +44 -0
  119. data/lib/rubocop/cop/rails/three_state_boolean_column.rb +73 -0
  120. data/lib/rubocop/cop/rails/time_zone.rb +37 -32
  121. data/lib/rubocop/cop/rails/time_zone_assignment.rb +4 -4
  122. data/lib/rubocop/cop/rails/to_formatted_s.rb +46 -0
  123. data/lib/rubocop/cop/rails/to_s_with_argument.rb +78 -0
  124. data/lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb +49 -0
  125. data/lib/rubocop/cop/rails/transaction_exit_statement.rb +99 -0
  126. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +32 -41
  127. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +16 -9
  128. data/lib/rubocop/cop/rails/unknown_env.rb +3 -5
  129. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +9 -2
  130. data/lib/rubocop/cop/rails/validation.rb +5 -13
  131. data/lib/rubocop/cop/rails/where_equals.rb +6 -2
  132. data/lib/rubocop/cop/rails/where_exists.rb +11 -10
  133. data/lib/rubocop/cop/rails/where_missing.rb +118 -0
  134. data/lib/rubocop/cop/rails/where_not.rb +2 -2
  135. data/lib/rubocop/cop/rails/where_not_with_multiple_conditions.rb +55 -0
  136. data/lib/rubocop/cop/rails_cops.rb +34 -0
  137. data/lib/rubocop/rails/schema_loader/schema.rb +8 -5
  138. data/lib/rubocop/rails/version.rb +1 -1
  139. data/lib/rubocop/rails.rb +1 -1
  140. data/lib/rubocop-rails.rb +19 -0
  141. metadata +42 -8
  142. data/bin/setup +0 -7
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Checks for multiple scopes in a model that have the same `where` clause. This
7
+ # often means you copy/pasted a scope, updated the name, and forgot to change the condition.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # scope :visible, -> { where(visible: true) }
13
+ # scope :hidden, -> { where(visible: true) }
14
+ #
15
+ # # good
16
+ # scope :visible, -> { where(visible: true) }
17
+ # scope :hidden, -> { where(visible: false) }
18
+ #
19
+ class DuplicateScope < Base
20
+ include ClassSendNodeHelper
21
+
22
+ MSG = 'Multiple scopes share this same where clause.'
23
+
24
+ def_node_matcher :scope, <<~PATTERN
25
+ (send nil? :scope _ $...)
26
+ PATTERN
27
+
28
+ def on_class(class_node)
29
+ offenses(class_node).each do |node|
30
+ add_offense(node)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def offenses(class_node)
37
+ class_send_nodes(class_node).select { |node| scope(node) }
38
+ .group_by { |node| scope(node) }
39
+ .select { |_, nodes| nodes.length > 1 }
40
+ .values
41
+ .flatten
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Checks if a duration is added to or subtracted from `Time.current`.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # Time.current - 1.minute
11
+ # Time.current + 2.days
12
+ #
13
+ # # good - using relative would make it harder to express and read
14
+ # Date.yesterday + 3.days
15
+ # created_at - 1.minute
16
+ # 3.days - 1.hour
17
+ #
18
+ # # good
19
+ # 1.minute.ago
20
+ # 2.days.from_now
21
+ class DurationArithmetic < Base
22
+ extend AutoCorrector
23
+
24
+ MSG = 'Do not add or subtract duration.'
25
+
26
+ RESTRICT_ON_SEND = %i[+ -].freeze
27
+
28
+ DURATIONS = Set[:second, :seconds, :minute, :minutes, :hour, :hours,
29
+ :day, :days, :week, :weeks, :fortnight, :fortnights,
30
+ :month, :months, :year, :years]
31
+
32
+ # @!method duration_arithmetic_argument?(node)
33
+ # Match duration subtraction or addition with current time.
34
+ #
35
+ # @example source that matches
36
+ # Time.current - 1.hour
37
+ #
38
+ # @example source that matches
39
+ # ::Time.zone.now + 1.hour
40
+ #
41
+ # @param node [RuboCop::AST::Node]
42
+ # @yield operator and duration
43
+ def_node_matcher :duration_arithmetic_argument?, <<~PATTERN
44
+ (send #time_current? ${ :+ :- } $#duration?)
45
+ PATTERN
46
+
47
+ # @!method duration?(node)
48
+ # Match a literal Duration
49
+ #
50
+ # @example source that matches
51
+ # 1.hour
52
+ #
53
+ # @example source that matches
54
+ # 9.5.weeks
55
+ #
56
+ # @param node [RuboCop::AST::Node]
57
+ # @return [Boolean] true if matches
58
+ def_node_matcher :duration?, '(send { int float (send nil _) } DURATIONS)'
59
+
60
+ # @!method time_current?(node)
61
+ # Match Time.current
62
+ #
63
+ # @example source that matches
64
+ # Time.current
65
+ #
66
+ # @example source that matches
67
+ # ::Time.zone.now
68
+ #
69
+ # @param node [RuboCop::AST::Node]
70
+ # @return [Boolean] true if matches
71
+ def_node_matcher :time_current?, <<~PATTERN
72
+ {
73
+ (send (const {nil? cbase} :Time) :current)
74
+ (send (send (const {nil? cbase} :Time) :zone) :now)
75
+ }
76
+ PATTERN
77
+
78
+ def on_send(node)
79
+ duration_arithmetic_argument?(node) do |*operation|
80
+ add_offense(node) do |corrector|
81
+ corrector.replace(node, corrected_source(*operation))
82
+ end
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def corrected_source(operator, duration)
89
+ if operator == :-
90
+ "#{duration.source}.ago"
91
+ else
92
+ "#{duration.source}.from_now"
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -3,10 +3,14 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks dynamic `find_by_*` methods.
6
+ # Checks dynamic `find_by_*` methods.
7
7
  # Use `find_by` instead of dynamic method.
8
8
  # See. https://rails.rubystyle.guide#find_by
9
9
  #
10
+ # @safety
11
+ # It is certainly unsafe when not configured properly, i.e. user-defined `find_by_xxx`
12
+ # method is not added to cop's `AllowedMethods`.
13
+ #
10
14
  # @example
11
15
  # # bad
12
16
  # User.find_by_name(name)
@@ -18,19 +22,23 @@ module RuboCop
18
22
  # User.find_by(name: name, email: email)
19
23
  # User.find_by!(email: email)
20
24
  #
21
- # @example AllowedMethods: find_by_sql
25
+ # @example AllowedMethods: ['find_by_sql', 'find_by_token_for'] (default)
22
26
  # # bad
23
27
  # User.find_by_query(users_query)
28
+ # User.find_by_token_for(:password_reset, token)
24
29
  #
25
30
  # # good
26
31
  # User.find_by_sql(users_sql)
32
+ # User.find_by_token_for(:password_reset, token)
27
33
  #
28
- # @example AllowedReceivers: Gem::Specification
34
+ # @example AllowedReceivers: ['Gem::Specification', 'page'] (default)
29
35
  # # bad
30
36
  # Specification.find_by_name('backend').gem_dir
37
+ # page.find_by_id('a_dom_id').click
31
38
  #
32
39
  # # good
33
40
  # Gem::Specification.find_by_name('backend').gem_dir
41
+ # page.find_by_id('a_dom_id').click
34
42
  class DynamicFindBy < Base
35
43
  include ActiveRecordHelper
36
44
  extend AutoCorrector
@@ -40,12 +48,12 @@ module RuboCop
40
48
  IGNORED_ARGUMENT_TYPES = %i[hash splat].freeze
41
49
 
42
50
  def on_send(node)
43
- return if node.receiver.nil? && !inherit_active_record_base?(node) || allowed_invocation?(node)
51
+ return if (node.receiver.nil? && !inherit_active_record_base?(node)) || allowed_invocation?(node)
44
52
 
45
53
  method_name = node.method_name
46
54
  static_name = static_method_name(method_name)
47
55
  return unless static_name
48
- return if node.arguments.any? { |argument| IGNORED_ARGUMENT_TYPES.include?(argument.type) }
56
+ return unless dynamic_find_by_arguments?(node)
49
57
 
50
58
  message = format(MSG, static_name: static_name, method: method_name)
51
59
  add_offense(node, message: message) do |corrector|
@@ -57,17 +65,12 @@ module RuboCop
57
65
  private
58
66
 
59
67
  def autocorrect(corrector, node)
60
- keywords = column_keywords(node.method_name)
61
-
62
- return if keywords.size != node.arguments.size
63
-
64
68
  autocorrect_method_name(corrector, node)
65
- autocorrect_argument_keywords(corrector, node, keywords)
69
+ autocorrect_argument_keywords(corrector, node, column_keywords(node.method_name))
66
70
  end
67
71
 
68
72
  def allowed_invocation?(node)
69
- allowed_method?(node) || allowed_receiver?(node) ||
70
- whitelisted?(node)
73
+ allowed_method?(node) || allowed_receiver?(node) || whitelisted?(node)
71
74
  end
72
75
 
73
76
  def allowed_method?(node)
@@ -91,13 +94,12 @@ module RuboCop
91
94
  end
92
95
 
93
96
  def autocorrect_method_name(corrector, node)
94
- corrector.replace(node.loc.selector,
95
- static_method_name(node.method_name.to_s))
97
+ corrector.replace(node.loc.selector, static_method_name(node.method_name.to_s))
96
98
  end
97
99
 
98
100
  def autocorrect_argument_keywords(corrector, node, keywords)
99
101
  keywords.each.with_index do |keyword, idx|
100
- corrector.insert_before(node.arguments[idx].loc.expression, keyword)
102
+ corrector.insert_before(node.arguments[idx], keyword)
101
103
  end
102
104
  end
103
105
 
@@ -114,6 +116,20 @@ module RuboCop
114
116
 
115
117
  match[2] ? 'find_by!' : 'find_by'
116
118
  end
119
+
120
+ def dynamic_find_by_arguments?(node)
121
+ dynamic_find_by_arguments_count?(node) && dynamic_find_by_arguments_type?(node)
122
+ end
123
+
124
+ def dynamic_find_by_arguments_count?(node)
125
+ column_keywords(node.method_name).size == node.arguments.size
126
+ end
127
+
128
+ def dynamic_find_by_arguments_type?(node)
129
+ node.arguments.none? do |argument|
130
+ IGNORED_ARGUMENT_TYPES.include?(argument.type)
131
+ end
132
+ end
117
133
  end
118
134
  end
119
135
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that blocks are used for interpolated strings passed to
6
+ # Checks that blocks are used for interpolated strings passed to
7
7
  # `Rails.logger.debug`.
8
8
  #
9
9
  # By default, Rails production environments use the `:info` log level.
@@ -37,6 +37,10 @@ module RuboCop
37
37
  )
38
38
  PATTERN
39
39
 
40
+ def self.autocorrect_incompatible_with
41
+ [Style::MethodCallWithArgsParentheses]
42
+ end
43
+
40
44
  def on_send(node)
41
45
  return if node.parent&.block_type?
42
46
 
@@ -55,7 +59,7 @@ module RuboCop
55
59
  private
56
60
 
57
61
  def replacement_range(node)
58
- stop = node.loc.expression.end
62
+ stop = node.source_range.end
59
63
  start = node.loc.selector.end
60
64
 
61
65
  if node.parenthesized_call?
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for enums written with array syntax.
6
+ # Looks for enums written with array syntax.
7
7
  #
8
8
  # When using array syntax, adding an element in a
9
9
  # position other than the last causes all previous
@@ -20,8 +20,7 @@ module RuboCop
20
20
  class EnumHash < Base
21
21
  extend AutoCorrector
22
22
 
23
- MSG = 'Enum defined as an array found in `%<enum>s` enum declaration. '\
24
- 'Use hash syntax instead.'
23
+ MSG = 'Enum defined as an array found in `%<enum>s` enum declaration. Use hash syntax instead.'
25
24
  RESTRICT_ON_SEND = %i[enum].freeze
26
25
 
27
26
  def_node_matcher :enum?, <<~PATTERN
@@ -43,7 +42,7 @@ module RuboCop
43
42
  "#{source(elem)} => #{index}"
44
43
  end.join(', ')
45
44
 
46
- corrector.replace(array.loc.expression, "{#{hash}}")
45
+ corrector.replace(array, "{#{hash}}")
47
46
  end
48
47
  end
49
48
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for duplicate values in enum declarations.
6
+ # Looks for duplicate values in enum declarations.
7
7
  #
8
8
  # @example
9
9
  # # bad
@@ -20,8 +20,7 @@ module RuboCop
20
20
  class EnumUniqueness < Base
21
21
  include Duplication
22
22
 
23
- MSG = 'Duplicate value `%<value>s` found in `%<enum>s` ' \
24
- 'enum declaration.'
23
+ MSG = 'Duplicate value `%<value>s` found in `%<enum>s` enum declaration.'
25
24
  RESTRICT_ON_SEND = %i[enum].freeze
26
25
 
27
26
  def_node_matcher :enum?, <<~PATTERN
@@ -41,9 +40,7 @@ module RuboCop
41
40
  next unless duplicates?(items)
42
41
 
43
42
  consecutive_duplicates(items).each do |item|
44
- add_offense(item, message: format(
45
- MSG, value: item.source, enum: enum_name(key)
46
- ))
43
+ add_offense(item, message: format(MSG, value: item.source, enum: enum_name(key)))
47
44
  end
48
45
  end
49
46
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that Rails.env is compared using `.production?`-like
6
+ # Checks that Rails.env is compared using `.production?`-like
7
7
  # methods instead of equality against a string or symbol.
8
8
  #
9
9
  # @example
@@ -84,7 +84,7 @@ module RuboCop
84
84
  def autocorrect(corrector, node)
85
85
  replacement = build_predicate_method(node)
86
86
 
87
- corrector.replace(node.source_range, replacement)
87
+ corrector.replace(node, replacement)
88
88
  end
89
89
 
90
90
  def build_predicate_method(node)
@@ -96,8 +96,7 @@ module RuboCop
96
96
  end
97
97
 
98
98
  def rails_env_on_lhs?(node)
99
- comparing_str_env_with_rails_env_on_lhs?(node) ||
100
- comparing_sym_env_with_rails_env_on_lhs?(node)
99
+ comparing_str_env_with_rails_env_on_lhs?(node) || comparing_sym_env_with_rails_env_on_lhs?(node)
101
100
  end
102
101
 
103
102
  def build_predicate_method_for_rails_env_on_lhs(node)
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for direct access to environment variables through the
6
+ # Looks for direct access to environment variables through the
7
7
  # `ENV` variable within the application code. This can lead to runtime
8
8
  # errors due to misconfiguration that could have been discovered at boot
9
9
  # time if the environment variables were loaded as part of initialization
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop enforces that `exit` calls are not used within a rails app.
6
+ # Enforces that `exit` calls are not used within a rails app.
7
7
  # Valid options are instead to raise an error, break, return, or some
8
8
  # other form of stopping execution of current request.
9
9
  #
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks for expanded date range. It only compatible `..` range is targeted.
6
+ # Checks for expanded date range. It only compatible `..` range is targeted.
7
7
  # Incompatible `...` range is ignored.
8
8
  #
9
9
  # @example
@@ -29,14 +29,6 @@ module RuboCop
29
29
 
30
30
  minimum_target_rails_version 5.1
31
31
 
32
- def_node_matcher :expanded_date_range, <<~PATTERN
33
- (irange
34
- (send
35
- $_ {:beginning_of_day :beginning_of_week :beginning_of_month :beginning_of_quarter :beginning_of_year})
36
- (send
37
- $_ {:end_of_day :end_of_week :end_of_month :end_of_quarter :end_of_year}))
38
- PATTERN
39
-
40
32
  PREFERRED_METHODS = {
41
33
  beginning_of_day: 'all_day',
42
34
  beginning_of_week: 'all_week',
@@ -54,31 +46,55 @@ module RuboCop
54
46
  }.freeze
55
47
 
56
48
  def on_irange(node)
57
- return unless expanded_date_range(node)
58
-
59
49
  begin_node = node.begin
60
50
  end_node = node.end
61
- return unless same_receiver?(begin_node, end_node)
62
-
63
- beginning_method = begin_node.method_name
64
- end_method = end_node.method_name
65
- return unless use_mapped_methods?(beginning_method, end_method)
51
+ return if allow?(begin_node, end_node)
66
52
 
67
- preferred_method = "#{begin_node.receiver.source}.#{PREFERRED_METHODS[beginning_method]}"
53
+ preferred_method = preferred_method(begin_node)
54
+ if begin_node.method?(:beginning_of_week) && begin_node.arguments.one?
55
+ return unless same_argument?(begin_node, end_node)
68
56
 
69
- add_offense(node, message: format(MSG, preferred_method: preferred_method)) do |corrector|
70
- corrector.replace(node, preferred_method)
57
+ preferred_method << "(#{begin_node.first_argument.source})"
58
+ elsif any_arguments?(begin_node, end_node)
59
+ return
71
60
  end
61
+
62
+ register_offense(node, preferred_method)
72
63
  end
73
64
 
74
65
  private
75
66
 
76
- def same_receiver?(begin_node, end_node)
77
- begin_node.receiver.source == end_node.receiver.source
67
+ def allow?(begin_node, end_node)
68
+ return true unless (begin_source = receiver_source(begin_node))
69
+ return true unless (end_source = receiver_source(end_node))
70
+
71
+ begin_source != end_source || MAPPED_DATE_RANGE_METHODS[begin_node.method_name] != end_node.method_name
72
+ end
73
+
74
+ def receiver_source(node)
75
+ return if !node&.send_type? || node.receiver.nil?
76
+
77
+ node.receiver.source
78
78
  end
79
79
 
80
- def use_mapped_methods?(beginning_method, end_method)
81
- MAPPED_DATE_RANGE_METHODS[beginning_method] == end_method
80
+ def same_argument?(begin_node, end_node)
81
+ begin_node.first_argument.source == end_node.first_argument.source
82
+ end
83
+
84
+ def preferred_method(begin_node)
85
+ +"#{begin_node.receiver.source}.#{PREFERRED_METHODS[begin_node.method_name]}"
86
+ end
87
+
88
+ def any_arguments?(begin_node, end_node)
89
+ begin_node.arguments.any? || end_node.arguments.any?
90
+ end
91
+
92
+ def register_offense(node, preferred_method)
93
+ message = format(MSG, preferred_method: preferred_method)
94
+
95
+ add_offense(node, message: message) do |corrector|
96
+ corrector.replace(node, preferred_method)
97
+ end
82
98
  end
83
99
  end
84
100
  end
@@ -3,55 +3,66 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop is used to identify usages of file path joining process
7
- # to use `Rails.root.join` clause. It is used to add uniformity when
8
- # joining paths.
6
+ # Identifies usages of file path joining process to use `Rails.root.join` clause.
7
+ # It is used to add uniformity when joining paths.
9
8
  #
10
- # @example EnforcedStyle: arguments
9
+ # @example EnforcedStyle: slashes (default)
11
10
  # # bad
11
+ # Rails.root.join('app', 'models', 'goober')
12
+ #
13
+ # # good
12
14
  # Rails.root.join('app/models/goober')
15
+ #
16
+ # # bad
13
17
  # File.join(Rails.root, 'app/models/goober')
14
18
  # "#{Rails.root}/app/models/goober"
15
19
  #
16
20
  # # good
17
- # Rails.root.join('app', 'models', 'goober')
21
+ # Rails.root.join('app/models/goober').to_s
18
22
  #
19
- # @example EnforcedStyle: slashes (default)
23
+ # @example EnforcedStyle: arguments
20
24
  # # bad
25
+ # Rails.root.join('app/models/goober')
26
+ #
27
+ # # good
21
28
  # Rails.root.join('app', 'models', 'goober')
29
+ #
30
+ # # bad
22
31
  # File.join(Rails.root, 'app/models/goober')
23
32
  # "#{Rails.root}/app/models/goober"
24
33
  #
25
34
  # # good
26
- # Rails.root.join('app/models/goober')
35
+ # Rails.root.join('app', 'models', 'goober').to_s
27
36
  #
28
37
  class FilePath < Base
29
38
  include ConfigurableEnforcedStyle
30
39
  include RangeHelp
31
40
 
32
- MSG_SLASHES = 'Prefer `Rails.root.join(\'path/to\')`.'
33
- MSG_ARGUMENTS = 'Prefer `Rails.root.join(\'path\', \'to\')`.'
41
+ MSG_SLASHES = 'Prefer `Rails.root.join(\'path/to\')%<to_s>s`.'
42
+ MSG_ARGUMENTS = 'Prefer `Rails.root.join(\'path\', \'to\')%<to_s>s`.'
34
43
  RESTRICT_ON_SEND = %i[join].freeze
35
44
 
36
45
  def_node_matcher :file_join_nodes?, <<~PATTERN
37
- (send (const nil? :File) :join ...)
46
+ (send (const {nil? cbase} :File) :join ...)
38
47
  PATTERN
39
48
 
40
49
  def_node_search :rails_root_nodes?, <<~PATTERN
41
- (send (const nil? :Rails) :root)
50
+ (send (const {nil? cbase} :Rails) :root)
42
51
  PATTERN
43
52
 
44
53
  def_node_matcher :rails_root_join_nodes?, <<~PATTERN
45
- (send (send (const nil? :Rails) :root) :join ...)
54
+ (send #rails_root_nodes? :join ...)
46
55
  PATTERN
47
56
 
48
57
  def on_dstr(node)
49
58
  return unless rails_root_nodes?(node)
50
59
  return unless node.children.last.str_type?
51
- return unless node.children.last.source.start_with?('.') ||
52
- node.children.last.source.include?(File::SEPARATOR)
53
60
 
54
- register_offense(node)
61
+ last_child_source = node.children.last.source
62
+ return unless last_child_source.start_with?('.') || last_child_source.include?(File::SEPARATOR)
63
+ return if last_child_source.start_with?(':')
64
+
65
+ register_offense(node, require_to_s: true)
55
66
  end
56
67
 
57
68
  def on_send(node)
@@ -66,7 +77,7 @@ module RuboCop
66
77
  return unless file_join_nodes?(node)
67
78
  return unless node.arguments.any? { |e| rails_root_nodes?(e) }
68
79
 
69
- register_offense(node)
80
+ register_offense(node, require_to_s: true)
70
81
  end
71
82
 
72
83
  def check_for_rails_root_join_with_string_arguments(node)
@@ -76,7 +87,7 @@ module RuboCop
76
87
  return unless node.arguments.size > 1
77
88
  return unless node.arguments.all?(&:str_type?)
78
89
 
79
- register_offense(node)
90
+ register_offense(node, require_to_s: false)
80
91
  end
81
92
 
82
93
  def check_for_rails_root_join_with_slash_separated_path(node)
@@ -85,22 +96,28 @@ module RuboCop
85
96
  return unless rails_root_join_nodes?(node)
86
97
  return unless node.arguments.any? { |arg| string_with_slash?(arg) }
87
98
 
88
- register_offense(node)
99
+ register_offense(node, require_to_s: false)
89
100
  end
90
101
 
91
102
  def string_with_slash?(node)
92
103
  node.str_type? && node.source.include?('/')
93
104
  end
94
105
 
95
- def register_offense(node)
106
+ def register_offense(node, require_to_s:)
96
107
  line_range = node.loc.column...node.loc.last_column
97
- source_range = source_range(processed_source.buffer, node.first_line,
98
- line_range)
99
- add_offense(source_range)
108
+ source_range = source_range(processed_source.buffer, node.first_line, line_range)
109
+ require_to_s = false if node.dstr_type?
110
+
111
+ message = build_message(require_to_s)
112
+
113
+ add_offense(source_range, message: message)
100
114
  end
101
115
 
102
- def message(_range)
103
- format(style == :arguments ? MSG_ARGUMENTS : MSG_SLASHES)
116
+ def build_message(require_to_s)
117
+ message_template = style == :arguments ? MSG_ARGUMENTS : MSG_SLASHES
118
+ to_s = require_to_s ? '.to_s' : ''
119
+
120
+ format(message_template, to_s: to_s)
104
121
  end
105
122
  end
106
123
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop is used to identify usages of `where.take` and change them to use `find_by` instead.
6
+ # Identifies usages of `where.take` and change them to use `find_by` instead.
7
7
  #
8
8
  # And `where(...).first` can return different results from `find_by`.
9
9
  # (They order records differently, so the "first" record can be different.)
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop enforces that `ActiveRecord#find` is used instead of
6
+ # Enforces that `ActiveRecord#find` is used instead of
7
7
  # `where.take!`, `find_by!`, and `find_by_id!` to retrieve a single record
8
8
  # by primary key when you expect it to be found.
9
9
  #
@@ -65,11 +65,11 @@ module RuboCop
65
65
  end
66
66
 
67
67
  def where_take_offense_range(node, where)
68
- range_between(where.loc.selector.begin_pos, node.loc.expression.end_pos)
68
+ range_between(where.loc.selector.begin_pos, node.source_range.end_pos)
69
69
  end
70
70
 
71
71
  def find_by_offense_range(node)
72
- range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos)
72
+ range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
73
73
  end
74
74
 
75
75
  def build_good_method(id_value)