feat(unstable): support comments in lint plugin (#29189)

This PR adds support for comments in the AST for lint plugins.

- The `Program` node has a `comments` field pointing to an array of all
comments.
- `SourceCode.getAllComments()`: Returns all comments (same as
`program.comments`)
- `SourceCode.getCommentsBefore(node)`: Get all comments before this
Node
- `SourceCode.getCommentsAfter(node)`: Get all comments after this Node
- `SourceCode.getCommentsInside(node)`: Get all comments inside this
Node

ESLint docs:
-
https://eslint.org/docs/latest/extend/custom-rules#accessing-the-source-code
- https://eslint.org/docs/latest/extend/custom-rules#accessing-comments
This commit is contained in:
Marvin Hagemeister 2025-05-08 21:59:36 +02:00 committed by GitHub
parent e1e67a703c
commit c015b8affd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 352 additions and 7 deletions

View file

@ -1406,6 +1406,27 @@ declare namespace Deno {
* current node.
*/
getAncestors(node: Node): Node[];
/**
* Get all comments inside the source.
*/
getAllComments(): Array<LineComment | BlockComment>;
/**
* Get leading comments before a node.
*/
getCommentsBefore(node: Node): Array<LineComment | BlockComment>;
/**
* Get trailing comments after a node.
*/
getCommentsAfter(node: Node): Array<LineComment | BlockComment>;
/**
* Get comments inside a node.
*/
getCommentsInside(node: Node): Array<LineComment | BlockComment>;
/**
* Get the full source code.
*/
@ -1532,6 +1553,7 @@ declare namespace Deno {
range: Range;
sourceType: "module" | "script";
body: Statement[];
comments: Array<LineComment | BlockComment>;
}
/**
@ -4335,6 +4357,28 @@ declare namespace Deno {
| TSUnknownKeyword
| TSVoidKeyword;
/**
* A single line comment
* @category Linter
* @experimental
*/
export interface LineComment {
type: "Line";
range: Range;
value: string;
}
/**
* A potentially multi-line block comment
* @category Linter
* @experimental
*/
export interface BlockComment {
type: "Block";
range: Range;
value: string;
}
/**
* Union type of all possible AST nodes
* @category Linter
@ -4394,7 +4438,9 @@ declare namespace Deno {
| TSIndexSignature
| TSTypeAnnotation
| TSTypeParameterDeclaration
| TSTypeParameter;
| TSTypeParameter
| LineComment
| BlockComment;
export {}; // only export exports
}