Skip to content

Commit

Permalink
Merge pull request #357 from qwertykg/dev
Browse files Browse the repository at this point in the history
readded change to allow user to turn off return types for anonymous functions
  • Loading branch information
adireddy authored Sep 4, 2017
2 parents ab4e107 + 8417ed1 commit c5837bc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions resources/default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@
},
{
"props": {
"enforceReturnTypeForAnonymous": false,
"allowEmptyReturn": true,
"enforceReturnType": false
},
Expand Down
9 changes: 8 additions & 1 deletion src/checkstyle/checks/type/ReturnCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ class ReturnCheck extends Check {

public var allowEmptyReturn:Bool;
public var enforceReturnType:Bool;
public var enforceReturnTypeForAnonymous:Bool;

public function new() {
super(AST);
allowEmptyReturn = true;
enforceReturnType = false;
enforceReturnTypeForAnonymous = false;
categories = [Category.CLARITY];
points = 2;
}
Expand Down Expand Up @@ -105,7 +107,12 @@ class ReturnCheck extends Check {
}

function warnReturnTypeMissing(name:String, pos:Position) {
if (name == null) logPos("Return type not specified for anonymous method", pos);
if (name == null) {
if (!enforceReturnTypeForAnonymous) {
return;
}
logPos("Return type not specified for anonymous method", pos);
}
else logPos('Return type not specified for method "${name}"', pos);
}
}
28 changes: 28 additions & 0 deletions test/checks/type/ReturnCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ class ReturnCheckTest extends CheckTestCase<ReturnCheckTests> {
assertNoMsg(check, CORRECT_RETURN);
}

public function testCorrectReturnForAnonymous() {
var check = new ReturnCheck();
assertNoMsg(check, CORRECT_RETURN_ANONYMOUS);
check.enforceReturnTypeForAnonymous = true;
assertNoMsg(check, CORRECT_RETURN_ANONYMOUS);
}

public function testIncorrectReturnForAnonymous() {
var check = new ReturnCheck();
assertNoMsg(check, TEST5);
check.enforceReturnTypeForAnonymous = true;
assertMsg(check, TEST5, MSG_NO_ANON_RETURN);

}

public function testVoid() {
assertMsg(new ReturnCheck(), TEST1, MSG_VOID_RETURN);
}
Expand All @@ -28,6 +43,9 @@ class ReturnCheckTest extends CheckTestCase<ReturnCheckTests> {
assertMsg(check, TEST2B, MSG_NOT_TEST1_RETURN);
assertMsg(check, TEST_FOR, MSG_NOT_TEST1_RETURN);
assertMsg(check, TEST_WHILE, MSG_NOT_TEST1_RETURN);

assertNoMsg(check, TEST5);
check.enforceReturnTypeForAnonymous = true;
assertMsg(check, TEST5, MSG_NO_ANON_RETURN);
}

Expand Down Expand Up @@ -171,6 +189,16 @@ abstract ReturnCheckTests(String) to String {
public function test1();
}";

var CORRECT_RETURN_ANONYMOUS =
"abstractAndClass Test {
public function test7() {
var x = function(i):Int{
return i * i;
}
return;
}
}";

var CORRECT_RETURN =
"abstractAndClass Test {
public function test1():Int {
Expand Down

0 comments on commit c5837bc

Please sign in to comment.