Skip to content

Commit

Permalink
fixed false unnecessary constructor warning (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe authored Oct 9, 2016
1 parent e24aec8 commit 877be71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/checkstyle/checks/design/UnnecessaryConstructorCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ class UnnecessaryConstructorCheck extends Check {
var acceptableTokens:Array<TokenTree> = cls.filter([
Kwd(KwdFunction),
Kwd(KwdVar)
], ALL);
], FIRST);

var haveConstructor:Bool = false;
var staticTokens:Int = 0;
var constructorPos = null;
for (token in acceptableTokens) {
if (token.filter([Kwd(KwdNew)], FIRST).length > 0) {
if (token.filter([Kwd(KwdNew)], FIRST, 2).length > 0) {
haveConstructor = true;
constructorPos = token.pos;
continue;
}

if (token.filter([Kwd(KwdStatic)], FIRST).length > 0) {
if (token.filter([Kwd(KwdStatic)], FIRST, 2).length > 0) {
staticTokens++;
continue;
}
Expand All @@ -42,4 +42,4 @@ class UnnecessaryConstructorCheck extends Check {
}
}
}
}
}
4 changes: 2 additions & 2 deletions test/TestMain.hx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TestMain {
Sys.exit(success ? 0 : 1);
}

static function setupCoverageReport() {
function setupCoverageReport() {
var client:PrintClient = new PrintClient();
var logger = MCoverage.getLogger();
logger.addClient(client);
Expand Down Expand Up @@ -71,4 +71,4 @@ class TestMain {
}
}

typedef LineCoverageResult = Dynamic;
typedef LineCoverageResult = Dynamic;
17 changes: 16 additions & 1 deletion test/checks/design/UnnecessaryConstructorCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class UnnecessaryConstructorCheckTest extends CheckTestCase<UnnecessaryConstruct
public function testJustWithConstructor() {
assertNoMsg(new UnnecessaryConstructorCheck(), TEST6);
}

public function testStaticOnlyWithNew() {
assertNoMsg(new UnnecessaryConstructorCheck(), TEST_STATIC_ONLY_WITH_NEW);
}
}

@:enum
Expand Down Expand Up @@ -89,4 +93,15 @@ abstract UnnecessaryConstructorCheckTests(String) to String {
class Test {
public function new() {}
}";
}

var TEST_STATIC_ONLY_WITH_NEW = "
class Test
{
public static var VAR1(default, null):String;
public static function init():Void
{
VAR1 = new String();
}
}";
}

0 comments on commit 877be71

Please sign in to comment.