From 762b1e5ebe3a01fb4b23a7044b3f9c05dcccd168 Mon Sep 17 00:00:00 2001 From: AlexHaxe Date: Thu, 17 Mar 2016 11:17:12 +0100 Subject: [PATCH] added support for import.hx files (see #189) --- .../checks/imports/UnusedImportCheck.hx | 7 +++++++ test/checks/CheckTestCase.hx | 20 +++++++++---------- test/checks/imports/UnusedImportCheckTest.hx | 15 ++++++++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/checkstyle/checks/imports/UnusedImportCheck.hx b/src/checkstyle/checks/imports/UnusedImportCheck.hx index c0ffd362..cf082023 100644 --- a/src/checkstyle/checks/imports/UnusedImportCheck.hx +++ b/src/checkstyle/checks/imports/UnusedImportCheck.hx @@ -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; @@ -25,6 +26,7 @@ class UnusedImportCheck extends Check { override function actualRun() { var seenModules:Array = []; + if (isImportHx()) return; var root:TokenTree = checker.getTokenTree(); var packageName:String = detectPackageName(root); var imports:Array = root.filter([Kwd(KwdImport)], ALL); @@ -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 = root.filter([Kwd(KwdPackage)], ALL); if ((packageToken == null) || (packageToken.length <= 0)) return null; diff --git a/test/checks/CheckTestCase.hx b/test/checks/CheckTestCase.hx index 7f72d518..50f16d8f 100644 --- a/test/checks/CheckTestCase.hx +++ b/test/checks/CheckTestCase.hx @@ -17,25 +17,25 @@ class CheckTestCase extends haxe.unit.TestCase { override public function setup() {} - function assertMsg(check:Check, testCase:T, expected:String, ?defines:Array>, ?pos:PosInfos) { + function assertMsg(check:Check, testCase:T, expected:String, ?defines:Array>, ?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>, ?pos:PosInfos) { - var msg = checkMessage(testCase, check, defines); + function actualAssertMsg(check:Check, testCase:String, expected:String, ?defines:Array>, ?fileName:String, ?pos:PosInfos) { + var msg = checkMessage(testCase, check, defines, fileName, pos); assertEquals(expected, msg, pos); } - function checkMessage(src:String, check:Check, defines:Array>):String { + function checkMessage(src:String, check:Check, defines:Array>, 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(); @@ -44,7 +44,7 @@ class CheckTestCase 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; } diff --git a/test/checks/imports/UnusedImportCheckTest.hx b/test/checks/imports/UnusedImportCheckTest.hx index 44e5babf..fe497c8f 100644 --- a/test/checks/imports/UnusedImportCheckTest.hx +++ b/test/checks/imports/UnusedImportCheckTest.hx @@ -54,6 +54,21 @@ class UnusedImportCheckTest extends CheckTestCase { 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