Skip to content

Commit

Permalink
Merge pull request #390 from koic/import_enforce_superclass_module
Browse files Browse the repository at this point in the history
Import `EnforceSuperclass` module
  • Loading branch information
koic authored Nov 24, 2020
2 parents dadaf1b + 52b8480 commit 4890714
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
40 changes: 40 additions & 0 deletions lib/rubocop/cop/mixin/enforce_superclass.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module RuboCop
module Cop
# Common functionality for enforcing a specific superclass.
module EnforceSuperclass
def self.included(base)
base.def_node_matcher :class_definition, <<~PATTERN
(class (const _ !:#{base::SUPERCLASS}) #{base::BASE_PATTERN} ...)
PATTERN

base.def_node_matcher :class_new_definition, <<~PATTERN
[!^(casgn {nil? cbase} :#{base::SUPERCLASS} ...)
!^^(casgn {nil? cbase} :#{base::SUPERCLASS} (block ...))
(send (const {nil? cbase} :Class) :new #{base::BASE_PATTERN})]
PATTERN
end

def on_class(node)
class_definition(node) do
register_offense(node.children[1])
end
end

def on_send(node)
class_new_definition(node) do
register_offense(node.children.last)
end
end

private

def register_offense(offense_node)
add_offense(offense_node) do |corrector|
corrector.replace(offense_node.source_range, self.class::SUPERCLASS)
end
end
end
end
end
10 changes: 3 additions & 7 deletions lib/rubocop/cop/rails/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ module Rails
# class MyController < ActionController::Base
# # ...
# end
class ApplicationController < Cop
class ApplicationController < Base
extend AutoCorrector

MSG = 'Controllers should subclass `ApplicationController`.'
SUPERCLASS = 'ApplicationController'
BASE_PATTERN = '(const (const nil? :ActionController) :Base)'

# rubocop:disable Layout/ClassStructure
include RuboCop::Cop::EnforceSuperclass
# rubocop:enable Layout/ClassStructure

def autocorrect(node)
lambda do |corrector|
corrector.replace(node.source_range, self.class::SUPERCLASS)
end
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/rails/application_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module Rails
# class Rails4Job < ActiveJob::Base
# # ...
# end
class ApplicationJob < Cop
class ApplicationJob < Base
extend AutoCorrector
extend TargetRailsVersion

minimum_target_rails_version 5.0
Expand Down
9 changes: 2 additions & 7 deletions lib/rubocop/cop/rails/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module Rails
# class MyMailer < ActionMailer::Base
# # ...
# end
class ApplicationMailer < Cop
class ApplicationMailer < Base
extend AutoCorrector
extend TargetRailsVersion

minimum_target_rails_version 5.0
Expand All @@ -28,12 +29,6 @@ class ApplicationMailer < Cop
# rubocop:disable Layout/ClassStructure
include RuboCop::Cop::EnforceSuperclass
# rubocop:enable Layout/ClassStructure

def autocorrect(node)
lambda do |corrector|
corrector.replace(node.source_range, self.class::SUPERCLASS)
end
end
end
end
end
Expand Down
9 changes: 2 additions & 7 deletions lib/rubocop/cop/rails/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module Rails
# class Rails4Model < ActiveRecord::Base
# # ...
# end
class ApplicationRecord < Cop
class ApplicationRecord < Base
extend AutoCorrector
extend TargetRailsVersion

minimum_target_rails_version 5.0
Expand All @@ -28,12 +29,6 @@ class ApplicationRecord < Cop
# rubocop:disable Layout/ClassStructure
include RuboCop::Cop::EnforceSuperclass
# rubocop:enable Layout/ClassStructure

def autocorrect(node)
lambda do |corrector|
corrector.replace(node.source_range, self.class::SUPERCLASS)
end
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative 'mixin/active_record_helper'
require_relative 'mixin/enforce_superclass'
require_relative 'mixin/index_method'
require_relative 'mixin/target_rails_version'

Expand Down

0 comments on commit 4890714

Please sign in to comment.