rubocop-rails 2.0.1 → 2.19.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +52 -5
  4. data/config/default.yml +726 -32
  5. data/config/obsoletion.yml +17 -0
  6. data/lib/rubocop/cop/mixin/active_record_helper.rb +106 -0
  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 +40 -0
  10. data/lib/rubocop/cop/mixin/index_method.rb +165 -0
  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 +11 -21
  15. data/lib/rubocop/cop/rails/action_order.rb +116 -0
  16. data/lib/rubocop/cop/rails/active_record_aliases.rb +23 -24
  17. data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +143 -0
  18. data/lib/rubocop/cop/rails/active_record_override.rb +3 -6
  19. data/lib/rubocop/cop/rails/active_support_aliases.rb +13 -22
  20. data/lib/rubocop/cop/rails/active_support_on_load.rb +70 -0
  21. data/lib/rubocop/cop/rails/add_column_index.rb +61 -0
  22. data/lib/rubocop/cop/rails/after_commit_override.rb +81 -0
  23. data/lib/rubocop/cop/rails/application_controller.rb +36 -0
  24. data/lib/rubocop/cop/rails/application_job.rb +9 -4
  25. data/lib/rubocop/cop/rails/application_mailer.rb +39 -0
  26. data/lib/rubocop/cop/rails/application_record.rb +9 -9
  27. data/lib/rubocop/cop/rails/arel_star.rb +47 -0
  28. data/lib/rubocop/cop/rails/assert_not.rb +8 -10
  29. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +90 -0
  30. data/lib/rubocop/cop/rails/belongs_to.rb +12 -24
  31. data/lib/rubocop/cop/rails/blank.rb +40 -36
  32. data/lib/rubocop/cop/rails/bulk_change_table.rb +40 -35
  33. data/lib/rubocop/cop/rails/compact_blank.rb +111 -0
  34. data/lib/rubocop/cop/rails/content_tag.rb +93 -0
  35. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +22 -15
  36. data/lib/rubocop/cop/rails/date.rb +41 -36
  37. data/lib/rubocop/cop/rails/default_scope.rb +61 -0
  38. data/lib/rubocop/cop/rails/delegate.rb +33 -29
  39. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +9 -10
  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 +76 -31
  46. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +82 -0
  47. data/lib/rubocop/cop/rails/enum_hash.rb +75 -0
  48. data/lib/rubocop/cop/rails/enum_uniqueness.rb +30 -12
  49. data/lib/rubocop/cop/rails/environment_comparison.rb +70 -22
  50. data/lib/rubocop/cop/rails/environment_variable_access.rb +67 -0
  51. data/lib/rubocop/cop/rails/exit.rb +7 -13
  52. data/lib/rubocop/cop/rails/expanded_date_range.rb +102 -0
  53. data/lib/rubocop/cop/rails/file_path.rb +48 -31
  54. data/lib/rubocop/cop/rails/find_by.rb +43 -24
  55. data/lib/rubocop/cop/rails/find_by_id.rb +94 -0
  56. data/lib/rubocop/cop/rails/find_each.rb +42 -18
  57. data/lib/rubocop/cop/rails/freeze_time.rb +79 -0
  58. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +4 -3
  59. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +62 -25
  60. data/lib/rubocop/cop/rails/helper_instance_variable.rb +32 -4
  61. data/lib/rubocop/cop/rails/http_positional_arguments.rb +61 -32
  62. data/lib/rubocop/cop/rails/http_status.rb +27 -23
  63. data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +96 -0
  64. data/lib/rubocop/cop/rails/i18n_locale_assignment.rb +37 -0
  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 +9 -16
  68. data/lib/rubocop/cop/rails/index_by.rb +65 -0
  69. data/lib/rubocop/cop/rails/index_with.rb +68 -0
  70. data/lib/rubocop/cop/rails/inquiry.rb +39 -0
  71. data/lib/rubocop/cop/rails/inverse_of.rb +33 -27
  72. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +62 -32
  73. data/lib/rubocop/cop/rails/link_to_blank.rb +31 -32
  74. data/lib/rubocop/cop/rails/mailer_name.rb +90 -0
  75. data/lib/rubocop/cop/rails/match_route.rb +120 -0
  76. data/lib/rubocop/cop/rails/migration_class_name.rb +63 -0
  77. data/lib/rubocop/cop/rails/negate_include.rb +42 -0
  78. data/lib/rubocop/cop/rails/not_null_column.rb +16 -12
  79. data/lib/rubocop/cop/rails/order_by_id.rb +51 -0
  80. data/lib/rubocop/cop/rails/output.rb +29 -10
  81. data/lib/rubocop/cop/rails/output_safety.rb +9 -4
  82. data/lib/rubocop/cop/rails/pick.rb +64 -0
  83. data/lib/rubocop/cop/rails/pluck.rb +96 -0
  84. data/lib/rubocop/cop/rails/pluck_id.rb +59 -0
  85. data/lib/rubocop/cop/rails/pluck_in_where.rb +71 -0
  86. data/lib/rubocop/cop/rails/pluralization_grammar.rb +14 -19
  87. data/lib/rubocop/cop/rails/presence.rb +54 -26
  88. data/lib/rubocop/cop/rails/present.rb +40 -37
  89. data/lib/rubocop/cop/rails/rake_environment.rb +112 -0
  90. data/lib/rubocop/cop/rails/read_write_attribute.rb +56 -18
  91. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +33 -45
  92. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +77 -0
  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 +34 -32
  95. data/lib/rubocop/cop/rails/redundant_travel_back.rb +57 -0
  96. data/lib/rubocop/cop/rails/reflection_class_name.rb +56 -7
  97. data/lib/rubocop/cop/rails/refute_methods.rb +56 -35
  98. data/lib/rubocop/cop/rails/relative_date_constant.rb +52 -33
  99. data/lib/rubocop/cop/rails/render_inline.rb +41 -0
  100. data/lib/rubocop/cop/rails/render_plain_text.rb +71 -0
  101. data/lib/rubocop/cop/rails/request_referer.rb +10 -11
  102. data/lib/rubocop/cop/rails/require_dependency.rb +38 -0
  103. data/lib/rubocop/cop/rails/response_parsed_body.rb +57 -0
  104. data/lib/rubocop/cop/rails/reversible_migration.rb +122 -82
  105. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +66 -0
  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 +55 -43
  110. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +50 -0
  111. data/lib/rubocop/cop/rails/save_bang.rb +89 -63
  112. data/lib/rubocop/cop/rails/schema_comment.rb +104 -0
  113. data/lib/rubocop/cop/rails/scope_args.rb +8 -3
  114. data/lib/rubocop/cop/rails/short_i18n.rb +71 -0
  115. data/lib/rubocop/cop/rails/skips_model_validations.rb +53 -16
  116. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +87 -0
  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 +83 -67
  121. data/lib/rubocop/cop/rails/time_zone_assignment.rb +37 -0
  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 +40 -49
  127. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +172 -0
  128. data/lib/rubocop/cop/rails/unknown_env.rb +52 -21
  129. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +76 -0
  130. data/lib/rubocop/cop/rails/validation.rb +54 -23
  131. data/lib/rubocop/cop/rails/where_equals.rb +102 -0
  132. data/lib/rubocop/cop/rails/where_exists.rb +138 -0
  133. data/lib/rubocop/cop/rails/where_missing.rb +118 -0
  134. data/lib/rubocop/cop/rails/where_not.rb +101 -0
  135. data/lib/rubocop/cop/rails/where_not_with_multiple_conditions.rb +55 -0
  136. data/lib/rubocop/cop/rails_cops.rb +78 -8
  137. data/lib/rubocop/rails/inject.rb +1 -1
  138. data/lib/rubocop/rails/schema_loader/schema.rb +191 -0
  139. data/lib/rubocop/rails/schema_loader.rb +61 -0
  140. data/lib/rubocop/rails/version.rb +5 -1
  141. data/lib/rubocop/rails.rb +3 -1
  142. data/lib/rubocop-rails.rb +22 -0
  143. metadata +120 -19
  144. data/bin/setup +0 -7
