iterateJsdoc.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.parseComment = void 0;
  6. var _lodash = _interopRequireDefault(require("lodash"));
  7. var _commentParser = _interopRequireDefault(require("comment-parser"));
  8. var _jsdocUtils = _interopRequireDefault(require("./jsdocUtils"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. var parseComment = function parseComment(commentNode, indent) {
  11. // Preserve JSDoc block start/end indentation.
  12. return (0, _commentParser.default)(indent + '/*' + commentNode.value + indent + '*/', {
  13. // @see https://github.com/yavorskiy/comment-parser/issues/21
  14. parsers: [_commentParser.default.PARSERS.parse_tag, _commentParser.default.PARSERS.parse_type, function (str, data) {
  15. if (_lodash.default.includes(['return', 'returns', 'throws', 'exception'], data.tag)) {
  16. return null;
  17. }
  18. return _commentParser.default.PARSERS.parse_name(str, data);
  19. }, _commentParser.default.PARSERS.parse_description]
  20. })[0] || {};
  21. };
  22. exports.parseComment = parseComment;
  23. var curryUtils = function curryUtils(functionNode, jsdoc, tagNamePreference, exampleCodeRegex, rejectExampleCodeRegex, additionalTagNames, baseConfig, configFile, captionRequired, matchingFileName, eslintrcForExamples, allowInlineConfig, allowEmptyNamepaths, reportUnusedDisableDirectives, noDefaultExampleRules, allowOverrideWithoutParam, allowImplementsWithoutParam, allowAugmentsExtendsWithoutParam, checkSeesForNamepaths, ancestors, sourceCode) {
  24. var utils = {};
  25. utils.getFunctionParameterNames = function () {
  26. return _jsdocUtils.default.getFunctionParameterNames(functionNode);
  27. };
  28. utils.getFunctionSourceCode = function () {
  29. return sourceCode.getText(functionNode);
  30. };
  31. utils.isConstructor = function () {
  32. return functionNode.parent && functionNode.parent.kind === 'constructor';
  33. };
  34. utils.getJsdocParameterNamesDeep = function () {
  35. return _jsdocUtils.default.getJsdocParameterNamesDeep(jsdoc, utils.getPreferredTagName('param'));
  36. };
  37. utils.getJsdocParameterNames = function () {
  38. return _jsdocUtils.default.getJsdocParameterNames(jsdoc, utils.getPreferredTagName('param'));
  39. };
  40. utils.getPreferredTagName = function (name) {
  41. return _jsdocUtils.default.getPreferredTagName(name, tagNamePreference);
  42. };
  43. utils.getExampleCodeRegex = function () {
  44. return exampleCodeRegex;
  45. };
  46. utils.getRejectExampleCodeRegex = function () {
  47. return rejectExampleCodeRegex;
  48. };
  49. utils.getMatchingFileName = function () {
  50. return matchingFileName;
  51. };
  52. utils.isValidTag = function (name) {
  53. return _jsdocUtils.default.isValidTag(name, additionalTagNames);
  54. };
  55. utils.hasATag = function (name) {
  56. return _jsdocUtils.default.hasATag(jsdoc, name);
  57. };
  58. utils.hasTag = function (name) {
  59. return _jsdocUtils.default.hasTag(jsdoc, name);
  60. };
  61. utils.useEslintrcForExamples = function () {
  62. return eslintrcForExamples;
  63. };
  64. utils.allowInlineConfig = function () {
  65. return allowInlineConfig;
  66. };
  67. utils.reportUnusedDisableDirectives = function () {
  68. return reportUnusedDisableDirectives;
  69. };
  70. utils.hasNoDefaultExampleRules = function () {
  71. return noDefaultExampleRules;
  72. };
  73. utils.getBaseConfig = function () {
  74. return baseConfig;
  75. };
  76. utils.getConfigFile = function () {
  77. return configFile;
  78. };
  79. utils.isCaptionRequired = function () {
  80. return captionRequired;
  81. };
  82. utils.isOverrideAllowedWithoutParam = function () {
  83. return allowOverrideWithoutParam;
  84. };
  85. utils.isImplementsAllowedWithoutParam = function () {
  86. return allowImplementsWithoutParam;
  87. };
  88. utils.isAugmentsExtendsAllowedWithoutParam = function () {
  89. return allowAugmentsExtendsWithoutParam;
  90. };
  91. utils.isNamepathType = function (tagName) {
  92. return _jsdocUtils.default.isNamepathType(tagName, checkSeesForNamepaths);
  93. };
  94. utils.passesEmptyNamepathCheck = function (tag) {
  95. return !tag.name && allowEmptyNamepaths && _lodash.default.includes([// These may serve some minor purpose when empty
  96. 'callback', 'event', 'listens', 'fires', 'emits'], tag.tag);
  97. };
  98. utils.getClassJsdocNode = function () {
  99. var greatGrandParent = ancestors.slice(-3)[0];
  100. var greatGrandParentValue = greatGrandParent && sourceCode.getFirstToken(greatGrandParent).value;
  101. if (greatGrandParentValue === 'class') {
  102. var classJsdocNode = sourceCode.getJSDocComment(greatGrandParent);
  103. return classJsdocNode;
  104. }
  105. return false;
  106. };
  107. utils.classHasTag = function (tagName) {
  108. var classJsdocNode = utils.getClassJsdocNode();
  109. if (classJsdocNode) {
  110. var indent = _lodash.default.repeat(' ', classJsdocNode.loc.start.column);
  111. var classJsdoc = parseComment(classJsdocNode, indent);
  112. if (_jsdocUtils.default.hasTag(classJsdoc, tagName)) {
  113. return true;
  114. }
  115. }
  116. return false;
  117. };
  118. return utils;
  119. };
  120. var _default = function _default(iterator) {
  121. return function (context) {
  122. var sourceCode = context.getSourceCode();
  123. var tagNamePreference = _lodash.default.get(context, 'settings.jsdoc.tagNamePreference') || {};
  124. var exampleCodeRegex = _lodash.default.get(context, 'settings.jsdoc.exampleCodeRegex') || null;
  125. var rejectExampleCodeRegex = _lodash.default.get(context, 'settings.jsdoc.rejectExampleCodeRegex') || null;
  126. var matchingFileName = _lodash.default.get(context, 'settings.jsdoc.matchingFileName') || null;
  127. var additionalTagNames = _lodash.default.get(context, 'settings.jsdoc.additionalTagNames') || {};
  128. var baseConfig = _lodash.default.get(context, 'settings.jsdoc.baseConfig') || {};
  129. var configFile = _lodash.default.get(context, 'settings.jsdoc.configFile');
  130. var eslintrcForExamples = _lodash.default.get(context, 'settings.jsdoc.eslintrcForExamples') !== false;
  131. var allowInlineConfig = _lodash.default.get(context, 'settings.jsdoc.allowInlineConfig') !== false;
  132. var allowEmptyNamepaths = _lodash.default.get(context, 'settings.jsdoc.allowEmptyNamepaths') !== false;
  133. var reportUnusedDisableDirectives = _lodash.default.get(context, 'settings.jsdoc.reportUnusedDisableDirectives') !== false;
  134. var captionRequired = Boolean(_lodash.default.get(context, 'settings.jsdoc.captionRequired'));
  135. var noDefaultExampleRules = Boolean(_lodash.default.get(context, 'settings.jsdoc.noDefaultExampleRules'));
  136. var allowOverrideWithoutParam = Boolean(_lodash.default.get(context, 'settings.jsdoc.allowOverrideWithoutParam'));
  137. var allowImplementsWithoutParam = Boolean(_lodash.default.get(context, 'settings.jsdoc.allowImplementsWithoutParam'));
  138. var allowAugmentsExtendsWithoutParam = Boolean(_lodash.default.get(context, 'settings.jsdoc.allowAugmentsExtendsWithoutParam'));
  139. var checkSeesForNamepaths = Boolean(_lodash.default.get(context, 'settings.jsdoc.checkSeesForNamepaths'));
  140. var checkJsdoc = function checkJsdoc(functionNode) {
  141. var jsdocNode = sourceCode.getJSDocComment(functionNode);
  142. if (!jsdocNode) {
  143. return;
  144. }
  145. var ancestors = context.getAncestors();
  146. var indent = _lodash.default.repeat(' ', jsdocNode.loc.start.column);
  147. var jsdoc = parseComment(jsdocNode, indent);
  148. var report = function report(message) {
  149. var fixer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  150. var jsdocLoc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  151. var loc;
  152. if (jsdocLoc) {
  153. var lineNumber = jsdocNode.loc.start.line + jsdocLoc.line;
  154. loc = {
  155. end: {
  156. line: lineNumber
  157. },
  158. start: {
  159. line: lineNumber
  160. }
  161. };
  162. if (jsdocLoc.column) {
  163. var colNumber = jsdocNode.loc.start.column + jsdocLoc.column;
  164. loc.end.column = colNumber;
  165. loc.start.column = colNumber;
  166. }
  167. }
  168. if (fixer === null) {
  169. context.report({
  170. loc,
  171. message,
  172. node: jsdocNode
  173. });
  174. } else {
  175. context.report({
  176. fix: fixer,
  177. loc,
  178. message,
  179. node: jsdocNode
  180. });
  181. }
  182. };
  183. var utils = curryUtils(functionNode, jsdoc, tagNamePreference, exampleCodeRegex, rejectExampleCodeRegex, additionalTagNames, baseConfig, configFile, captionRequired, matchingFileName, eslintrcForExamples, allowInlineConfig, allowEmptyNamepaths, reportUnusedDisableDirectives, noDefaultExampleRules, allowOverrideWithoutParam, allowImplementsWithoutParam, allowAugmentsExtendsWithoutParam, checkSeesForNamepaths, ancestors, sourceCode);
  184. iterator({
  185. context,
  186. functionNode,
  187. indent,
  188. jsdoc,
  189. jsdocNode,
  190. report,
  191. sourceCode,
  192. utils
  193. });
  194. };
  195. return {
  196. ArrowFunctionExpression: checkJsdoc,
  197. FunctionDeclaration: checkJsdoc,
  198. FunctionExpression: checkJsdoc
  199. };
  200. };
  201. };
  202. exports.default = _default;
  203. //# sourceMappingURL=iterateJsdoc.js.map