matchName.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _iterateJsdoc = _interopRequireDefault(require("../iterateJsdoc.js"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. // eslint-disable-next-line complexity
  9. var _default = exports.default = (0, _iterateJsdoc.default)(({
  10. context,
  11. jsdoc,
  12. report,
  13. info: {
  14. lastIndex
  15. },
  16. utils
  17. }) => {
  18. const {
  19. match
  20. } = context.options[0] || {};
  21. if (!match) {
  22. report('Rule `no-restricted-syntax` is missing a `match` option.');
  23. return;
  24. }
  25. const {
  26. allowName,
  27. disallowName,
  28. replacement,
  29. tags = ['*']
  30. } = match[( /** @type {import('../iterateJsdoc.js').Integer} */lastIndex)];
  31. const allowNameRegex = allowName && utils.getRegexFromString(allowName);
  32. const disallowNameRegex = disallowName && utils.getRegexFromString(disallowName);
  33. let applicableTags = jsdoc.tags;
  34. if (!tags.includes('*')) {
  35. applicableTags = utils.getPresentTags(tags);
  36. }
  37. let reported = false;
  38. for (const tag of applicableTags) {
  39. const allowed = !allowNameRegex || allowNameRegex.test(tag.name);
  40. const disallowed = disallowNameRegex && disallowNameRegex.test(tag.name);
  41. const hasRegex = allowNameRegex || disallowNameRegex;
  42. if (hasRegex && allowed && !disallowed) {
  43. continue;
  44. }
  45. if (!hasRegex && reported) {
  46. continue;
  47. }
  48. const fixer = () => {
  49. for (const src of tag.source) {
  50. if (src.tokens.name) {
  51. src.tokens.name = src.tokens.name.replace(disallowNameRegex, replacement);
  52. break;
  53. }
  54. }
  55. };
  56. let {
  57. message
  58. } = match[( /** @type {import('../iterateJsdoc.js').Integer} */lastIndex)];
  59. if (!message) {
  60. if (hasRegex) {
  61. message = disallowed ? `Only allowing names not matching \`${disallowNameRegex}\` but found "${tag.name}".` : `Only allowing names matching \`${allowNameRegex}\` but found "${tag.name}".`;
  62. } else {
  63. message = `Prohibited context for "${tag.name}".`;
  64. }
  65. }
  66. utils.reportJSDoc(message, hasRegex ? tag : null,
  67. // We could match up
  68. disallowNameRegex && replacement !== undefined ? fixer : null, false, {
  69. // Could also supply `context`, `comment`, `tags`
  70. allowName,
  71. disallowName,
  72. name: tag.name
  73. });
  74. if (!hasRegex) {
  75. reported = true;
  76. }
  77. }
  78. }, {
  79. matchContext: true,
  80. meta: {
  81. docs: {
  82. description: 'Reports the name portion of a JSDoc tag if matching or not matching a given regular expression.',
  83. url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/match-name.md#repos-sticky-header'
  84. },
  85. fixable: 'code',
  86. schema: [{
  87. additionalProperties: false,
  88. properties: {
  89. match: {
  90. additionalProperties: false,
  91. items: {
  92. properties: {
  93. allowName: {
  94. type: 'string'
  95. },
  96. comment: {
  97. type: 'string'
  98. },
  99. context: {
  100. type: 'string'
  101. },
  102. disallowName: {
  103. type: 'string'
  104. },
  105. message: {
  106. type: 'string'
  107. },
  108. tags: {
  109. items: {
  110. type: 'string'
  111. },
  112. type: 'array'
  113. }
  114. },
  115. type: 'object'
  116. },
  117. type: 'array'
  118. }
  119. },
  120. required: ['match'],
  121. type: 'object'
  122. }],
  123. type: 'suggestion'
  124. }
  125. });
  126. module.exports = exports.default;
  127. //# sourceMappingURL=matchName.js.map