Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autocorrect for Style/StaticClass #10986

Merged
merged 1 commit into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/new_add_autocorrect_for_style_static_class.md
@@ -0,0 +1 @@
* Add autocorrect for `Style/StaticClass`. ([@FnControlOption][])
33 changes: 32 additions & 1 deletion lib/rubocop/cop/style/static_class.rb
Expand Up @@ -44,18 +44,49 @@ module Style
# end
#
class StaticClass < Base
include RangeHelp
include VisibilityHelp
extend AutoCorrector

MSG = 'Prefer modules to classes with only class methods.'

def on_class(class_node)
return if class_node.parent_class
return unless class_convertible_to_module?(class_node)

add_offense(class_node) if class_convertible_to_module?(class_node)
add_offense(class_node) do |corrector|
autocorrect(corrector, class_node)
end
end

private

def autocorrect(corrector, class_node)
corrector.replace(class_node.loc.keyword, 'module')
corrector.insert_after(class_node.loc.name, "\nmodule_function\n")

class_elements(class_node).each do |node|
if node.defs_type?
autocorrect_def(corrector, node)
elsif node.sclass_type?
autocorrect_sclass(corrector, node)
end
end
end

def autocorrect_def(corrector, node)
corrector.remove(
range_between(node.receiver.source_range.begin_pos, node.loc.name.begin_pos)
)
end

def autocorrect_sclass(corrector, node)
corrector.remove(
range_between(node.loc.keyword.begin_pos, node.identifier.source_range.end_pos)
)
corrector.remove(node.loc.end)
end

def class_convertible_to_module?(class_node)
nodes = class_elements(class_node)
return false if nodes.empty?
Expand Down
39 changes: 39 additions & 0 deletions spec/rubocop/cop/style/static_class_spec.rb
Expand Up @@ -8,6 +8,14 @@ class C
def self.class_method; end
end
RUBY

expect_correction(<<~RUBY)
module C
module_function

def class_method; end
end
RUBY
end

it 'registers an offense when class has `class << self` with class methods' do
Expand All @@ -21,6 +29,18 @@ def other_class_method; end
end
end
RUBY

expect_correction(<<~RUBY)
module C
module_function

def class_method; end

#{trailing_whitespace}
def other_class_method; end
#{trailing_whitespace}
end
RUBY
end

it 'does not register an offense when class has `class << self` with macro calls' do
Expand All @@ -44,6 +64,16 @@ class C
def self.class_method; end
end
RUBY

expect_correction(<<~RUBY)
module C
module_function

CONST = 1

def class_method; end
end
RUBY
end

it 'does not register an offense when class has instance method' do
Expand Down Expand Up @@ -103,6 +133,15 @@ class C
def self.class_method; end
end
RUBY

expect_correction(<<~RUBY)
module C
module_function

extend M
def class_method; end
end
RUBY
end

it 'does not register an offense for modules' do
Expand Down