@@ -3,12 +3,15 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop is used to identify usages of http methods like `get`, `post`,
6
+ # Identifies usages of http methods like `get`, `post`,
7
7
  # `put`, `patch` without the usage of keyword arguments in your tests and
8
8
  # change them to use keyword args. This cop only applies to Rails >= 5.
9
9
  # If you are running Rails < 5 you should disable the
10
10
  # Rails/HttpPositionalArguments cop or set your TargetRailsVersion in your
11
- # .rubocop.yml file to 4.0, etc.
11
+ # .rubocop.yml file to 4.2.
12
+ #
13
+ # NOTE: It does not detect any cases where `include Rack::Test::Methods` is used
14
+ # which makes the http methods incompatible behavior.
12
15
  #
13
16
  # @example
14
17
  # # bad
@@ -16,55 +19,76 @@ module RuboCop
16
19
  #
17
20
  # # good
18
21
  # get :new, params: { user_id: 1 }
19
- class HttpPositionalArguments < Cop
22
+ # get :new, **options
23
+ class HttpPositionalArguments < Base
24
+ include RangeHelp
25
+ extend AutoCorrector
20
26
  extend TargetRailsVersion
21
27
 
22
- MSG = 'Use keyword arguments instead of ' \
23
- 'positional arguments for http call: `%<verb>s`.'
24
- KEYWORD_ARGS = %i[
25
- method params session body flash xhr as headers env
26
- ].freeze
27
- HTTP_METHODS = %i[get post put patch delete head].freeze
28
+ MSG = 'Use keyword arguments instead of positional arguments for http call: `%<verb>s`.'
29
+ KEYWORD_ARGS = %i[method params session body flash xhr as headers env to].freeze
30
+ ROUTING_METHODS = %i[draw routes].freeze
31
+ RESTRICT_ON_SEND = %i[get post put patch delete head].freeze
28
32
 
