Skip to content

Commit

Permalink
feat: get following comment (experimental)
Browse files Browse the repository at this point in the history
Also:
- test: coverage
  • Loading branch information
brettz9 committed Jul 8, 2024
1 parent b2255cf commit f6c26c8
Show file tree
Hide file tree
Showing 9 changed files with 1,153 additions and 66 deletions.
2 changes: 0 additions & 2 deletions .ncurc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@

module.exports = {
reject: [
// Wait until ash-nazg can work with it and use flat config
'eslint-plugin-array-func'
]
};
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGES for `@es-joy/jsdoccomment`

## ?

- feat: get following comment (experimental)

## 0.44.0

- feat: add `getNonJsdocComment` for getting non-JSDoc comments above node
Expand Down
81 changes: 70 additions & 11 deletions dist/index.cjs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -753,15 +753,13 @@ const getTSFunctionComment = function (astNode) {
}
return greatGreatGrandparent.parent;
}

/* v8 ignore next */
return astNode;
case 'FunctionExpression':
/* v8 ignore next 3 */
if (!greatGreatGrandparent) {
return astNode;
}
/* v8 ignore next 3 */
if (greatGrandparent.type === 'MethodDefinition') {
return greatGrandparent;
}
Expand All @@ -778,28 +776,22 @@ const getTSFunctionComment = function (astNode) {
if (!greatGreatGrandparent) {
return astNode;
}

/* v8 ignore next */
switch (greatGrandparent.type) {
case 'ArrowFunctionExpression':
/* v8 ignore next 6 */
if (greatGreatGrandparent.type === 'VariableDeclarator' && greatGreatGrandparent.parent.type === 'VariableDeclaration') {
return greatGreatGrandparent.parent;
}

/* v8 ignore next */
return astNode;
case 'FunctionDeclaration':
return greatGrandparent;
case 'VariableDeclarator':
/* v8 ignore next 3 */
if (greatGreatGrandparent.type === 'VariableDeclaration') {
return greatGreatGrandparent;
}

/* v8 ignore next 2 */
// Fallthrough
default:
/* v8 ignore next */
/* v8 ignore next 3 */
return astNode;
}
};
Expand Down Expand Up @@ -890,7 +882,6 @@ const getReducedASTNode = function (node, sourceCode) {
* @param {{nonJSDoc?: boolean}} [opts]
* @returns {Token|null} The Block comment token containing the JSDoc comment
* for the given node or null if not found.
* @private
*/
const findJSDocComment = (astNode, sourceCode, settings, opts = {}) => {
var _parenthesisToken, _parenthesisToken2;
Expand Down Expand Up @@ -980,6 +971,73 @@ const getNonJsdocComment = function (sourceCode, node, settings) {
});
};

/**
* @param {import('estree').Node|import('eslint').AST.Token|
* import('estree').Comment
* } nodeA The AST node or token to compare
* @param {import('estree').Node|import('eslint').AST.Token|
* import('estree').Comment} nodeB The
* AST node or token to compare
*/
const compareLocEndToStart = (nodeA, nodeB) => {
var _nodeA$loc$end$line, _nodeA$loc, _nodeB$loc$start$line, _nodeB$loc;
/* v8 ignore next */
return ((_nodeA$loc$end$line = (_nodeA$loc = nodeA.loc) === null || _nodeA$loc === void 0 ? void 0 : _nodeA$loc.end.line) !== null && _nodeA$loc$end$line !== void 0 ? _nodeA$loc$end$line : 0) === ((_nodeB$loc$start$line = (_nodeB$loc = nodeB.loc) === null || _nodeB$loc === void 0 ? void 0 : _nodeB$loc.start.line) !== null && _nodeB$loc$start$line !== void 0 ? _nodeB$loc$start$line : 0);
};

