|
| 1 | +/** |
| 2 | + * Obtained from {@link https://github.com/eslint/eslint/blob/master/lib/util/source-code.js#L313} |
| 3 | + * @license MIT |
| 4 | + */ |
| 5 | +import astUtils from 'eslint/lib/util/ast-utils'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Check to see if its a ES6 export declaration. |
| 9 | + * |
| 10 | + * @param {ASTNode} astNode An AST node. |
| 11 | + * @returns {boolean} whether the given node represents an export declaration. |
| 12 | + * @private |
| 13 | + */ |
| 14 | +const looksLikeExport = function (astNode) { |
| 15 | + return astNode.type === 'ExportDefaultDeclaration' || astNode.type === 'ExportNamedDeclaration' || |
| 16 | + astNode.type === 'ExportAllDeclaration' || astNode.type === 'ExportSpecifier'; |
| 17 | +}; |
| 18 | + |
| 19 | +/** |
| 20 | + * Retrieves the JSDoc comment for a given node. |
| 21 | + * |
| 22 | + * @param {SourceCode} sourceCode The ESLint SourceCode |
| 23 | + * @param {ASTNode} node The AST node to get the comment for. |
| 24 | + * @returns {Token|null} The Block comment token containing the JSDoc comment |
| 25 | + * for the given node or null if not found. |
| 26 | + * @public |
| 27 | + * @deprecated |
| 28 | + */ |
| 29 | +const getJSDocComment = function (sourceCode, node) { |
| 30 | + /** |
| 31 | + * Checks for the presence of a JSDoc comment for the given node and returns it. |
| 32 | + * |
| 33 | + * @param {ASTNode} astNode The AST node to get the comment for. |
| 34 | + * @returns {Token|null} The Block comment token containing the JSDoc comment |
| 35 | + * for the given node or null if not found. |
| 36 | + * @private |
| 37 | + */ |
| 38 | + const findJSDocComment = (astNode) => { |
| 39 | + const tokenBefore = sourceCode.getTokenBefore(astNode, {includeComments: true}); |
| 40 | + |
| 41 | + if ( |
| 42 | + tokenBefore && |
| 43 | + astUtils.isCommentToken(tokenBefore) && |
| 44 | + tokenBefore.type === 'Block' && |
| 45 | + tokenBefore.value.charAt(0) === '*' && |
| 46 | + astNode.loc.start.line - tokenBefore.loc.end.line <= 1 |
| 47 | + ) { |
| 48 | + return tokenBefore; |
| 49 | + } |
| 50 | + |
| 51 | + return null; |
| 52 | + }; |
| 53 | + let parent = node.parent; |
| 54 | + |
| 55 | + switch (node.type) { |
| 56 | + case 'ClassDeclaration': |
| 57 | + case 'FunctionDeclaration': |
| 58 | + return findJSDocComment(looksLikeExport(parent) ? parent : node); |
| 59 | + |
| 60 | + case 'ClassExpression': |
| 61 | + return findJSDocComment(parent.parent); |
| 62 | + |
| 63 | + case 'ArrowFunctionExpression': |
| 64 | + case 'FunctionExpression': |
| 65 | + if (parent.type !== 'CallExpression' && parent.type !== 'NewExpression') { |
| 66 | + while ( |
| 67 | + !sourceCode.getCommentsBefore(parent).length && |
| 68 | + !/Function/u.test(parent.type) && |
| 69 | + parent.type !== 'MethodDefinition' && |
| 70 | + parent.type !== 'Property' |
| 71 | + ) { |
| 72 | + parent = parent.parent; |
| 73 | + |
| 74 | + if (!parent) { |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (parent && parent.type !== 'FunctionDeclaration' && parent.type !== 'Program') { |
| 80 | + return findJSDocComment(parent); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return findJSDocComment(node); |
| 85 | + |
| 86 | + // falls through |
| 87 | + default: |
| 88 | + return null; |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +export default getJSDocComment; |
0 commit comments