Skip to content

Commit

Permalink
fix wrap indentation (#388)
Browse files Browse the repository at this point in the history
* fix wrap indentation
* refactored NO -> NONE
* fixed checkstyle issues in unittests
  • Loading branch information
AlexHaxe authored Apr 22, 2018
1 parent 2f6e0c6 commit 8c4326b
Show file tree
Hide file tree
Showing 33 changed files with 374 additions and 317 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## next version (2.2.1)

- New check IndentationCheck [#387](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/387)
- Added CHANGES.md
- Added a reset function for checks ([#279](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/279))
- Added unittest for [#78](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/78)
- Added IndentationCheck [#387](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/387)
- Updated formula for number of pre-parsed files [#386](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/386)
- Removed conditional section for unittest [#181](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/181)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class SimplifyBooleanExpressionCheck extends Check {
function checkToken(token:TokenTree) {
var parent = token.parent;
if (parent.is(Binop(OpEq)) || parent.is(Binop(OpNotEq)) || parent.is(Unop(OpNot)) ||
parent.is(Binop(OpOr)) || parent.is(Binop(OpAnd)) || parent.is(Binop(OpBoolOr)) ||
parent.is(Binop(OpBoolAnd))) {
parent.is(Binop(OpOr)) || parent.is(Binop(OpAnd)) || parent.is(Binop(OpBoolOr)) ||
parent.is(Binop(OpBoolAnd))) {
logPos("Boolean expression can be simplified", token.pos);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/checkstyle/checks/metrics/CyclomaticComplexityCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class CyclomaticComplexityCheck extends Check {
return switch (e.expr) {
case ExprDef.EArray(e1, e2) : evaluateExpr(e1) + evaluateExpr(e2);
case ExprDef.EBinop(op, e1, e2) : evaluateExpr(e1) + evaluateExpr(e2) + switch (op) {
case haxe.macro.Expr.Binop.OpBoolAnd : 1;
case haxe.macro.Expr.Binop.OpBoolOr : 1;
default : 0;
};
case haxe.macro.Expr.Binop.OpBoolAnd : 1;
case haxe.macro.Expr.Binop.OpBoolOr : 1;
default : 0;
};
case ExprDef.EParenthesis(e) : evaluateExpr(e);
case ExprDef.EObjectDecl(fields) :
fields.map(function(f):Expr {
Expand Down
42 changes: 40 additions & 2 deletions src/checkstyle/checks/whitespace/IndentationCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class IndentationCheck extends Check {
var wrappedStatements:Array<Bool> = calcWrapStatements();
var tolerateViolations:Array<Bool> = calcIgnoreLineIndentation();

correctWrappedIndentation(lineIndentation, wrappedStatements);

var splitChar:String = character;
if (splitChar == "tab") splitChar = "\t";
for (i in 0...checker.lines.length) {
Expand All @@ -48,6 +50,7 @@ class IndentationCheck extends Check {
if (tolerate) return;
if (wrapped) {
switch (wrapPolicy) {
case NONE:
case EXACT:
case LARGER:
if (actual >= expected) return;
Expand All @@ -56,6 +59,35 @@ class IndentationCheck extends Check {
log('Indentation mismatch: expected: $expected, actual: $actual', line + 1, 0);
}

function correctWrappedIndentation(lineIndentation:Array<Int>, wrappedStatements:Array<Bool>) {
if (wrapPolicy == NONE) return;
var currentIndent:Int = 0;
for (i in 0...lineIndentation.length) {
if (!wrappedStatements[i]) {
currentIndent = lineIndentation[i];
continue;
}
if (currentIndent < lineIndentation[i]) {
currentIndent = -1;
continue;
}
if (currentIndent == lineIndentation[i]) {
lineIndentation[i]++;
}
}
var currentIndent:Int = 0;
for (i in 0...lineIndentation.length) {
var newIndent = lineIndentation[i];
if (newIndent == currentIndent) continue;
if (newIndent > currentIndent) {
currentIndent++;
lineIndentation[i] = currentIndent;
continue;
}
currentIndent = newIndent;
}
}

function calcLineIndentation():Array<Int> {
var lineIndentation:Array<Int> = [for (i in 0...checker.lines.length) 0];

Expand All @@ -65,7 +97,10 @@ class IndentationCheck extends Check {
switch (token.tok) {
case BkOpen:
var child:TokenTree = token.getFirstChild();
if (child.is(BrOpen)) continue;
if (child.is(BrOpen)) {
// only indent once, if directly next to each other `[{`
if (token.pos.min + 1 == child.pos.min) continue;
}
increaseBlockIndent(token, lineIndentation);
case BrOpen:
increaseBlockIndent(token, lineIndentation);
Expand Down Expand Up @@ -111,6 +146,8 @@ class IndentationCheck extends Check {
var tokenList:Array<TokenTree> = checker.getTokenTree().filter(searchFor, ALL);
for (token in tokenList) {
var pos = token.getPos();
var child:TokenTree = token.getFirstChild();
if (child.is(BkOpen)) continue;
ignoreRange(pos, wrapped);
}
return wrapped;
Expand All @@ -130,7 +167,7 @@ class IndentationCheck extends Check {
});
for (token in tokenList) {
switch (token.tok) {
case POpen, Const(CString(_)):
case Const(CString(_)):
ignoreRange(token.getPos(), ignoreIndentation);
case Comment(_):
if (ignoreComments) ignoreRange(token.getPos(), ignoreIndentation, false);
Expand Down Expand Up @@ -179,6 +216,7 @@ class IndentationCheck extends Check {

@:enum
abstract WrappedIndentationPolicy(String) {
var NONE = "none";
var EXACT = "exact";
var LARGER = "larger";
}
2 changes: 1 addition & 1 deletion test/TestMain.hx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TestMain {

var missingStatements:Array<Statement> = cls.getMissingStatements();
for (stmt in missingStatements) {
for (line in stmt.lines) coverageData[line] = 0;
for (line in stmt.lines) coverageData[line + 1] = 0;
}
var missingBranches:Array<Branch> = cls.getMissingBranches();
for (branch in missingBranches) {
Expand Down
16 changes: 8 additions & 8 deletions test/checks/block/EmptyBlockCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,28 @@ abstract EmptyBlockCheckTests(String) to String {
}
}";

var BLOCK_WITH_STATEMENT =
"class Test {
var BLOCK_WITH_STATEMENT = "
class Test {
public function new() { var a:Int;
}
}";

var BLOCK_WITH_STATEMENT2 =
"class Test {
var BLOCK_WITH_STATEMENT2 = "
class Test {
public function new() {
var a:Int; }
}";

var BLOCK_WITH_COMMENT =
"class Test {
var BLOCK_WITH_COMMENT = "
class Test {
public function new() {
// comment
}
}";

var EMPTY_OBJECT_DECL =
"class Test {
var EMPTY_OBJECT_DECL = "
class Test {
public function new() {
var a = {};
}
Expand Down
4 changes: 2 additions & 2 deletions test/checks/coding/NestedForDepthCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ abstract NestedForDepthCheckTests(String) to String {
}
}";

var TEST2 =
"abstractAndClass Test {
var TEST2 = "
abstractAndClass Test {
public function test1(param:Array<Int>) {
for (outerParam in params) { // level 0
for (middleParam in params) { // level 1
Expand Down
4 changes: 2 additions & 2 deletions test/checks/coding/NestedIfDepthCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ abstract NestedIfDepthCheckTests(String) to String {
}
}";

var TEST2 =
"abstractAndClass Test {
var TEST2 = "
abstractAndClass Test {
public function test1(param:Int) {
if (param == 1) { // level 0
return 1;
Expand Down
4 changes: 2 additions & 2 deletions test/checks/coding/NestedTryDepthCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ abstract NestedTryDepthCheckTests(String) to String {
}
}";

var TEST2 =
"abstractAndClass Test {
var TEST2 = "
abstractAndClass Test {
public function test1() {
try { // level 0
try { // level 0
Expand Down
10 changes: 5 additions & 5 deletions test/checks/coding/VariableInitialisationCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class VariableInitialisationCheckTest extends CheckTestCase<VariableInitialisati
@Test
public function testVar() {
assertMsg(new VariableInitialisationCheck(), TEST1,
'Invalid variable "_a" initialisation (move initialisation to constructor or function)');
'Invalid variable "_a" initialisation (move initialisation to constructor or function)');
}

@Test
Expand All @@ -33,15 +33,15 @@ abstract VariableInitialisationCheckTests(String) to String {
public function new() {}
}";

var TEST2 =
"abstractAndClass Test {
var TEST2 = "
abstractAndClass Test {
static inline var TEST:Int = 1;
public function new() {}
}";

var TEST3 =
"@:enum
var TEST3 = "
@:enum
abstract Test(Int) {
var VALUE = 0;
}";
Expand Down
12 changes: 6 additions & 6 deletions test/checks/literal/ERegLiteralCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ abstract ERegLiteralCheckTests(String) to String {
var _reg:EReg = new EReg('test', 'i');
}";

var TEST2 =
"abstractAndClass Test {
var TEST2 = "
abstractAndClass Test {
var _reg:EReg = ~/test/i;
}";

var ISSUE_43 =
"abstractAndClass Test {
var ISSUE_43 = "
abstractAndClass Test {
function test() {
cast (Type.createInstance(Array, []));
}
}";

var REGEX_WITH_STRING_INTERPOLATION =
"abstractAndClass Test {
var REGEX_WITH_STRING_INTERPOLATION = "
abstractAndClass Test {
var regex = new EReg('^${pattern}$', 'ig');
}";
}
8 changes: 4 additions & 4 deletions test/checks/literal/HexadecimalLiteralCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ abstract HexadecimalLiteralCheckTests(String) to String {
var clr = 0xffffff;
}";

var TEST2 =
"abstractAndClass Test {
var TEST2 = "
abstractAndClass Test {
var clr = 0x0033FF;
}";

var TEST3 =
"abstractAndClass Test {
var TEST3 = "
abstractAndClass Test {
var clr = 0x0033FF;
}";
}
28 changes: 14 additions & 14 deletions test/checks/modifier/ModifierOrderCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,38 @@ abstract ModifierOrderCheckTests(String) to String {
public static inline function test6() {}
}";

var TEST2 =
"abstractAndClass Test {
var TEST2 = "
abstractAndClass Test {
public override function test() {}
}";

var TEST3 =
"abstractAndClass Test {
var TEST3 = "
abstractAndClass Test {
public inline static function test() {}
}";

var TEST4 =
"abstractAndClass Test {
var TEST4 = "
abstractAndClass Test {
public macro function test() {}
}";

var TEST5 =
"abstractAndClass Test {
var TEST5 = "
abstractAndClass Test {
dynamic public function test() {}
}";

var TEST6 =
"interface Test {
var TEST6 = "
interface Test {
dynamic public function test();
}";

var TEST7 =
"abstractAndClass Test {
var TEST7 = "
abstractAndClass Test {
inline public var test:String=0;
}";

var TEST8 =
"abstractAndClass Test {
var TEST8 = "
abstractAndClass Test {
inline public var test(default,null):String=0;
}";
}
4 changes: 2 additions & 2 deletions test/checks/naming/CatchParameterNameCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ abstract CatchParameterNameCheckTests(String) to String {
}
}";

var TEST2 =
"class Test {
var TEST2 = "
class Test {
public function test() {
try {
var Count:Int = 1;
Expand Down
8 changes: 4 additions & 4 deletions test/checks/naming/ConstantNameCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ abstract ConstantNameCheckTests(String) to String {
}
}";

var TEST2 =
"class Test {
var TEST2 = "
class Test {
static inline var Count:Int = 1;
public function test() {
var Count:Int;
}
}";

var TEST3 =
"extern class Test {
var TEST3 = "
extern class Test {
static var Count:Int = 1;
static inline var Count:Int = 1;
public function test() {
Expand Down
8 changes: 4 additions & 4 deletions test/checks/naming/LocalVariableNameCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ abstract LocalVariableNameCheckTests(String) to String {
}
}";

var TEST3 =
"typedef Test = {
var TEST3 = "
typedef Test = {
public function test() {
var Count:Int;
}
}";

var TEST4 =
"extern class Test {
var TEST4 = "
extern class Test {
public function test() {
var Count:Int = 1;
}
Expand Down
Loading

0 comments on commit 8c4326b

Please sign in to comment.