checkIndentation.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /**
  9. * @param {string} str
  10. * @param {string[]} excludeTags
  11. * @returns {string}
  12. */
  13. const maskExcludedContent = (str, excludeTags) => {
  14. const regContent = new RegExp(`([ \\t]+\\*)[ \\t]@(?:${excludeTags.join('|')})(?=[ \\n])([\\w|\\W]*?\\n)(?=[ \\t]*\\*(?:[ \\t]*@\\w+\\s|\\/))`, 'gu');
  15. return str.replace(regContent, (_match, margin, code) => {
  16. return (margin + '\n').repeat(code.match(/\n/gu).length);
  17. });
  18. };
  19. /**
  20. * @param {string} str
  21. * @returns {string}
  22. */
  23. const maskCodeBlocks = str => {
  24. const regContent = /([ \t]+\*)[ \t]```[^\n]*?([\w|\W]*?\n)(?=[ \t]*\*(?:[ \t]*(?:```|@\w+\s)|\/))/gu;
  25. return str.replaceAll(regContent, (_match, margin, code) => {
  26. return (margin + '\n').repeat(code.match(/\n/gu).length);
  27. });
  28. };
  29. var _default = exports.default = (0, _iterateJsdoc.default)(({
  30. sourceCode,
  31. jsdocNode,
  32. report,
  33. context
  34. }) => {
  35. const options = context.options[0] || {};
  36. const /** @type {{excludeTags: string[]}} */{
  37. excludeTags = ['example']
  38. } = options;
  39. const reg = /^(?:\/?\**|[ \t]*)\*[ \t]{2}/gmu;
  40. const textWithoutCodeBlocks = maskCodeBlocks(sourceCode.getText(jsdocNode));
  41. const text = excludeTags.length ? maskExcludedContent(textWithoutCodeBlocks, excludeTags) : textWithoutCodeBlocks;
  42. if (reg.test(text)) {
  43. const lineBreaks = text.slice(0, reg.lastIndex).match(/\n/gu) || [];
  44. report('There must be no indentation.', null, {
  45. line: lineBreaks.length
  46. });
  47. }
  48. }, {
  49. iterateAllJsdocs: true,
  50. meta: {
  51. docs: {
  52. description: 'Reports invalid padding inside JSDoc blocks.',
  53. url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/check-indentation.md#repos-sticky-header'
  54. },
  55. schema: [{
  56. additionalProperties: false,
  57. properties: {
  58. excludeTags: {
  59. items: {
  60. pattern: '^\\S+$',
  61. type: 'string'
  62. },
  63. type: 'array'
  64. }
  65. },
  66. type: 'object'
  67. }],
  68. type: 'layout'
  69. }
  70. });
  71. module.exports = exports.default;
  72. //# sourceMappingURL=checkIndentation.js.map