|
| 1 | +import { |
| 2 | + set, |
| 3 | +} from 'lodash'; |
| 4 | +import iterateJsdoc from '../iterateJsdoc'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Aux method until we consider the dev envs support `String.prototype.matchAll` (Node 12+). |
| 8 | + * |
| 9 | + * @param {string} string String that will be checked. |
| 10 | + * @param {RegExp} regexp Regular expression to run. |
| 11 | + * @param {Function} callback Function to be called each iteration. |
| 12 | + * @param {int} limit Limit of matches that we want to exec. |
| 13 | + * |
| 14 | + * @todo [engine:node@>=12]: Remove function and use `String.prototype.matchAll` instead. |
| 15 | + */ |
| 16 | +const matchAll = (string, regexp, callback, limit) => { |
| 17 | + let result; |
| 18 | + let index = 0; |
| 19 | + |
| 20 | + while ((result = regexp.exec(string)) && index <= limit - 1) { |
| 21 | + // eslint-disable-next-line promise/prefer-await-to-callbacks |
| 22 | + callback(result, index++); |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * Get the full description from a line. |
| 28 | + * |
| 29 | + * @param {string} lineString The line string. |
| 30 | + * |
| 31 | + * @returns {string} The full description. |
| 32 | + */ |
| 33 | +const getFullDescription = (lineString) => { |
| 34 | + return /(?:\S+\s+){4}(.*)/.exec(lineString)[1]; |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * Get the expected positions for each part. |
| 39 | + * |
| 40 | + * @param {int[]} partsMaxLength Max length of each part. |
| 41 | + * @param {int} indentLevel JSDoc indent level. |
| 42 | + * |
| 43 | + * @returns {int[]} Expected position for each part. |
| 44 | + */ |
| 45 | +const getExpectedPositions = (partsMaxLength, indentLevel) => { |
| 46 | + // eslint-disable-next-line unicorn/no-reduce |
| 47 | + return partsMaxLength.reduce( |
| 48 | + (acc, cur, index) => { |
| 49 | + return [...acc, cur + acc[index] + 1]; |
| 50 | + }, |
| 51 | + [indentLevel], |
| 52 | + ); |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * Check is not aligned. |
| 57 | + * |
| 58 | + * @param {int[]} expectedPositions Expected position for each part. |
| 59 | + * @param {Array[]} partsMatrix Parts matrix. |
| 60 | + * |
| 61 | + * @returns {boolean} |
| 62 | + */ |
| 63 | +const isNotAligned = (expectedPositions, partsMatrix) => { |
| 64 | + return partsMatrix.some((line) => { |
| 65 | + return line.some( |
| 66 | + ({position}, partIndex) => { |
| 67 | + return position !== expectedPositions[partIndex]; |
| 68 | + }, |
| 69 | + ); |
| 70 | + }); |
| 71 | +}; |
| 72 | + |
| 73 | +/** |
| 74 | + * Fix function creator for the report. It creates a function which fix |
| 75 | + * the JSDoc with the correct alignment. |
| 76 | + * |
| 77 | + * @param {object} comment Comment node. |
| 78 | + * @param {int[]} expectedPositions Array with the expected positions. |
| 79 | + * @param {Array[]} partsMatrix Parts matrix. |
| 80 | + * @param {RegExp} lineRegExp Line regular expression. |
| 81 | + * @param {string} tagIndentation Tag indentation. |
| 82 | + * |
| 83 | + * @returns {Function} Function which fixes the JSDoc alignment. |
| 84 | + */ |
| 85 | +const createFixer = (comment, expectedPositions, partsMatrix, lineRegExp, tagIndentation) => { |
| 86 | + return (fixer) => { |
| 87 | + let lineIndex = 0; |
| 88 | + |
| 89 | + // Replace every line with the correct spacings. |
| 90 | + const fixed = comment.value.replace(lineRegExp, () => { |
| 91 | + // eslint-disable-next-line unicorn/no-reduce |
| 92 | + return partsMatrix[lineIndex++].reduce( |
| 93 | + (acc, {string}, index) => { |
| 94 | + const spacings = ' '.repeat(expectedPositions[index] - acc.length); |
| 95 | + |
| 96 | + return acc + (index === 0 ? tagIndentation : spacings) + string; |
| 97 | + }, |
| 98 | + '', |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + return fixer.replaceText(comment, '/*' + fixed + '*/'); |
| 103 | + }; |
| 104 | +}; |
| 105 | + |
| 106 | +/** |
| 107 | + * Check comment per tag. |
| 108 | + * |
| 109 | + * @param {object} comment Comment node. |
| 110 | + * @param {string} tag Tag string. |
| 111 | + * @param {string} tagIndentation Tag indentation. |
| 112 | + * @param {Function} report Report function. |
| 113 | + */ |
| 114 | +const checkCommentPerTag = (comment, tag, tagIndentation, report) => { |
| 115 | + const lineRegExp = new RegExp(`.*@${tag}[\\s].*`, 'gm'); |
| 116 | + const lines = comment.value.match(lineRegExp); |
| 117 | + |
| 118 | + if (!lines) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * A matrix containing the current position and the string of each part for each line. |
| 124 | + * 0 - Asterisk. |
| 125 | + * 1 - Tag. |
| 126 | + * 2 - Type. |
| 127 | + * 3 - Variable name. |
| 128 | + * 4 - Description (Optional). |
| 129 | + */ |
| 130 | + const partsMatrix = []; |
| 131 | + |
| 132 | + /** |
| 133 | + * The max length of each part, comparing all the lines. |
| 134 | + */ |
| 135 | + const partsMaxLength = []; |
| 136 | + |
| 137 | + // Loop (lines x parts) to populate partsMatrix and partsMaxLength. |
| 138 | + lines.forEach((lineString, lineIndex) => { |
| 139 | + // All line parts until the first word of the description (if description exists). |
| 140 | + matchAll( |
| 141 | + lineString, |
| 142 | + /\S+/g, |
| 143 | + ({0: match, index: position}, partIndex) => { |
| 144 | + set(partsMatrix, [lineIndex, partIndex], { |
| 145 | + position, |
| 146 | + string: partIndex === 4 ? getFullDescription(lineString) : match, |
| 147 | + }); |
| 148 | + |
| 149 | + const partLength = match.length; |
| 150 | + const maxLength = partsMaxLength[partIndex]; |
| 151 | + |
| 152 | + partsMaxLength[partIndex] = maxLength > partLength ? maxLength : partLength; |
| 153 | + }, |
| 154 | + 5, |
| 155 | + ); |
| 156 | + }); |
| 157 | + |
| 158 | + const expectedPositions = getExpectedPositions(partsMaxLength, tagIndentation.length); |
| 159 | + |
| 160 | + if (isNotAligned(expectedPositions, partsMatrix)) { |
| 161 | + report( |
| 162 | + 'Expected JSDoc block lines to be aligned.', |
| 163 | + createFixer( |
| 164 | + comment, |
| 165 | + expectedPositions, |
| 166 | + partsMatrix, |
| 167 | + lineRegExp, |
| 168 | + tagIndentation, |
| 169 | + ), |
| 170 | + ); |
| 171 | + } |
| 172 | +}; |
| 173 | + |
| 174 | +export default iterateJsdoc(({ |
| 175 | + jsdocNode, |
| 176 | + report, |
| 177 | + context, |
| 178 | + indent, |
| 179 | +}) => { |
| 180 | + if (context.options[0] === 'never') { |
| 181 | + report('The `never` option is not yet implemented for this rule.'); |
| 182 | + |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + if (context.options[0] !== 'always') { |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + // `indent` is whitespace from line 1 (`/**`), so slice and account for "/". |
| 191 | + const tagIndentation = indent + ' '; |
| 192 | + |
| 193 | + ['param', 'arg', 'argument', 'property', 'prop'].forEach((tag) => { |
| 194 | + checkCommentPerTag(jsdocNode, tag, tagIndentation, report); |
| 195 | + }); |
| 196 | +}, { |
| 197 | + iterateAllJsdocs: true, |
| 198 | + meta: { |
| 199 | + docs: { |
| 200 | + description: 'Reports invalid alignment of JSDoc block lines.', |
| 201 | + url: 'https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-check-lines-alignment', |
| 202 | + }, |
| 203 | + fixable: 'whitespace', |
| 204 | + schema: [ |
| 205 | + { |
| 206 | + enum: ['always', 'never'], |
| 207 | + type: 'string', |
| 208 | + }, |
| 209 | + ], |
| 210 | + type: 'layout', |
| 211 | + }, |
| 212 | +}); |
0 commit comments