Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

checkParamNames.js 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _iterateJsdoc = _interopRequireDefault(require("../iterateJsdoc"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. const validateParameterNames = (targetTagName, allowExtraTrailingParamDocs, checkDestructured, checkRestProperty, checkTypesRegex, disableExtraPropertyReporting, enableFixer, functionParameterNames, jsdoc, _jsdocNode, utils, report) => {
  9. const paramTags = Object.entries(jsdoc.tags).filter(([, tag]) => {
  10. return tag.tag === targetTagName;
  11. });
  12. const paramTagsNonNested = paramTags.filter(([, tag]) => {
  13. return !tag.name.includes('.');
  14. });
  15. let dotted = 0;
  16. return paramTags.some(([, tag], index) => {
  17. let tagsIndex;
  18. const dupeTagInfo = paramTags.find(([tgsIndex, tg], idx) => {
  19. tagsIndex = tgsIndex;
  20. return tg.name === tag.name && idx !== index;
  21. });
  22. if (dupeTagInfo) {
  23. utils.reportJSDoc(`Duplicate @${targetTagName} "${tag.name}"`, dupeTagInfo[1], enableFixer ? () => {
  24. utils.removeTag(tagsIndex);
  25. } : null);
  26. return true;
  27. }
  28. if (tag.name.includes('.')) {
  29. dotted++;
  30. return false;
  31. }
  32. const functionParameterName = functionParameterNames[index - dotted];
  33. if (!functionParameterName) {
  34. if (allowExtraTrailingParamDocs) {
  35. return false;
  36. }
  37. report(`@${targetTagName} "${tag.name}" does not match an existing function parameter.`, null, tag);
  38. return true;
  39. }
  40. if (Array.isArray(functionParameterName)) {
  41. if (!checkDestructured) {
  42. return false;
  43. }
  44. if (tag.type && tag.type.search(checkTypesRegex) === -1) {
  45. return false;
  46. }
  47. const [parameterName, {
  48. names: properties,
  49. hasPropertyRest,
  50. rests,
  51. annotationParamName
  52. }] = functionParameterName;
  53. if (annotationParamName !== undefined) {
  54. const name = tag.name.trim();
  55. if (name !== annotationParamName) {
  56. report(`@${targetTagName} "${name}" does not match parameter name "${annotationParamName}"`, null, tag);
  57. }
  58. }
  59. const tagName = parameterName === undefined ? tag.name.trim() : parameterName;
  60. const expectedNames = properties.map(name => {
  61. return `${tagName}.${name}`;
  62. });
  63. const actualNames = paramTags.map(([, paramTag]) => {
  64. return paramTag.name.trim();
  65. });
  66. const actualTypes = paramTags.map(([, paramTag]) => {
  67. return paramTag.type;
  68. });
  69. const missingProperties = [];
  70. const notCheckingNames = [];
  71. expectedNames.forEach((name, idx) => {
  72. if (notCheckingNames.some(notCheckingName => {
  73. return name.startsWith(notCheckingName);
  74. })) {
  75. return;
  76. }
  77. const actualNameIdx = actualNames.findIndex(actualName => {
  78. return utils.comparePaths(name)(actualName);
  79. });
  80. if (actualNameIdx === -1) {
  81. if (!checkRestProperty && rests[idx]) {
  82. return;
  83. }
  84. const missingIndex = actualNames.findIndex(actualName => {
  85. return utils.pathDoesNotBeginWith(name, actualName);
  86. });
  87. const line = tag.source[0].number - 1 + (missingIndex > -1 ? missingIndex : actualNames.length);
  88. missingProperties.push({
  89. name,
  90. tagPlacement: {
  91. line: line === 0 ? 1 : line
  92. }
  93. });
  94. } else if (actualTypes[actualNameIdx].search(checkTypesRegex) === -1 && actualTypes[actualNameIdx] !== '') {
  95. notCheckingNames.push(name);
  96. }
  97. });
  98. const hasMissing = missingProperties.length;
  99. if (hasMissing) {
  100. missingProperties.forEach(({
  101. tagPlacement,
  102. name: missingProperty
  103. }) => {
  104. report(`Missing @${targetTagName} "${missingProperty}"`, null, tagPlacement);
  105. });
  106. }
  107. if (!hasPropertyRest || checkRestProperty) {
  108. const extraProperties = [];
  109. actualNames.forEach((name, idx) => {
  110. const match = name.startsWith(tag.name.trim() + '.');
  111. if (match && !expectedNames.some(utils.comparePaths(name)) && !utils.comparePaths(name)(tag.name) && (!disableExtraPropertyReporting || properties.some(prop => {
  112. return prop.split('.').length >= name.split('.').length - 1;
  113. }))) {
  114. extraProperties.push([name, paramTags[idx][1]]);
  115. }
  116. });
  117. if (extraProperties.length) {
  118. extraProperties.forEach(([extraProperty, tg]) => {
  119. report(`@${targetTagName} "${extraProperty}" does not exist on ${tag.name}`, null, tg);
  120. });
  121. return true;
  122. }
  123. }
  124. return hasMissing;
  125. }
  126. let funcParamName;
  127. if (typeof functionParameterName === 'object') {
  128. const {
  129. name
  130. } = functionParameterName;
  131. funcParamName = name;
  132. } else {
  133. funcParamName = functionParameterName;
  134. }
  135. if (funcParamName !== tag.name.trim()) {
  136. // Todo: Improve for array or object child items
  137. const actualNames = paramTagsNonNested.map(([, {
  138. name
  139. }]) => {
  140. return name.trim();
  141. });
  142. const expectedNames = functionParameterNames.map((item, idx) => {
  143. var _item$;
  144. if (item !== null && item !== void 0 && (_item$ = item[1]) !== null && _item$ !== void 0 && _item$.names) {
  145. return actualNames[idx];
  146. }
  147. return item;
  148. }).join(', ');
  149. report(`Expected @${targetTagName} names to be "${expectedNames}". Got "${actualNames.join(', ')}".`, null, tag);
  150. return true;
  151. }
  152. return false;
  153. });
  154. };
  155. const validateParameterNamesDeep = (targetTagName, _allowExtraTrailingParamDocs, jsdocParameterNames, jsdoc, report) => {
  156. let lastRealParameter;
  157. return jsdocParameterNames.some(({
  158. name: jsdocParameterName,
  159. idx
  160. }) => {
  161. const isPropertyPath = jsdocParameterName.includes('.');
  162. if (isPropertyPath) {
  163. if (!lastRealParameter) {
  164. report(`@${targetTagName} path declaration ("${jsdocParameterName}") appears before any real parameter.`, null, jsdoc.tags[idx]);
  165. return true;
  166. }
  167. let pathRootNodeName = jsdocParameterName.slice(0, jsdocParameterName.indexOf('.'));
  168. if (pathRootNodeName.endsWith('[]')) {
  169. pathRootNodeName = pathRootNodeName.slice(0, -2);
  170. }
  171. if (pathRootNodeName !== lastRealParameter) {
  172. report(`@${targetTagName} path declaration ("${jsdocParameterName}") root node name ("${pathRootNodeName}") ` + `does not match previous real parameter name ("${lastRealParameter}").`, null, jsdoc.tags[idx]);
  173. return true;
  174. }
  175. } else {
  176. lastRealParameter = jsdocParameterName;
  177. }
  178. return false;
  179. });
  180. };
  181. var _default = (0, _iterateJsdoc.default)(({
  182. context,
  183. jsdoc,
  184. jsdocNode,
  185. report,
  186. utils
  187. }) => {
  188. const {
  189. allowExtraTrailingParamDocs,
  190. checkDestructured = true,
  191. checkRestProperty = false,
  192. checkTypesPattern = '/^(?:[oO]bject|[aA]rray|PlainObject|Generic(?:Object|Array))$/',
  193. enableFixer = false,
  194. useDefaultObjectProperties = false,
  195. disableExtraPropertyReporting = false
  196. } = context.options[0] || {};
  197. const checkTypesRegex = utils.getRegexFromString(checkTypesPattern);
  198. const jsdocParameterNamesDeep = utils.getJsdocTagsDeep('param');
  199. if (!jsdocParameterNamesDeep.length) {
  200. return;
  201. }
  202. const functionParameterNames = utils.getFunctionParameterNames(useDefaultObjectProperties);
  203. const targetTagName = utils.getPreferredTagName({
  204. tagName: 'param'
  205. });
  206. const isError = validateParameterNames(targetTagName, allowExtraTrailingParamDocs, checkDestructured, checkRestProperty, checkTypesRegex, disableExtraPropertyReporting, enableFixer, functionParameterNames, jsdoc, jsdocNode, utils, report);
  207. if (isError || !checkDestructured) {
  208. return;
  209. }
  210. validateParameterNamesDeep(targetTagName, allowExtraTrailingParamDocs, jsdocParameterNamesDeep, jsdoc, report);
  211. }, {
  212. meta: {
  213. docs: {
  214. description: 'Ensures that parameter names in JSDoc match those in the function declaration.',
  215. url: 'https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-check-param-names'
  216. },
  217. fixable: 'code',
  218. schema: [{
  219. additionalProperties: false,
  220. properties: {
  221. allowExtraTrailingParamDocs: {
  222. type: 'boolean'
  223. },
  224. checkDestructured: {
  225. type: 'boolean'
  226. },
  227. checkRestProperty: {
  228. type: 'boolean'
  229. },
  230. checkTypesPattern: {
  231. type: 'string'
  232. },
  233. disableExtraPropertyReporting: {
  234. type: 'boolean'
  235. },
  236. enableFixer: {
  237. type: 'boolean'
  238. },
  239. useDefaultObjectProperties: {
  240. type: 'boolean'
  241. }
  242. },
  243. type: 'object'
  244. }],
  245. type: 'suggestion'
  246. }
  247. });
  248. exports.default = _default;
  249. module.exports = exports.default;
  250. //# sourceMappingURL=checkParamNames.js.map