-
Notifications
You must be signed in to change notification settings - Fork 0
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 #11 from euglena1215/refactoring
Refactoring
- Loading branch information
Showing
11 changed files
with
213 additions
and
99 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module PackwerkYard | ||
class ConstantizeType | ||
extend T::Sig | ||
|
||
# Array Syntax e.g. Array<String> | ||
ARRAY_REGEXP = T.let(/\AArray<(.+)>/.freeze, Regexp) | ||
private_constant :ARRAY_REGEXP | ||
|
||
# Hash Syntax e.g. Hash<String, String> | ||
HASH_REGEXP = T.let(/\AHash<([^,]*),\s?(.*)>/.freeze, Regexp) | ||
private_constant :HASH_REGEXP | ||
|
||
sig { params(yard_type: String).void } | ||
def initialize(yard_type) | ||
@yard_type = yard_type | ||
end | ||
|
||
sig { returns(T::Array[String]) } | ||
def types | ||
split_type(@yard_type).select { |type| constantize?(type) }.uniq | ||
end | ||
|
||
private | ||
|
||
sig { params(name: String).returns(T::Boolean) } | ||
def constantize?(name) | ||
Object.const_get(name) # rubocop:disable Sorbet/ConstantsFromStrings | ||
true | ||
rescue NameError | ||
false | ||
end | ||
|
||
sig { params(type: String).returns(T::Array[String]) } | ||
def split_type(type) | ||
matched_types = Array(ARRAY_REGEXP.match(type).to_a[1]) | ||
matched_types = Array(HASH_REGEXP.match(type).to_a[1..2]) if matched_types.empty? | ||
matched_types.empty? ? [type] : matched_types.map { |t| split_type(t) }.flatten | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module PackwerkYard | ||
class YardHandler | ||
extend T::Sig | ||
|
||
sig { returns(T::Array[String]) } | ||
attr_reader :return_types | ||
|
||
sig { returns(T::Array[String]) } | ||
attr_reader :param_types | ||
|
||
sig { params(return_types: T::Array[String], param_types: T::Array[String]).void } | ||
def initialize(return_types, param_types) | ||
@return_types = return_types | ||
@param_types = param_types | ||
end | ||
|
||
class << self | ||
extend T::Sig | ||
|
||
sig { params(code: String).returns(YardHandler) } | ||
def from_source(code) | ||
YARD::Registry.clear | ||
YARD::Logger.instance.enter_level(YARD::Logger::ERROR) do | ||
YARD::Parser::SourceParser.parse_string(code) | ||
end | ||
|
||
return_types = [] | ||
param_types = [] | ||
|
||
YARD::Registry.all(:method).each do |method_object| | ||
method_object.tags("param").each do |tag| | ||
param_types << tag.types if tag.types | ||
end | ||
|
||
return_tag = method_object.tag("return") | ||
return_types << return_tag.types if return_tag&.types | ||
end | ||
|
||
new(return_types.flatten.uniq, param_types.flatten.uniq) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,45 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
module PackwerkYard | ||
class ConstantizeTypeTest < Minitest::Test | ||
def test_types_returns_constantize_types_with_primitive_value | ||
types = PackwerkYard::ConstantizeType.new("String").types | ||
|
||
assert_equal(1, types.size) | ||
assert_equal("String", types[0]) | ||
end | ||
|
||
def test_types_returns_constantize_types_with_simple_array | ||
types = PackwerkYard::ConstantizeType.new("Array<String>").types | ||
|
||
assert_equal(["String"], types) | ||
end | ||
|
||
def test_types_returns_constantize_types_with_nested_array | ||
types = PackwerkYard::ConstantizeType.new("Array<Array<String>>").types | ||
|
||
assert_equal(["String"], types) | ||
end | ||
|
||
def test_types_returns_constantize_types_with_simple_hash | ||
types = PackwerkYard::ConstantizeType.new("Hash<Integer, String>").types | ||
|
||
assert_equal(["Integer", "String"], types) | ||
end | ||
|
||
def test_types_returns_constantize_types_with_nested_hash | ||
types = PackwerkYard::ConstantizeType.new("Hash<Integer, Hash<String, Integer>>").types | ||
|
||
assert_equal(["Integer", "String"], types) | ||
end | ||
|
||
def test_types_returns_constantize_types_with_not_existing_class | ||
types = PackwerkYard::ConstantizeType.new("NotExistsClass").types | ||
|
||
assert_equal([], types) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
module PackwerkYard | ||
class YardHandlerTest < Minitest::Test | ||
def test_from_source_with_simple_param_and_return | ||
code = <<~RUBY | ||
class Foo | ||
# @param [String] foo | ||
# @return [String] | ||
def bar(foo) | ||
foo | ||
end | ||
end | ||
RUBY | ||
|
||
handler = PackwerkYard::YardHandler.from_source(code) | ||
|
||
assert_equal(["String"], handler.param_types) | ||
assert_equal(["String"], handler.return_types) | ||
end | ||
|
||
def test_from_source_with_multiple_param | ||
code = <<~RUBY | ||
class Foo | ||
# @param [String] foo | ||
# @param [Integer] bar | ||
def baz(foo, bar) | ||
end | ||
end | ||
RUBY | ||
|
||
handler = PackwerkYard::YardHandler.from_source(code) | ||
|
||
assert_equal(["String", "Integer"], handler.param_types) | ||
end | ||
|
||
def test_from_source_with_nested_array | ||
code = <<~RUBY | ||
class Foo | ||
# @param [Array<Array<String>>] foo | ||
# @return [Array<Array<String>>] | ||
def bar(foo) | ||
foo | ||
end | ||
end | ||
RUBY | ||
|
||
handler = PackwerkYard::YardHandler.from_source(code) | ||
|
||
assert_equal(["Array<Array<String>>"], handler.param_types) | ||
assert_equal(["Array<Array<String>>"], handler.return_types) | ||
end | ||
|
||
def test_from_source_with_polymorphic_param_and_return | ||
code = <<~RUBY | ||
class Foo | ||
# @param [String, Integer] foo | ||
# @return [String, Integer] | ||
def bar(foo) | ||
foo | ||
end | ||
end | ||
RUBY | ||
|
||
handler = PackwerkYard::YardHandler.from_source(code) | ||
|
||
assert_equal(["String", "Integer"], handler.param_types) | ||
assert_equal(["String", "Integer"], handler.return_types) | ||
end | ||
end | ||
end |