Skip to content

Commit 0d25b3f

Browse files
authored
added relax mode to ConfigParser, fixes #441 (#443)
* added relax mode to ConfigParser, fixes #441 * Fixed indentaion calculation for functions bodys without curly braces * Fixed segmentaion faults in NeedBraces and CatchParameterName checks * Changed message of NestedForLoop check
1 parent ddaa30e commit 0d25b3f

File tree

10 files changed

+87
-10
lines changed

10 files changed

+87
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
- New check DocCommentStyle [#440](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/440)
44
- New check FieldDocComment [#442](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/442)
55
- New check TypeDocComment [#440](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/440)
6+
- Added relaxed mode to ConfigParser, fixes [#441](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/441) ([#443](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/443))
67
- Fixed handling of comments between types in ExtendedEmptyLines [#440](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/440)
78
- Fixed unittest and coverage reporting for Haxe 4 [#442](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/442)
9+
- Fixed indentation calculation for functions bodys without curly braces [#443](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/443)
10+
- Fixed segmentaion faults in NeedBraces and CatchParameterName checks [#443](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/443)
11+
- Changed message of NestedForLoop check [#443](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/443)
812
- Refactored `Checker.getLinePos` to use binary search, reduces runtime from O(N/2) to O(log N) [#439](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/439)
913

1014
## version 2.4.1 (2018-06-14)

src/checkstyle/Main.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class Main {
9494
}
9595
}
9696
else configParser.paths.push("/code");
97+
configParser.validateMode = RELAXED;
9798

9899
if (defaultConfig.config != null) configPath = defaultConfig.config;
99100
if (defaultConfig.exclude != null) excludePath = defaultConfig.exclude;

src/checkstyle/checks/block/NeedBracesCheck.hx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class NeedBracesCheck extends Check {
6161
checkIfChild(tok);
6262
case Kwd(KwdElse):
6363
var firstChild = tok.getFirstChild();
64+
if (firstChild == null) continue;
6465
if (firstChild.is(Kwd(KwdIf))) checkIfChild(firstChild);
6566
else checkLastChild(tok);
6667
case Kwd(KwdFunction):
@@ -147,6 +148,7 @@ class NeedBracesCheck extends Check {
147148
}
148149

149150
function checkNoBraces(parent:TokenTree, child:TokenTree) {
151+
if ((parent == null) || (child == null)) return;
150152
var minLine:LinePos = checker.getLinePos(parent.pos.min);
151153
var maxLine:LinePos = checker.getLinePos(child.getPos().max);
152154
var singleLine:Bool = (minLine.line == maxLine.line);
@@ -157,11 +159,11 @@ class NeedBracesCheck extends Check {
157159
}
158160
else {
159161
if (singleLine) {
160-
logPos('Body of "$parent" on same line', child.pos);
162+
logPos('Body of "$parent" on same line', child.getPos());
161163
return;
162164
}
163165
}
164-
logPos('No braces used for body of "$parent"', child.pos);
166+
logPos('No braces used for body of "$parent"', child.getPos());
165167
}
166168

167169
function checkIfElseSingleline(parent:TokenTree, child:TokenTree):Bool {

src/checkstyle/checks/coding/NestedForDepthCheck.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class NestedForDepthCheck extends Check {
5656
}
5757

5858
function warnNestedForDepth(depth:Int, pos:Position) {
59-
logPos('Nested for depth is $depth (max allowed is ${max})', pos);
59+
logPos('Nested loop depth is $depth (max allowed is ${max})', pos);
6060
}
6161

6262
override public function detectableInstances():DetectableInstances {

src/checkstyle/checks/naming/CatchParameterNameCheck.hx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ class CatchParameterNameCheck extends Check {
2424

2525
for (tkn in catchTokens) {
2626
for (item in tkn.children) {
27-
switch (item.getFirstChild().tok) {
27+
var child:TokenTree = item.getFirstChild();
28+
if (child == null) continue;
29+
switch (child.tok) {
2830
case Const(CIdent(name)):
2931
if (item.is(POpen)) {
3032
if (!formatRE.match(name)) logPos('"$name" must match pattern "~/${format}/"', item.pos);

src/checkstyle/checks/whitespace/IndentationCheck.hx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class IndentationCheck extends Check {
136136
Sharp("elseif"),
137137
Sharp("end"),
138138
Sharp("error"),
139+
Kwd(KwdFunction),
139140
Kwd(KwdIf),
140141
Kwd(KwdElse),
141142
Kwd(KwdFor),
@@ -151,6 +152,8 @@ class IndentationCheck extends Check {
151152
calcLineIndentationBkOpen(token, lineIndentation);
152153
case BrOpen:
153154
increaseBlockIndent(token, lineIndentation);
155+
case Kwd(KwdFunction):
156+
calcLineIndentationFunction(token, lineIndentation);
154157
case Kwd(KwdIf), Kwd(KwdElse):
155158
calcLineIndentationIf(token, lineIndentation);
156159
case Kwd(KwdFor), Kwd(KwdDo), Kwd(KwdWhile):
@@ -182,6 +185,13 @@ class IndentationCheck extends Check {
182185
increaseBlockIndent(token, lineIndentation);
183186
}
184187

188+
function calcLineIndentationFunction(token:TokenTree, lineIndentation:Array<Int>) {
189+
var body:TokenTree = TokenTreeAccessHelper.access(token).firstChild().lastChild().token;
190+
if (body == null) return;
191+
if (body.is(BrOpen)) return;
192+
increaseIndentIfNextLine(token, body, lineIndentation);
193+
}
194+
185195
function calcLineIndentationIf(token:TokenTree, lineIndentation:Array<Int>) {
186196
switch (token.tok) {
187197
case Kwd(KwdIf):

src/checkstyle/config/ConfigParser.hx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@ class ConfigParser {
1919
public var overrideCheckerThreads:Int;
2020
public var info:ChecksInfo;
2121
public var checker:Checker;
22+
public var validateMode:ConfigValidateMode;
2223

2324
var seenConfigPaths:Array<String>;
24-
var failWith:String -> Void;
25+
var failWithCallback:String -> Void;
2526

2627
public function new(failCallback:String -> Void) {
2728
info = new ChecksInfo();
2829
checker = new Checker();
29-
failWith = failCallback;
30+
failWithCallback = failCallback;
3031

3132
paths = [];
3233
allExcludes = [];
3334
seenConfigPaths = [];
3435
excludesMap = new Map();
3536
numberOfCheckerThreads = 5;
3637
overrideCheckerThreads = 0;
38+
validateMode = STRICT;
3739
}
3840

3941
public function loadConfig(path:String) {
@@ -151,7 +153,11 @@ class ConfigParser {
151153
function createCheck(checkConf:CheckConfig):Check {
152154
var check:Check = info.build(checkConf.type);
153155
if (check == null) {
154-
Sys.stdout().writeString('Unknown check \'${checkConf.type}\'');
156+
switch (validateMode) {
157+
case STRICT:
158+
failWith('Unknown check "${checkConf.type}"');
159+
case RELAXED:
160+
}
155161
return null;
156162
}
157163
checker.addCheck(check);
@@ -167,6 +173,7 @@ class ConfigParser {
167173
var val = Reflect.field(checkConf.props, prop);
168174
if (!checkFields.contains(prop)) {
169175
failWith('Check ${check.getModuleName()} has no property named \'$prop\'');
176+
continue;
170177
}
171178
try {
172179
check.configureProperty(prop, val);
@@ -190,7 +197,7 @@ class ConfigParser {
190197

191198
function validateDefines(defines:Array<String>) {
192199
for (define in defines) {
193-
if (define.split("=").length > 2) throw "Found a define with more than one = sign: '" + define + "'";
200+
if (define.split("=").length > 2) failWith("Found a define with more than one = sign: '" + define + "'");
194201
}
195202
}
196203

@@ -235,4 +242,16 @@ class ConfigParser {
235242
}
236243
return count;
237244
}
245+
246+
function failWith(msg:String) {
247+
switch (validateMode) {
248+
case STRICT: failWithCallback(msg);
249+
case RELAXED:
250+
}
251+
}
252+
}
253+
254+
enum ConfigValidateMode {
255+
STRICT;
256+
RELAXED;
238257
}

test/checks/coding/NestedForDepthCheckTest.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class NestedForDepthCheckTest extends CheckTestCase<NestedForDepthCheckTests> {
1313
@Test
1414
public function testDefaultTooMany() {
1515
var check = new NestedForDepthCheck();
16-
assertMsg(check, TEST2, "Nested for depth is 2 (max allowed is 1)");
16+
assertMsg(check, TEST2, "Nested loop depth is 2 (max allowed is 1)");
1717
}
1818

1919
@Test
@@ -26,7 +26,7 @@ class NestedForDepthCheckTest extends CheckTestCase<NestedForDepthCheckTests> {
2626

2727
check.max = 0;
2828
assertNoMsg(check, TEST1);
29-
assertMsg(check, TEST2, "Nested for depth is 1 (max allowed is 0)");
29+
assertMsg(check, TEST2, "Nested loop depth is 1 (max allowed is 0)");
3030
}
3131
}
3232

test/checks/whitespace/IndentationCheckTest.hx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class IndentationCheckTest extends CheckTestCase<IndentationCheckTests> {
1010
var check = new IndentationCheck();
1111
check.severity = SeverityLevel.INFO;
1212
assertNoMsg(check, CORRECT_TAB_INDENT);
13+
assertNoMsg(check, FUNCTION_BODY_NO_BRACES);
1314
assertMsg(check, CORRECT_SPACE_INDENT, 'Indentation mismatch: expected: "\\t"[1], actual: no indentation');
1415
}
1516

@@ -301,4 +302,10 @@ class Test {
301302
/*
302303
* test comment
303304
*/";
305+
306+
var FUNCTION_BODY_NO_BRACES = "
307+
class Test {
308+
public function toString()
309+
return 'Test class';
310+
}";
304311
}

test/config/ConfigParserTest.hx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,36 @@ class ConfigParserTest {
173173

174174
Assert.areEqual("exclude configuration file has unknown version: 0", failMessage);
175175
}
176+
177+
@Test
178+
public function testValidateMode() {
179+
var failMessage:String = "";
180+
var configParser:ConfigParser = new ConfigParser(function (message:String) {
181+
failMessage = message;
182+
});
183+
configParser.validateMode = RELAXED;
184+
185+
Assert.isNotNull(configParser.checker.checks);
186+
Assert.isTrue(configParser.checker.checks.length == 0);
187+
188+
var config:Config = {
189+
version: 0,
190+
checks: [{
191+
"type": "non existing check name"
192+
},
193+
{
194+
"type": "Trace",
195+
"props": {
196+
"non_existing_property": 100
197+
}
198+
}]
199+
};
200+
configParser.parseAndValidateConfig(config, LOCAL_PATH);
201+
202+
Assert.areEqual("", failMessage);
203+
204+
configParser.validateMode = STRICT;
205+
configParser.parseAndValidateConfig(config, LOCAL_PATH);
206+
Assert.areEqual("Check Trace has no property named 'non_existing_property'", failMessage);
207+
}
176208
}

0 commit comments

Comments
 (0)