29
33
  minimum_target_rails_version 5.0
30
34
 
31
- def_node_matcher :http_request?, <<-PATTERN
32
- (send nil? {#{HTTP_METHODS.map(&:inspect).join(' ')}} !nil? $_ ...)
35
+ def_node_matcher :http_request?, <<~PATTERN
36
+ (send nil? {#{RESTRICT_ON_SEND.map(&:inspect).join(' ')}} !nil? $_ ...)
37
+ PATTERN
38
+
39
+ def_node_matcher :kwsplat_hash?, <<~PATTERN
40
+ (hash (kwsplat _))
41
+ PATTERN
42
+
43
+ def_node_matcher :include_rack_test_methods?, <<~PATTERN
44
+ (send nil? :include
45
+ (const
46
+ (const
47
+ (const {nil? cbase} :Rack) :Test) :Methods))
33
48
  PATTERN
34
49
 
35
50
  def on_send(node)
51
+ return if in_routing_block?(node) || use_rack_test_methods?
52
+
36
53
  http_request?(node) do |data|
37
54
  return unless needs_conversion?(data)
38
55
 
39
- add_offense(node, location: :selector,
40
- message: format(MSG, verb: node.method_name))
56
+ message = format(MSG, verb: node.method_name)
57
+
58
+ add_offense(highlight_range(node), message: message) do |corrector|
59
+ # given a pre Rails 5 method: get :new, {user_id: @user.id}, {}
60
+ #
61
+ # @return lambda of auto correct procedure
62
+ # the result should look like:
63
+ # get :new, params: { user_id: @user.id }, session: {}
64
+ # the http_method is the method used to call the controller
65
+ # the controller node can be a symbol, method, object or string
66
+ # that represents the path/action on the Rails controller
67
+ # the data is the http parameters and environment sent in
68
+ # the Rails 5 http call
69
+ corrector.replace(node, correction(node))
70
+ end
41
71
  end
42
72
  end
43
73
 
44
- # given a pre Rails 5 method: get :new, {user_id: @user.id}, {}
45
- #
46
- # @return lambda of auto correct procedure
47
- # the result should look like:
48
- # get :new, params: { user_id: @user.id }, session: {}
49
- # the http_method is the method used to call the controller
50
- # the controller node can be a symbol, method, object or string
51
- # that represents the path/action on the Rails controller
52
- # the data is the http parameters and environment sent in
53
- # the Rails 5 http call
54
- def autocorrect(node)
55
- lambda do |corrector|
56
- corrector.replace(node.loc.expression, correction(node))
57
- end
74
+ private
75
+
76
+ def in_routing_block?(node)
77
+ !!node.each_ancestor(:block).detect { |block| ROUTING_METHODS.include?(block.method_name) }
58
78
  end
59
79
 
60
- private
80
+ def use_rack_test_methods?
81
+ processed_source.ast.each_descendant(:send).any? do |node|
82
+ include_rack_test_methods?(node)
83
+ end
84
+ end
61
85
 
62
86
  def needs_conversion?(data)
63
87
  return true unless data.hash_type?
88
+ return false if kwsplat_hash?(data)
64
89
 
65
90
  data.each_pair.none? do |pair|
66
- special_keyword_arg?(pair.key) ||
67
- format_arg?(pair.key) && data.pairs.one?
91
+ special_keyword_arg?(pair.key) || (format_arg?(pair.key) && data.pairs.one?)
68
92
  end
69
93
  end
70
94
 
@@ -76,12 +100,17 @@ module RuboCop
76
100
  node.sym_type? && node.value == :format
77
101
  end
78
102
 
103
+ def highlight_range(node)
104
+ _http_path, *data = *node.arguments
105
+
106
+ range_between(data.first.source_range.begin_pos, data.last.source_range.end_pos)
107
+ end
108
+
79
109
  def convert_hash_data(data, type)
80
110
  return '' if data.hash_type? && data.empty?
81
111
 
82
112
  hash_data = if data.hash_type?
83
- format('{ %<data>s }',
84
- data: data.pairs.map(&:source).join(', '))
113
+ format('{ %<data>s }', data: data.pairs.map(&:source).join(', '))
85
114
  else
86
115
  # user supplies an object,
87
116
  # no need to surround with braces
@@ -11,12 +11,14 @@ module RuboCop
11
11
  # render json: { foo: 'bar' }, status: 200
12
12
  # render plain: 'foo/bar', status: 304
13
13
  # redirect_to root_url, status: 301
14
+ # head 200
14
15
  #
15
16
  # # good
16
17
  # render :foo, status: :ok
17
18
  # render json: { foo: 'bar' }, status: :ok
18
19
  # render plain: 'foo/bar', status: :not_modified
19
20
  # redirect_to root_url, status: :moved_permanently
21
+ # head :ok
20
22
  #
21
23
  # @example EnforcedStyle: numeric
22
24
  # # bad
@@ -24,43 +26,48 @@ module RuboCop
24
26
  # render json: { foo: 'bar' }, status: :not_found
25
27
  # render plain: 'foo/bar', status: :not_modified
26
28
  # redirect_to root_url, status: :moved_permanently
29
+ # head :ok
27
30
  #
28
31
  # # good
29
32
  # render :foo, status: 200
30
33
  # render json: { foo: 'bar' }, status: 404
31
34
  # render plain: 'foo/bar', status: 304
32
35
  # redirect_to root_url, status: 301
36
+ # head 200
33
37
  #
34
- class HttpStatus < Cop
38
+ class HttpStatus < Base
35
39
  include ConfigurableEnforcedStyle
40
+ extend AutoCorrector
36
41
 
37
- def_node_matcher :http_status, <<-PATTERN
42
+ RESTRICT_ON_SEND = %i[render redirect_to head].freeze
43
+
44
+ def_node_matcher :http_status, <<~PATTERN
38
45
  {
39
46
  (send nil? {:render :redirect_to} _ $hash)
40
47
  (send nil? {:render :redirect_to} $hash)
48
+ (send nil? :head ${int sym} ...)
41
49
  }
42
50
  PATTERN
43
51
 
44
- def_node_matcher :status_code, <<-PATTERN
52
+ def_node_matcher :status_code, <<~PATTERN
45
53
  (hash <(pair (sym :status) ${int sym}) ...>)
46
54
  PATTERN
47
55
 
48
56
  def on_send(node)
49
- http_status(node) do |hash_node|
50
- status = status_code(hash_node)
57
+ http_status(node) do |hash_node_or_status_code|
58
+ status = if hash_node_or_status_code.hash_type?
59
+ status_code(hash_node_or_status_code)
60
+ else
61
+ hash_node_or_status_code
62
+ end
51
63
  return unless status
52
64
 
53
65
  checker = checker_class.new(status)
54
66
  return unless checker.offensive?
55
67
 
56
- add_offense(checker.node, message: checker.message)
57
- end
58
- end
59
-
60
- def autocorrect(node)
61
- lambda do |corrector|
62
- checker = checker_class.new(node)
63
- corrector.replace(node.loc.expression, checker.preferred_style)
68
+ add_offense(checker.node, message: checker.message) do |corrector|
69
+ corrector.replace(checker.node, checker.preferred_style)
70
+ end
64
71
  end
65
72
  end
66
73
 
@@ -77,12 +84,11 @@ module RuboCop
77
84
 
78
85
  # :nodoc:
79
86
  class SymbolicStyleChecker
80
- MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
81
- 'to define HTTP status code.'
82
- DEFAULT_MSG = 'Prefer `symbolic` over `numeric` ' \
83
- 'to define HTTP status code.'
87
+ MSG = 'Prefer `%<prefer>s` over `%<current>s` to define HTTP status code.'
88
+ DEFAULT_MSG = 'Prefer `symbolic` over `numeric` to define HTTP status code.'
84
89
 
85
90
  attr_reader :node
91
+
86
92
  def initialize(node)
87
93
  @node = node
88
94
  end
@@ -110,20 +116,18 @@ module RuboCop
110
116
  end
111
117
 
112
118
  def custom_http_status_code?
113
- node.int_type? &&
114
- !::Rack::Utils::SYMBOL_TO_STATUS_CODE.value?(number)
119
+ node.int_type? && !::Rack::Utils::SYMBOL_TO_STATUS_CODE.value?(number)
115
120
  end
116
121
  end
117
122
 
118
123
  # :nodoc:
119
124
  class NumericStyleChecker
120
- MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
121
- 'to define HTTP status code.'
122
- DEFAULT_MSG = 'Prefer `numeric` over `symbolic` ' \
123
- 'to define HTTP status code.'
125
+ MSG = 'Prefer `%<prefer>s` over `%<current>s` to define HTTP status code.'
126
+ DEFAULT_MSG = 'Prefer `numeric` over `symbolic` to define HTTP status code.'
124
127
  PERMITTED_STATUS = %i[error success missing redirect].freeze
125
128
 
126
129
  attr_reader :node
130
+
127
131
  def initialize(node)
128
132
  @node = node
129
133
  end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Checks for places where I18n "lazy" lookup can be used.
7
+ #
8
+ # @example
9
+ # # en.yml
10
+ # # en:
11
+ # # books:
12
+ # # create:
13
+ # # success: Book created!
14
+ #
15
+ # # bad
16
+ # class BooksController < ApplicationController
17
+ # def create
18
+ # # ...
19
+ # redirect_to books_url, notice: t('books.create.success')
20
+ # end
21
+ # end
22
+ #
23
+ # # good
24
+ # class BooksController < ApplicationController
25
+ # def create
26
+ # # ...
27
+ # redirect_to books_url, notice: t('.success')
28
+ # end
29
+ # end
30
+ #
31
+ class I18nLazyLookup < Base
32
+ include VisibilityHelp
33
+ extend AutoCorrector
34
+
35
+ MSG = 'Use "lazy" lookup for the text used in controllers.'
36
+
37
+ RESTRICT_ON_SEND = %i[translate t].freeze
38
+
39
+ def_node_matcher :translate_call?, <<~PATTERN
40
+ (send nil? {:translate :t} ${sym_type? str_type?} ...)
41
+ PATTERN
42
+
43
+ def on_send(node)
44
+ translate_call?(node) do |key_node|
45
+ key = key_node.value
46
+ return if key.to_s.start_with?('.')
47
+
48
+ controller, action = controller_and_action(node)
49
+ return unless controller && action
50
+
51
+ scoped_key = get_scoped_key(key_node, controller, action)
52
+ return unless key == scoped_key
53
+
54
+ add_offense(key_node) do |corrector|
55
+ unscoped_key = key_node.value.to_s.split('.').last
56
+ corrector.replace(key_node, "'.#{unscoped_key}'")
57
+ end
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def controller_and_action(node)
64
+ action_node = node.each_ancestor(:def).first
65
+ return unless action_node && node_visibility(action_node) == :public
66
+
67
+ controller_node = node.each_ancestor(:class).first
68
+ return unless controller_node && controller_node.identifier.source.end_with?('Controller')
69
+
70
+ [controller_node, action_node]
71
+ end
72
+
73
+ def get_scoped_key(key_node, controller, action)
74
+ path = controller_path(controller).tr('/', '.')
75
+ action_name = action.method_name
76
+ key = key_node.value.to_s.split('.').last
77
+
78
+ "#{path}.#{action_name}.#{key}"
79
+ end
80
+
81
+ def controller_path(controller)
82
+ module_name = controller.parent_module_name
83
+ controller_name = controller.identifier.source
84
+
85
+ path = if module_name == 'Object'
86
+ controller_name
87
+ else
88
+ "#{module_name}::#{controller_name}"
89
+ end
90
+
91
+ path.delete_suffix('Controller').underscore
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Checks for the use of `I18n.locale=` method.
7
+ #
8
+ # The `locale` attribute persists for the rest of the Ruby runtime, potentially causing
9
+ # unexpected behavior at a later time.
10
+ # Using `I18n.with_locale` ensures the code passed in the block is the only place `I18n.locale` is affected.
11
+ # It eliminates the possibility of a `locale` sticking around longer than intended.
12
+ #
13
+ # @example
14
+ # # bad
15
+ # I18n.locale = :fr
16
+ #
17
+ # # good
18
+ # I18n.with_locale(:fr) do
19
+ # end
20
+ #
21
+ class I18nLocaleAssignment < Base
22
+ MSG = 'Use `I18n.with_locale` with block instead of `I18n.locale=`.'
23
+ RESTRICT_ON_SEND = %i[locale=].freeze
24
+
25
+ def_node_matcher :i18n_locale_assignment?, <<~PATTERN
26
+ (send (const {nil? cbase} :I18n) :locale= ...)
27
+ PATTERN
28
+
29
+ def on_send(node)
30
+ return unless i18n_locale_assignment?(node)
31
+
32
+ add_offense(node)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Enforces use of I18n and locale files instead of locale specific strings.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # class User < ApplicationRecord
11
+ # validates :email, presence: { message: "must be present" }
12
+ # end
13
+ #
14
+ # # good
15
+ # # config/locales/en.yml
16
+ # # en:
17
+ # # activerecord:
18
+ # # errors:
19
+ # # models:
20
+ # # user:
21
+ # # blank: "must be present"
22
+ #
23
+ # class User < ApplicationRecord
24
+ # validates :email, presence: true
25
+ # end
26
+ #
27
+ # # bad
28
+ # class PostsController < ApplicationController
29
+ # def create
30
+ # # ...
31
+ # redirect_to root_path, notice: "Post created!"
32
+ # end
33
+ # end
34
+ #
35
+ # # good
36
+ # # config/locales/en.yml
37
+ # # en:
38
+ # # posts:
39
+ # # create:
40
+ # # success: "Post created!"
41
+ #
42
+ # class PostsController < ApplicationController
43
+ # def create
44
+ # # ...
45
+ # redirect_to root_path, notice: t(".success")
46
+ # end
47
+ # end
48
+ #
49
+ # # bad
50
+ # class UserMailer < ApplicationMailer
51
+ # def welcome(user)
52
+ # mail(to: user.email, subject: "Welcome to My Awesome Site")
53
+ # end
54
+ # end
55
+ #
56
+ # # good
57
+ # # config/locales/en.yml
58
+ # # en:
59
+ # # user_mailer:
60
+ # # welcome:
61
+ # # subject: "Welcome to My Awesome Site"
62
+ #
63
+ # class UserMailer < ApplicationMailer
64
+ # def welcome(user)
65
+ # mail(to: user.email)
66
+ # end
67
+ # end
68
+ #
69
+ class I18nLocaleTexts < Base
70
+ MSG = 'Move locale texts to the locale files in the `config/locales` directory.'
71
+
72
+ RESTRICT_ON_SEND = %i[validates redirect_to redirect_back []= mail].freeze
73
+
74
+ def_node_search :validation_message, <<~PATTERN
75
+ (pair (sym :message) $str)
76
+ PATTERN
77
+
78
+ def_node_search :redirect_to_flash, <<~PATTERN
79
+ (pair (sym {:notice :alert}) $str)
80
+ PATTERN
81
+
82
+ def_node_matcher :flash_assignment?, <<~PATTERN
83
+ (send (send nil? :flash) :[]= _ $str)
84
+ PATTERN
85
+
86
+ def_node_search :mail_subject, <<~PATTERN
87
+ (pair (sym :subject) $str)
88
+ PATTERN
89
+
90
+ def on_send(node)
91
+ case node.method_name
92
+ when :validates
93
+ validation_message(node) do |text_node|
94
+ add_offense(text_node)
95
+ end
96
+ return
97
+ when :redirect_to, :redirect_back
98
+ text_node = redirect_to_flash(node).to_a.last
99
+ when :[]=
100
+ text_node = flash_assignment?(node)
101
+ when :mail
102
+ text_node = mail_subject(node).to_a.last
103
+ end
104
+
105
+ add_offense(text_node) if text_node
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Looks for assignments of `ignored_columns` that may override previous
7
+ # assignments.
8
+ #
9
+ # Overwriting previous assignments is usually a mistake, since it will
10
+ # un-ignore the first set of columns. Since duplicate column names is not
11
+ # a problem, it is better to simply append to the list.
12
+ #
13
+ # @example
14
+ #
15
+ # # bad
16
+ # class User < ActiveRecord::Base
17
+ # self.ignored_columns = [:one]
18
+ # end
19
+ #
20
+ # # bad
21
+ # class User < ActiveRecord::Base
22
+ # self.ignored_columns = [:one, :two]
23
+ # end
24
+ #
25
+ # # good
26
+ # class User < ActiveRecord::Base
27
+ # self.ignored_columns += [:one, :two]
28
+ # end
29
+ #
30
+ # # good
31
+ # class User < ActiveRecord::Base
32
+ # self.ignored_columns += [:one]
33
+ # self.ignored_columns += [:two]
34
+ # end
35
+ #
36
+ class IgnoredColumnsAssignment < Base
37
+ extend AutoCorrector
38
+
39
+ MSG = 'Use `+=` instead of `=`.'
40
+ RESTRICT_ON_SEND = %i[ignored_columns=].freeze
41
+
42
+ def on_send(node)
43
+ add_offense(node.loc.operator) do |corrector|
44
+ corrector.replace(node.loc.operator, '+=')
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that `if` and `only` (or `except`) are not used together
6
+ # Checks that `if` and `only` (or `except`) are not used together
7
7
  # as options of `skip_*` action filter.
8
8
  #
9
9
  # The `if` option will be ignored when `if` and `only` are used together.
@@ -35,21 +35,16 @@ module RuboCop
35
35
  # skip_before_action :login_required,
36
36
  # if: -> { trusted_origin? && action_name != "admin" }
37
37
  # end
38
- #
39
- # @see https://api.rubyonrails.org/classes/AbstractController/Callbacks/ClassMethods.html#method-i-_normalize_callback_options
40
- class IgnoredSkipActionFilterOption < Cop
38
+ class IgnoredSkipActionFilterOption < Base
41
39
  MSG = <<~MSG.chomp.freeze
42
40
  `%<ignore>s` option will be ignored when `%<prefer>s` and `%<ignore>s` are used together.
43
41
  MSG
44
42
 
45
- FILTERS = %w[
46
- :skip_after_action
47
- :skip_around_action
48
- :skip_before_action
49
- :skip_action_callback
50
- ].freeze
43
+ RESTRICT_ON_SEND = %i[skip_after_action skip_around_action skip_before_action skip_action_callback].freeze
44
+
45
+ FILTERS = RESTRICT_ON_SEND.map { |method_name| ":#{method_name}" }
51
46
 
52
- def_node_matcher :filter_options, <<-PATTERN
47
+ def_node_matcher :filter_options, <<~PATTERN
53
48
  (send
54
49
  nil?
55
50
  {#{FILTERS.join(' ')}}
@@ -65,11 +60,9 @@ module RuboCop
65
60
  options = options_hash(options)
66
61
 
67
62
  if if_and_only?(options)
68
- add_offense(options[:if],
69
- message: format(MSG, prefer: :only, ignore: :if))
63
+ add_offense(options[:if], message: format(MSG, prefer: :only, ignore: :if))
70
64
  elsif if_and_except?(options)
71
- add_offense(options[:except],
72
- message: format(MSG, prefer: :if, ignore: :except))
65
+ add_offense(options[:except], message: format(MSG, prefer: :if, ignore: :except))
73
66
  end
74
67
  end
75
68
 
@@ -78,7 +71,7 @@ module RuboCop
78
71
  def options_hash(options)
79
72
  options.pairs
80
73
  .select { |pair| pair.key.sym_type? }
81
- .map { |pair| [pair.key.value, pair] }.to_h
74
+ .to_h { |pair| [pair.key.value, pair] }
82
75
  end
83
76
 
84
77
  def if_and_only?(options)
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Looks for uses of `each_with_object({}) { ... }`,
7
+ # `map { ... }.to_h`, and `Hash[map { ... }]` that are transforming
8
+ # an enumerable into a hash where the values are the original elements.
9
+ # Rails provides the `index_by` method for this purpose.
10
+ #
11
+ # @example
12
+ # # bad
13
+ # [1, 2, 3].each_with_object({}) { |el, h| h[foo(el)] = el }
14
+ # [1, 2, 3].to_h { |el| [foo(el), el] }
15
+ # [1, 2, 3].map { |el| [foo(el), el] }.to_h
16
+ # Hash[[1, 2, 3].collect { |el| [foo(el), el] }]
17
+ #
18
+ # # good
19
+ # [1, 2, 3].index_by { |el| foo(el) }
20
+ class IndexBy < Base
21
+ include IndexMethod
22
+ extend AutoCorrector
23
+
24
+ def_node_matcher :on_bad_each_with_object, <<~PATTERN
25
+ (block
26
+ (call _ :each_with_object (hash))
27
+ (args (arg $_el) (arg _memo))
28
+ (call (lvar _memo) :[]= $!`_memo (lvar _el)))
29
+ PATTERN
30
+
31
+ def_node_matcher :on_bad_to_h, <<~PATTERN
32
+ (block
33
+ (call _ :to_h)
34
+ (args (arg $_el))
35
+ (array $_ (lvar _el)))
36
+ PATTERN
37
+
38
+ def_node_matcher :on_bad_map_to_h, <<~PATTERN
39
+ (call
40
+ (block
41
+ (call _ {:map :collect})
42
+ (args (arg $_el))
43
+ (array $_ (lvar _el)))
44
+ :to_h)
45
+ PATTERN
46
+
47
+ def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
48
+ (send
49
+ (const {nil? cbase} :Hash)
50
+ :[]
51
+ (block
52
+ (call _ {:map :collect})
53
+ (args (arg $_el))
54
+ (array $_ (lvar _el))))
55
+ PATTERN
56
+
57
+ private
58
+
59
+ def new_method_name
60
+ 'index_by'
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end