-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cop to forbid extend T::Sig in shims
- Loading branch information
Ryan Brushett
committed
Mar 11, 2021
1 parent
68c0113
commit b8d285f
Showing
6 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# This cop ensures RBI shims do not include a call to extend T::Sig | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# module SomeModule | ||
# extend T::Sig | ||
# | ||
# sig { returns(String) } | ||
# def foo; end | ||
# end | ||
# | ||
# # good | ||
# module SomeModule | ||
# sig { returns(String) } | ||
# def foo; end | ||
# end | ||
class ForbidExtendTSigInShims < RuboCop::Cop::Cop | ||
include RangeHelp | ||
|
||
MSG = 'Extending T::Sig in a shim is unnecessary' | ||
RESTRICT_ON_SEND = [:extend] | ||
|
||
def_node_matcher :extend_t_sig?, <<~PATTERN | ||
(send nil? :extend (const (const nil? :T) :Sig)) | ||
PATTERN | ||
|
||
def autocorrect(node) | ||
-> (corrector) do | ||
corrector.remove( | ||
range_by_whole_lines(node.source_range, include_final_newline: true) | ||
) | ||
end | ||
end | ||
|
||
def on_send(node) | ||
add_offense(node) if extend_t_sig?(node) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
spec/rubocop/cop/sorbet/forbid_extend_t_sig_in_shims_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe(RuboCop::Cop::Sorbet::ForbidExtendTSigInShims, :config) do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
describe('offences') do | ||
it 'adds an offence when a targeted class or module extends T::Sig' do | ||
expect_offense(<<~RBI) | ||
module MyModule | ||
extend T::Sig | ||
^^^^^^^^^^^^^ Extending T::Sig in a shim is unnecessary | ||
sig { returns(String) } | ||
def foo; end | ||
end | ||
RBI | ||
end | ||
end | ||
|
||
describe('no offences') do | ||
it 'does not add an offence to uses of extend that are not T::Sig' do | ||
expect_no_offenses(<<~RBI) | ||
module MyModule | ||
extend ActiverSupport::Concern | ||
def foo; end | ||
end | ||
RBI | ||
end | ||
end | ||
|
||
describe('autocorrect') do | ||
it 'autocorrects usages of extend T::Sig by removing them' do | ||
source = <<~RBI | ||
module MyModule | ||
extend T::Sig | ||
extend ActiveSupport::Concern | ||
sig { returns(String) } | ||
def foo; end | ||
end | ||
RBI | ||
expect(autocorrect_source(source)) | ||
.to(eq(<<~RBI)) | ||
module MyModule | ||
extend ActiveSupport::Concern | ||
sig { returns(String) } | ||
def foo; end | ||
end | ||
RBI | ||
end | ||
end | ||
end |