diff --git a/src/compiler/crystal/tools/doc/method.cr b/src/compiler/crystal/tools/doc/method.cr index db3ea5dcf25f..0024e2c0d7f5 100644 --- a/src/compiler/crystal/tools/doc/method.cr +++ b/src/compiler/crystal/tools/doc/method.cr @@ -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? @@ -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