Skip to content

Commit b49d9c3

Browse files
authored
added parameter check to FieldDocComment check, fixes #87 (#444)
* added parameter check to FieldDocComment check, fixes #87 * adjusted autodetection for nested check * fixed reported position of EmptyBlock check
1 parent 0d25b3f commit b49d9c3

File tree

8 files changed

+120
-15
lines changed

8 files changed

+120
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
## dev branch / next version (2.x.x)
22

33
- New check DocCommentStyle [#440](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/440)
4-
- New check FieldDocComment [#442](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/442)
4+
- New check FieldDocComment, fixes [#87](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/87) ([#442](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/442) + [#444](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/444))
55
- New check TypeDocComment [#440](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/440)
66
- Added relaxed mode to ConfigParser, fixes [#441](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/441) ([#443](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/443))
77
- Fixed handling of comments between types in ExtendedEmptyLines [#440](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/440)
88
- Fixed unittest and coverage reporting for Haxe 4 [#442](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/442)
99
- Fixed indentation calculation for functions bodys without curly braces [#443](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/443)
1010
- Fixed segmentaion faults in NeedBraces and CatchParameterName checks [#443](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/443)
11+
- Fixed reported position of EmptyBlock check [#444](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/444)
1112
- Changed message of NestedForLoop check [#443](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/443)
13+
- Changed autodetection for nested for/if/try checks to start at zero [#444](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/444)
1214
- 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)
1315

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

src/checkstyle/checks/block/EmptyBlockCheck.hx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ class EmptyBlockCheck extends Check {
109109

110110
function checkForText(brOpen:TokenTree) {
111111
if (brOpen.children.length == 1) {
112-
logPos("Empty block should contain a comment or a statement", brOpen.pos);
112+
logPos("Empty block should contain a comment or a statement", brOpen.getPos());
113113
return;
114114
}
115115
}
116116

117117
function checkForStatement(brOpen:TokenTree) {
118118
if (brOpen.children.length == 1) {
119-
logPos("Empty block should contain a statement", brOpen.pos);
119+
logPos("Empty block should contain a statement", brOpen.getPos());
120120
return;
121121
}
122122
var onlyComments:Bool = true;
@@ -129,14 +129,14 @@ class EmptyBlockCheck extends Check {
129129
break;
130130
}
131131
}
132-
if (onlyComments) logPos("Block should contain a statement", brOpen.pos);
132+
if (onlyComments) logPos("Block should contain a statement", brOpen.getPos());
133133
}
134134

135135
function checkForEmpty(brOpen:TokenTree) {
136136
if ((brOpen.children == null) || (brOpen.children.length > 1)) return;
137137
var brClose:TokenTree = TokenTreeAccessHelper.access(brOpen).firstChild().is(BrClose).token;
138138
if (brClose == null) return;
139-
if (brOpen.pos.max != brClose.pos.min) logPos('Empty block should be written as "{}"', brOpen.pos);
139+
if (brOpen.pos.max != brClose.pos.min) logPos('Empty block should be written as "{}"', brOpen.getPos());
140140
}
141141
}
142142

src/checkstyle/checks/coding/NestedForDepthCheck.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class NestedForDepthCheck extends Check {
6464
fixed: [],
6565
properties: [{
6666
propertyName: "max",
67-
values: [for (i in 1...5) i]
67+
values: [for (i in 0...5) i]
6868
}]
6969
}];
7070
}

src/checkstyle/checks/coding/NestedIfDepthCheck.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class NestedIfDepthCheck extends Check {
6363
fixed: [],
6464
properties: [{
6565
propertyName: "max",
66-
values: [for (i in 1...10) i]
66+
values: [for (i in 0...10) i]
6767
}]
6868
}];
6969
}

src/checkstyle/checks/coding/NestedTryDepthCheck.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class NestedTryDepthCheck extends Check {
6969
fixed: [],
7070
properties: [{
7171
propertyName: "max",
72-
values: [for (i in 1...5) i]
72+
values: [for (i in 0...5) i]
7373
}]
7474
}];
7575
}

src/checkstyle/checks/comments/FieldDocCommentCheck.hx

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,25 +187,74 @@ class FieldDocCommentCheck extends Check {
187187
function checkParams(fieldName:String, token:TokenTree, docToken:TokenTree, text:String) {
188188
var popenToken:TokenTree = TokenTreeAccessHelper.access(token).firstChild().firstOf(POpen).token;
189189
if (popenToken == null) return;
190+
var params:Array<String> = [];
190191
if (popenToken.children != null) {
191192
for (child in popenToken.children) {
192193
switch (child.tok) {
193-
case Const(CIdent(ident)): checkParam(fieldName, ident, docToken, text);
194+
case Const(CIdent(ident)): params.push(ident);
194195
default:
195196
}
196197
}
197198
}
199+
checkParamsAndOrder(fieldName, params, docToken, text);
198200
}
199201

200-
function checkParam(fieldName:String, name:String, docToken:TokenTree, text:String) {
201-
var search:String = '@param $name ';
202-
if (text.indexOf(search) >= 0) return;
203-
logPos('Documentation for parameter "$name" of field "$fieldName" missing', docToken.pos);
202+
function checkParamsAndOrder(fieldName:String, params:Array<String>, docToken:TokenTree, text:String) {
203+
if (params.length <= 0) return;
204+
var lines:Array<String> = text.split(checker.lineSeparator);
205+
var paramOrder:Array<Int> = [];
206+
var missingParams:Array<String> = [];
207+
for (param in params) {
208+
var search:String = '@param $param ';
209+
var index:Int = 0;
210+
var found:Bool = false;
211+
while (index < lines.length) {
212+
var line:String = lines[index++];
213+
var pos:Int = line.indexOf(search);
214+
if (pos >= 0) {
215+
paramOrder.push(index);
216+
var desc:String = line.substr(pos + search.length);
217+
found = !~/^[\-\s]*$/.match(desc);
218+
break;
219+
}
220+
}
221+
if (!found) missingParams.push(param);
222+
}
223+
if (missingParams.length > 0) logMissingParams(fieldName, missingParams, docToken);
224+
else checkParamOrder(fieldName, params, paramOrder, docToken);
225+
}
226+
227+
function checkParamOrder(fieldName:String, params:Array<String>, paramOrder:Array<Int>, docToken:TokenTree) {
228+
var start:Int = 0;
229+
for (index in 0...paramOrder.length) {
230+
var value:Int = paramOrder[index];
231+
if (value > start) {
232+
start = value;
233+
continue;
234+
}
235+
var param:String = params[index];
236+
logPos('Incorrect order of documentation for parameter "$param" of field "$fieldName"', docToken.pos);
237+
}
238+
}
239+
240+
function logMissingParams(fieldName:String, params:Array<String>, docToken:TokenTree) {
241+
for (param in params) logPos('Documentation for parameter "$param" of field "$fieldName" missing', docToken.pos);
204242
}
205243

206244
function checkReturn(name:String, docToken:TokenTree, text:String) {
207245
var search:String = "@return ";
208-
if (text.indexOf(search) >= 0) return;
246+
var pos:Int = text.indexOf(search);
247+
if (pos < 0) {
248+
logPos('Documentation for return value of field "$name" missing', docToken.pos);
249+
return;
250+
}
251+
var desc:String = text.substr(pos + search.length);
252+
var lines:Array<String> = desc.split(checker.lineSeparator);
253+
if (lines.length < 0) {
254+
logPos('Documentation for return value of field "$name" missing', docToken.pos);
255+
return;
256+
}
257+
if (!~/^[\-\s]*$/.match(lines[0])) return;
209258
logPos('Documentation for return value of field "$name" missing', docToken.pos);
210259
}
211260

src/checkstyle/checks/whitespace/ListOfEmptyLines.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ListOfEmptyLines {
2525

2626
/**
2727
checks policy on a single empty line range
28-
@param policy - empty lione policy to check
28+
@param policy - empty line policy to check
2929
@param max - maximum number of empty lines
3030
@param range - range to check
3131
@param line - line to check

test/checks/comments/FieldDocCommentCheckTest.hx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,32 @@ class FieldDocCommentCheckTest extends CheckTestCase<FieldDocCommentCheckTests>
118118
assertNoMsg(check, ONLY_PUBLIC_CLASS_FIELDS_COMMENTED);
119119
assertNoMsg(check, MISSING_PARAM);
120120
assertMsg(check, MISSING_RETURN, MSG_DOC_RETURN_FUNC8);
121+
assertNoMsg(check, WRONG_PARAM_ORDER);
122+
assertNoMsg(check, NO_PARAM_TEXT);
121123

122124
check.requireParams = true;
123125
assertNoMsg(check, ALL_CLASS_FIELDS_COMMENTED);
124126
assertNoMsg(check, ONLY_PUBLIC_CLASS_FIELDS_COMMENTED);
125127
assertMsg(check, MISSING_PARAM, MSG_DOC_PARAM1_FUNC8);
126128
assertMsg(check, MISSING_RETURN, MSG_DOC_RETURN_FUNC8);
129+
assertMsg(check, WRONG_PARAM_ORDER, 'Incorrect order of documentation for parameter "param2" of field "func8"');
130+
assertMsg(check, NO_PARAM_TEXT, MSG_DOC_PARAM1_FUNC8);
131+
}
132+
133+
@Test
134+
public function testRequireReturn() {
135+
var check = new FieldDocCommentCheck();
136+
check.requireReturn = false;
137+
assertNoMsg(check, ALL_CLASS_FIELDS_COMMENTED);
138+
assertNoMsg(check, ONLY_PUBLIC_CLASS_FIELDS_COMMENTED);
139+
assertNoMsg(check, MISSING_RETURN);
140+
assertNoMsg(check, EMPTY_RETURN);
141+
142+
check.requireReturn = true;
143+
assertNoMsg(check, ALL_CLASS_FIELDS_COMMENTED);
144+
assertNoMsg(check, ONLY_PUBLIC_CLASS_FIELDS_COMMENTED);
145+
assertMsg(check, MISSING_RETURN, MSG_DOC_RETURN_FUNC8);
146+
assertMsg(check, EMPTY_RETURN, MSG_DOC_RETURN_FUNC8);
127147
}
128148

129149
@Test
@@ -346,4 +366,38 @@ abstract FieldDocCommentCheckTests(String) to String {
346366
var field1:String;
347367
}
348368
";
369+
370+
var WRONG_PARAM_ORDER = "
371+
class Test2 {
372+
/**
373+
comment
374+
@param param2 - param2
375+
@param param1 - param1
376+
@return value
377+
**/
378+
public function func8(param1:String, param2:String):String {}
379+
}
380+
";
381+
382+
var NO_PARAM_TEXT = "
383+
class Test2 {
384+
/**
385+
comment
386+
@param param1 -
387+
@return value
388+
**/
389+
public function func8(param1:String):String {}
390+
}
391+
";
392+
393+
var EMPTY_RETURN = "
394+
class Test2 {
395+
/**
396+
comment
397+
@param param1 - param1
398+
@return
399+
**/
400+
public function func8(param1:String):String {}
401+
}
402+
";
349403
}

0 commit comments

Comments
 (0)