@@ -4,11 +4,31 @@ import type { TSESTree } from '@typescript-eslint/types'
4
4
/**
5
5
* Returns a list of comments before a given node, excluding ones that are
6
6
* right after code. Includes comment blocks.
7
+ * @param node The node to get comments before
8
+ * @param source The source code
9
+ * @param tokenValueToIgnoreBefore Allows the following token to directly precede the node
7
10
*/
8
- export let getCommentsBefore = (
11
+ export const getCommentsBefore = (
9
12
node : TSESTree . Node ,
10
13
source : TSESLint . SourceCode ,
11
- ) : TSESTree . Comment [ ] =>
14
+ tokenValueToIgnoreBefore ?: string ,
15
+ ) : TSESTree . Comment [ ] => {
16
+ let commentsBefore = getCommentsBeforeNodeOrToken ( source , node )
17
+ let tokenBeforeNode = source . getTokenBefore ( node )
18
+ if (
19
+ commentsBefore . length ||
20
+ ! tokenValueToIgnoreBefore ||
21
+ tokenBeforeNode ?. value !== tokenValueToIgnoreBefore
22
+ ) {
23
+ return commentsBefore
24
+ }
25
+ return getCommentsBeforeNodeOrToken ( source , tokenBeforeNode )
26
+ }
27
+
28
+ const getCommentsBeforeNodeOrToken = (
29
+ source : TSESLint . SourceCode ,
30
+ node : TSESTree . Token | TSESTree . Node ,
31
+ ) =>
12
32
source . getCommentsBefore ( node ) . filter ( comment => {
13
33
// 'getCommentsBefore' also returns comments that are right after code, filter those out
14
34
let tokenBeforeComment = source . getTokenBefore ( comment )
0 commit comments