Skip to content

Commit 3115021

Browse files
authored
refactor: simplify JSDoc comment detection logic (#20360)
* refactor: simplify JSDoc comment detection logic * clean up JSDoc comments and rename looksLikeExport
1 parent 4345b17 commit 3115021

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

lib/rules/utils/ast-utils.js

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -704,60 +704,63 @@ function isKeywordToken(token) {
704704
}
705705

706706
/**
707-
* Check to see if its a ES6 export declaration.
708-
* @param {ASTNode} astNode An AST node.
709-
* @returns {boolean} whether the given node represents an export declaration.
707+
* Checks whether the given node represents an ES6 export declaration.
708+
* @param {ASTNode} node A node to check.
709+
* @returns {boolean} `true` if the node is an export declaration.
710710
* @private
711711
*/
712-
function looksLikeExport(astNode) {
712+
function isExportDeclaration(node) {
713713
return (
714-
astNode.type === "ExportDefaultDeclaration" ||
715-
astNode.type === "ExportNamedDeclaration" ||
716-
astNode.type === "ExportAllDeclaration" ||
717-
astNode.type === "ExportSpecifier"
714+
node.type === "ExportDefaultDeclaration" ||
715+
node.type === "ExportNamedDeclaration" ||
716+
node.type === "ExportAllDeclaration"
718717
);
719718
}
720719

721720
/**
722-
* Retrieves the JSDoc comment for a given node.
723-
* @param {ASTNode} node The AST node to get the comment for.
721+
* Checks for the presence of a JSDoc comment for the given node and returns it.
722+
* @param {ASTNode} node The node to get the comment for.
724723
* @param {SourceCode} sourceCode A SourceCode instance to get comments.
725724
* @returns {Token|null} The Block comment token containing the JSDoc comment for the given node or null if not found.
726725
* @private
727726
*/
728-
function getJSDocComment(node, sourceCode) {
729-
/**
730-
* Checks for the presence of a JSDoc comment for the given node and returns it.
731-
* @param {ASTNode} astNode The AST node to get the comment for.
732-
* @returns {Token|null} The Block comment token containing the JSDoc comment for the given node or null if not found.
733-
* @private
734-
*/
735-
function findJSDocComment(astNode) {
736-
const tokenBefore = sourceCode.getTokenBefore(astNode, {
737-
includeComments: true,
738-
});
727+
function findJSDocComment(node, sourceCode) {
728+
const tokenBefore = sourceCode.getTokenBefore(node, {
729+
includeComments: true,
730+
});
739731

740-
if (
741-
tokenBefore &&
742-
isCommentToken(tokenBefore) &&
743-
tokenBefore.type === "Block" &&
744-
tokenBefore.value.charAt(0) === "*" &&
745-
astNode.loc.start.line - tokenBefore.loc.end.line <= 1
746-
) {
747-
return tokenBefore;
748-
}
749-
750-
return null;
732+
if (
733+
tokenBefore &&
734+
tokenBefore.type === "Block" &&
735+
tokenBefore.value.charAt(0) === "*" &&
736+
node.loc.start.line - tokenBefore.loc.end.line <= 1
737+
) {
738+
return tokenBefore;
751739
}
740+
741+
return null;
742+
}
743+
744+
/**
745+
* Retrieves the JSDoc comment for a given node.
746+
* @param {ASTNode} node The node to get the comment for.
747+
* @param {SourceCode} sourceCode A SourceCode instance to get comments.
748+
* @returns {Token|null} The Block comment token containing the JSDoc comment for the given node or null if not found.
749+
* @private
750+
*/
751+
function getJSDocComment(node, sourceCode) {
752752
let parent = node.parent;
753753

754754
switch (node.type) {
755755
case "ClassDeclaration":
756756
case "FunctionDeclaration":
757-
return findJSDocComment(looksLikeExport(parent) ? parent : node);
757+
return findJSDocComment(
758+
isExportDeclaration(parent) ? parent : node,
759+
sourceCode,
760+
);
758761

759762
case "ClassExpression":
760-
return findJSDocComment(parent.parent);
763+
return findJSDocComment(parent.parent, sourceCode);
761764

762765
case "ArrowFunctionExpression":
763766
case "FunctionExpression":
@@ -783,11 +786,11 @@ function getJSDocComment(node, sourceCode) {
783786
parent.type !== "FunctionDeclaration" &&
784787
parent.type !== "Program"
785788
) {
786-
return findJSDocComment(parent);
789+
return findJSDocComment(parent, sourceCode);
787790
}
788791
}
789792

790-
return findJSDocComment(node);
793+
return findJSDocComment(node, sourceCode);
791794

792795
// falls through
793796
default:

0 commit comments

Comments
 (0)