diff options
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/rspec/modify_sidekiq_middleware.rb | 50 | ||||
-rw-r--r-- | rubocop/cop/static_translation_definition.rb | 43 | ||||
-rw-r--r-- | rubocop/migration_helpers.rb | 1 |
3 files changed, 94 insertions, 0 deletions
diff --git a/rubocop/cop/rspec/modify_sidekiq_middleware.rb b/rubocop/cop/rspec/modify_sidekiq_middleware.rb new file mode 100644 index 00000000000..c38f074eb3a --- /dev/null +++ b/rubocop/cop/rspec/modify_sidekiq_middleware.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module RSpec + # This cop checks for `Sidekiq::Testing.server_middleware` + # usage in specs. + # + # @example + # + # # bad + # Sidekiq::Testing.server_middleware do |chain| + # chain.add(MyMiddlewareUnderTest) + # end + # + # + # # good + # with_custom_sidekiq_middleware do |chain| + # chain.add(MyMiddlewareUnderTest) + # end + # + # + class ModifySidekiqMiddleware < RuboCop::Cop::Cop + MSG = <<~MSG + Don't modify global sidekiq middleware, use the `#with_sidekiq_server_middleware` + helper instead + MSG + + def_node_search :modifies_sidekiq_middleware?, <<~PATTERN + (send + (const + (const nil? :Sidekiq) :Testing) :server_middleware) + PATTERN + + def on_send(node) + return unless modifies_sidekiq_middleware?(node) + + add_offense(node, location: :expression) + end + + def autocorrect(node) + -> (corrector) do + corrector.replace(node.loc.expression, + 'with_sidekiq_server_middleware') + end + end + end + end + end +end diff --git a/rubocop/cop/static_translation_definition.rb b/rubocop/cop/static_translation_definition.rb new file mode 100644 index 00000000000..736d8767342 --- /dev/null +++ b/rubocop/cop/static_translation_definition.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + class StaticTranslationDefinition < RuboCop::Cop::Cop + MSG = "The text you're translating will be already in the translated form when it's assigned to the constant. When a users changes the locale, these texts won't be translated again. Consider moving the translation logic to a method.".freeze + + TRANSLATION_METHODS = %i[_ s_ n_].freeze + + def_node_matcher :translation_method?, <<~PATTERN + (send _ _ str*) + PATTERN + + def_node_matcher :lambda_node?, <<~PATTERN + (send _ :lambda) + PATTERN + + def on_send(node) + return unless translation_method?(node) + + method_name = node.children[1] + return unless TRANSLATION_METHODS.include?(method_name) + + node.each_ancestor do |ancestor| + receiver, _ = *ancestor + break if lambda_node?(receiver) # translations defined in lambda nodes should be allowed + + if constant_assignment?(ancestor) + add_offense(node, location: :expression) + + break + end + end + end + + private + + def constant_assignment?(node) + node.type == :casgn + end + end + end +end diff --git a/rubocop/migration_helpers.rb b/rubocop/migration_helpers.rb index 5fa6f8c2a2c..a987ae360d3 100644 --- a/rubocop/migration_helpers.rb +++ b/rubocop/migration_helpers.rb @@ -32,6 +32,7 @@ module RuboCop project_authorizations projects project_ci_cd_settings + project_features push_event_payloads resource_label_events routes |