textEscaping.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // We could disallow raw gt, quot, and apos, but allow for parity; but we do
  9. // not allow hex or decimal character references
  10. const htmlRegex = /(<|&(?!(?:amp|lt|gt|quot|apos);))(?=\S)/u;
  11. const markdownRegex = /(?<!\\)(`+)([^`]+)\1(?!`)/u;
  12. /**
  13. * @param {string} desc
  14. * @returns {string}
  15. */
  16. const htmlReplacer = desc => {
  17. return desc.replaceAll(new RegExp(htmlRegex, 'gu'), _ => {
  18. if (_ === '<') {
  19. return '&lt;';
  20. }
  21. return '&amp;';
  22. });
  23. };
  24. /**
  25. * @param {string} desc
  26. * @returns {string}
  27. */
  28. const markdownReplacer = desc => {
  29. return desc.replaceAll(new RegExp(markdownRegex, 'gu'), (_, backticks, encapsed) => {
  30. const bookend = '`'.repeat(backticks.length);
  31. return `\\${bookend}${encapsed}${bookend}`;
  32. });
  33. };
  34. var _default = exports.default = (0, _iterateJsdoc.default)(({
  35. context,
  36. jsdoc,
  37. utils
  38. }) => {
  39. const {
  40. escapeHTML,
  41. escapeMarkdown
  42. } = context.options[0] || {};
  43. if (!escapeHTML && !escapeMarkdown) {
  44. context.report({
  45. loc: {
  46. end: {
  47. column: 1,
  48. line: 1
  49. },
  50. start: {
  51. column: 1,
  52. line: 1
  53. }
  54. },
  55. message: 'You must include either `escapeHTML` or `escapeMarkdown`'
  56. });
  57. return;
  58. }
  59. const {
  60. descriptions
  61. } = utils.getDescription();
  62. if (escapeHTML) {
  63. if (descriptions.some(desc => {
  64. return htmlRegex.test(desc);
  65. })) {
  66. const line = utils.setDescriptionLines(htmlRegex, htmlReplacer);
  67. utils.reportJSDoc('You have unescaped HTML characters < or &', {
  68. line
  69. }, () => {}, true);
  70. return;
  71. }
  72. for (const tag of jsdoc.tags) {
  73. if ( /** @type {string[]} */utils.getTagDescription(tag, true).some(desc => {
  74. return htmlRegex.test(desc);
  75. })) {
  76. const line = utils.setTagDescription(tag, htmlRegex, htmlReplacer) + tag.source[0].number;
  77. utils.reportJSDoc('You have unescaped HTML characters < or & in a tag', {
  78. line
  79. }, () => {}, true);
  80. }
  81. }
  82. return;
  83. }
  84. if (descriptions.some(desc => {
  85. return markdownRegex.test(desc);
  86. })) {
  87. const line = utils.setDescriptionLines(markdownRegex, markdownReplacer);
  88. utils.reportJSDoc('You have unescaped Markdown backtick sequences', {
  89. line
  90. }, () => {}, true);
  91. return;
  92. }
  93. for (const tag of jsdoc.tags) {
  94. if ( /** @type {string[]} */utils.getTagDescription(tag, true).some(desc => {
  95. return markdownRegex.test(desc);
  96. })) {
  97. const line = utils.setTagDescription(tag, markdownRegex, markdownReplacer) + tag.source[0].number;
  98. utils.reportJSDoc('You have unescaped Markdown backtick sequences in a tag', {
  99. line
  100. }, () => {}, true);
  101. }
  102. }
  103. }, {
  104. iterateAllJsdocs: true,
  105. meta: {
  106. docs: {
  107. description: '',
  108. url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/text-escaping.md#repos-sticky-header'
  109. },
  110. fixable: 'code',
  111. schema: [{
  112. additionalProperties: false,
  113. properties: {
  114. // Option properties here (or remove the object)
  115. escapeHTML: {
  116. type: 'boolean'
  117. },
  118. escapeMarkdown: {
  119. type: 'boolean'
  120. }
  121. },
  122. type: 'object'
  123. }],
  124. type: 'suggestion'
  125. }
  126. });
  127. module.exports = exports.default;
  128. //# sourceMappingURL=textEscaping.js.map