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.

jsdocUtils.js 38KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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 _WarnSettings = _interopRequireDefault(require("./WarnSettings"));
  8. var _getDefaultTagStructureForMode = _interopRequireDefault(require("./getDefaultTagStructureForMode"));
  9. var _tagNames = require("./tagNames");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. let tagStructure;
  12. const setTagStructure = mode => {
  13. tagStructure = (0, _getDefaultTagStructureForMode.default)(mode);
  14. }; // Given a nested array of property names, reduce them to a single array,
  15. // appending the name of the root element along the way if present.
  16. const flattenRoots = (params, root = '') => {
  17. let hasRestElement = false;
  18. let hasPropertyRest = false;
  19. const rests = [];
  20. const names = params.reduce((acc, cur) => {
  21. if (Array.isArray(cur)) {
  22. let nms;
  23. if (Array.isArray(cur[1])) {
  24. nms = cur[1];
  25. } else {
  26. if (cur[1].hasRestElement) {
  27. hasRestElement = true;
  28. }
  29. if (cur[1].hasPropertyRest) {
  30. hasPropertyRest = true;
  31. }
  32. nms = cur[1].names;
  33. }
  34. const flattened = flattenRoots(nms, root ? `${root}.${cur[0]}` : cur[0]);
  35. if (flattened.hasRestElement) {
  36. hasRestElement = true;
  37. }
  38. if (flattened.hasPropertyRest) {
  39. hasPropertyRest = true;
  40. }
  41. const inner = [root ? `${root}.${cur[0]}` : cur[0], ...flattened.names].filter(Boolean);
  42. rests.push(false, ...flattened.rests);
  43. return acc.concat(inner);
  44. }
  45. if (typeof cur === 'object') {
  46. if (cur.isRestProperty) {
  47. hasPropertyRest = true;
  48. rests.push(true);
  49. } else {
  50. rests.push(false);
  51. }
  52. if (cur.restElement) {
  53. hasRestElement = true;
  54. }
  55. acc.push(root ? `${root}.${cur.name}` : cur.name);
  56. } else if (typeof cur !== 'undefined') {
  57. rests.push(false);
  58. acc.push(root ? `${root}.${cur}` : cur);
  59. }
  60. return acc;
  61. }, []);
  62. return {
  63. hasPropertyRest,
  64. hasRestElement,
  65. names,
  66. rests
  67. };
  68. };
  69. const getPropertiesFromPropertySignature = propSignature => {
  70. if (propSignature.type === 'TSIndexSignature' || propSignature.type === 'TSConstructSignatureDeclaration' || propSignature.type === 'TSCallSignatureDeclaration') {
  71. return undefined;
  72. }
  73. if (propSignature.typeAnnotation && propSignature.typeAnnotation.typeAnnotation.type === 'TSTypeLiteral') {
  74. return [propSignature.key.name, propSignature.typeAnnotation.typeAnnotation.members.map(member => {
  75. return getPropertiesFromPropertySignature(member);
  76. })];
  77. }
  78. return propSignature.key.name;
  79. };
  80. const getFunctionParameterNames = (functionNode, checkDefaultObjects) => {
  81. // eslint-disable-next-line complexity
  82. const getParamName = (param, isProperty) => {
  83. var _param$left, _param$left3;
  84. if (_lodash.default.has(param, 'typeAnnotation') || _lodash.default.has(param, 'left.typeAnnotation')) {
  85. const typeAnnotation = _lodash.default.has(param, 'left.typeAnnotation') ? param.left.typeAnnotation : param.typeAnnotation;
  86. if (typeAnnotation.typeAnnotation.type === 'TSTypeLiteral') {
  87. const propertyNames = typeAnnotation.typeAnnotation.members.map(member => {
  88. return getPropertiesFromPropertySignature(member);
  89. });
  90. const flattened = { ...flattenRoots(propertyNames),
  91. annotationParamName: param.name
  92. };
  93. if (_lodash.default.has(param, 'name') || _lodash.default.has(param, 'left.name')) {
  94. return [_lodash.default.has(param, 'left.name') ? param.left.name : param.name, flattened];
  95. }
  96. return [undefined, flattened];
  97. }
  98. }
  99. if (_lodash.default.has(param, 'name')) {
  100. return param.name;
  101. }
  102. if (_lodash.default.has(param, 'left.name')) {
  103. return param.left.name;
  104. }
  105. if (param.type === 'ObjectPattern' || ((_param$left = param.left) === null || _param$left === void 0 ? void 0 : _param$left.type) === 'ObjectPattern') {
  106. var _param$left2;
  107. const properties = param.properties || ((_param$left2 = param.left) === null || _param$left2 === void 0 ? void 0 : _param$left2.properties);
  108. const roots = properties.map(prop => {
  109. return getParamName(prop, true);
  110. });
  111. return [undefined, flattenRoots(roots)];
  112. }
  113. if (param.type === 'Property') {
  114. switch (param.value.type) {
  115. case 'ArrayPattern':
  116. return [param.key.name, param.value.elements.map((prop, idx) => {
  117. return {
  118. name: idx,
  119. restElement: prop.type === 'RestElement'
  120. };
  121. })];
  122. case 'ObjectPattern':
  123. return [param.key.name, param.value.properties.map(prop => {
  124. return getParamName(prop, isProperty);
  125. })];
  126. case 'AssignmentPattern':
  127. {
  128. switch (param.value.left.type) {
  129. case 'Identifier':
  130. // Default parameter
  131. if (checkDefaultObjects && param.value.right.type === 'ObjectExpression') {
  132. return [param.key.name, param.value.right.properties.map(prop => {
  133. return getParamName(prop, isProperty);
  134. })];
  135. }
  136. break;
  137. case 'ObjectPattern':
  138. return [param.key.name, param.value.left.properties.map(prop => {
  139. return getParamName(prop, isProperty);
  140. })];
  141. case 'ArrayPattern':
  142. return [param.key.name, param.value.left.elements.map((prop, idx) => {
  143. return {
  144. name: idx,
  145. restElement: prop.type === 'RestElement'
  146. };
  147. })];
  148. }
  149. }
  150. }
  151. switch (param.key.type) {
  152. case 'Identifier':
  153. return param.key.name;
  154. // The key of an object could also be a string or number
  155. case 'Literal':
  156. return param.key.raw || // istanbul ignore next -- `raw` may not be present in all parsers
  157. param.key.value;
  158. // case 'MemberExpression':
  159. default:
  160. // Todo: We should really create a structure (and a corresponding
  161. // option analogous to `checkRestProperty`) which allows for
  162. // (and optionally requires) dynamic properties to have a single
  163. // line of documentation
  164. return undefined;
  165. }
  166. }
  167. if (param.type === 'ArrayPattern' || ((_param$left3 = param.left) === null || _param$left3 === void 0 ? void 0 : _param$left3.type) === 'ArrayPattern') {
  168. var _param$left4;
  169. const elements = param.elements || ((_param$left4 = param.left) === null || _param$left4 === void 0 ? void 0 : _param$left4.elements);
  170. const roots = elements.map((prop, idx) => {
  171. return {
  172. name: `"${idx}"`,
  173. restElement: (prop === null || prop === void 0 ? void 0 : prop.type) === 'RestElement'
  174. };
  175. });
  176. return [undefined, flattenRoots(roots)];
  177. }
  178. if (['RestElement', 'ExperimentalRestProperty'].includes(param.type)) {
  179. return {
  180. isRestProperty: isProperty,
  181. name: param.argument.name,
  182. restElement: true
  183. };
  184. }
  185. if (param.type === 'TSParameterProperty') {
  186. return getParamName(param.parameter, true);
  187. }
  188. throw new Error(`Unsupported function signature format: \`${param.type}\`.`);
  189. };
  190. return (functionNode.params || functionNode.value.params).map(param => {
  191. return getParamName(param);
  192. });
  193. };
  194. const hasParams = functionNode => {
  195. // Should also check `functionNode.value.params` if supporting `MethodDefinition`
  196. return functionNode.params.length;
  197. };
  198. /**
  199. * Gets all names of the target type, including those that refer to a path, e.g.
  200. * "@param foo; @param foo.bar".
  201. */
  202. const getJsdocTagsDeep = (jsdoc, targetTagName) => {
  203. const ret = [];
  204. jsdoc.tags.forEach(({
  205. name,
  206. tag,
  207. type
  208. }, idx) => {
  209. if (tag !== targetTagName) {
  210. return;
  211. }
  212. ret.push({
  213. idx,
  214. name,
  215. type
  216. });
  217. });
  218. return ret;
  219. };
  220. const modeWarnSettings = (0, _WarnSettings.default)();
  221. const getTagNamesForMode = (mode, context) => {
  222. switch (mode) {
  223. case 'jsdoc':
  224. return _tagNames.jsdocTags;
  225. case 'typescript':
  226. return _tagNames.typeScriptTags;
  227. case 'closure':
  228. case 'permissive':
  229. return _tagNames.closureTags;
  230. default:
  231. if (!modeWarnSettings.hasBeenWarned(context, 'mode')) {
  232. context.report({
  233. loc: {
  234. start: {
  235. column: 1,
  236. line: 1
  237. }
  238. },
  239. message: `Unrecognized value \`${mode}\` for \`settings.jsdoc.mode\`.`
  240. });
  241. modeWarnSettings.markSettingAsWarned(context, 'mode');
  242. } // We'll avoid breaking too many other rules
  243. return _tagNames.jsdocTags;
  244. }
  245. };
  246. const getPreferredTagName = (context, mode, name, tagPreference = {}) => {
  247. var _Object$entries$find;
  248. const prefValues = Object.values(tagPreference);
  249. if (prefValues.includes(name) || prefValues.some(prefVal => {
  250. return prefVal && typeof prefVal === 'object' && prefVal.replacement === name;
  251. })) {
  252. return name;
  253. } // Allow keys to have a 'tag ' prefix to avoid upstream bug in ESLint
  254. // that disallows keys that conflict with Object.prototype,
  255. // e.g. 'tag constructor' for 'constructor':
  256. // https://github.com/eslint/eslint/issues/13289
  257. // https://github.com/gajus/eslint-plugin-jsdoc/issues/537
  258. const tagPreferenceFixed = _lodash.default.mapKeys(tagPreference, (_value, key) => {
  259. return key.replace('tag ', '');
  260. });
  261. if (_lodash.default.has(tagPreferenceFixed, name)) {
  262. return tagPreferenceFixed[name];
  263. }
  264. const tagNames = getTagNamesForMode(mode, context);
  265. const preferredTagName = (_Object$entries$find = Object.entries(tagNames).find(([, aliases]) => {
  266. return aliases.includes(name);
  267. })) === null || _Object$entries$find === void 0 ? void 0 : _Object$entries$find[0];
  268. if (preferredTagName) {
  269. return preferredTagName;
  270. }
  271. return name;
  272. };
  273. const isValidTag = (context, mode, name, definedTags) => {
  274. const tagNames = getTagNamesForMode(mode, context);
  275. const validTagNames = Object.keys(tagNames).concat(Object.values(tagNames).flat());
  276. const additionalTags = definedTags;
  277. const allTags = validTagNames.concat(additionalTags);
  278. return allTags.includes(name);
  279. };
  280. const hasTag = (jsdoc, targetTagName) => {
  281. const targetTagLower = targetTagName.toLowerCase();
  282. return _lodash.default.some(jsdoc.tags, doc => {
  283. return doc.tag.toLowerCase() === targetTagLower;
  284. });
  285. };
  286. const hasATag = (jsdoc, targetTagNames) => {
  287. return targetTagNames.some(targetTagName => {
  288. return hasTag(jsdoc, targetTagName);
  289. });
  290. };
  291. /**
  292. * Checks if the JSDoc comment declares a defined type.
  293. *
  294. * @param {JsDocTag} tag
  295. * the tag which should be checked.
  296. * @returns {boolean}
  297. * true in case a defined type is declared; otherwise false.
  298. */
  299. const hasDefinedTypeTag = tag => {
  300. // The function should not continue in the event the type is not defined...
  301. if (typeof tag === 'undefined' || tag === null) {
  302. return false;
  303. } // .. same applies if it declares an `{undefined}` or `{void}` type
  304. const tagType = tag.type.trim();
  305. if (tagType === 'undefined' || tagType === 'void') {
  306. return false;
  307. } // In any other case, a type is present
  308. return true;
  309. };
  310. const ensureMap = (map, tag) => {
  311. if (!map.has(tag)) {
  312. map.set(tag, new Map());
  313. }
  314. return map.get(tag);
  315. };
  316. const overrideTagStructure = (structuredTags, tagMap = tagStructure) => {
  317. Object.entries(structuredTags).forEach(([tag, {
  318. name,
  319. type,
  320. required = []
  321. }]) => {
  322. const tagStruct = ensureMap(tagMap, tag);
  323. tagStruct.set('nameContents', name);
  324. tagStruct.set('typeAllowed', type);
  325. const requiredName = required.includes('name');
  326. if (requiredName && name === false) {
  327. throw new Error('Cannot add "name" to `require` with the tag\'s `name` set to `false`');
  328. }
  329. tagStruct.set('nameRequired', requiredName);
  330. const requiredType = required.includes('type');
  331. if (requiredType && type === false) {
  332. throw new Error('Cannot add "type" to `require` with the tag\'s `type` set to `false`');
  333. }
  334. tagStruct.set('typeRequired', requiredType);
  335. const typeOrNameRequired = required.includes('typeOrNameRequired');
  336. if (typeOrNameRequired && name === false) {
  337. throw new Error('Cannot add "typeOrNameRequired" to `require` with the tag\'s `name` set to `false`');
  338. }
  339. if (typeOrNameRequired && type === false) {
  340. throw new Error('Cannot add "typeOrNameRequired" to `require` with the tag\'s `type` set to `false`');
  341. }
  342. tagStruct.set('typeOrNameRequired', typeOrNameRequired);
  343. });
  344. };
  345. const getTagStructureForMode = (mode, structuredTags) => {
  346. const tagStruct = (0, _getDefaultTagStructureForMode.default)(mode);
  347. try {
  348. overrideTagStructure(structuredTags, tagStruct);
  349. } catch {//
  350. }
  351. return tagStruct;
  352. };
  353. const isNamepathDefiningTag = (tag, tagMap = tagStructure) => {
  354. const tagStruct = ensureMap(tagMap, tag);
  355. return tagStruct.get('nameContents') === 'namepath-defining';
  356. };
  357. const tagMustHaveTypePosition = (tag, tagMap = tagStructure) => {
  358. const tagStruct = ensureMap(tagMap, tag);
  359. return tagStruct.get('typeRequired');
  360. };
  361. const tagMightHaveTypePosition = (tag, tagMap = tagStructure) => {
  362. if (tagMustHaveTypePosition(tag, tagMap)) {
  363. return true;
  364. }
  365. const tagStruct = ensureMap(tagMap, tag);
  366. const ret = tagStruct.get('typeAllowed');
  367. return ret === undefined ? true : ret;
  368. };
  369. const namepathTypes = new Set(['namepath-defining', 'namepath-referencing']);
  370. const tagMightHaveNamePosition = (tag, tagMap = tagStructure) => {
  371. const tagStruct = ensureMap(tagMap, tag);
  372. const ret = tagStruct.get('nameContents');
  373. return ret === undefined ? true : Boolean(ret);
  374. };
  375. const tagMightHaveNamepath = (tag, tagMap = tagStructure) => {
  376. const tagStruct = ensureMap(tagMap, tag);
  377. return namepathTypes.has(tagStruct.get('nameContents'));
  378. };
  379. const tagMustHaveNamePosition = (tag, tagMap = tagStructure) => {
  380. const tagStruct = ensureMap(tagMap, tag);
  381. return tagStruct.get('nameRequired');
  382. };
  383. const tagMightHaveEitherTypeOrNamePosition = (tag, tagMap) => {
  384. return tagMightHaveTypePosition(tag, tagMap) || tagMightHaveNamepath(tag, tagMap);
  385. };
  386. const tagMustHaveEitherTypeOrNamePosition = (tag, tagMap) => {
  387. const tagStruct = ensureMap(tagMap, tag);
  388. return tagStruct.get('typeOrNameRequired');
  389. };
  390. const tagMissingRequiredTypeOrNamepath = (tag, tagMap = tagStructure) => {
  391. const mustHaveTypePosition = tagMustHaveTypePosition(tag.tag, tagMap);
  392. const mightHaveTypePosition = tagMightHaveTypePosition(tag.tag, tagMap);
  393. const hasTypePosition = mightHaveTypePosition && Boolean(tag.type);
  394. const hasNameOrNamepathPosition = (tagMustHaveNamePosition(tag.tag, tagMap) || tagMightHaveNamepath(tag.tag, tagMap)) && Boolean(tag.name);
  395. const mustHaveEither = tagMustHaveEitherTypeOrNamePosition(tag.tag, tagMap);
  396. const hasEither = tagMightHaveEitherTypeOrNamePosition(tag.tag, tagMap) && (hasTypePosition || hasNameOrNamepathPosition);
  397. return mustHaveEither && !hasEither && !mustHaveTypePosition;
  398. };
  399. /**
  400. * Checks if a node is a promise but has no resolve value or an empty value.
  401. * An `undefined` resolve does not count.
  402. *
  403. * @param {object} node
  404. * @returns {boolean}
  405. */
  406. const isNewPromiseExpression = node => {
  407. return node.type === 'NewExpression' && node.callee.type === 'Identifier' && node.callee.name === 'Promise';
  408. };
  409. /**
  410. * @callback PromiseFilter
  411. * @param {object} node
  412. * @returns {boolean}
  413. */
  414. /**
  415. * Checks if a node has a return statement. Void return does not count.
  416. *
  417. * @param {object} node
  418. * @param {PromiseFilter} promFilter
  419. * @returns {boolean|Node}
  420. */
  421. // eslint-disable-next-line complexity
  422. const hasReturnValue = (node, promFilter) => {
  423. var _node$returnType, _node$returnType$type;
  424. if (!node) {
  425. return false;
  426. }
  427. switch (node.type) {
  428. case 'TSFunctionType':
  429. case 'TSMethodSignature':
  430. return !['TSVoidKeyword', 'TSUndefinedKeyword'].includes(node === null || node === void 0 ? void 0 : (_node$returnType = node.returnType) === null || _node$returnType === void 0 ? void 0 : (_node$returnType$type = _node$returnType.typeAnnotation) === null || _node$returnType$type === void 0 ? void 0 : _node$returnType$type.type);
  431. case 'MethodDefinition':
  432. return hasReturnValue(node.value, promFilter);
  433. case 'FunctionExpression':
  434. case 'FunctionDeclaration':
  435. case 'ArrowFunctionExpression':
  436. {
  437. return node.expression || hasReturnValue(node.body, promFilter);
  438. }
  439. case 'BlockStatement':
  440. {
  441. return node.body.some(bodyNode => {
  442. return bodyNode.type !== 'FunctionDeclaration' && hasReturnValue(bodyNode, promFilter);
  443. });
  444. }
  445. case 'LabeledStatement':
  446. case 'WhileStatement':
  447. case 'DoWhileStatement':
  448. case 'ForStatement':
  449. case 'ForInStatement':
  450. case 'ForOfStatement':
  451. case 'WithStatement':
  452. {
  453. return hasReturnValue(node.body, promFilter);
  454. }
  455. case 'IfStatement':
  456. {
  457. return hasReturnValue(node.consequent, promFilter) || hasReturnValue(node.alternate, promFilter);
  458. }
  459. case 'TryStatement':
  460. {
  461. return hasReturnValue(node.block, promFilter) || hasReturnValue(node.handler && node.handler.body, promFilter) || hasReturnValue(node.finalizer, promFilter);
  462. }
  463. case 'SwitchStatement':
  464. {
  465. return node.cases.some(someCase => {
  466. return someCase.consequent.some(nde => {
  467. return hasReturnValue(nde, promFilter);
  468. });
  469. });
  470. }
  471. case 'ReturnStatement':
  472. {
  473. // void return does not count.
  474. if (node.argument === null) {
  475. return false;
  476. }
  477. if (promFilter && isNewPromiseExpression(node.argument)) {
  478. // Let caller decide how to filter, but this is, at the least,
  479. // a return of sorts and truthy
  480. return promFilter(node.argument);
  481. }
  482. return true;
  483. }
  484. default:
  485. {
  486. return false;
  487. }
  488. }
  489. };
  490. /**
  491. * Avoids further checking child nodes if a nested function shadows the
  492. * resolver, but otherwise, if name is used (by call or passed in as an
  493. * argument to another function), will be considered as non-empty.
  494. *
  495. * This could check for redeclaration of the resolver, but as such is
  496. * unlikely, we avoid the performance cost of checking everywhere for
  497. * (re)declarations or assignments.
  498. *
  499. * @param {AST} node
  500. * @param {string} resolverName
  501. * @returns {boolean}
  502. */
  503. // eslint-disable-next-line complexity
  504. const hasNonEmptyResolverCall = (node, resolverName) => {
  505. if (!node) {
  506. return false;
  507. } // Arrow function without block
  508. switch (node.type) {
  509. // istanbul ignore next -- In Babel?
  510. case 'OptionalCallExpression':
  511. case 'CallExpression':
  512. return node.callee.name === resolverName && ( // Implicit or expliit undefined
  513. node.arguments.length > 1 || node.arguments[0] !== undefined) || node.arguments.some(nde => {
  514. // Being passed in to another function (which might invoke it)
  515. return nde.type === 'Identifier' && nde.name === resolverName || // Handle nested items
  516. hasNonEmptyResolverCall(nde, resolverName);
  517. });
  518. case 'ChainExpression':
  519. case 'Decorator':
  520. case 'ExpressionStatement':
  521. return hasNonEmptyResolverCall(node.expression, resolverName);
  522. case 'ClassBody':
  523. case 'BlockStatement':
  524. return node.body.some(bodyNode => {
  525. return hasNonEmptyResolverCall(bodyNode, resolverName);
  526. });
  527. case 'FunctionExpression':
  528. case 'FunctionDeclaration':
  529. case 'ArrowFunctionExpression':
  530. {
  531. var _node$params$;
  532. // Shadowing
  533. if (((_node$params$ = node.params[0]) === null || _node$params$ === void 0 ? void 0 : _node$params$.name) === resolverName) {
  534. return false;
  535. }
  536. return hasNonEmptyResolverCall(node.body, resolverName);
  537. }
  538. case 'LabeledStatement':
  539. case 'WhileStatement':
  540. case 'DoWhileStatement':
  541. case 'ForStatement':
  542. case 'ForInStatement':
  543. case 'ForOfStatement':
  544. case 'WithStatement':
  545. {
  546. return hasNonEmptyResolverCall(node.body, resolverName);
  547. }
  548. case 'ConditionalExpression':
  549. case 'IfStatement':
  550. {
  551. return hasNonEmptyResolverCall(node.test, resolverName) || hasNonEmptyResolverCall(node.consequent, resolverName) || hasNonEmptyResolverCall(node.alternate, resolverName);
  552. }
  553. case 'TryStatement':
  554. {
  555. return hasNonEmptyResolverCall(node.block, resolverName) || hasNonEmptyResolverCall(node.handler && node.handler.body, resolverName) || hasNonEmptyResolverCall(node.finalizer, resolverName);
  556. }
  557. case 'SwitchStatement':
  558. {
  559. return node.cases.some(someCase => {
  560. return someCase.consequent.some(nde => {
  561. return hasNonEmptyResolverCall(nde, resolverName);
  562. });
  563. });
  564. }
  565. case 'ArrayPattern':
  566. case 'ArrayExpression':
  567. return node.elements.some(element => {
  568. return hasNonEmptyResolverCall(element, resolverName);
  569. });
  570. case 'AssignmentPattern':
  571. return hasNonEmptyResolverCall(node.right, resolverName);
  572. case 'AssignmentExpression':
  573. case 'BinaryExpression':
  574. case 'LogicalExpression':
  575. {
  576. return hasNonEmptyResolverCall(node.left, resolverName) || hasNonEmptyResolverCall(node.right, resolverName);
  577. }
  578. // Comma
  579. case 'SequenceExpression':
  580. case 'TemplateLiteral':
  581. return node.expressions.some(subExpression => {
  582. return hasNonEmptyResolverCall(subExpression, resolverName);
  583. });
  584. case 'ObjectPattern':
  585. case 'ObjectExpression':
  586. return node.properties.some(property => {
  587. return hasNonEmptyResolverCall(property, resolverName);
  588. });
  589. // istanbul ignore next -- In Babel?
  590. case 'ClassMethod':
  591. case 'MethodDefinition':
  592. return node.decorators && node.decorators.some(decorator => {
  593. return hasNonEmptyResolverCall(decorator, resolverName);
  594. }) || node.computed && hasNonEmptyResolverCall(node.key, resolverName) || hasNonEmptyResolverCall(node.value, resolverName);
  595. // istanbul ignore next -- In Babel?
  596. case 'ObjectProperty':
  597. /* eslint-disable no-fallthrough */
  598. // istanbul ignore next -- In Babel?
  599. case 'ClassProperty':
  600. /* eslint-enable no-fallthrough */
  601. case 'Property':
  602. return node.computed && hasNonEmptyResolverCall(node.key, resolverName) || hasNonEmptyResolverCall(node.value, resolverName);
  603. // istanbul ignore next -- In Babel?
  604. case 'ObjectMethod':
  605. // istanbul ignore next -- In Babel?
  606. return node.computed && hasNonEmptyResolverCall(node.key, resolverName) || node.arguments.some(nde => {
  607. return hasNonEmptyResolverCall(nde, resolverName);
  608. });
  609. case 'ClassExpression':
  610. case 'ClassDeclaration':
  611. return hasNonEmptyResolverCall(node.body, resolverName);
  612. case 'AwaitExpression':
  613. case 'SpreadElement':
  614. case 'UnaryExpression':
  615. case 'YieldExpression':
  616. return hasNonEmptyResolverCall(node.argument, resolverName);
  617. case 'VariableDeclaration':
  618. {
  619. return node.declarations.some(nde => {
  620. return hasNonEmptyResolverCall(nde, resolverName);
  621. });
  622. }
  623. case 'VariableDeclarator':
  624. {
  625. return hasNonEmptyResolverCall(node.id, resolverName) || hasNonEmptyResolverCall(node.init, resolverName);
  626. }
  627. case 'TaggedTemplateExpression':
  628. return hasNonEmptyResolverCall(node.quasi, resolverName);
  629. // ?.
  630. // istanbul ignore next -- In Babel?
  631. case 'OptionalMemberExpression':
  632. case 'MemberExpression':
  633. return hasNonEmptyResolverCall(node.object, resolverName) || hasNonEmptyResolverCall(node.property, resolverName);
  634. // istanbul ignore next -- In Babel?
  635. case 'Import':
  636. case 'ImportExpression':
  637. return hasNonEmptyResolverCall(node.source, resolverName);
  638. case 'ReturnStatement':
  639. {
  640. if (node.argument === null) {
  641. return false;
  642. }
  643. return hasNonEmptyResolverCall(node.argument, resolverName);
  644. }
  645. /*
  646. // Shouldn't need to parse literals/literal components, etc.
  647. case 'Identifier':
  648. case 'TemplateElement':
  649. case 'Super':
  650. // Exports not relevant in this context
  651. */
  652. default:
  653. return false;
  654. }
  655. };
  656. /**
  657. * Checks if a Promise executor has no resolve value or an empty value.
  658. * An `undefined` resolve does not count.
  659. *
  660. * @param {object} node
  661. * @param {boolean} anyPromiseAsReturn
  662. * @returns {boolean}
  663. */
  664. const hasValueOrExecutorHasNonEmptyResolveValue = (node, anyPromiseAsReturn) => {
  665. return hasReturnValue(node, prom => {
  666. if (anyPromiseAsReturn) {
  667. return true;
  668. }
  669. const [{
  670. params,
  671. body
  672. } = {}] = prom.arguments;
  673. if (!(params !== null && params !== void 0 && params.length)) {
  674. return false;
  675. }
  676. const [{
  677. name: resolverName
  678. }] = params;
  679. return hasNonEmptyResolverCall(body, resolverName);
  680. });
  681. }; // eslint-disable-next-line complexity
  682. const hasNonFunctionYield = (node, checkYieldReturnValue) => {
  683. if (!node) {
  684. return false;
  685. }
  686. switch (node.type) {
  687. case 'BlockStatement':
  688. {
  689. return node.body.some(bodyNode => {
  690. return !['ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression'].includes(bodyNode.type) && hasNonFunctionYield(bodyNode, checkYieldReturnValue);
  691. });
  692. }
  693. // istanbul ignore next -- In Babel?
  694. case 'OptionalCallExpression':
  695. case 'CallExpression':
  696. return node.arguments.some(element => {
  697. return hasNonFunctionYield(element, checkYieldReturnValue);
  698. });
  699. case 'ChainExpression':
  700. case 'ExpressionStatement':
  701. {
  702. return hasNonFunctionYield(node.expression, checkYieldReturnValue);
  703. }
  704. case 'LabeledStatement':
  705. case 'WhileStatement':
  706. case 'DoWhileStatement':
  707. case 'ForStatement':
  708. case 'ForInStatement':
  709. case 'ForOfStatement':
  710. case 'WithStatement':
  711. {
  712. return hasNonFunctionYield(node.body, checkYieldReturnValue);
  713. }
  714. case 'ConditionalExpression':
  715. case 'IfStatement':
  716. {
  717. return hasNonFunctionYield(node.test, checkYieldReturnValue) || hasNonFunctionYield(node.consequent, checkYieldReturnValue) || hasNonFunctionYield(node.alternate, checkYieldReturnValue);
  718. }
  719. case 'TryStatement':
  720. {
  721. return hasNonFunctionYield(node.block, checkYieldReturnValue) || hasNonFunctionYield(node.handler && node.handler.body, checkYieldReturnValue) || hasNonFunctionYield(node.finalizer, checkYieldReturnValue);
  722. }
  723. case 'SwitchStatement':
  724. {
  725. return node.cases.some(someCase => {
  726. return someCase.consequent.some(nde => {
  727. return hasNonFunctionYield(nde, checkYieldReturnValue);
  728. });
  729. });
  730. }
  731. case 'ArrayPattern':
  732. case 'ArrayExpression':
  733. return node.elements.some(element => {
  734. return hasNonFunctionYield(element, checkYieldReturnValue);
  735. });
  736. case 'AssignmentPattern':
  737. return hasNonFunctionYield(node.right, checkYieldReturnValue);
  738. case 'VariableDeclaration':
  739. {
  740. return node.declarations.some(nde => {
  741. return hasNonFunctionYield(nde, checkYieldReturnValue);
  742. });
  743. }
  744. case 'VariableDeclarator':
  745. {
  746. return hasNonFunctionYield(node.id, checkYieldReturnValue) || hasNonFunctionYield(node.init, checkYieldReturnValue);
  747. }
  748. case 'AssignmentExpression':
  749. case 'BinaryExpression':
  750. case 'LogicalExpression':
  751. {
  752. return hasNonFunctionYield(node.left, checkYieldReturnValue) || hasNonFunctionYield(node.right, checkYieldReturnValue);
  753. }
  754. // Comma
  755. case 'SequenceExpression':
  756. case 'TemplateLiteral':
  757. return node.expressions.some(subExpression => {
  758. return hasNonFunctionYield(subExpression, checkYieldReturnValue);
  759. });
  760. case 'ObjectPattern':
  761. case 'ObjectExpression':
  762. return node.properties.some(property => {
  763. return hasNonFunctionYield(property, checkYieldReturnValue);
  764. });
  765. // istanbul ignore next -- In Babel?
  766. case 'ObjectProperty':
  767. /* eslint-disable no-fallthrough */
  768. // istanbul ignore next -- In Babel?
  769. case 'ClassProperty':
  770. /* eslint-enable no-fallthrough */
  771. case 'Property':
  772. return node.computed && hasNonFunctionYield(node.key, checkYieldReturnValue) || hasNonFunctionYield(node.value, checkYieldReturnValue);
  773. // istanbul ignore next -- In Babel?
  774. case 'ObjectMethod':
  775. // istanbul ignore next -- In Babel?
  776. return node.computed && hasNonFunctionYield(node.key, checkYieldReturnValue) || node.arguments.some(nde => {
  777. return hasNonFunctionYield(nde, checkYieldReturnValue);
  778. });
  779. case 'SpreadElement':
  780. case 'UnaryExpression':
  781. return hasNonFunctionYield(node.argument, checkYieldReturnValue);
  782. case 'TaggedTemplateExpression':
  783. return hasNonFunctionYield(node.quasi, checkYieldReturnValue);
  784. // ?.
  785. // istanbul ignore next -- In Babel?
  786. case 'OptionalMemberExpression':
  787. case 'MemberExpression':
  788. return hasNonFunctionYield(node.object, checkYieldReturnValue) || hasNonFunctionYield(node.property, checkYieldReturnValue);
  789. // istanbul ignore next -- In Babel?
  790. case 'Import':
  791. case 'ImportExpression':
  792. return hasNonFunctionYield(node.source, checkYieldReturnValue);
  793. case 'ReturnStatement':
  794. {
  795. if (node.argument === null) {
  796. return false;
  797. }
  798. return hasNonFunctionYield(node.argument, checkYieldReturnValue);
  799. }
  800. case 'YieldExpression':
  801. {
  802. if (checkYieldReturnValue) {
  803. if (node.parent.type === 'VariableDeclarator') {
  804. return true;
  805. }
  806. return false;
  807. } // void return does not count.
  808. if (node.argument === null) {
  809. return false;
  810. }
  811. return true;
  812. }
  813. default:
  814. {
  815. return false;
  816. }
  817. }
  818. };
  819. /**
  820. * Checks if a node has a return statement. Void return does not count.
  821. *
  822. * @param {object} node
  823. * @returns {boolean}
  824. */
  825. const hasYieldValue = (node, checkYieldReturnValue) => {
  826. return node.generator && (node.expression || hasNonFunctionYield(node.body, checkYieldReturnValue));
  827. };
  828. /**
  829. * Checks if a node has a throws statement.
  830. *
  831. * @param {object} node
  832. * @param {boolean} innerFunction
  833. * @returns {boolean}
  834. */
  835. // eslint-disable-next-line complexity
  836. const hasThrowValue = (node, innerFunction) => {
  837. if (!node) {
  838. return false;
  839. } // There are cases where a function may execute its inner function which
  840. // throws, but we're treating functions atomically rather than trying to
  841. // follow them
  842. switch (node.type) {
  843. case 'FunctionExpression':
  844. case 'FunctionDeclaration':
  845. case 'ArrowFunctionExpression':
  846. {
  847. return !innerFunction && !node.async && hasThrowValue(node.body, true);
  848. }
  849. case 'BlockStatement':
  850. {
  851. return node.body.some(bodyNode => {
  852. return bodyNode.type !== 'FunctionDeclaration' && hasThrowValue(bodyNode);
  853. });
  854. }
  855. case 'LabeledStatement':
  856. case 'WhileStatement':
  857. case 'DoWhileStatement':
  858. case 'ForStatement':
  859. case 'ForInStatement':
  860. case 'ForOfStatement':
  861. case 'WithStatement':
  862. {
  863. return hasThrowValue(node.body);
  864. }
  865. case 'IfStatement':
  866. {
  867. return hasThrowValue(node.consequent) || hasThrowValue(node.alternate);
  868. }
  869. // We only consider it to throw an error if the catch or finally blocks throw an error.
  870. case 'TryStatement':
  871. {
  872. return hasThrowValue(node.handler && node.handler.body) || hasThrowValue(node.finalizer);
  873. }
  874. case 'SwitchStatement':
  875. {
  876. return node.cases.some(someCase => {
  877. return someCase.consequent.some(nde => {
  878. return hasThrowValue(nde);
  879. });
  880. });
  881. }
  882. case 'ThrowStatement':
  883. {
  884. return true;
  885. }
  886. default:
  887. {
  888. return false;
  889. }
  890. }
  891. };
  892. /** @param {string} tag */
  893. /*
  894. const isInlineTag = (tag) => {
  895. return /^(@link|@linkcode|@linkplain|@tutorial) /u.test(tag);
  896. };
  897. */
  898. /**
  899. * Parses GCC Generic/Template types
  900. *
  901. * @see {https://github.com/google/closure-compiler/wiki/Generic-Types}
  902. * @see {https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#template}
  903. * @param {JsDocTag} tag
  904. * @returns {Array<string>}
  905. */
  906. const parseClosureTemplateTag = tag => {
  907. return tag.name.split(',').map(type => {
  908. return type.trim();
  909. });
  910. };
  911. /**
  912. * Checks user option for `contexts` array, defaulting to
  913. * contexts designated by the rule. Returns an array of
  914. * ESTree AST types, indicating allowable contexts.
  915. *
  916. * @param {*} context
  917. * @param {true|string[]} defaultContexts
  918. * @returns {string[]}
  919. */
  920. const enforcedContexts = (context, defaultContexts) => {
  921. const {
  922. contexts = defaultContexts === true ? ['ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression'] : defaultContexts
  923. } = context.options[0] || {};
  924. return contexts;
  925. };
  926. /**
  927. * @param {string[]} contexts
  928. * @param {Function} checkJsdoc
  929. * @param {Function} handler
  930. */
  931. const getContextObject = (contexts, checkJsdoc, handler) => {
  932. const properties = {};
  933. contexts.forEach((prop, idx) => {
  934. if (typeof prop === 'object') {
  935. const selInfo = {
  936. lastIndex: idx,
  937. selector: prop.context
  938. };
  939. if (prop.comment) {
  940. properties[prop.context] = checkJsdoc.bind(null, { ...selInfo,
  941. comment: prop.comment
  942. }, handler.bind(null, prop.comment));
  943. } else {
  944. properties[prop.context] = checkJsdoc.bind(null, selInfo, null);
  945. }
  946. } else {
  947. const selInfo = {
  948. lastIndex: idx,
  949. selector: prop
  950. };
  951. properties[prop] = checkJsdoc.bind(null, selInfo, null);
  952. }
  953. });
  954. return properties;
  955. };
  956. const filterTags = (tags, filter) => {
  957. return tags.filter(filter);
  958. };
  959. const tagsWithNamesAndDescriptions = new Set(['param', 'arg', 'argument', 'property', 'prop', 'template', // These two are parsed by our custom parser as though having a `name`
  960. 'returns', 'return']);
  961. const getTagsByType = (context, mode, tags, tagPreference) => {
  962. const descName = getPreferredTagName(context, mode, 'description', tagPreference);
  963. const tagsWithoutNames = [];
  964. const tagsWithNames = filterTags(tags, tag => {
  965. const {
  966. tag: tagName
  967. } = tag;
  968. const tagWithName = tagsWithNamesAndDescriptions.has(tagName);
  969. if (!tagWithName && tagName !== descName) {
  970. tagsWithoutNames.push(tag);
  971. }
  972. return tagWithName;
  973. });
  974. return {
  975. tagsWithNames,
  976. tagsWithoutNames
  977. };
  978. };
  979. const getIndent = sourceCode => {
  980. var _sourceCode$text$matc, _sourceCode$text$matc2;
  981. return ((_sourceCode$text$matc = (_sourceCode$text$matc2 = sourceCode.text.match(/^\n*([ \t]+)/u)) === null || _sourceCode$text$matc2 === void 0 ? void 0 : _sourceCode$text$matc2[1]) !== null && _sourceCode$text$matc !== void 0 ? _sourceCode$text$matc : '') + ' ';
  982. };
  983. const isConstructor = node => {
  984. var _node$parent;
  985. return (node === null || node === void 0 ? void 0 : node.type) === 'MethodDefinition' && node.kind === 'constructor' || (node === null || node === void 0 ? void 0 : (_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : _node$parent.kind) === 'constructor';
  986. };
  987. const isGetter = node => {
  988. return node && node.parent.kind === 'get';
  989. };
  990. const isSetter = node => {
  991. return node && node.parent.kind === 'set';
  992. };
  993. const hasAccessorPair = node => {
  994. const {
  995. type,
  996. kind: sourceKind,
  997. key: {
  998. name: sourceName
  999. }
  1000. } = node;
  1001. const oppositeKind = sourceKind === 'get' ? 'set' : 'get';
  1002. const children = type === 'MethodDefinition' ? 'body' : 'properties';
  1003. return node.parent[children].some(({
  1004. kind,
  1005. key: {
  1006. name
  1007. }
  1008. }) => {
  1009. return kind === oppositeKind && name === sourceName;
  1010. });
  1011. };
  1012. const exemptSpeciaMethods = (jsdoc, node, context, schema) => {
  1013. const hasSchemaOption = prop => {
  1014. var _context$options$0$pr, _context$options$;
  1015. const schemaProperties = schema[0].properties;
  1016. return (_context$options$0$pr = (_context$options$ = context.options[0]) === null || _context$options$ === void 0 ? void 0 : _context$options$[prop]) !== null && _context$options$0$pr !== void 0 ? _context$options$0$pr : schemaProperties[prop] && schemaProperties[prop].default;
  1017. };
  1018. const checkGetters = hasSchemaOption('checkGetters');
  1019. const checkSetters = hasSchemaOption('checkSetters');
  1020. return !hasSchemaOption('checkConstructors') && (isConstructor(node) || hasATag(jsdoc, ['class', 'constructor'])) || isGetter(node) && (!checkGetters || checkGetters === 'no-setter' && hasAccessorPair(node.parent)) || isSetter(node) && (!checkSetters || checkSetters === 'no-getter' && hasAccessorPair(node.parent));
  1021. };
  1022. /**
  1023. * Since path segments may be unquoted (if matching a reserved word,
  1024. * identifier or numeric literal) or single or double quoted, in either
  1025. * the `@param` or in source, we need to strip the quotes to give a fair
  1026. * comparison.
  1027. *
  1028. * @param {string} str
  1029. * @returns {string}
  1030. */
  1031. const dropPathSegmentQuotes = str => {
  1032. return str.replace(/\.(['"])(.*)\1/gu, '.$2');
  1033. };
  1034. const comparePaths = name => {
  1035. return otherPathName => {
  1036. return otherPathName === name || dropPathSegmentQuotes(otherPathName) === dropPathSegmentQuotes(name);
  1037. };
  1038. };
  1039. const pathDoesNotBeginWith = (name, otherPathName) => {
  1040. return !name.startsWith(otherPathName) && !dropPathSegmentQuotes(name).startsWith(dropPathSegmentQuotes(otherPathName));
  1041. };
  1042. const getRegexFromString = (regexString, requiredFlags) => {
  1043. const match = regexString.match(/^\/(.*)\/([gimyus]*)$/us);
  1044. let flags = 'u';
  1045. let regex = regexString;
  1046. if (match) {
  1047. [, regex, flags] = match;
  1048. if (!flags) {
  1049. flags = 'u';
  1050. }
  1051. }
  1052. const uniqueFlags = [...new Set(flags + (requiredFlags || ''))];
  1053. flags = uniqueFlags.join('');
  1054. return new RegExp(regex, flags);
  1055. };
  1056. var _default = {
  1057. comparePaths,
  1058. dropPathSegmentQuotes,
  1059. enforcedContexts,
  1060. exemptSpeciaMethods,
  1061. filterTags,
  1062. flattenRoots,
  1063. getContextObject,
  1064. getFunctionParameterNames,
  1065. getIndent,
  1066. getJsdocTagsDeep,
  1067. getPreferredTagName,
  1068. getRegexFromString,
  1069. getTagsByType,
  1070. getTagStructureForMode,
  1071. hasATag,
  1072. hasDefinedTypeTag,
  1073. hasParams,
  1074. hasReturnValue,
  1075. hasTag,
  1076. hasThrowValue,
  1077. hasValueOrExecutorHasNonEmptyResolveValue,
  1078. hasYieldValue,
  1079. isConstructor,
  1080. isGetter,
  1081. isNamepathDefiningTag,
  1082. isSetter,
  1083. isValidTag,
  1084. overrideTagStructure,
  1085. parseClosureTemplateTag,
  1086. pathDoesNotBeginWith,
  1087. setTagStructure,
  1088. tagMightHaveNamepath,
  1089. tagMightHaveNamePosition,
  1090. tagMightHaveTypePosition,
  1091. tagMissingRequiredTypeOrNamepath,
  1092. tagMustHaveNamePosition,
  1093. tagMustHaveTypePosition
  1094. };
  1095. exports.default = _default;
  1096. module.exports = exports.default;
  1097. //# sourceMappingURL=jsdocUtils.js.map