From 046aa4cdee0f013c27b54778c778492e6faf2a40 Mon Sep 17 00:00:00 2001 From: Jens Fischer Date: Wed, 11 Apr 2018 12:57:27 +0200 Subject: [PATCH] Contain haxe_ver checks in a single place This should also fix an issue when built with haxe_ver < 4 where the return value of sortFields() was unused. --- src/dox/Api.hx | 6 +----- src/dox/Container.hx | 22 ++++++++++++++++++++++ src/dox/Processor.hx | 41 +++++++++++------------------------------ 3 files changed, 34 insertions(+), 35 deletions(-) create mode 100644 src/dox/Container.hx diff --git a/src/dox/Api.hx b/src/dox/Api.hx index 6b5945e51..2ef72c74d 100644 --- a/src/dox/Api.hx +++ b/src/dox/Api.hx @@ -474,11 +474,7 @@ enum FieldKind { /** Field is a method with arguments `args` and return type `ret`. **/ - #if (haxe_ver < 4) - Method(args: List, ret: CType); - #else - Method(args: Array, ret: CType); - #end + Method(args: Container, ret: CType); } /** diff --git a/src/dox/Container.hx b/src/dox/Container.hx new file mode 100644 index 000000000..7a3cdca3f --- /dev/null +++ b/src/dox/Container.hx @@ -0,0 +1,22 @@ +package dox; + +using Lambda; + +private typedef ContainerType = #if (haxe_ver < 4) List #else Array #end; + +@:forward(filter) +abstract Container(ContainerType) from ContainerType to ContainerType { + public inline function new() { + this = new ContainerType(); + } + + public function sort(f:T->T->Int) { + #if (haxe_ver < 4) + var a = this.array(); + a.sort(f); + return a.list(); + #else + return this; + #end + } +} diff --git a/src/dox/Processor.hx b/src/dox/Processor.hx index 27d0628ab..c5f6d98d0 100644 --- a/src/dox/Processor.hx +++ b/src/dox/Processor.hx @@ -87,13 +87,8 @@ class Processor { } case TAbstractdecl(t): if (t.impl != null) { - #if (haxe_ver < 4) - if (t.impl.fields == null) t.impl.fields = new List(); - if (t.impl.statics == null) t.impl.statics = new List(); - #else - if (t.impl.fields == null) t.impl.fields = new Array(); - if (t.impl.statics == null) t.impl.statics = new Array(); - #end + if (t.impl.fields == null) t.impl.fields = new Container(); + if (t.impl.statics == null) t.impl.statics = new Container(); t.impl.statics.iter(function(cf) { if (cf.meta.exists(function(m) return m.name == ":impl")) { if (cf.name == "_new") cf.name = "new"; @@ -120,11 +115,7 @@ class Processor { return newRoot; } - #if (haxe_ver < 4) - function filterFields(fields:List) { - #else - function filterFields(fields:Array) { - #end + function filterFields(fields:Container) { return fields.filter(function(cf) { var hide = Infos.hasDoxMetadata(cf.meta, "hide"); var show = Infos.hasDoxMetadata(cf.meta, "show"); @@ -134,11 +125,7 @@ class Processor { }); } - #if (haxe_ver < 4) - function filterEnumFields(fields:List) { - #else - function filterEnumFields(fields:Array) { - #end + function filterEnumFields(fields:Container) { return fields.filter(function(cf) { return !Infos.hasDoxMetadata(cf.meta, "hide") || Infos.hasDoxMetadata(cf.meta, "show"); }); @@ -175,17 +162,9 @@ class Processor { cf1.name < cf2.name ? -1 : 1; } - #if (haxe_ver < 4) - function sortFields(fields:List) { - var a = fields.array(); - a.sort(compareFields); - return a.list(); + inline function sortFields(fields:Container) { + return fields.sort(compareFields); } - #else - inline function sortFields(fields:Array) { - fields.sort(compareFields); - } - #end function sort(t:TypeTree) { switch(t) { @@ -193,12 +172,14 @@ class Processor { subs.sort(compare); subs.iter(sort); case TClassdecl(c) | TAbstractdecl({impl: c}) if (c != null): - sortFields(c.fields); - sortFields(c.statics); + c.fields = sortFields(c.fields); + c.statics = sortFields(c.statics); case TTypedecl(t): switch(t.type) { - case CAnonymous(fields): sortFields(fields); t.type = CAnonymous(fields); + case CAnonymous(fields): + fields = sortFields(fields); + t.type = CAnonymous(fields); default: } case _: