requireJsdoc.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var OPTIONS_SCHEMA = {
  7. additionalProperties: false,
  8. properties: {
  9. require: {
  10. additionalProperties: false,
  11. default: {},
  12. properties: {
  13. ArrowFunctionExpression: {
  14. default: false,
  15. type: 'boolean'
  16. },
  17. ClassDeclaration: {
  18. default: false,
  19. type: 'boolean'
  20. },
  21. FunctionDeclaration: {
  22. default: true,
  23. type: 'boolean'
  24. },
  25. FunctionExpression: {
  26. default: false,
  27. type: 'boolean'
  28. },
  29. MethodDefinition: {
  30. default: false,
  31. type: 'boolean'
  32. }
  33. },
  34. type: 'object'
  35. }
  36. },
  37. type: 'object'
  38. };
  39. var getOption = function getOption(context, key) {
  40. if (!context.options.length) {
  41. return OPTIONS_SCHEMA.properties.require.properties[key].default;
  42. }
  43. if (!context.options[0] || !context.options[0].require) {
  44. return OPTIONS_SCHEMA.properties.require.properties[key].default;
  45. }
  46. if (!context.options[0].require.hasOwnProperty(key)) {
  47. return OPTIONS_SCHEMA.properties.require.properties[key].default;
  48. }
  49. return context.options[0].require[key];
  50. };
  51. var getOptions = function getOptions(context) {
  52. return {
  53. ArrowFunctionExpression: getOption(context, 'ArrowFunctionExpression'),
  54. ClassDeclaration: getOption(context, 'ClassDeclaration'),
  55. FunctionDeclaration: getOption(context, 'FunctionDeclaration'),
  56. FunctionExpression: getOption(context, 'FunctionExpression'),
  57. MethodDefinition: getOption(context, 'MethodDefinition')
  58. };
  59. };
  60. var checkJsDoc = function checkJsDoc(context, node) {
  61. var jsDocNode = context.getSourceCode().getJSDocComment(node);
  62. if (jsDocNode) {
  63. return;
  64. }
  65. context.report({
  66. messageId: 'missingJsDoc',
  67. node
  68. });
  69. };
  70. var _default = {
  71. /**
  72. * The entrypoint for the JSDoc rule.
  73. *
  74. * @param {*} context
  75. * a reference to the context which hold all important information
  76. * like settings and the sourcecode to check.
  77. * @returns {*}
  78. * a list with parser callback function.
  79. */
  80. create(context) {
  81. var options = getOptions(context);
  82. return {
  83. ArrowFunctionExpression: function ArrowFunctionExpression(node) {
  84. if (!options.ArrowFunctionExpression) {
  85. return;
  86. }
  87. if (node.parent.type !== 'VariableDeclarator') {
  88. return;
  89. }
  90. checkJsDoc(context, node);
  91. },
  92. ClassDeclaration: function ClassDeclaration(node) {
  93. if (!options.ClassDeclaration) {
  94. return;
  95. }
  96. checkJsDoc(context, node);
  97. },
  98. FunctionDeclaration: function FunctionDeclaration(node) {
  99. if (!options.FunctionDeclaration) {
  100. return;
  101. }
  102. checkJsDoc(context, node);
  103. },
  104. FunctionExpression: function FunctionExpression(node) {
  105. if (options.MethodDefinition && node.parent.type === 'MethodDefinition') {
  106. checkJsDoc(context, node);
  107. return;
  108. }
  109. if (!options.FunctionExpression) {
  110. return;
  111. }
  112. if (node.parent.type === 'VariableDeclarator') {
  113. checkJsDoc(context, node);
  114. }
  115. if (node.parent.type === 'Property' && node === node.parent.value) {
  116. checkJsDoc(context, node);
  117. }
  118. }
  119. };
  120. },
  121. meta: {
  122. doc: {
  123. category: 'Stylistic Issues',
  124. description: 'Require JSDoc comments',
  125. recommended: 'true',
  126. url: 'https://github.com/gajus/eslint-plugin-jsdoc'
  127. },
  128. messages: {
  129. missingJsDoc: 'Missing JSDoc comment.'
  130. },
  131. schema: [OPTIONS_SCHEMA],
  132. type: 'suggestion'
  133. }
  134. };
  135. exports.default = _default;
  136. module.exports = exports.default;
  137. //# sourceMappingURL=requireJsdoc.js.map