Skip to content

Commit

Permalink
Rebase to use JSON.def_to_json from crystal-lang#4772
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Aug 1, 2017
1 parent 5f68808 commit 64ade24
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 159 deletions.
10 changes: 5 additions & 5 deletions src/compiler/crystal/tools/doc/constant.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class Crystal::Doc::Constant
Highlighter.highlight value.to_s
end

Item.def_to_json(
name: {nilable: false},
value: {nilable: true, stringify: true},
doc: {nilable: true},
summary: {nilable: true, property: :formatted_summary},
JSON.def_to_json(
name: true,
value: {converter: JSON::StringConverter},
doc: true,
summary: {converter: JSON::StringConverter},
)
end
8 changes: 4 additions & 4 deletions src/compiler/crystal/tools/doc/generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,10 @@ class Crystal::Doc::Generator
end

record RelativeLocation, filename : String, line_number : Int32, url : String? do
Item.def_to_json(
filename: {nilable: false},
line_number: {nilable: false},
url: {nilable: true}
JSON.def_to_json(
filename: true,
line_number: true,
url: true
)
end
SRC_SEP = "src#{File::SEPARATOR}"
Expand Down
64 changes: 0 additions & 64 deletions src/compiler/crystal/tools/doc/item.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,4 @@ module Crystal::Doc::Item
def formatted_summary
@generator.summary(self)
end

macro def_to_json(type, properties)
Item.def_to_json(nil, {{properties}})
end

macro def_to_json(type, **properties)
Item.def_to_json({{type}}, {{properties}})
end

macro def_to_json(type, properties)
def to_json({% if type %}value : {{type.id}}, {% end %}json : ::JSON::Builder)
json.object do
{% for key, value in properties %}
{% keyid = (value[:property] || key).id %}
_{{keyid}} = {{ (type ? "value" : "self").id }}.{{keyid}}

{% unless value[:emit_null] %}
unless _{{keyid}}.nil?
{% end %}

json.field({{key.id.stringify}}) do
{% if value[:root] %}
{% if value[:emit_null] %}
if _{{keyid}}.nil?
nil.to_json(json)
else
{% end %}

json.object do
json.field({{value[:root]}}) do
{% end %}

{% if value[:converter] %}
if _{{keyid}}
{{ value[:converter] }}.to_json(_{{keyid}}, json)
else
nil.to_json(json)
end
{% elsif value[:stringify] %}
_{{keyid}}.to_s.to_json(json)
{% else %}
_{{keyid}}.to_json(json)
{% end %}

{% if value[:root] %}
{% if value[:emit_null] %}
end
{% end %}
end
end
{% end %}
end

{% unless value[:emit_null] %}
end
{% end %}
{% end %}
end
end
end

macro def_to_json(**properties)
Item.def_to_json(nil, {{properties}})
end
end
67 changes: 33 additions & 34 deletions src/compiler/crystal/tools/doc/json_converter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ module Crystal::Doc
end
end

Item.def_to_json(
repository_name: {nilable: false},
body: {nilable: false},
program: {nilable: false}
JSON.def_to_json(
repository_name: true,
body: true,
program: true
)
end

Expand All @@ -26,42 +26,41 @@ module Crystal::Doc
end
end

Item.def_to_json(Type,
html_id: {nilable: false},
# json_path: {nilable: false},
kind: {nilable: true},
full_name: {nilable: false},
name: {nilable: false}
JSON.def_to_json(Type,
html_id: true,
kind: true,
full_name: true,
name: true
)

Item.def_to_json(Crystal::Def,
name: {nilable: false},
args: {nilable: false, converter: JsonConverter},
double_splat: {nilable: true, converter: JsonConverter},
splat_index: {nilable: true},
yields: {nilable: true},
block_arg: {nilable: true, converter: JsonConverter},
return_type: {nilable: true, stringify: true},
visibility: {nilable: false, stringify: true},
body: {nilable: true, stringify: true},
JSON.def_to_json(Crystal::Def,
name: true,
args: {converter: JsonConverter},
double_splat: {converter: JsonConverter},
splat_index: true,
yields: true,
block_arg: {converter: JsonConverter},
return_type: {converter: JSON::StringConverter},
visibility: {converter: JSON::StringConverter},
body: {converter: JSON::StringConverter},
)

Item.def_to_json(Crystal::Macro,
name: {nilable: false},
args: {nilable: false, converter: JsonConverter},
double_splat: {nilable: true, converter: JsonConverter},
splat_index: {nilable: true},
block_arg: {nilable: true, converter: JsonConverter},
visibility: {nilable: false, stringify: true},
body: {nilable: true, stringify: true},
JSON.def_to_json(Crystal::Macro,
name: true,
args: {converter: JsonConverter},
double_splat: {converter: JsonConverter},
splat_index: true,
block_arg: {converter: JsonConverter},
visibility: {converter: JSON::StringConverter},
body: {converter: JSON::StringConverter},
)

Item.def_to_json(Crystal::Arg,
name: {nilable: false},
doc: {nilable: true},
default_value: {nilable: true, stringify: true},
external_name: {nilable: false, stringify: true},
restriction: {nilable: true, stringify: true},
JSON.def_to_json(Crystal::Arg,
name: true,
doc: true,
default_value: {converter: JSON::StringConverter},
external_name: {converter: JSON::StringConverter},
restriction: {converter: JSON::StringConverter},
)
end
end
22 changes: 11 additions & 11 deletions src/compiler/crystal/tools/doc/macro.cr
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ class Crystal::Doc::Macro
@generator.must_include? @macro
end

Item.def_to_json(
id: {nilable: false},
html_id: {nilable: false},
name: {nilable: false},
doc: {nilable: true},
summary: {nilable: true, property: :formatted_summary},
abstract: {nilable: false, property: :abstract?},
args: {nilable: true, converter: JsonConverter},
args_string: {nilable: true, property: :args_to_s},
source_link: {nilable: true},
def: {nilable: false, property: :macro, converter: JsonConverter}
JSON.def_to_json(
id: true,
html_id: true,
name: true,
doc: true,
summary: {property: :formatted_summary},
abstract: {property: :abstract?},
args: {converter: JsonConverter},
args_string: {property: :args_to_s},
source_link: true,
def: {property: :macro, converter: JsonConverter}
)
end
22 changes: 11 additions & 11 deletions src/compiler/crystal/tools/doc/method.cr
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,16 @@ class Crystal::Doc::Method
!@def.args.empty? || @def.block_arg || @def.yields
end

Item.def_to_json(
id: {nilable: false},
html_id: {nilable: false},
name: {nilable: false},
doc: {nilable: true},
summary: {nilable: true, property: :formatted_summary},
abstract: {nilable: false, property: :abstract?},
args: {nilable: true, converter: JsonConverter},
args_string: {nilable: true, property: :args_to_s},
source_link: {nilable: true},
def: {nilable: false, converter: JsonConverter}
JSON.def_to_json(
id: true,
html_id: true,
name: true,
doc: true,
summary: {property: :formatted_summary},
abstract: {property: :abstract?},
args: {converter: JsonConverter},
args_string: {property: :args_to_s},
source_link: true,
def: {converter: JsonConverter}
)
end
60 changes: 30 additions & 30 deletions src/compiler/crystal/tools/doc/type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -773,35 +773,35 @@ class Crystal::Doc::Type

delegate to_s, inspect, to: @type

Item.def_to_json(
html_id: {nilable: false},
path: {nilable: false},
# json_path: {nilable: false},
kind: {nilable: true},
full_name: {nilable: false},
name: {nilable: false},
abstract: {nilable: false, property: :abstract?},
superclass: {nilable: true, converter: JsonConverter},
ancestors: {nilable: false, converter: JsonConverter},
locations: {nilable: false},
repository_name: {nilable: false},
program: {nilable: false, property: :program?},
enum: {nilable: false, property: :enum?},
alias: {nilable: false, property: :alias?},
aliased: {nilable: true, property: :alias_definition, stringify: true},
const: {nilable: false, property: :const?},
constants: {nilable: false},
included_modules: {nilable: false, converter: JsonConverter},
extended_modules: {nilable: false, converter: JsonConverter},
subclasses: {nilable: false, converter: JsonConverter},
including_types: {nilable: false, converter: JsonConverter},
namespace: {nilable: true, converter: JsonConverter},
doc: {nilable: true},
summary: {nilable: true, property: :formatted_summary},
class_methods: {nilable: false},
constructors: {nilable: false},
instance_methods: {nilable: false},
macros: {nilable: false},
types: {nilable: false},
JSON.def_to_json(
html_id: true,
path: true,
# json_path: true,
kind: true,
full_name: true,
name: true,
abstract: {property: :abstract?},
superclass: {converter: JsonConverter},
ancestors: {converter: JsonConverter},
locations: true,
repository_name: true,
program: {property: :program?},
enum: {property: :enum?},
alias: {property: :alias?},
aliased: {property: :alias_definition, converter: JSON::StringConverter},
const: {property: :const?},
constants: true,
included_modules: {converter: JsonConverter},
extended_modules: {converter: JsonConverter},
subclasses: {converter: JsonConverter},
including_types: {converter: JsonConverter},
namespace: {converter: JsonConverter},
doc: true,
summary: {property: :formatted_summary},
class_methods: true,
constructors: true,
instance_methods: true,
macros: true,
types: true,
)
end

0 comments on commit 64ade24

Please sign in to comment.