Skip to content

Commit

Permalink
Add nodoc filter to doc type methods (#14910)
Browse files Browse the repository at this point in the history
The doc generator is creating links to non-documented type. This patch adds filters on `Doc::Type#ancestors`, `Doc::Type#included_modules` and `Doc::Type#extended_modules` as it was already done in `Doc::Type#subclasses`.
  • Loading branch information
spuun authored Aug 26, 2024
1 parent bd49e2e commit 791b0e4
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
136 changes: 136 additions & 0 deletions spec/compiler/crystal/tools/doc/type_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,140 @@ describe Doc::Type do
type.macros.map(&.name).should eq ["+", "~", "foo"]
end
end

describe "#subclasses" do
it "only include types with docs" do
program = semantic(<<-CRYSTAL, wants_doc: true).program
class Foo
end
class Bar < Foo
end
# :nodoc:
class Baz < Foo
end
module Mod1
class Bar < ::Foo
end
end
# :nodoc:
module Mod2
class Baz < ::Foo
end
end
CRYSTAL

generator = Doc::Generator.new program, [""]
type = generator.type(program.types["Foo"])
type.subclasses.map(&.full_name).should eq ["Bar", "Mod1::Bar"]
end
end

describe "#ancestors" do
it "only include types with docs" do
program = semantic(<<-CRYSTAL, wants_doc: true).program
# :nodoc:
module Mod3
class Baz
end
end
class Mod2::Baz < Mod3::Baz
end
module Mod1
# :nodoc:
class Baz < Mod2::Baz
end
end
class Baz < Mod1::Baz
end
class Foo < Baz
end
CRYSTAL

generator = Doc::Generator.new program, [""]
type = generator.type(program.types["Foo"])
type.ancestors.map(&.full_name).should eq ["Baz", "Mod2::Baz"]
end
end

describe "#included_modules" do
it "only include types with docs" do
program = semantic(<<-CRYSTAL, wants_doc: true).program
# :nodoc:
module Mod3
module Baz
end
end
module Mod2
# :nodoc:
module Baz
end
end
module Mod1
module Baz
end
end
module Baz
end
class Foo
include Baz
include Mod1::Baz
include Mod2::Baz
include Mod3::Baz
end
CRYSTAL

generator = Doc::Generator.new program, [""]
type = generator.type(program.types["Foo"])
type.included_modules.map(&.full_name).should eq ["Baz", "Mod1::Baz"]
end
end

describe "#included_modules" do
it "only include types with docs" do
program = semantic(<<-CRYSTAL, wants_doc: true).program
# :nodoc:
module Mod3
module Baz
end
end
module Mod2
# :nodoc:
module Baz
end
end
module Mod1
module Baz
end
end
module Baz
end
class Foo
extend Baz
extend Mod1::Baz
extend Mod2::Baz
extend Mod3::Baz
end
CRYSTAL

generator = Doc::Generator.new program, [""]
type = generator.type(program.types["Foo"])
type.extended_modules.map(&.full_name).should eq ["Baz", "Mod1::Baz"]
end
end
end
3 changes: 3 additions & 0 deletions src/compiler/crystal/tools/doc/type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class Crystal::Doc::Type

unless ast_node?
@type.ancestors.each do |ancestor|
next unless @generator.must_include? ancestor
doc_type = @generator.type(ancestor)
ancestors << doc_type
break if ancestor == @generator.program.object || doc_type.ast_node?
Expand Down Expand Up @@ -258,6 +259,7 @@ class Crystal::Doc::Type
included_modules = [] of Type
@type.parents.try &.each do |parent|
if parent.module?
next unless @generator.must_include? parent
included_modules << @generator.type(parent)
end
end
Expand All @@ -272,6 +274,7 @@ class Crystal::Doc::Type
extended_modules = [] of Type
@type.metaclass.parents.try &.each do |parent|
if parent.module?
next unless @generator.must_include? parent
extended_modules << @generator.type(parent)
end
end
Expand Down

0 comments on commit 791b0e4

Please sign in to comment.