Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
adireddy committed May 21, 2015
2 parents d76cfcc + b11ae32 commit ba17fa9
Show file tree
Hide file tree
Showing 12 changed files with 458 additions and 20 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ More information in [wiki page](https://github.com/adireddy/haxe-checkstyle/wiki
"privateUnderscorePrefix": false
}
},
{
"type": "NestedForDepth",
"props": {
"severity": "ERROR",
"max": 1
}
},
{
"type": "NestedIfDepth",
"props": {
"severity": "ERROR",
"max": 1
}
},
{
"type": "NestedTryDepth",
"props": {
"severity": "ERROR",
"max": 1
}
},
{
"type": "Override",
"props": {
Expand All @@ -112,7 +133,7 @@ More information in [wiki page](https://github.com/adireddy/haxe-checkstyle/wiki
"type": "ParameterNumber",
"props": {
"severity": "INFO",
"max": 10,
"max": 7,
"ignoreOverriddenMethods": false
}
},
Expand Down
66 changes: 66 additions & 0 deletions checkstyle/checks/NestedForDepthCheck.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package checkstyle.checks;

import checkstyle.LintMessage.SeverityLevel;
import haxeparser.Data;
import haxe.macro.Expr;

@name("NestedForDepth")
@desc("Max number of nested for blocks (default 1)")
class NestedForDepthCheck extends Check {

public var severity:String = "ERROR";
public var max:Int = 1;

override function _actualRun() {
for (td in _checker.ast.decls) {
switch (td.decl) {
case EClass(d):
checkFields(d);
default:
}
}
}

function checkFields(d:Definition<ClassFlag, Array<Field>>) {
for (field in d.data) {
checkField(field);
}
}

function checkField(f:Field) {
switch (f.kind) {
case FFun(fun):
scanBlock(fun.expr, -1);
default:
}
}

function scanBlock(e:Expr, depth:Int) {
if (e == null) return;
if (depth > max) {
_warnNestedForDepth(depth, e.pos);
return;
}
switch(e.expr) {
case EBlock(exprs):
scanExprs(exprs, depth);
default:
}
}

function scanExprs(exprs:Array<Expr>, depth:Int) {
for (e in exprs) {
switch(e.expr) {
case EFor(_, expr):
scanBlock(expr, depth + 1);
case EWhile(_, expr, _):
scanBlock(expr, depth + 1);
default:
}
}
}

function _warnNestedForDepth(depth:Int, pos:Position) {
logPos('Nested for depth is $depth (max allowed is ${max})', pos, Reflect.field(SeverityLevel, severity));
}
}
65 changes: 65 additions & 0 deletions checkstyle/checks/NestedIfDepthCheck.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package checkstyle.checks;

import checkstyle.LintMessage.SeverityLevel;
import haxeparser.Data;
import haxe.macro.Expr;

@name("NestedIfDepth")
@desc("Max number of nested if-else blocks (default 1)")
class NestedIfDepthCheck extends Check {

public var severity:String = "ERROR";
public var max:Int = 1;

override function _actualRun() {
for (td in _checker.ast.decls) {
switch (td.decl) {
case EClass(d):
checkFields(d);
default:
}
}
}

function checkFields(d:Definition<ClassFlag, Array<Field>>) {
for (field in d.data) {
checkField(field);
}
}

function checkField(f:Field) {
switch (f.kind) {
case FFun(fun):
scanBlock(fun.expr, -1);
default:
}
}

function scanBlock(e:Expr, depth:Int) {
if (e == null) return;
if (depth > max) {
_warnNestedIfDepth(depth, e.pos);
return;
}
switch(e.expr) {
case EBlock(exprs):
scanExprs(exprs, depth);
default:
}
}

function scanExprs(exprs:Array<Expr>, depth:Int) {
for (e in exprs) {
switch(e.expr) {
case EIf(_, ifPart,elsePart):
scanBlock(ifPart, depth + 1);
scanBlock(elsePart, depth + 1);
default:
}
}
}

function _warnNestedIfDepth(depth:Int, pos:Position) {
logPos('Nested if-else depth is $depth (max allowed is ${max})', pos, Reflect.field(SeverityLevel, severity));
}
}
71 changes: 71 additions & 0 deletions checkstyle/checks/NestedTryDepthCheck.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package checkstyle.checks;

import checkstyle.LintMessage.SeverityLevel;
import haxeparser.Data;
import haxe.macro.Expr;

@name("NestedTryDepth")
@desc("Max number of nested try blocks (default 1)")
class NestedTryDepthCheck extends Check {

public var severity:String = "ERROR";
public var max:Int = 1;

override function _actualRun() {
for (td in _checker.ast.decls) {
switch (td.decl) {
case EClass(d):
checkFields(d);
default:
}
}
}

function checkFields(d:Definition<ClassFlag, Array<Field>>) {
for (field in d.data) {
checkField(field);
}
}

function checkField(f:Field) {
switch (f.kind) {
case FFun(fun):
scanBlock(fun.expr, -1);
default:
}
}

function scanBlock(e:Expr, depth:Int) {
if (e == null) return null;
if (depth > max) {
_warnNestedTryDepth(depth, e.pos);
return;
}
switch(e.expr) {
case EBlock(exprs):
scanExprs(exprs, depth);
default:
}
}

function scanExprs(exprs:Array<Expr>, depth:Int) {
for (e in exprs) {
switch(e.expr) {
case ETry(expr,catches):
scanBlock(expr, depth + 1);
scanCatches(catches, depth + 1);
default:
}
}
}

function scanCatches(catches:Array<Catch>, depth:Int) {
for (c in catches) {
scanBlock(c.expr, depth);
}
}

function _warnNestedTryDepth(depth:Int, pos:Position) {
logPos('Nested try depth is $depth (max allowed is ${max})', pos, Reflect.field(SeverityLevel, severity));
}
}
6 changes: 3 additions & 3 deletions checkstyle/checks/ParameterNumberCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import haxeparser.Data;
import haxe.macro.Expr;

@name("ParameterNumber")
@desc("Max number of parameters per method (default 10)")
@desc("Max number of parameters per method (default 7)")
class ParameterNumberCheck extends Check {

public var severity:String = "INFO";
public var max:Int = 10;
public var max:Int = 7;
public var ignoreOverriddenMethods:Bool = false;

override function _actualRun() {
for (td in _checker.ast.decls) {
switch (td.decl){
switch (td.decl) {
case EClass(d):
checkFields(d);
default:
Expand Down
23 changes: 22 additions & 1 deletion resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@
"privateUnderscorePrefix": false
}
},
{
"type": "NestedForDepth",
"props": {
"severity": "ERROR",
"max": 1
}
},
{
"type": "NestedIfDepth",
"props": {
"severity": "ERROR",
"max": 1
}
},
{
"type": "NestedTryDepth",
"props": {
"severity": "ERROR",
"max": 1
}
},
{
"type": "Override",
"props": {
Expand All @@ -95,7 +116,7 @@
"type": "ParameterNumber",
"props": {
"severity": "INFO",
"max": 10,
"max": 7,
"ignoreOverriddenMethods": false
}
},
Expand Down
Binary file modified run.n
Binary file not shown.
62 changes: 62 additions & 0 deletions test/NestedForDepthCheckTest.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ;

import checkstyle.checks.NestedForDepthCheck;

class NestedForDepthCheckTest extends CheckTestCase {

public function testDefault() {
var msg = checkMessage(NestedForDepthTests.TEST1, new NestedForDepthCheck());
assertEquals('', msg);
}

public function testDefaultTooMany() {
var msg = checkMessage(NestedForDepthTests.TEST2, new NestedForDepthCheck());
assertEquals('Nested for depth is 2 (max allowed is 1)', msg);
}

public function testMaxParameter() {
var check = new NestedForDepthCheck();
check.max = 2;

var msg = checkMessage(NestedForDepthTests.TEST1, check);
assertEquals('', msg);

msg = checkMessage(NestedForDepthTests.TEST2, check);
assertEquals('', msg);

check.max = 0;

msg = checkMessage(NestedForDepthTests.TEST1, check);
assertEquals('', msg);

msg = checkMessage(NestedForDepthTests.TEST2, check);
assertEquals('Nested for depth is 1 (max allowed is 0)', msg);
}
}

class NestedForDepthTests {
public static inline var TEST1:String = "
class Test {
public function test(params:Array<Int>):Void {
for (param in params) trace(param); // level 0
for (i in 0...params.length) { // level 0
trace ('$i ${params[i]}');
}
}
}";

public static inline var TEST2:String =
"class Test {
public function test1(param:Array<Int>) {
for (outerParam in params) { // level 0
for (middleParam in params) { // level 1
for (innerParam in params) { // level 2
if (outerParam == innerParam) {
trace (param);
}
}
}
}
}
}";
}
Loading

0 comments on commit ba17fa9

Please sign in to comment.