Skip to content

Commit 4c1a80d

Browse files
committed
improved and configurable listener check
1 parent 5592f41 commit 4c1a80d

File tree

12 files changed

+247
-42
lines changed

12 files changed

+247
-42
lines changed

build.hxml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,12 @@
66
-lib compiletime:2.5.0
77
-lib hxargs:3.0.0
88

9+
--each
10+
911
-main checkstyle.Main
10-
-neko run.n
12+
-neko run.n
13+
14+
--next
15+
-cp test
16+
-main TestMain
17+
--interp
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package checkstyle.checks;
22

3+
import Array;
34
import haxe.macro.Expr;
45
import haxe.macro.Expr.Field;
56
import haxeparser.Data.Definition;
@@ -13,9 +14,10 @@ import haxeparser.Data.Token;
1314
class ListenerNameCheck extends Check {
1415

1516
public var severity:String = "ERROR";
17+
public var listeners:Array<String> = ["addEventListener", "addListener", "on", "once", "add", "addOnce", "addWithPriority", "addOnceWithPriority"];
1618

1719
override public function actualRun() {
18-
ExprUtils.walkFile(_checker.ast,function(e) {
20+
ExprUtils.walkFile(_checker.ast, function(e) {
1921
switch(e.expr){
2022
case ECall(e, params):
2123
searchCall(e, params);
@@ -24,34 +26,32 @@ class ListenerNameCheck extends Check {
2426
});
2527
}
2628

27-
function searchCall(e,p){
28-
if (! searchLeftCall(e)) return;
29-
searchCallParam(p);
29+
function searchCall(e, p) {
30+
for (listener in listeners) if (searchLeftCall(e, listener)) searchCallParam(p);
3031
}
3132

32-
function searchLeftCall(e){
33-
var name = "addEventListener";
33+
function searchLeftCall(e, name) {
3434
switch(e.expr){
35-
case EConst(CIdent(ident)): return ident == name;
36-
case EField(e2,field): return field == name;
37-
default:return false;
35+
case EConst(CIdent(ident)): return ident == name;
36+
case EField(e2, field): return field == name;
37+
default:return false;
3838
}
3939
}
4040

41-
function searchCallParam(p:Array<Expr>){
41+
function searchCallParam(p:Array<Expr>) {
4242
if (p.length < 2) return;
4343
var listener = p[1];
4444
switch(listener.expr){
45-
case EConst(CIdent(ident)):
46-
var lp = _checker.getLinePos(listener.pos.min);
47-
checkListenerName(ident, lp.line, lp.ofs);
48-
default:
45+
case EConst(CIdent(ident)):
46+
var lp = _checker.getLinePos(listener.pos.min);
47+
checkListenerName(ident, lp.line, lp.ofs);
48+
default:
4949
}
5050
}
5151

52-
function checkListenerName(name:String, line:Int, col:Int){
52+
function checkListenerName(name:String, line:Int, col:Int) {
5353
var re = ~/^_?on.*/;
5454
var match = re.match(name);
55-
if (!match) log("Wrong listener name: " + name, line, col, Reflect.field(SeverityLevel, severity));
55+
if (!match) log('Wrong listener name, prefix with "on": ' + name, line, col, Reflect.field(SeverityLevel, severity));
5656
}
5757
}

checkstyle/reporter/Reporter.hx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class Reporter implements IReporter {
1414
}
1515
}
1616

17-
public function start():Void {}
17+
public function start() {}
1818

19-
public function finish():Void {}
19+
public function finish() {}
2020

21-
public function fileStart(f:LintFile):Void {}
21+
public function fileStart(f:LintFile) {}
2222

23-
public function fileFinish(f:LintFile):Void {}
23+
public function fileFinish(f:LintFile) {}
2424

2525
public function addMessage(m:LintMessage) {
2626
var sb:StringBuf = new StringBuf();

run.n

282 Bytes
Binary file not shown.

test/AnonymousCheckTest.hx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
package ;
22

3-
class AnonymousCheckTest{
3+
import checkstyle.checks.AnonymousCheck;
44

5-
var _anonymous:{a:Int, b:Int};
5+
class AnonymousCheckTest extends CheckTestCase {
66

7-
public var chips:Array<{chipIndex:Int, position:String}>;
8-
9-
public function test1(_chips:Array<{chipIndex:Int, position:String}>) {
10-
chips = _chips;
7+
public function testAnonymousStructureClassVar(){
8+
var msg = checkMessage(AnonymousTests.TEST1, new AnonymousCheck());
9+
assertEquals(msg, 'Anonymous structure found, it is advised to use a typedef instead "_anonymous"');
1110
}
1211

13-
public function new() {
14-
var b:{a:Int, b:Int};
15-
_anonymous = {a: 2, b: 5};
12+
public function testAnonymousStructureLocalVar(){
13+
var msg = checkMessage(AnonymousTests.TEST2, new AnonymousCheck());
14+
assertEquals(msg, 'Anonymous structure found, it is advised to use a typedef instead "b"');
1615
}
1716
}
1817

19-
typedef SettingsBucket = {
20-
var width:Float;
21-
var height:Float;
18+
class AnonymousTests {
19+
public static inline var TEST1:String = "
20+
class Test {
21+
var _anonymous:{a:Int, b:Int};
22+
}";
23+
24+
public static inline var TEST2:String =
25+
"class Test {
26+
public function new() {
27+
var b:{a:Int, b:Int};
28+
}
29+
}";
2230
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ;
2+
3+
import checkstyle.checks.ArrayInstantiationCheck;
4+
5+
class ArrayInstantiationCheckTest extends CheckTestCase {
6+
7+
public function testWrongArrayInstantiation(){
8+
var msg = checkMessage(ArrayInstantiationTests.TEST1, new ArrayInstantiationCheck());
9+
assertEquals(msg, 'Bad array instantiation, use the array literal notation [] which is faster');
10+
}
11+
12+
public function testCorrectArrayInstantiation(){
13+
var msg = checkMessage(ArrayInstantiationTests.TEST2, new ArrayInstantiationCheck());
14+
assertEquals(msg, '');
15+
}
16+
}
17+
18+
class ArrayInstantiationTests {
19+
public static inline var TEST1:String = "
20+
class Test {
21+
var _arr:Array<Int> = new Array<Int>();
22+
}";
23+
24+
public static inline var TEST2:String = "
25+
class Test {
26+
var _arr:Array<Int> = [];
27+
}";
28+
}

test/CheckTestCase.hx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ;
2+
3+
import checkstyle.LintMessage;
4+
import checkstyle.LintFile;
5+
import checkstyle.reporter.IReporter;
6+
import checkstyle.Checker;
7+
8+
class CheckTestCase extends haxe.unit.TestCase {
9+
10+
static inline var FILE_NAME = "Test.hx";
11+
12+
function messageEquals(expected:LintMessage, actual:LintMessage) {
13+
assertEquals(expected.fileName, actual.fileName);
14+
assertEquals(expected.moduleName, actual.moduleName);
15+
assertEquals(expected.line, actual.line);
16+
assertEquals(expected.column, actual.column);
17+
assertEquals(expected.severity, actual.severity);
18+
assertEquals(expected.message, actual.message);
19+
}
20+
21+
function checkMessage(src, check):String {
22+
var checker = new Checker();
23+
var rep = new TestReporter();
24+
checker.addCheck(check);
25+
checker.addReporter(rep);
26+
checker.process([{name:FILE_NAME, content:src}]);
27+
return rep.message;
28+
}
29+
}
30+
31+
class TestReporter implements IReporter {
32+
33+
public var message:String;
34+
35+
public function new() {
36+
message = "";
37+
}
38+
39+
public function start() {}
40+
41+
public function finish() {}
42+
43+
public function fileStart(f:LintFile) {}
44+
45+
public function fileFinish(f:LintFile) {}
46+
47+
public function addMessage(m:LintMessage) {
48+
message = m.message;
49+
}
50+
}

test/ITest.hx

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/ListenerNameCheckTest.hx

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package ;
2+
3+
import checkstyle.checks.ListenerNameCheck;
4+
5+
class ListenerNameCheckTest extends CheckTestCase {
6+
7+
public function testCorrectListenerName(){
8+
var msg = checkMessage(ListernerTests.TEST, new ListenerNameCheck());
9+
assertEquals(msg, '');
10+
}
11+
12+
public function testListenerName1(){
13+
var msg = checkMessage(ListernerTests.TEST1, new ListenerNameCheck());
14+
assertEquals(msg, 'Wrong listener name, prefix with "on": _testUpdate');
15+
}
16+
17+
public function testListenerName2(){
18+
var msg = checkMessage(ListernerTests.TEST2, new ListenerNameCheck());
19+
assertEquals(msg, 'Wrong listener name, prefix with "on": _testUpdate');
20+
}
21+
22+
public function testListenerName3(){
23+
var msg = checkMessage(ListernerTests.TEST3, new ListenerNameCheck());
24+
assertEquals(msg, 'Wrong listener name, prefix with "on": _testUpdate');
25+
}
26+
27+
public function testListenerName4(){
28+
var msg = checkMessage(ListernerTests.TEST4, new ListenerNameCheck());
29+
assertEquals(msg, 'Wrong listener name, prefix with "on": _testUpdate');
30+
}
31+
32+
public function testListenerName5(){
33+
var msg = checkMessage(ListernerTests.TEST5, new ListenerNameCheck());
34+
assertEquals(msg, 'Wrong listener name, prefix with "on": _testUpdate');
35+
}
36+
}
37+
38+
class ListernerTests {
39+
public static inline var TEST:String = "
40+
class Test {
41+
var a:Stage;
42+
public function new() {
43+
a.addOnce('update', _onUpdate);
44+
}
45+
46+
function _onUpdate() {}
47+
}";
48+
49+
public static inline var TEST1:String = "
50+
class Test {
51+
var a:Stage;
52+
public function new() {
53+
a.addEventListener('update', _testUpdate);
54+
}
55+
56+
function _testUpdate() {}
57+
}";
58+
59+
public static inline var TEST2:String = "
60+
class Test {
61+
var a:Stage;
62+
public function new() {
63+
a.on('update', _testUpdate);
64+
}
65+
66+
function _testUpdate() {}
67+
}";
68+
69+
public static inline var TEST3:String = "
70+
class Test {
71+
var a:Stage;
72+
public function new() {
73+
a.once('update', _testUpdate);
74+
}
75+
76+
function _testUpdate() {}
77+
}";
78+
79+
public static inline var TEST4:String = "
80+
class Test {
81+
var a:Stage;
82+
public function new() {
83+
a.add('update', _testUpdate);
84+
}
85+
86+
function _testUpdate() {}
87+
}";
88+
89+
public static inline var TEST5:String = "
90+
class Test {
91+
var a:Stage;
92+
public function new() {
93+
a.addOnce('update', _testUpdate);
94+
}
95+
96+
function _testUpdate() {}
97+
}";
98+
}

test/MultipleChecksTest.hx renamed to test/ManualChecks.hx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ;
22

3-
class MultipleChecksTest {
3+
class ManualChecks {
44

55
var _arr1:Array;
66
var _arr2:Array;
@@ -60,7 +60,7 @@ class MultipleChecksTest {
6060

6161
// Listener check
6262
function _listener() {
63-
addEventListener("click", onClick);
63+
addEventListener("click", testclick);
6464
}
65-
function onClick() {}
65+
function testclick() {}
6666
}

0 commit comments

Comments
 (0)