-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
458 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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)); | ||
} | ||
} |
This file contains 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
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)); | ||
} | ||
} |
This file contains 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
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)); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"; | ||
} |
Oops, something went wrong.