checkAlignment.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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} string
  10. * @returns {string}
  11. */
  12. const trimStart = string => {
  13. return string.replace(/^\s+/u, '');
  14. };
  15. var _default = exports.default = (0, _iterateJsdoc.default)(({
  16. sourceCode,
  17. jsdocNode,
  18. report,
  19. indent
  20. }) => {
  21. // `indent` is whitespace from line 1 (`/**`), so slice and account for "/".
  22. const indentLevel = indent.length + 1;
  23. const sourceLines = sourceCode.getText(jsdocNode).split('\n').slice(1).map(line => {
  24. return line.split('*')[0];
  25. }).filter(line => {
  26. return !trimStart(line).length;
  27. });
  28. /** @type {import('eslint').Rule.ReportFixer} */
  29. const fix = fixer => {
  30. const replacement = sourceCode.getText(jsdocNode).split('\n').map((line, index) => {
  31. // Ignore the first line and all lines not starting with `*`
  32. const ignored = !index || trimStart(line.split('*')[0]).length;
  33. return ignored ? line : `${indent} ${trimStart(line)}`;
  34. }).join('\n');
  35. return fixer.replaceText(jsdocNode, replacement);
  36. };
  37. sourceLines.some((line, lineNum) => {
  38. if (line.length !== indentLevel) {
  39. report('Expected JSDoc block to be aligned.', fix, {
  40. line: lineNum + 1
  41. });
  42. return true;
  43. }
  44. return false;
  45. });
  46. }, {
  47. iterateAllJsdocs: true,
  48. meta: {
  49. docs: {
  50. description: 'Reports invalid alignment of JSDoc block asterisks.',
  51. url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/check-alignment.md#repos-sticky-header'
  52. },
  53. fixable: 'code',
  54. type: 'layout'
  55. }
  56. });
  57. module.exports = exports.default;
  58. //# sourceMappingURL=checkAlignment.js.map