-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from dduugg/implicit-conversion-method-cop
Add new `Sorbet/ImplicitConversionMethod` cop
- Loading branch information
Showing
6 changed files
with
141 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,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rubocop" | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# This cop disallows declaring implicit conversion methods. | ||
# Since Sorbet is a nominal (not structural) type system, | ||
# implicit conversion is currently unsupported. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# def to_str; end | ||
# | ||
# # good | ||
# def to_str(x); end | ||
# | ||
# # bad | ||
# def self.to_str; end | ||
# | ||
# # good | ||
# def self.to_str(x); end | ||
# | ||
# # bad | ||
# alias to_str to_s | ||
# | ||
# @see https://docs.ruby-lang.org/en/master/implicit_conversion_rdoc.html | ||
# @note Since the arity of aliased methods is not checked, false positives may result. | ||
class ImplicitConversionMethod < RuboCop::Cop::Base | ||
IMPLICIT_CONVERSION_METHODS = [:to_ary, :to_int, :to_hash, :to_str].freeze | ||
MSG = "Avoid implicit conversion methods, as Sorbet does not support them. " \ | ||
"Explicity convert to the desired type instead." | ||
RESTRICT_ON_SEND = [:alias_method].freeze | ||
|
||
def on_alias(node) | ||
new_id = node.new_identifier | ||
add_offense(new_id) if IMPLICIT_CONVERSION_METHODS.include?(new_id.value) | ||
end | ||
|
||
def on_def(node) | ||
return unless IMPLICIT_CONVERSION_METHODS.include?(node.method_name) | ||
return unless node.arguments.empty? | ||
|
||
add_offense(node) | ||
end | ||
alias_method :on_defs, :on_def | ||
|
||
def on_send(node) | ||
add_offense(node.first_argument) if IMPLICIT_CONVERSION_METHODS.include?(node.first_argument.value) | ||
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
47 changes: 47 additions & 0 deletions
47
spec/rubocop/cop/sorbet/implicit_conversion_method_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,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe(RuboCop::Cop::Sorbet::ImplicitConversionMethod, :config) do | ||
def message | ||
"Avoid implicit conversion methods, as Sorbet does not support them. " \ | ||
"Explicity convert to the desired type instead." | ||
end | ||
|
||
it "adds offense when defining implicit conversion instance method" do | ||
expect_offense(<<~RUBY) | ||
def to_ary | ||
^^^^^^^^^^ #{message} | ||
end | ||
RUBY | ||
end | ||
|
||
it "adds offense when defining implicit conversion class method" do | ||
expect_offense(<<~RUBY) | ||
def self.to_int | ||
^^^^^^^^^^^^^^^ #{message} | ||
end | ||
RUBY | ||
end | ||
|
||
it "does not add offense when method arguments exist" do | ||
expect_no_offenses(<<~RUBY) | ||
def to_int(foo) | ||
end | ||
RUBY | ||
end | ||
|
||
it "adds offense when declaring an implicit conversion method via alias" do | ||
expect_offense(<<~RUBY) | ||
alias to_str to_s | ||
^^^^^^ #{message} | ||
RUBY | ||
end | ||
|
||
it "adds offense when declaring an implicit conversion method via alias_method" do | ||
expect_offense(<<~RUBY) | ||
alias_method :to_hash, :to_h | ||
^^^^^^^^ #{message} | ||
RUBY | ||
end | ||
end |