Skip to content

Commit

Permalink
added support for import.hx files (see HaxeCheckstyle#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe committed Mar 17, 2016
1 parent ae70c21 commit 762b1e5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/checkstyle/checks/imports/UnusedImportCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import checkstyle.token.TokenTree;
import checkstyle.utils.TokenTreeCheckUtils;
import haxe.macro.Expr;
import haxe.macro.Expr;
import haxe.io.Path;
import haxeparser.Data;

using checkstyle.utils.ArrayUtils;
Expand All @@ -25,6 +26,7 @@ class UnusedImportCheck extends Check {

override function actualRun() {
var seenModules:Array<String> = [];
if (isImportHx()) return;
var root:TokenTree = checker.getTokenTree();
var packageName:String = detectPackageName(root);
var imports:Array<TokenTree> = root.filter([Kwd(KwdImport)], ALL);
Expand Down Expand Up @@ -62,6 +64,11 @@ class UnusedImportCheck extends Check {
}
}

function isImportHx():Bool {
var fileName:String = Path.withoutDirectory(checker.file.name);
return fileName == "import.hx";
}

function detectPackageName(root:TokenTree):String {
var packageToken:Array<TokenTree> = root.filter([Kwd(KwdPackage)], ALL);
if ((packageToken == null) || (packageToken.length <= 0)) return null;
Expand Down
20 changes: 10 additions & 10 deletions test/checks/CheckTestCase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ class CheckTestCase<T:String> extends haxe.unit.TestCase {

override public function setup() {}

function assertMsg(check:Check, testCase:T, expected:String, ?defines:Array<Array<String>>, ?pos:PosInfos) {
function assertMsg(check:Check, testCase:T, expected:String, ?defines:Array<Array<String>>, ?fileName:String, ?pos:PosInfos) {
var re = ~/abstractAndClass ([a-zA-Z0-9]*)/g;
if (re.match(testCase)) {
actualAssertMsg(check, re.replace(testCase, "class $1"), expected, pos);
actualAssertMsg(check, re.replace(testCase, "abstract $1(Int)"), expected, pos);
actualAssertMsg(check, re.replace(testCase, "class $1"), expected, fileName, pos);
actualAssertMsg(check, re.replace(testCase, "abstract $1(Int)"), expected, fileName, pos);
}
else actualAssertMsg(check, testCase, expected, defines, pos);
else actualAssertMsg(check, testCase, expected, defines, fileName, pos);
}

function assertNoMsg(check:Check, testCase:T, ?pos:PosInfos) {
assertMsg(check, testCase, '', null, pos);
function assertNoMsg(check:Check, testCase:T, ?fileName:String, ?pos:PosInfos) {
assertMsg(check, testCase, '', null, fileName, pos);
}

function actualAssertMsg(check:Check, testCase:String, expected:String, ?defines:Array<Array<String>>, ?pos:PosInfos) {
var msg = checkMessage(testCase, check, defines);
function actualAssertMsg(check:Check, testCase:String, expected:String, ?defines:Array<Array<String>>, ?fileName:String, ?pos:PosInfos) {
var msg = checkMessage(testCase, check, defines, fileName, pos);
assertEquals(expected, msg, pos);
}

function checkMessage(src:String, check:Check, defines:Array<Array<String>>):String {
function checkMessage(src:String, check:Check, defines:Array<Array<String>>, fileName:String = FILE_NAME, ?pos:PosInfos):String {
// a fresh Checker and Reporter for every checkMessage
// to allow multiple independant checkMessage calls in a single test
checker = new Checker();
Expand All @@ -44,7 +44,7 @@ class CheckTestCase<T:String> extends haxe.unit.TestCase {
if (defines != null) checker.defineCombinations = defines;
checker.addCheck(check);
checker.addReporter(reporter);
checker.process([{name:FILE_NAME, content:src, index:0}], null);
checker.process([{name:fileName, content:src, index:0}], null);
return reporter.message;
}

Expand Down
15 changes: 15 additions & 0 deletions test/checks/imports/UnusedImportCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ class UnusedImportCheckTest extends CheckTestCase<UnusedImportCheckTests> {
assertNoMsg(check, IMPORT_TYPE_MAP);
assertMsg(check, UNUSED_IMPORT_TYPE_MAP, MSG_UNUSED_TYPEMAP);
}

public function testImportHx() {
var check = new UnusedImportCheck();
assertNoMsg(check, IMPORT_NOT_USED, "import.hx");
assertNoMsg(check, DUPLICATE_IMPORT, "import.hx");
assertNoMsg(check, IMPORT_NAME_REUSED, "import.hx");
assertNoMsg(check, TOP_LEVEL_IMPORT, "import.hx");
assertNoMsg(check, UNUSED_IMPORT_AS, "import.hx");
assertNoMsg(check, UNUSED_IMPORT_IN, "import.hx");
assertNoMsg(check, UNUSED_IMPORT_IN_STATIC_FUNC, "import.hx");
assertNoMsg(check, SAME_PACKAGE_IMPORT, "import.hx");
assertNoMsg(check, SAME_PACKAGE_TYPE_MAP, "import.hx");
assertNoMsg(check, IMPORT_TYPE_MAP, "import.hx");
assertNoMsg(check, UNUSED_IMPORT_TYPE_MAP, "import.hx");
}
}

@:enum
Expand Down

0 comments on commit 762b1e5

Please sign in to comment.