blob: 169b6f524139cb4cfa819e658c3bd94a6067f5dd [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001/**
2 * @fileoverview Rule to check for jsdoc presence.
3 * @author Gyandeep Singh
Tim van der Lippe0fb47802021-11-08 16:23:10 +00004 * @deprecated in ESLint v5.10.0
Yang Guo4fd355c2019-09-19 10:59:03 +02005 */
6"use strict";
7
Tim van der Lippe0ceb4652022-01-06 14:23:36 +01008/** @type {import('../shared/types').Rule} */
Yang Guo4fd355c2019-09-19 10:59:03 +02009module.exports = {
10 meta: {
11 type: "suggestion",
12
13 docs: {
14 description: "require JSDoc comments",
Yang Guo4fd355c2019-09-19 10:59:03 +020015 recommended: false,
16 url: "https://eslint.org/docs/rules/require-jsdoc"
17 },
18
19 schema: [
20 {
21 type: "object",
22 properties: {
23 require: {
24 type: "object",
25 properties: {
26 ClassDeclaration: {
27 type: "boolean",
28 default: false
29 },
30 MethodDefinition: {
31 type: "boolean",
32 default: false
33 },
34 FunctionDeclaration: {
35 type: "boolean",
36 default: true
37 },
38 ArrowFunctionExpression: {
39 type: "boolean",
40 default: false
41 },
42 FunctionExpression: {
43 type: "boolean",
44 default: false
45 }
46 },
47 additionalProperties: false,
48 default: {}
49 }
50 },
51 additionalProperties: false
52 }
53 ],
54
55 deprecated: true,
Tim van der Lippe16aca392020-11-13 11:37:13 +000056 replacedBy: [],
57
58 messages: {
59 missingJSDocComment: "Missing JSDoc comment."
60 }
Yang Guo4fd355c2019-09-19 10:59:03 +020061 },
62
63 create(context) {
64 const source = context.getSourceCode();
65 const DEFAULT_OPTIONS = {
66 FunctionDeclaration: true,
67 MethodDefinition: false,
68 ClassDeclaration: false,
69 ArrowFunctionExpression: false,
70 FunctionExpression: false
71 };
72 const options = Object.assign(DEFAULT_OPTIONS, context.options[0] && context.options[0].require);
73
74 /**
75 * Report the error message
76 * @param {ASTNode} node node to report
77 * @returns {void}
78 */
79 function report(node) {
Tim van der Lippe16aca392020-11-13 11:37:13 +000080 context.report({ node, messageId: "missingJSDocComment" });
Yang Guo4fd355c2019-09-19 10:59:03 +020081 }
82
83 /**
84 * Check if the jsdoc comment is present or not.
85 * @param {ASTNode} node node to examine
86 * @returns {void}
87 */
88 function checkJsDoc(node) {
89 const jsdocComment = source.getJSDocComment(node);
90
91 if (!jsdocComment) {
92 report(node);
93 }
94 }
95
96 return {
97 FunctionDeclaration(node) {
98 if (options.FunctionDeclaration) {
99 checkJsDoc(node);
100 }
101 },
102 FunctionExpression(node) {
103 if (
104 (options.MethodDefinition && node.parent.type === "MethodDefinition") ||
105 (options.FunctionExpression && (node.parent.type === "VariableDeclarator" || (node.parent.type === "Property" && node === node.parent.value)))
106 ) {
107 checkJsDoc(node);
108 }
109 },
110 ClassDeclaration(node) {
111 if (options.ClassDeclaration) {
112 checkJsDoc(node);
113 }
114 },
115 ArrowFunctionExpression(node) {
116 if (options.ArrowFunctionExpression && node.parent.type === "VariableDeclarator") {
117 checkJsDoc(node);
118 }
119 }
120 };
121 }
122};