Skip to content

Commit

Permalink
Treat factory methods with known return types as constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Mar 31, 2017
1 parent d619fb6 commit 2ef7a26
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/compiler/crystal/tools/doc/method.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,31 @@ class Crystal::Doc::Method
end

def constructor?
@class_method && name == "new"
return false unless @class_method
return true if name == "new"
return true if {name, "self"}.includes?(return_type.to_s)
end

def abstract?
@def.abstract?
end

def return_type
return_type = @def.return_type

# If the def's body is a single instance variable, we include
# a return type since instance vars must have a fixed/guessed type,
# so docs will be better and easier to navigate.
if !return_type && (body = @def.body).is_a?(InstanceVar)
owner = type.type
if owner.is_a?(NonGenericClassType)
ivar = owner.lookup_instance_var?(body.name)
return_type = ivar.try &.type?
end
end
return_type
end

def kind
case
when @type.program?
Expand Down Expand Up @@ -94,18 +112,7 @@ class Crystal::Doc::Method
end

def args_to_html(io, links = true)
return_type = @def.return_type

# If the def's body is a single instance variable, we include
# a return type since instance vars must have a fixed/guessed type,
# so docs will be better and easier to navigate.
if !return_type && (body = @def.body).is_a?(InstanceVar)
owner = type.type
if owner.is_a?(NonGenericClassType)
ivar = owner.lookup_instance_var?(body.name)
return_type = ivar.try &.type?
end
end
return_type = self.return_type

return unless has_args? || return_type

Expand Down

0 comments on commit 2ef7a26

Please sign in to comment.