Skip to content

Commit cd266b4

Browse files
committed
docs(linter/plugins): improve docs for comments APIs (#14754)
Follow-on after #14715. I for one had completely misunderstood what the comments APIs are meant to do. Expand the docs for these methods to clarify their behavior.
1 parent ec816ba commit cd266b4

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

apps/oxlint/src-js/plugins/comments.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const WHITESPACE_ONLY_REGEXP = /^\s*$/;
77

88
/**
99
* Retrieve an array containing all comments in the source code.
10-
* @returns Array of `Comment`s in occurrence order.
10+
* @returns Array of `Comment`s in order they appear in source.
1111
*/
1212
export function getAllComments(): Comment[] {
1313
if (ast === null) initAst();
@@ -16,7 +16,19 @@ export function getAllComments(): Comment[] {
1616
}
1717

1818
/**
19-
* Get all comment tokens directly before the given node or token.
19+
* Get all comments directly before the given node or token.
20+
*
21+
* "Directly before" means only comments before this node, and after the preceding token.
22+
*
23+
* ```js
24+
* // Define `x`
25+
* const x = 1;
26+
* // Define `y`
27+
* const y = 2;
28+
* ```
29+
*
30+
* `sourceCode.getCommentsBefore(varDeclY)` will only return "Define `y`" comment, not also "Define `x`".
31+
*
2032
* @param nodeOrToken - The AST node or token to check for adjacent comment tokens.
2133
* @returns Array of `Comment`s in occurrence order.
2234
*/
@@ -65,6 +77,19 @@ export function getCommentsBefore(nodeOrToken: NodeOrToken): Comment[] {
6577

6678
/**
6779
* Get all comment tokens directly after the given node or token.
80+
*
81+
* "Directly after" means only comments between end of this node, and the next token following it.
82+
*
83+
* ```js
84+
* const x = 1;
85+
* // Define `y`
86+
* const y = 2;
87+
* // Define `z`
88+
* const z = 3;
89+
* ```
90+
*
91+
* `sourceCode.getCommentsAfter(varDeclX)` will only return "Define `y`" comment, not also "Define `z`".
92+
*
6893
* @param nodeOrToken - The AST node or token to check for adjacent comment tokens.
6994
* @returns Array of `Comment`s in occurrence order.
7095
*/
@@ -140,9 +165,9 @@ export function getCommentsInside(node: Node): Comment[] {
140165

141166
/**
142167
* Check whether any comments exist or not between the given 2 nodes.
143-
* @param nodeOrToken1 - The node to check.
144-
* @param nodeOrToken2 - The node to check.
145-
* @returns `true` if one or more comments exist.
168+
* @param nodeOrToken1 - Start node/token.
169+
* @param nodeOrToken2 - End node/token.
170+
* @returns `true` if one or more comments exist between the two.
146171
*/
147172
export function commentsExistBetween(nodeOrToken1: NodeOrToken, nodeOrToken2: NodeOrToken): boolean {
148173
if (ast === null) initAst();

0 commit comments

Comments
 (0)