-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
Sorbet/ForbidSigWithoutRuntime
This allows us to forbid usages of `T::Sig::WithoutRuntime.sig`. By default, this cop is disabled, we only enable it in the `rbi.yml` configuration. Signed-off-by: Alexandre Terrasa <[email protected]>
- Loading branch information
Showing
8 changed files
with
196 additions
and
11 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
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
43 changes: 43 additions & 0 deletions
43
lib/rubocop/cop/sorbet/signatures/forbid_sig_without_runtime.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,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require "stringio" | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# Check that `sig` is used instead of `T::Sig::WithoutRuntime.sig`. | ||
# | ||
# Good: | ||
# | ||
# ``` | ||
# sig { void } | ||
# def foo; end | ||
# ``` | ||
# | ||
# Bad: | ||
# | ||
# ``` | ||
# T::Sig::WithoutRuntime.sig { void } | ||
# def foo; end | ||
# ``` | ||
class ForbidSigWithoutRuntime < ::RuboCop::Cop::Base | ||
include SignatureHelp | ||
extend AutoCorrector | ||
|
||
MSG = "Do not use `T::Sig::WithoutRuntime.sig`. Use `sig` instead." | ||
|
||
def on_signature(node) | ||
return unless sig_without_runtime?(node) | ||
|
||
sig = node.children[0] | ||
# puts "source: #{sig.children.inspect}" | ||
# puts "sig: #{sig.source}" | ||
|
||
add_offense(sig) do |corrector| | ||
corrector.replace(sig, sig.source.gsub(/T\s*::\s*Sig\s*::\s*WithoutRuntime\s*\.\s*/m, "")) | ||
end | ||
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
97 changes: 97 additions & 0 deletions
97
spec/rubocop/cop/sorbet/signatures/forbid_sig_without_runtime_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,97 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe(RuboCop::Cop::Sorbet::ForbidSigWithoutRuntime, :config) do | ||
def message | ||
RuboCop::Cop::Sorbet::ForbidSigWithoutRuntime::MSG | ||
end | ||
|
||
describe("offenses") do | ||
it("allows using sig") do | ||
expect_no_offenses(<<~RUBY) | ||
sig { void } | ||
def foo; end | ||
RUBY | ||
end | ||
|
||
it("disallows using T::Sig::WithoutRuntime.sig { ... }") do | ||
expect_offense(<<~RUBY) | ||
T::Sig::WithoutRuntime.sig { void } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message} | ||
def foo; end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
sig { void } | ||
def foo; end | ||
RUBY | ||
end | ||
|
||
it("disallows using T::Sig::WithoutRuntime.sig do ... end") do | ||
expect_offense(<<~RUBY) | ||
T::Sig::WithoutRuntime.sig do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message} | ||
void | ||
end | ||
def self.foo(x); end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
sig do | ||
void | ||
end | ||
def self.foo(x); end | ||
RUBY | ||
end | ||
|
||
it("autocorrects with the correct parameters and block") do | ||
expect_offense(<<~RUBY) | ||
T::Sig::WithoutRuntime.sig(:final) do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message} | ||
params( | ||
x: A, | ||
y: B, | ||
).returns(C) | ||
end | ||
def self.foo(x, y); end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
sig(:final) do | ||
params( | ||
x: A, | ||
y: B, | ||
).returns(C) | ||
end | ||
def self.foo(x, y); end | ||
RUBY | ||
end | ||
|
||
it("autocorrects with the correct parameters and block in a multiline sig") do | ||
expect_offense(<<~RUBY) | ||
T:: | ||
^^^ #{message} | ||
Sig:: | ||
WithoutRuntime | ||
.sig(:final) do | ||
params( | ||
x: A, | ||
y: B, | ||
).returns(C) | ||
end | ||
def self.foo(x, y); end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
sig(:final) do | ||
params( | ||
x: A, | ||
y: B, | ||
).returns(C) | ||
end | ||
def self.foo(x, y); end | ||
RUBY | ||
end | ||
end | ||
end |