Skip to content

Commit b1fa854

Browse files
authored
added allowFinal to VariableInitialisation, fixes #491 (#492)
* added allowFinal to VariableInitialisation, fixes #491
1 parent e32c1e8 commit b1fa854

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## dev branch / next version (2.x.x)
44

5+
- Added `allowFinal` setting to `VariableInitialisation`, fixes [#491](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/491) ([#492](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/492))
6+
57
## version 2.6.0 (2019-12-01)
68

79
- **Breaking Change** changed `MethodLength.countEmpty` into `ignoreEmptyLines` ([#486](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/486))

resources/checkstyle-schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3900,6 +3900,11 @@
39003900
"description": "Checks for instance variables that are initialised at class level.",
39013901
"additionalProperties": false,
39023902
"properties": {
3903+
"allowFinal": {
3904+
"description": "final fields must be initialised either immediately or in constructor\n\t\twhen allowFinal is true then VariableInitialisation won't complain about initialisation at class level for final fields",
3905+
"type": "boolean",
3906+
"propertyOrder": 0
3907+
},
39033908
"severity": {
39043909
"description": "sets gravity of reported violations:\n\t- IGNORE = do not report violations, violations do not appear anywhere in output\n\t- INFO = all violations have info / lowest priority\n\t- WARNING = all violations have warning / medium priority\n\t- ERROR = all violations have error / highest priority",
39053910
"type": "string",
@@ -3909,7 +3914,7 @@
39093914
"ERROR",
39103915
"IGNORE"
39113916
],
3912-
"propertyOrder": 0
3917+
"propertyOrder": 1
39133918
}
39143919
},
39153920
"type": "object"

resources/default-config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,9 @@
648648
},
649649
{
650650
"type": "VariableInitialisation",
651-
"props": {}
651+
"props": {
652+
"allowFinal": false
653+
}
652654
},
653655
{
654656
"type": "WhitespaceAfter",

src/checkstyle/checks/coding/VariableInitialisationCheck.hx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ package checkstyle.checks.coding;
66
@name("VariableInitialisation")
77
@desc("Checks for instance variables that are initialised at class level.")
88
class VariableInitialisationCheck extends Check {
9+
/**
10+
final fields must be initialised either immediately or in constructor
11+
when allowFinal is true then VariableInitialisation won't complain about initialisation at class level for final fields
12+
**/
13+
public var allowFinal:Bool;
14+
915
public function new() {
1016
super(AST);
1117
categories = [Category.STYLE, Category.CLARITY];
1218
points = 2;
19+
allowFinal = false;
1320
}
1421

1522
override function actualRun() {
@@ -21,14 +28,20 @@ class VariableInitialisationCheck extends Check {
2128

2229
var isPrivate = false;
2330
var isPublic = false;
24-
var isInline = false;
25-
var isStatic = false;
31+
var isInline = f.access.contains(AInline);
32+
var isStatic = f.access.contains(AStatic);
33+
var isFinal = false;
34+
35+
if (isInline || isStatic) return;
2636

27-
if (f.access.contains(AInline)) isInline = true;
28-
else if (f.access.contains(AStatic)) isStatic = true;
29-
else if (f.access.contains(APublic)) isPublic = true;
37+
if (f.access.contains(APublic)) isPublic = true;
3038
else isPrivate = true;
3139

40+
#if haxe4
41+
if (f.access.contains(AFinal)) isFinal = true;
42+
#end
43+
if (allowFinal && isFinal) return;
44+
3245
if (isPrivate || isPublic) {
3346
switch (f.kind) {
3447
case FVar(t, e):
@@ -40,6 +53,6 @@ class VariableInitialisationCheck extends Check {
4053
}
4154

4255
function warnVarInit(name:String, pos:Position) {
43-
logPos('Invalid variable "${name}" initialisation (move initialisation to constructor or function)', pos);
56+
logPos('Invalid variable initialisation for "${name}" (move initialisation to constructor or function)', pos);
4457
}
4558
}

test/checkstyle/checks/coding/VariableInitialisationCheckTest.hx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package checkstyle.checks.coding;
33
class VariableInitialisationCheckTest extends CheckTestCase<VariableInitialisationCheckTests> {
44
@Test
55
public function testVar() {
6-
assertMsg(new VariableInitialisationCheck(), TEST1, 'Invalid variable "_a" initialisation (move initialisation to constructor or function)');
6+
assertMsg(new VariableInitialisationCheck(), TEST1, 'Invalid variable initialisation for "_a" (move initialisation to constructor or function)');
77
}
88

99
@Test
@@ -15,6 +15,18 @@ class VariableInitialisationCheckTest extends CheckTestCase<VariableInitialisati
1515
public function testEnumAbstract() {
1616
assertNoMsg(new VariableInitialisationCheck(), TEST3);
1717
}
18+
19+
#if haxe4
20+
@Test
21+
public function testFinal() {
22+
var check:VariableInitialisationCheck = new VariableInitialisationCheck();
23+
assertMsg(check, FINAL, 'Invalid variable initialisation for "VALUE" (move initialisation to constructor or function)');
24+
assertNoMsg(check, FINAL_CONSTRUCTOR);
25+
check.allowFinal = true;
26+
assertNoMsg(check, FINAL);
27+
assertNoMsg(check, FINAL_CONSTRUCTOR);
28+
}
29+
#end
1830
}
1931

2032
@:enum
@@ -31,6 +43,7 @@ abstract VariableInitialisationCheckTests(String) to String {
3143
var TEST2 = "
3244
abstractAndClass Test {
3345
static inline var TEST:Int = 1;
46+
inline var TEST2:Int = 1;
3447
3548
public function new() {}
3649
}";
@@ -39,4 +52,16 @@ abstract VariableInitialisationCheckTests(String) to String {
3952
abstract Test(Int) {
4053
var VALUE = 0;
4154
}";
55+
var FINAL = "
56+
abstractAndClass Test {
57+
final VALUE = 0;
58+
}";
59+
var FINAL_CONSTRUCTOR = "
60+
abstractAndClass Test {
61+
final VALUE;
62+
63+
public function new() {
64+
VALUE = 1;
65+
}
66+
}";
4267
}

0 commit comments

Comments
 (0)