/**
* Checks for the presence of a comment following the given node and
* returns it.
*
* @param {import('eslint').SourceCode} sourceCode
* @param {import('eslint').Rule.Node} astNode The AST node to get
* the comment for.
* @returns {Token|null} The comment token containing the comment
* for the given node or null if not found.
*/
const getFollowingComment = function (sourceCode, astNode) {
/**
* @param {import('estree').Node} node The
* AST node to get the comment for.
*/
const getTokensAfterIgnoringParentheses = node => {
const tokenAfter = sourceCode.getTokenAfter(node, {
includeComments: true
});

// while (
// tokenAfter && tokenAfter.type === 'Punctuator' &&
// tokenAfter.value === ')'
// ) {
// [tokenAfter] = sourceCode.getTokensAfter(tokenAfter, {
// includeComments: true
// });
// }
return tokenAfter;
};

/**
* @param {import('estree').Node} node The
* AST node to get the comment for.
*/
const tokenAfterIgnoringParentheses = node => {
const tokenAfter = getTokensAfterIgnoringParentheses(node);
return tokenAfter && isCommentToken(tokenAfter) && compareLocEndToStart(node, tokenAfter) ? tokenAfter : null;
};
let tokenAfter = tokenAfterIgnoringParentheses(astNode);
if (!tokenAfter) {
switch (astNode.type) {
case 'FunctionDeclaration':
tokenAfter = tokenAfterIgnoringParentheses(astNode.body);
break;
case 'ExpressionStatement':
tokenAfter = tokenAfterIgnoringParentheses(astNode.expression);
break;
}
}
return tokenAfter;
};

/**
* @param {RegExpMatchArray & {
* indices: {
Expand Down Expand Up @@ -1251,6 +1309,7 @@ exports.defaultNoTypes = defaultNoTypes;
exports.estreeToString = estreeToString;
exports.findJSDocComment = findJSDocComment;
exports.getDecorator = getDecorator;
exports.getFollowingComment = getFollowingComment;
exports.getJSDocComment = getJSDocComment;
exports.getNonJsdocComment = getNonJsdocComment;
exports.getReducedASTNode = getReducedASTNode;
Expand Down
13 changes: 12 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ declare function getDecorator(
* @param {{nonJSDoc?: boolean}} [opts]
* @returns {Token|null} The Block comment token containing the JSDoc comment
* for the given node or null if not found.
* @private
*/
declare function findJSDocComment(
astNode: eslint.Rule.Node,
Expand All @@ -234,6 +233,17 @@ declare function findJSDocComment(
nonJSDoc?: boolean;
},
): Token | null;
/**
* Checks for the presence of a comment following the given node and
* returns it.
*
* @param {import('eslint').SourceCode} sourceCode
* @param {import('eslint').Rule.Node} astNode The AST node to get
* the comment for.
* @returns {Token|null} The comment token containing the comment
* for the given node or null if not found.
*/
declare function getFollowingComment(sourceCode: eslint.SourceCode, astNode: eslint.Rule.Node): Token | null;

declare function hasSeeWithLink(spec: comment_parser.Spec): boolean;
declare const defaultNoTypes: string[];
Expand Down Expand Up @@ -336,6 +346,7 @@ export {
estreeToString,
findJSDocComment,
getDecorator,
getFollowingComment,
getJSDocComment,
getNonJsdocComment,
getReducedASTNode,
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"prepublishOnly": "pnpm i && npm run build",
"test": "npm run lint && npm run build && npm run test-ui",
"test-ui": "vitest --ui --coverage",
"test-cov": "vitest --coverage",
"tsc": "tsc",
"types": "esm-d-ts gen ./src/index.js --output ./dist/index.d.ts"
},
Expand All @@ -56,7 +57,6 @@
"dependencies": {
"@types/eslint": "^8.56.5",
"@types/estree": "^1.0.5",
"@typescript-eslint/types": "^7.2.0",
"comment-parser": "1.4.1",
"esquery": "^1.5.0",
"jsdoc-type-pratt-parser": "~4.0.0"
Expand All @@ -69,6 +69,8 @@
"@rollup/plugin-babel": "^6.0.4",
"@types/esquery": "^1.5.3",
"@types/estraverse": "^5.1.7",
"@typescript-eslint/types": "^7.2.0",
"@typescript-eslint/visitor-keys": "^7.15.0",
"@typhonjs-build-test/esm-d-ts": "0.3.0-next.1",
"@typhonjs-typedoc/typedoc-pkg": "^0.0.4",
"@vitest/coverage-v8": "^1.3.0",
Expand All @@ -93,6 +95,7 @@
"estraverse": "^5.3.0",
"rollup": "^4.9.6",
"typescript": "^5.4.2",
"typescript-eslint": "^7.15.0",
"vitest": "^1.3.0"
},
"files": [
Expand Down
Loading

0 comments on commit f6c26c8

Please sign in to comment.