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.

requireParam.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _lodash = _interopRequireDefault(require("lodash"));
  7. var _iterateJsdoc = _interopRequireDefault(require("../iterateJsdoc"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. const rootNamer = (desiredRoots, currentIndex) => {
  10. let name;
  11. let idx = currentIndex;
  12. const incremented = desiredRoots.length <= 1;
  13. if (incremented) {
  14. const base = desiredRoots[0];
  15. const suffix = idx++;
  16. name = `${base}${suffix}`;
  17. } else {
  18. name = desiredRoots.shift();
  19. }
  20. return [name, incremented, () => {
  21. return rootNamer(desiredRoots, idx);
  22. }];
  23. };
  24. var _default = (0, _iterateJsdoc.default)(({
  25. jsdoc,
  26. utils,
  27. context
  28. }) => {
  29. const preferredTagName = utils.getPreferredTagName({
  30. tagName: 'param'
  31. });
  32. if (!preferredTagName) {
  33. return;
  34. }
  35. const jsdocParameterNames = utils.getJsdocTagsDeep(preferredTagName);
  36. const shallowJsdocParameterNames = jsdocParameterNames.filter(tag => {
  37. return !tag.name.includes('.');
  38. }).map((tag, idx) => {
  39. return { ...tag,
  40. idx
  41. };
  42. });
  43. if (utils.avoidDocs()) {
  44. return;
  45. } // Param type is specified by type in @type
  46. if (utils.hasTag('type')) {
  47. return;
  48. }
  49. const {
  50. autoIncrementBase = 0,
  51. checkRestProperty = false,
  52. checkDestructured = true,
  53. checkDestructuredRoots = true,
  54. checkTypesPattern = '/^(?:[oO]bject|[aA]rray|PlainObject|Generic(?:Object|Array))$/',
  55. enableFixer = true,
  56. enableRootFixer = true,
  57. enableRestElementFixer = true,
  58. unnamedRootBase = ['root'],
  59. useDefaultObjectProperties = false
  60. } = context.options[0] || {};
  61. const checkTypesRegex = utils.getRegexFromString(checkTypesPattern);
  62. const missingTags = [];
  63. const functionParameterNames = utils.getFunctionParameterNames(useDefaultObjectProperties);
  64. const flattenedRoots = utils.flattenRoots(functionParameterNames).names;
  65. const paramIndex = {};
  66. const hasParamIndex = cur => {
  67. return _lodash.default.has(paramIndex, utils.dropPathSegmentQuotes(String(cur)));
  68. };
  69. const getParamIndex = cur => {
  70. return paramIndex[utils.dropPathSegmentQuotes(String(cur))];
  71. };
  72. const setParamIndex = (cur, idx) => {
  73. paramIndex[utils.dropPathSegmentQuotes(String(cur))] = idx;
  74. };
  75. flattenedRoots.forEach((cur, idx) => {
  76. setParamIndex(cur, idx);
  77. });
  78. const findExpectedIndex = (jsdocTags, indexAtFunctionParams) => {
  79. const remainingRoots = functionParameterNames.slice(indexAtFunctionParams || 0);
  80. const foundIndex = jsdocTags.findIndex(({
  81. name,
  82. newAdd
  83. }) => {
  84. return !newAdd && remainingRoots.some(remainingRoot => {
  85. if (Array.isArray(remainingRoot)) {
  86. return remainingRoot[1].names.includes(name);
  87. }
  88. if (typeof remainingRoot === 'object') {
  89. return name === remainingRoot.name;
  90. }
  91. return name === remainingRoot;
  92. });
  93. });
  94. const tags = foundIndex > -1 ? jsdocTags.slice(0, foundIndex) : jsdocTags.filter(({
  95. tag
  96. }) => {
  97. return tag === preferredTagName;
  98. });
  99. let tagLineCount = 0;
  100. tags.forEach(({
  101. source
  102. }) => {
  103. source.forEach(({
  104. tokens: {
  105. end
  106. }
  107. }) => {
  108. if (!end) {
  109. tagLineCount++;
  110. }
  111. });
  112. });
  113. return tagLineCount;
  114. };
  115. let [nextRootName, incremented, namer] = rootNamer([...unnamedRootBase], autoIncrementBase);
  116. functionParameterNames.forEach((functionParameterName, functionParameterIdx) => {
  117. let inc;
  118. if (Array.isArray(functionParameterName)) {
  119. const matchedJsdoc = shallowJsdocParameterNames[functionParameterIdx] || jsdocParameterNames[functionParameterIdx];
  120. let rootName;
  121. if (functionParameterName[0]) {
  122. rootName = functionParameterName[0];
  123. } else if (matchedJsdoc && matchedJsdoc.name) {
  124. rootName = matchedJsdoc.name;
  125. if (matchedJsdoc.type && matchedJsdoc.type.search(checkTypesRegex) === -1) {
  126. return;
  127. }
  128. } else {
  129. rootName = nextRootName;
  130. inc = incremented;
  131. [nextRootName, incremented, namer] = namer();
  132. }
  133. const {
  134. hasRestElement,
  135. hasPropertyRest,
  136. rests,
  137. names
  138. } = functionParameterName[1];
  139. const notCheckingNames = [];
  140. if (!enableRestElementFixer && hasRestElement) {
  141. return;
  142. }
  143. if (!checkDestructuredRoots) {
  144. return;
  145. }
  146. names.forEach((paramName, idx) => {
  147. // Add root if the root name is not in the docs (and is not already
  148. // in the tags to be fixed)
  149. if (!jsdocParameterNames.find(({
  150. name
  151. }) => {
  152. return name === rootName;
  153. }) && !missingTags.find(({
  154. functionParameterName: fpn
  155. }) => {
  156. return fpn === rootName;
  157. })) {
  158. const emptyParamIdx = jsdocParameterNames.findIndex(({
  159. name
  160. }) => {
  161. return !name;
  162. });
  163. if (emptyParamIdx > -1) {
  164. missingTags.push({
  165. functionParameterIdx: emptyParamIdx,
  166. functionParameterName: rootName,
  167. inc,
  168. remove: true
  169. });
  170. } else {
  171. missingTags.push({
  172. functionParameterIdx: hasParamIndex(rootName) ? getParamIndex(rootName) : getParamIndex(paramName),
  173. functionParameterName: rootName,
  174. inc
  175. });
  176. }
  177. }
  178. if (!checkDestructured) {
  179. return;
  180. }
  181. if (!checkRestProperty && rests[idx]) {
  182. return;
  183. }
  184. const fullParamName = `${rootName}.${paramName}`;
  185. const notCheckingName = jsdocParameterNames.find(({
  186. name,
  187. type: paramType
  188. }) => {
  189. return utils.comparePaths(name)(fullParamName) && paramType.search(checkTypesRegex) === -1 && paramType !== '';
  190. });
  191. if (notCheckingName !== undefined) {
  192. notCheckingNames.push(notCheckingName.name);
  193. }
  194. if (notCheckingNames.find(name => {
  195. return fullParamName.startsWith(name);
  196. })) {
  197. return;
  198. }
  199. if (jsdocParameterNames && !jsdocParameterNames.find(({
  200. name
  201. }) => {
  202. return utils.comparePaths(name)(fullParamName);
  203. })) {
  204. missingTags.push({
  205. functionParameterIdx: getParamIndex(functionParameterName[0] ? fullParamName : paramName),
  206. functionParameterName: fullParamName,
  207. inc,
  208. type: hasRestElement && !hasPropertyRest ? '{...any}' : undefined
  209. });
  210. }
  211. });
  212. return;
  213. }
  214. let funcParamName;
  215. let type;
  216. if (typeof functionParameterName === 'object') {
  217. if (!enableRestElementFixer && functionParameterName.restElement) {
  218. return;
  219. }
  220. funcParamName = functionParameterName.name;
  221. type = '{...any}';
  222. } else {
  223. funcParamName = functionParameterName;
  224. }
  225. if (jsdocParameterNames && !jsdocParameterNames.find(({
  226. name
  227. }) => {
  228. return name === funcParamName;
  229. })) {
  230. missingTags.push({
  231. functionParameterIdx: getParamIndex(funcParamName),
  232. functionParameterName: funcParamName,
  233. inc,
  234. type
  235. });
  236. }
  237. });
  238. const fix = ({
  239. functionParameterIdx,
  240. functionParameterName,
  241. remove,
  242. inc,
  243. type
  244. }) => {
  245. if (inc && !enableRootFixer) {
  246. return;
  247. }
  248. const createTokens = (tagIndex, sourceIndex, spliceCount) => {
  249. // console.log(sourceIndex, tagIndex, jsdoc.tags, jsdoc.source);
  250. const tokens = {
  251. number: sourceIndex + 1,
  252. tokens: {
  253. delimiter: '*',
  254. description: '',
  255. end: '',
  256. lineEnd: '',
  257. name: functionParameterName,
  258. newAdd: true,
  259. postDelimiter: ' ',
  260. postName: '',
  261. postTag: ' ',
  262. postType: type ? ' ' : '',
  263. start: jsdoc.source[sourceIndex].tokens.start,
  264. tag: `@${preferredTagName}`,
  265. type: type !== null && type !== void 0 ? type : ''
  266. }
  267. };
  268. jsdoc.tags.splice(tagIndex, spliceCount, {
  269. name: functionParameterName,
  270. newAdd: true,
  271. source: [tokens],
  272. tag: preferredTagName,
  273. type: type !== null && type !== void 0 ? type : ''
  274. });
  275. const firstNumber = jsdoc.source[0].number;
  276. jsdoc.source.splice(sourceIndex, spliceCount, tokens);
  277. jsdoc.source.slice(sourceIndex).forEach((src, idx) => {
  278. src.number = firstNumber + sourceIndex + idx;
  279. });
  280. };
  281. const offset = jsdoc.source.findIndex(({
  282. tokens: {
  283. tag,
  284. end
  285. }
  286. }) => {
  287. return tag || end;
  288. });
  289. if (remove) {
  290. createTokens(functionParameterIdx, offset + functionParameterIdx, 1);
  291. } else {
  292. const expectedIdx = findExpectedIndex(jsdoc.tags, functionParameterIdx);
  293. createTokens(expectedIdx, offset + expectedIdx, 0);
  294. }
  295. };
  296. const fixer = () => {
  297. missingTags.forEach(missingTag => {
  298. fix(missingTag);
  299. });
  300. };
  301. if (missingTags.length && jsdoc.source.length === 1) {
  302. utils.makeMultiline();
  303. }
  304. missingTags.forEach(({
  305. functionParameterName
  306. }) => {
  307. utils.reportJSDoc(`Missing JSDoc @${preferredTagName} "${functionParameterName}" declaration.`, null, enableFixer ? fixer : null);
  308. });
  309. }, {
  310. contextDefaults: true,
  311. meta: {
  312. docs: {
  313. description: 'Requires that all function parameters are documented.',
  314. url: 'https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-param'
  315. },
  316. fixable: 'code',
  317. schema: [{
  318. additionalProperties: false,
  319. properties: {
  320. autoIncrementBase: {
  321. default: 0,
  322. type: 'integer'
  323. },
  324. checkConstructors: {
  325. default: true,
  326. type: 'boolean'
  327. },
  328. checkDestructured: {
  329. default: true,
  330. type: 'boolean'
  331. },
  332. checkDestructuredRoots: {
  333. default: true,
  334. type: 'boolean'
  335. },
  336. checkGetters: {
  337. default: false,
  338. type: 'boolean'
  339. },
  340. checkRestProperty: {
  341. default: false,
  342. type: 'boolean'
  343. },
  344. checkSetters: {
  345. default: false,
  346. type: 'boolean'
  347. },
  348. checkTypesPattern: {
  349. type: 'string'
  350. },
  351. contexts: {
  352. items: {
  353. anyOf: [{
  354. type: 'string'
  355. }, {
  356. additionalProperties: false,
  357. properties: {
  358. comment: {
  359. type: 'string'
  360. },
  361. context: {
  362. type: 'string'
  363. }
  364. },
  365. type: 'object'
  366. }]
  367. },
  368. type: 'array'
  369. },
  370. enableFixer: {
  371. type: 'boolean'
  372. },
  373. enableRestElementFixer: {
  374. type: 'boolean'
  375. },
  376. enableRootFixer: {
  377. type: 'boolean'
  378. },
  379. exemptedBy: {
  380. items: {
  381. type: 'string'
  382. },
  383. type: 'array'
  384. },
  385. unnamedRootBase: {
  386. items: {
  387. type: 'string'
  388. },
  389. type: 'array'
  390. },
  391. useDefaultObjectProperties: {
  392. type: 'boolean'
  393. }
  394. },
  395. type: 'object'
  396. }],
  397. type: 'suggestion'
  398. }
  399. });
  400. exports.default = _default;
  401. module.exports = exports.default;
  402. //# sourceMappingURL=requireParam.js.map