Created
May 25, 2014 17:30
-
-
Save tmskst/200f5adb410bc02c6c03 to your computer and use it in GitHub Desktop.
CyclomaticComplexity の計測
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ; | |
import haxe.macro.Context; | |
import haxe.macro.Expr; | |
import haxe.macro.Expr.Field; | |
import haxe.macro.Expr.FieldType; | |
import haxe.macro.Expr.Function; | |
import haxe.macro.ExprTools; | |
using Lambda; | |
@:autoBuild(_MeasureCyclomaticComplexity.Impl.measure()) | |
interface MeasureCyclomaticComplexity { } | |
private class Impl { | |
macro public static function measure():Array<Field> { | |
for (e in filter_function(Context.getBuildFields())) { | |
cc = 0; | |
apply(e.f.expr); | |
trace(e.n + " : " + (cc + 1)); | |
} | |
return Context.getBuildFields(); | |
} | |
private static function filter_function(fields:Array<Field>):Array<{f:Function, n:String}> { | |
return fields | |
.filter(function (field) { return field.kind.match(FieldType.FFun); } ) | |
.map(function (field) { return switch (field.kind) { case FieldType.FFun(fun) : { f: fun, n: field.name }; case _ : null; }} ); | |
} | |
private static var cc:Int; | |
private static function apply(e:Expr):Void { | |
cc += switch (e.expr) { | |
case | |
EIf(_, _, _) | |
| EWhile(_, _, _) | |
| EFor(_, _) | |
| EContinue | |
| EBinop(OpBoolAnd, _, _) | |
| EBinop(OpBoolOr, _, _) | |
| ETernary(_, _, _) : | |
1; | |
case ESwitch(_, cases, _) : | |
cases.length; | |
case ETry(_, catches) : | |
catches.length; | |
case _ : | |
0; | |
} | |
ExprTools.iter(e, apply); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment