Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistently store documentation when reopening #4908

Merged
merged 2 commits into from
Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 60 additions & 25 deletions spec/compiler/semantic/doc_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,33 @@ describe "Semantic: doc" do
bar.doc.should eq("Hello")
end

{% for def_type in %w[def macro].map &.id %}
it "overwrites doc for {{def_type}} when redefining" do
result = semantic %(
module Foo
# Doc 1
{{def_type}} bar
end
end

module Foo
# Doc 2
{{def_type}} bar
end
end

module Foo
{{def_type}} bar
end
end
), wants_doc: true
program = result.program
foo = program.types["Foo"]
bar = foo.lookup_{{def_type}}s("bar").as(Array).first
bar.doc.should eq("Doc 2")
end
{% end %}

it "stores doc for macro" do
result = semantic %(
class Foo
Expand Down Expand Up @@ -295,34 +322,42 @@ describe "Semantic: doc" do
foo.doc.should eq("Hello")
end

it "stores doc for class if reopening" do
result = semantic %(
class Foo
end
{% for module_type in %w[class struct module enum].map &.id %}
it "stores doc for {{module_type}} if reopening" do
result = semantic %(
{{module_type}} Foo
A = 1
end

# Hello
class Foo
end
), wants_doc: true
program = result.program
foo = program.types["Foo"]
foo.doc.should eq("Hello")
foo.locations.not_nil!.size.should eq(2)
end
# Hello
{{module_type}} Foo
end
), wants_doc: true
program = result.program
foo = program.types["Foo"]
foo.doc.should eq("Hello")
foo.locations.not_nil!.size.should eq(2)
end

it "overwrites doc for {{module_type}} if reopening" do
result = semantic %(
# Doc 1
{{module_type}} Foo
A = 1
end

it "stores doc for module if reopening" do
result = semantic %(
module Foo
end
# Doc 2
{{module_type}} Foo
end

# Hello
module Foo
end
), wants_doc: true
program = result.program
foo = program.types["Foo"]
foo.doc.should eq("Hello")
end
{{module_type}} Foo
end
), wants_doc: true
program = result.program
foo = program.types["Foo"]
foo.doc.should eq("Doc 2")
end
{% end %}

it "stores locations for auto-generated module" do
result = semantic %(
Expand Down
19 changes: 12 additions & 7 deletions src/compiler/crystal/semantic/top_level_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
def visit(node : ClassDef)
check_outside_exp node, "declare class"

scope, name = lookup_type_def_name(node)

type = scope.types[name]?
scope, name, type = lookup_existing_type_def(node)

created_new_type = false
extern = false
Expand Down Expand Up @@ -185,9 +183,8 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
def visit(node : ModuleDef)
check_outside_exp node, "declare module"

scope, name = lookup_type_def_name(node)
scope, name, type = lookup_existing_type_def(node)

type = scope.types[name]?
if type
type = type.remove_alias

Expand Down Expand Up @@ -450,9 +447,8 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
attributes = check_valid_attributes node, ValidEnumDefAttributes, "enum"
attributes_doc = attributes_doc()

scope, name = lookup_type_def_name(node)
scope, name, enum_type = lookup_existing_type_def(node)

enum_type = scope.types[name]?
if enum_type
unless enum_type.is_a?(EnumType)
node.raise "#{name} is not a enum, it's a #{enum_type.type_desc}"
Expand Down Expand Up @@ -985,6 +981,15 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
{extern, extern_union, packed}
end

def lookup_existing_type_def(node : ASTNode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just lookup_type_def?

scope, name = lookup_type_def_name(node)
type = scope.types[name]?
if type && node.doc
type.doc = node.doc
end
{scope, name, type}
end

def lookup_type_def_name(node : ASTNode)
scope, name = lookup_type_def_name(node.name)
if current_type.is_a?(Program)
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ module Crystal
if ex_item.restriction_of?(item, self)
list[i] = item
a_def.previous = ex_item
a_def.doc ||= ex_item.def.doc
ex_item.def.next = a_def
return ex_item.def
else
Expand Down Expand Up @@ -781,6 +782,7 @@ module Crystal
array = (macros[a_def.name] ||= [] of Macro)
index = array.index { |existing_macro| a_def.overrides?(existing_macro) }
if index
a_def.doc ||= array[index].doc
array[index] = a_def
else
array.push a_def
Expand Down