Skip to content

Commit

Permalink
Contain haxe_ver checks in a single place
Browse files Browse the repository at this point in the history
This should also fix an issue when built with haxe_ver < 4 where the return value of sortFields() was unused.
  • Loading branch information
Gama11 committed Apr 11, 2018
1 parent d8f745e commit 046aa4c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
6 changes: 1 addition & 5 deletions src/dox/Api.hx
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,7 @@ enum FieldKind {
/**
Field is a method with arguments `args` and return type `ret`.
**/
#if (haxe_ver < 4)
Method(args: List<FunctionArgument>, ret: CType);
#else
Method(args: Array<FunctionArgument>, ret: CType);
#end
Method(args: Container<FunctionArgument>, ret: CType);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/dox/Container.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dox;

using Lambda;

private typedef ContainerType<T> = #if (haxe_ver < 4) List<T> #else Array<T> #end;

@:forward(filter)
abstract Container<T>(ContainerType<T>) from ContainerType<T> to ContainerType<T> {
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
}
}
41 changes: 11 additions & 30 deletions src/dox/Processor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClassField>();
if (t.impl.statics == null) t.impl.statics = new List<ClassField>();
#else
if (t.impl.fields == null) t.impl.fields = new Array<ClassField>();
if (t.impl.statics == null) t.impl.statics = new Array<ClassField>();
#end
if (t.impl.fields == null) t.impl.fields = new Container<ClassField>();
if (t.impl.statics == null) t.impl.statics = new Container<ClassField>();
t.impl.statics.iter(function(cf) {
if (cf.meta.exists(function(m) return m.name == ":impl")) {
if (cf.name == "_new") cf.name = "new";
Expand All @@ -120,11 +115,7 @@ class Processor {
return newRoot;
}

#if (haxe_ver < 4)
function filterFields(fields:List<ClassField>) {
#else
function filterFields(fields:Array<ClassField>) {
#end
function filterFields(fields:Container<ClassField>) {
return fields.filter(function(cf) {
var hide = Infos.hasDoxMetadata(cf.meta, "hide");
var show = Infos.hasDoxMetadata(cf.meta, "show");
Expand All @@ -134,11 +125,7 @@ class Processor {
});
}

#if (haxe_ver < 4)
function filterEnumFields(fields:List<EnumField>) {
#else
function filterEnumFields(fields:Array<EnumField>) {
#end
function filterEnumFields(fields:Container<EnumField>) {
return fields.filter(function(cf) {
return !Infos.hasDoxMetadata(cf.meta, "hide") || Infos.hasDoxMetadata(cf.meta, "show");
});
Expand Down Expand Up @@ -175,30 +162,24 @@ class Processor {
cf1.name < cf2.name ? -1 : 1;
}

#if (haxe_ver < 4)
function sortFields(fields:List<ClassField>) {
var a = fields.array();
a.sort(compareFields);
return a.list();
inline function sortFields(fields:Container<ClassField>) {
return fields.sort(compareFields);
}
#else
inline function sortFields(fields:Array<ClassField>) {
fields.sort(compareFields);
}
#end

function sort(t:TypeTree) {
switch(t) {
case TPackage(_, _, subs):
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 _:
Expand Down

0 comments on commit 046aa4c

Please # to comment.