Skip to content

Commit

Permalink
Reduce array allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed Aug 24, 2022
1 parent 41b2aec commit 3bed0ce
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
3 changes: 1 addition & 2 deletions lib/yard-sorbet/handlers/struct_prop_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def decorate_object(object, prop)
# Get the default prop value
sig { returns(T.nilable(String)) }
def default_value
default_node = statement.traverse { break _1 if _1.type == :label && _1.source == 'default:' }
default_node.parent[1].source if default_node
statement.traverse { break _1 if _1.type == :label && _1.source == 'default:' }&.parent&.[](1)&.source
end

sig { params(name: String).returns(TStructProp) }
Expand Down
35 changes: 18 additions & 17 deletions lib/yard-sorbet/sig_to_yard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ module YARDSorbet
module SigToYARD
extend T::Sig

REF_TYPES = T.let({
'T::Boolean' => ['Boolean'].freeze, # YARD convention for booleans
# YARD convention is use singleton objects when applicable:
# https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Literals
'FalseClass' => ['false'].freeze,
'NilClass' => ['nil'].freeze,
'TrueClass' => ['true'].freeze
}.freeze, T::Hash[String, [String]])
private_constant :REF_TYPES

# @see https://yardoc.org/types.html
sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
def self.convert(node)
Expand All @@ -17,7 +27,7 @@ def self.convert(node)
private_class_method def self.convert_node(node)
case node
when YARD::Parser::Ruby::MethodCallNode then convert_call(node)
when YARD::Parser::Ruby::ReferenceNode then convert_ref(node)
when YARD::Parser::Ruby::ReferenceNode then convert_ref(node.source)
else convert_node_type(node)
end
end
Expand Down Expand Up @@ -61,7 +71,7 @@ def self.convert(node)
end
end

sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
private_class_method def self.convert_array(node)
# https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Order-Dependent_Lists
member_types = node.first.children.map { convert_node(_1) }
Expand All @@ -74,14 +84,14 @@ def self.convert(node)
node.namespace.source == 'T' ? convert_t_method(node) : [node.source]
end

sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
private_class_method def self.convert_collection(node)
collection_type = node.first.source.split('::').last
member_type = convert_node(node.last.first).join(', ')
["#{collection_type}<#{member_type}>"]
end

sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
private_class_method def self.convert_hash(node)
kv = node.last.children
key_type = convert_node(kv.first).join(', ')
Expand All @@ -94,18 +104,9 @@ def self.convert(node)
node.children.size == 1 ? convert_node(node.children.first) : [node.source]
end

sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
private_class_method def self.convert_ref(node)
source = node.source
case source
when 'T::Boolean' then ['Boolean'] # YARD convention for booleans
# YARD convention is use singleton objects when applicable:
# https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Literals
when 'FalseClass' then ['false']
when 'NilClass' then ['nil']
when 'TrueClass' then ['true']
else [source]
end
sig { params(node_source: String).returns([String]) }
private_class_method def self.convert_ref(node_source)
REF_TYPES.fetch(node_source, [node_source])
end

sig { params(node: YARD::Parser::Ruby::MethodCallNode).returns(T::Array[String]) }
Expand All @@ -120,7 +121,7 @@ def self.convert(node)
end
end

sig { params(node: YARD::Parser::Ruby::AstNode).returns(T::Array[String]) }
sig { params(node: YARD::Parser::Ruby::AstNode).returns([String]) }
private_class_method def self.convert_unknown(node)
log.warn("Unsupported sig #{node.type} node #{node.source}")
[node.source]
Expand Down

0 comments on commit 3bed0ce

Please sign in to comment.