UNPKG

13.9 kBJavaScriptView Raw
1/**
2 * Validate jsdoc comments
3 *
4 * ## Usage
5 *
6 * ```json
7 * {
8 * "jsDoc": {
9 * "checkAnnotations": "closurecompiler",
10 * "checkTypes": "strictNativeCase",
11 * "enforceExistence": "exceptExports"
12 * ...
13 * }
14 * }
15 * ```
16 *
17 * ## Rules
18 *
19 * ### checkAnnotations
20 *
21 * Checks tag names are valid.
22 *
23 * There are 3 presets for `Closure Compiler`, `JSDoc3` and `JSDuck5`.
24 *
25 * By default it allows any tag from any preset. You can pass `Object`
26 * to select preset with `preset` field and add custom tags with `extra` field.
27 *
28 * Type: `Boolean` or `String` or `{"preset": String, "extra": Object}`
29 * (see [tag values](#user-content-tag-values)).
30 *
31 * Values: `true`, `"closurecompiler"`, `"jsdoc3"`, `"jsduck5"`, `Object`
32 *
33 * Context: `file`
34 *
35 * Tags: `*`
36 *
37 * #### Tag values
38 *
39 * `extra` field should contains tags in keys and there are options for values:
40 * - `false` means tag available with no value
41 * - `true` means tag available with any value
42 * - `"some"` means tag available and requires some value
43 *
44 * See also [tag presets](https://github.com/jscs-dev/jscs-jsdoc/tree/master/lib/tags).
45 *
46 * #### Example
47 *
48 * ```js
49 * "checkAnnotations": true
50 * ```
51 *
52 * ##### Valid
53 *
54 * ```js
55 * /**
56 * * @chainable
57 * * @param {string} message
58 * * @return {string}
59 * *\/
60 * function _f() {}
61 * ```
62 *
63 * ##### Invalid
64 *
65 * ```js
66 * /**
67 * * @pororo
68 * * @lalala
69 * *\/
70 * function _f() {}
71 * ```
72 *
73 * #### Example 2
74 *
75 * ```js
76 * "checkAnnotations": {
77 * "preset": "jsdoc3",
78 * "extra": {
79 * "boomer": false
80 * }
81 * }
82 * ```
83 *
84 * ##### Valid
85 *
86 * ```js
87 * /**
88 * * @boomer
89 * * @argument {String}
90 * *\/
91 * function _f() {}
92 * ```
93 *
94 * ##### Invalid
95 *
96 * ```js
97 * /** @still-invalid *\/
98 * ```
99 *
100 * ### checkParamExistence
101 *
102 * Checks all parameters are documented.
103 *
104 * Type: `Boolean`
105 *
106 * Values: `true`
107 *
108 *
109 * #### Example
110 *
111 * ```js
112 * "checkParamExistence": true
113 * ```
114 *
115 * ##### Valid
116 *
117 * ```js
118 * /**
119 * * @param {string} message
120 * * @return {string}
121 * *\/
122 * function _f ( message ) {
123 * return true;
124 * }
125 *
126 * /**
127 * * @inheritdoc
128 * *\/
129 * function _f ( message ) {
130 * return true;
131 * }
132 * ```
133 *
134 * ##### Invalid
135 *
136 * ```js
137 * /**
138 * * @return {string}
139 * *\/
140 * function _f ( message ) {
141 * return true;
142 * }
143 * ```
144 *
145 * ### checkParamNames
146 *
147 * Checks param names in jsdoc and in function declaration are equal.
148 *
149 * Type: `Boolean`
150 *
151 * Values: `true`
152 *
153 * Context: `functions`
154 *
155 * Tags: `param`, `arg`, `argument`
156 *
157 * #### Example
158 *
159 * ```js
160 * "checkParamNames": true
161 * ```
162 *
163 * ##### Valid
164 *
165 * ```js
166 * /**
167 * * @param {String} message
168 * * @param {Number|Object} [line]
169 * *\/
170 * function method(message, line) {}
171 * ```
172 *
173 * ##### Invalid
174 *
175 * ```js
176 * /**
177 * * @param {String} msg
178 * * @param {Number|Object} [line]
179 * *\/
180 * function method(message) {}
181 * ```
182 *
183 * ### requireParamTypes
184 *
185 * Checks params in jsdoc contains type.
186 *
187 * Type: `Boolean`
188 *
189 * Values: `true`
190 *
191 * Context: `functions`
192 *
193 * Tags: `param`, `arg`, `argument`
194 *
195 * #### Example
196 *
197 * ```js
198 * "requireParamTypes": true
199 * ```
200 *
201 * ##### Valid
202 *
203 * ```js
204 * /**
205 * * @param {String} message
206 * *\/
207 * function method() {}
208 * ```
209 *
210 * ##### Invalid
211 *
212 * ```js
213 * /**
214 * * @param message
215 * *\/
216 * function method() {}
217 * ```
218 *
219 * ### checkRedundantParams
220 *
221 * Reports redundant params in jsdoc.
222 *
223 * Type: `Boolean`
224 *
225 * Values: `true`
226 *
227 * Context: `functions`
228 *
229 * Tags: `param`, `arg`, `argument`
230 *
231 * #### Example
232 *
233 * ```js
234 * "checkRedundantParams": true
235 * ```
236 *
237 * ##### Valid
238 *
239 * ```js
240 * /**
241 * * @param {String} message
242 * *\/
243 * function method(message) {}
244 * ```
245 *
246 * ##### Invalid
247 *
248 * ```js
249 * /**
250 * * @param {String} message
251 * *\/
252 * function method() {}
253 * ```
254 *
255 * ### checkReturnTypes
256 *
257 * Checks for differences between the jsdoc and actual return types if both exist.
258 *
259 * Type: `Boolean`
260 *
261 * Values: `true`
262 *
263 * Context: `functions`
264 *
265 * Tags: `return`, `returns`
266 *
267 * #### Example
268 *
269 * ```js
270 * "checkReturnTypes": true
271 * ```
272 *
273 * ##### Valid
274 *
275 * ```js
276 * /**
277 * * @returns {String}
278 * *\/
279 * function method() {
280 * return 'foo';
281 * }
282 * ```
283 *
284 * ##### Invalid
285 *
286 * ```js
287 * /**
288 * * @returns {String}
289 * *\/
290 * function method(f) {
291 * if (f) {
292 * return true;
293 * }
294 * return 1;
295 * }
296 * ```
297 *
298 * ### checkRedundantReturns
299 *
300 * Report statements for functions without return.
301 *
302 * Type: `Boolean`
303 *
304 * Values: `true`
305 *
306 * Context: `functions`
307 *
308 * Tags: `return`, `returns`
309 *
310 * #### Example
311 *
312 * ```js
313 * "checkRedundantReturns": true
314 * ```
315 *
316 * ##### Valid
317 *
318 * ```js
319 * /**
320 * * @returns {string}
321 * *\/
322 * function f() {
323 * return 'yes';
324 * }
325 * ```
326 *
327 * ##### Invalid
328 *
329 * ```js
330 * /**
331 * * @returns {string}
332 * *\/
333 * function f() {
334 * // no return here
335 * }
336 * ```
337 *
338 * ### requireReturnTypes
339 *
340 * Checks returns in jsdoc contains type
341 *
342 * Type: `Boolean`
343 *
344 * Values: `true`
345 *
346 * Context: `functions`
347 *
348 * Tags: `return`, `returns`
349 *
350 * #### Example
351 *
352 * ```js
353 * "requireReturnTypes": true
354 * ```
355 *
356 * ##### Valid
357 *
358 * ```js
359 * /**
360 * * @returns {String}
361 * *\/
362 * function method() {}
363 *
364 * /**
365 * * no @return
366 * *\/
367 * function method() {}
368 * ```
369 *
370 * ##### Invalid
371 *
372 * ```js
373 * /**
374 * * @returns
375 * *\/
376 * function method() {}
377 * ```
378 *
379 * ### checkTypes
380 *
381 * Reports invalid types for bunch of tags.
382 *
383 * The `strictNativeCase` mode checks that case of natives is the same as in this
384 * list: `boolean`, `number`, `string`, `Object`, `Array`, `Date`, `RegExp`.
385 *
386 * The `capitalizedNativeCase` mode checks that the first letter in all native
387 * types and primitives is uppercased except the case with `function` in google
388 * closure format: `{function(...)}`
389 *
390 * Type: `Boolean` or `String`
391 *
392 * Values: `true` or `"strictNativeCase"` or `"capitalizedNativeCase"`
393 *
394 * Context: `*`
395 *
396 * Tags: `typedef`, `type`, `param`, `return`, `returns`, `enum`, `var`, `prop`,
397 * `property`, `arg`, `argument`, `cfg`, `lends`, `extends`, `implements`, `define`
398 *
399 * #### Example
400 *
401 * ```js
402 * "checkTypes": true
403 * ```
404 *
405 * ##### Valid
406 *
407 * ```js
408 * /**
409 * * @typedef {Object} ObjectLike
410 * * @property {boolean} hasFlag
411 * * @property {string} name
412 * *\/
413 *
414 * /** @type {number} *\/
415 * var bar = 1;
416 *
417 * /** @const {number} *\/
418 * var FOO = 2;
419 *
420 * /**
421 * * @const
422 * * @type {number}
423 * *\/
424 * var BAZ = 3;
425 *
426 * /**
427 * * @param {SomeX} x
428 * * @returns {string}
429 * *\/
430 * function method(x) {}
431 * ```
432 *
433 * ##### Invalid
434 *
435 * ```js
436 * /** @type {some~number} *\/
437 * var x = 1;
438 *
439 * /**
440 * * @param {function(redundantName: Number)} x
441 * *\/
442 * function method(x) {}
443 *
444 * /**
445 * * @param {Number|Boolean|object|array} x invalid for strictNativeCase
446 * *\/
447 * function method(x) {}
448 * ```
449 *
450 * ```js
451 * /** @type {some~number} *\/
452 * var x = 1;
453 * ```
454 *
455 * ### checkRedundantAccess
456 *
457 * Reports redundant access declarations.
458 *
459 * Type: `Boolean` or `String`
460 *
461 * Values: `true` or `"enforceLeadingUnderscore"` or `"enforceTrailingUnderscore"`
462 *
463 * Context: `functions`
464 *
465 * Tags: `access`, `private`, `protected`, `public`
466 *
467 * #### Example
468 *
469 * ```js
470 * "checkRedundantAccess": true
471 * "checkRedundantAccess": "enforceLeadingUnderscore"
472 * ```
473 *
474 * ##### Valid for true, "enforceLeadingUnderscore"
475 *
476 * ```js
477 * /**
478 * * @access private
479 * *\/
480 * function _f() {}
481 *
482 * /**
483 * * @access public
484 * *\/
485 * function f() {}
486 * ```
487 *
488 * ##### Invalid for true
489 *
490 * ```js
491 * /**
492 * * @private
493 * * @access private
494 * *\/
495 * function _f() {}
496 * ```
497 *
498 * ##### Invalid for "enforceLeadingUnderscore"
499 *
500 * ```js
501 * /**
502 * * @private
503 * *\/
504 * function _f() {}
505 * ```
506 *
507 * ### leadingUnderscoreAccess
508 *
509 * Checks access declaration is set for `_underscored` function names
510 *
511 * Ignores a bunch of popular identifiers:
512 * `__filename`, `__dirname`, `__proto__`, `__defineGetter__`, `super_`,
513 * `__constructor`, etc.
514 *
515 * Type: `Boolean` or `String`
516 *
517 * Values: `true` (means not public), `"private"`, `"protected"`
518 *
519 * Context: `functions`
520 *
521 * Tags: `access`, `private`, `protected`, `public`
522 *
523 * #### Example
524 *
525 * ```js
526 * "leadingUnderscoreAccess": "protected"
527 * ```
528 *
529 * ##### Valid
530 *
531 * ```js
532 * /**
533 * * @protected
534 * *\/
535 * function _f() {}
536 * ```
537 *
538 * ##### Invalid
539 *
540 * ```js
541 * function _g() {}
542 *
543 * /**
544 * * @private
545 * *\/
546 * function _e() {}
547 * ```
548 *
549 * ### enforceExistence
550 *
551 * Checks jsdoc block exists.
552 *
553 * Type: `Boolean`, `String` or `Object`
554 *
555 * Values:
556 * - `true`
557 * - `"exceptExports"` (*deprecated* use `"allExcept": ["exports"]`)
558 * - `Object`:
559 * - `"allExcept"` array of exceptions:
560 * - `"expressions"` skip expression functions
561 * - `"exports"` skip `module.exports = function () {};`
562 * - `"paramless-procedures"` functions without parameters and with empty
563 * return statements will be skipped
564 *
565 * Context: `functions`
566 *
567 * #### Example
568 *
569 * ```js
570 * "enforceExistence": true
571 * ```
572 *
573 * ##### Valid
574 *
575 * ```js
576 * /**
577 * * @protected
578 * *\/
579 * function _f() {}
580 * ```
581 *
582 * ##### Invalid
583 *
584 * ```js
585 * function _g() {}
586 * ```
587 *
588 *
589 * ### requireHyphenBeforeDescription
590 *
591 * Checks a param description has a hyphen before it (checks for `- `).
592 *
593 * Type: `Boolean`
594 *
595 * Values: `true`
596 *
597 * Context: `functions`
598 *
599 * Tags: `param`, `arg`, `argument`
600 *
601 * #### Example
602 *
603 * ```js
604 * "requireHyphenBeforeDescription": true
605 * ```
606 *
607 * ##### Valid
608 *
609 * ```js
610 * /**
611 * * @param {String} - message
612 * *\/
613 * function method() {}
614 * ```
615 *
616 * ##### Invalid
617 *
618 * ```js
619 * /**
620 * * @param {String} message
621 * *\/
622 * function method() {}
623 * ```
624 *
625 *
626 * ### requireNewlineAfterDescription
627 *
628 * Checks a doc comment description has padding newline.
629 *
630 * Type: `Boolean`
631 *
632 * Values: `true`
633 *
634 * Context: `functions`
635 *
636 * Tags: `*`
637 *
638 * #### Example
639 *
640 * ```js
641 * "requireNewlineAfterDescription": true
642 * ```
643 *
644 * ##### Valid
645 *
646 * ```js
647 * /**
648 * * @param {String} msg - message
649 * *\/
650 * function method(msg) {}
651 *
652 * /**
653 * * Description
654 * *\/
655 * function method() {}
656 *
657 * /**
658 * * Description
659 * *
660 * * @param {String} msg - message
661 * *\/
662 * function method(msg) {}
663 * ```
664 *
665 * ##### Invalid
666 *
667 * ```js
668 * /**
669 * * Description
670 * * @param {String} message
671 * *\/
672 * function method() {}
673 * ```
674 *
675 *
676 * ### disallowNewlineAfterDescription
677 *
678 * Checks a doc comment description has no padding newlines.
679 *
680 * Type: `Boolean`
681 *
682 * Values: `true`
683 *
684 * Context: `functions`
685 *
686 * Tags: `*`
687 *
688 * #### Example
689 *
690 * ```js
691 * "disallowNewlineAfterDescription": true
692 * ```
693 *
694 * ##### Valid
695 *
696 * ```js
697 * /**
698 * * @param {String} msg - message
699 * *\/
700 * function method(msg) {}
701 *
702 * /**
703 * * Description
704 * *\/
705 * function method() {}
706 *
707 * /**
708 * * Description
709 * * @param {String} msg - message
710 * *\/
711 * function method(msg) {}
712 * ```
713 *
714 * ##### Invalid
715 *
716 * ```js
717 * /**
718 * * Description
719 * *
720 * * @param {String} message
721 * *\/
722 * function method(message) {}
723 * ```
724 *
725 *
726 * ### requireDescriptionCompleteSentence
727 *
728 * Checks a doc comment description is a complete sentence.
729 *
730 * A complete sentence is defined as starting with an upper case letter and ending
731 * with a period.
732 *
733 * Type: `Boolean`
734 *
735 * Values: `true`
736 *
737 * Context: `functions`
738 *
739 * Tags: `*`
740 *
741 * #### Example
742 *
743 * ```js
744 * "requireDescriptionCompleteSentence": true
745 * ```
746 *
747 * ##### Valid
748 *
749 * ```js
750 * /**
751 * * @param {String} msg - message
752 * *\/
753 * function method(msg) {}
754 *
755 * /**
756 * * Description.
757 * *\/
758 * function method() {}
759 *
760 * /**
761 * * (Description).
762 * *\/
763 * function method() {}
764 *
765 * /**
766 * * Description.
767 * *
768 * * @param {String} msg - message
769 * *\/
770 * function method(msg) {}
771 *
772 * /**
773 * * Description
774 * * on multiple lines are allowed.
775 * *
776 * * @param {String} msg - message
777 * *\/
778 * function method(msg) {}
779 * ```
780 *
781 * ##### Invalid
782 *
783 * ```js
784 * /**
785 * * Description
786 * * @param {String} message
787 * *\/
788 * function method() {}
789 *
790 * /**
791 * * Description
792 * * On multiple lines should not start with an upper case.
793 * *
794 * * @param {String} - message
795 * *\/
796 * function method() {}
797 *
798 * /**
799 * * description starting with a lower case letter.
800 * * @param {String} message
801 * *\/
802 * function method() {}
803 *
804 * /**
805 * * Description period is offset .
806 * * @param {String} message
807 * *\/
808 * function method() {}
809 *
810 * /**
811 * * Description!
812 * * @param {String} message
813 * *\/
814 * function method() {}
815 * ```
816 *
817 *
818 * ### requireParamDescription
819 *
820 * Checks a param description exists.
821 *
822 * Type: `Boolean`
823 *
824 * Values: `true`
825 *
826 * Context: `functions`
827 *
828 * Tags: `param`, `arg`, `argument`
829 *
830 * #### Example
831 *
832 * ```js
833 * "requireParamDescription": true
834 * ```
835 *
836 * ##### Valid
837 *
838 * ```js
839 * /**
840 * * @param {String} arg message
841 * *\/
842 * function method(arg) {}
843 *
844 * /**
845 * * @param arg message
846 * *\/
847 * function method(arg) {}
848 * ```
849 *
850 * ##### Invalid
851 *
852 * ```js
853 * /**
854 * * @param {String} arg
855 * *\/
856 * function method(arg) {}
857 *
858 * /**
859 * * @param arg
860 * *\/
861 * function method(arg) {}
862 * ```
863 *
864 *
865 * ### requireReturnDescription
866 *
867 * Checks a return description exists.
868 *
869 * Type: `Boolean`
870 *
871 * Values: `true`
872 *
873 * Context: `functions`
874 *
875 * Tags: `return`, `returns`
876 *
877 * #### Example
878 *
879 * ```js
880 * "requireReturnDescription": true
881 * ```
882 *
883 * ##### Valid
884 *
885 * ```js
886 * /**
887 * * @returns {Boolean} Method result.
888 * *\/
889 * function method() {
890 * return false;
891 * }
892 *
893 * /**
894 * * @returns {String} method result
895 * *\/
896 * function method() {
897 * return 'Hello!';
898 * }
899 * ```
900 *
901 * ##### Invalid
902 *
903 * ```js
904 * /**
905 * * @returns {Boolean}
906 * *\/
907 * function method() {
908 * return false;
909 * }
910 * ```
911 *
912 */
913module.exports = require('jscs-jsdoc/lib/rules/validate-jsdoc');