Add Rubocop rule to ban include ActionView::Context
This commit is contained in:
parent
b02458ef52
commit
fad99d934f
3 changed files with 77 additions and 0 deletions
31
rubocop/cop/include_action_view_context.rb
Normal file
31
rubocop/cop/include_action_view_context.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require_relative '../spec_helpers'
|
||||
|
||||
module RuboCop
|
||||
module Cop
|
||||
# Cop that makes sure workers include `::Gitlab::ActionViewOutput::Context`, not `ActionView::Context`.
|
||||
class IncludeActionViewContext < RuboCop::Cop::Cop
|
||||
include SpecHelpers
|
||||
|
||||
MSG = 'Include `::Gitlab::ActionViewOutput::Context`, not `ActionView::Context`, for Rails 5.'.freeze
|
||||
|
||||
def_node_matcher :includes_action_view_context?, <<~PATTERN
|
||||
(send nil? :include (const (const nil? :ActionView) :Context))
|
||||
PATTERN
|
||||
|
||||
def on_send(node)
|
||||
return if in_spec?(node)
|
||||
return unless includes_action_view_context?(node)
|
||||
|
||||
add_offense(node.arguments.first, location: :expression)
|
||||
end
|
||||
|
||||
def autocorrect(node)
|
||||
lambda do |corrector|
|
||||
corrector.replace(node.source_range, '::Gitlab::ActionViewOutput::Context')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -4,6 +4,7 @@ require_relative 'cop/gitlab/predicate_memoization'
|
|||
require_relative 'cop/gitlab/httparty'
|
||||
require_relative 'cop/gitlab/finder_with_find_by'
|
||||
require_relative 'cop/gitlab/union'
|
||||
require_relative 'cop/include_action_view_context'
|
||||
require_relative 'cop/include_sidekiq_worker'
|
||||
require_relative 'cop/safe_params'
|
||||
require_relative 'cop/active_record_association_reload'
|
||||
|
|
45
spec/rubocop/cop/include_action_view_context_spec.rb
Normal file
45
spec/rubocop/cop/include_action_view_context_spec.rb
Normal file
|
@ -0,0 +1,45 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
require 'rubocop'
|
||||
require 'rubocop/rspec/support'
|
||||
|
||||
require_relative '../../../rubocop/cop/include_action_view_context'
|
||||
|
||||
describe RuboCop::Cop::IncludeActionViewContext do
|
||||
include CopHelper
|
||||
|
||||
subject(:cop) { described_class.new }
|
||||
|
||||
context 'when `ActionView::Context` is included' do
|
||||
let(:source) { 'include ActionView::Context' }
|
||||
let(:correct_source) { 'include ::Gitlab::ActionViewOutput::Context' }
|
||||
|
||||
it 'registers an offense' do
|
||||
inspect_source(source)
|
||||
|
||||
aggregate_failures do
|
||||
expect(cop.offenses.size).to eq(1)
|
||||
expect(cop.offenses.map(&:line)).to eq([1])
|
||||
expect(cop.highlights).to eq(['ActionView::Context'])
|
||||
end
|
||||
end
|
||||
|
||||
it 'autocorrects to the right version' do
|
||||
autocorrected = autocorrect_source(source)
|
||||
|
||||
expect(autocorrected).to eq(correct_source)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when `ActionView::Context` is not included' do
|
||||
it 'registers no offense' do
|
||||
inspect_source('include Context')
|
||||
|
||||
aggregate_failures do
|
||||
expect(cop.offenses.size).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue