Skip to content

Commit ed2f0f1

Browse files
author
Alexej Yaroshevich
committed
checkParamNames: out of order case and tests
Fixes #33
1 parent 7d2d1e7 commit ed2f0f1

File tree

2 files changed

+110
-3
lines changed

2 files changed

+110
-3
lines changed

lib/rules/validate-jsdoc/check-param-names.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ function validateCheckParamNames(node, err) {
1414
return;
1515
}
1616

17+
// outOfOrder
18+
var paramsByName = {};
19+
node.params.forEach(function(param) {
20+
paramsByName[param.name] = param;
21+
});
22+
var paramTagsByName = {};
23+
node.jsdoc.iterateByType(['param', 'arg', 'argument'], function(tag) {
24+
if (tag.name && tag.name.value) {
25+
paramTagsByName[tag.name.value] = tag;
26+
}
27+
});
28+
var outOfOrder = {};
29+
1730
node.jsdoc.iterateByType(['param', 'arg', 'argument'],
1831
/**
1932
* tag checker
@@ -33,9 +46,19 @@ function validateCheckParamNames(node, err) {
3346
}
3447

3548
// checking name
36-
if (tag.name.value !== param.name) {
37-
return err('expected ' + param.name + ' but got ' + tag.name.value,
38-
tag.name.loc || node.jsdoc.loc.start);
49+
var msg;
50+
if (tag.name.value !== param.name && !outOfOrder[tag.name.value]) {
51+
if (paramsByName[tag.name.value] && paramTagsByName[param.name]) {
52+
outOfOrder[tag.name.value] = outOfOrder[param.name] = true;
53+
msg = 'parameters ' + tag.name.value + ' and ' + param.name + ' are out of order';
54+
} else if (paramsByName[tag.name.value]) {
55+
outOfOrder[tag.name.value] = true;
56+
msg = 'parameter ' + tag.name.value + ' is out of order';
57+
} else {
58+
msg = 'expected ' + param.name + ' but got ' + tag.name.value;
59+
}
60+
61+
return err(msg, tag.name.loc || node.jsdoc.loc.start);
3962
}
4063
});
4164
}

test/lib/rules/validate-jsdoc/check-param-names.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,90 @@ describe('lib/rules/validate-jsdoc/check-param-names', function () {
127127
/* jshint ignore:end */
128128
]);
129129

130+
// out-of-order. issue #33
131+
checker.cases([
132+
/* jshint ignore:start */
133+
{
134+
it: 'should report out of order',
135+
code: function () {
136+
/**
137+
* @param xxx
138+
* @param yyy
139+
*/
140+
function funcName(yyy, xxx) {
141+
}
142+
},
143+
errors: [
144+
{message: 'parameters xxx and yyy are out of order', column: 10, line: 2, rule: "jsDoc"}
145+
]
146+
}, {
147+
it: 'should report out of order many times',
148+
code: function () {
149+
Cls.prototype = {
150+
/**
151+
* @param xxx
152+
* @param yyy
153+
* @param zzz
154+
*/
155+
run: function(zzz, xxx, yyy) {
156+
}
157+
};
158+
},
159+
errors: [
160+
{message: 'parameters xxx and zzz are out of order', column: 14, line: 3, rule: "jsDoc"},
161+
{message: 'parameters yyy and xxx are out of order', column: 14, line: 4, rule: "jsDoc"}
162+
]
163+
}, {
164+
it: 'should report out of order and expected',
165+
code: function () {
166+
Cls.prototype = {
167+
/**
168+
* @param xxx
169+
* @param yyy
170+
*/
171+
run: function(zzz, xxx) {
172+
}
173+
};
174+
},
175+
errors: [
176+
{message: 'parameter xxx is out of order', column: 14, line: 3, rule: "jsDoc"},
177+
{message: 'expected xxx but got yyy', column: 14, line: 4, rule: "jsDoc"}
178+
]
179+
}, {
180+
it: 'should report out of order and expected v2',
181+
code: function () {
182+
Cls.prototype = {
183+
/**
184+
* @param xxx
185+
* @param yyy
186+
*/
187+
run: function(yyy, zzz) {
188+
}
189+
};
190+
},
191+
errors: [
192+
{message: 'expected yyy but got xxx', column: 14, line: 3, rule: "jsDoc"},
193+
{message: 'parameter yyy is out of order', column: 14, line: 4, rule: "jsDoc"}
194+
]
195+
}, {
196+
it: 'should not report out of order but expected',
197+
code: function () {
198+
Cls.prototype = {
199+
/**
200+
* @param xxx
201+
* @param yyy
202+
*/
203+
run: function(zzz, yyy) {
204+
}
205+
};
206+
},
207+
errors: [
208+
{message: 'expected zzz but got xxx', column: 14, line: 3, rule: "jsDoc"}
209+
]
210+
}
211+
/* jshint ignore:end */
212+
]);
213+
130214
});
131215

132216
});

0 commit comments

Comments
 (0)