@@ -9,7 +9,7 @@ interface GetCommentsBeforeParameters {
99
1010/**
1111 * Returns a list of comments before a given node, excluding ones that are
12- * right after code. Includes comment blocks.
12+ * right after code. Includes comment blocks, ignore shebang comments .
1313 * @param {object } params - Parameters object.
1414 * @param {TSESTree.Node } params.node - The node to get comments before.
1515 * @param {TSESLint.SourceCode } params.sourceCode - The source code object.
@@ -22,7 +22,7 @@ export let getCommentsBefore = ({
2222 sourceCode,
2323 node,
2424} : GetCommentsBeforeParameters ) : TSESTree . Comment [ ] => {
25- let commentsBefore = getCommentsBeforeNodeOrToken ( sourceCode , node )
25+ let commentsBefore = getRelevantCommentsBeforeNodeOrToken ( sourceCode , node )
2626 let tokenBeforeNode = sourceCode . getTokenBefore ( node )
2727 if (
2828 commentsBefore . length > 0 ||
@@ -31,18 +31,25 @@ export let getCommentsBefore = ({
3131 ) {
3232 return commentsBefore
3333 }
34- return getCommentsBeforeNodeOrToken ( sourceCode , tokenBeforeNode )
34+ return getRelevantCommentsBeforeNodeOrToken ( sourceCode , tokenBeforeNode )
3535}
3636
37- let getCommentsBeforeNodeOrToken = (
37+ let getRelevantCommentsBeforeNodeOrToken = (
3838 source : TSESLint . SourceCode ,
3939 node : TSESTree . Token | TSESTree . Node ,
4040) : TSESTree . Comment [ ] =>
41- source . getCommentsBefore ( node ) . filter ( comment => {
42- /**
43- * `getCommentsBefore` also returns comments that are right after code,
44- * filter those out
45- */
46- let tokenBeforeComment = source . getTokenBefore ( comment )
47- return tokenBeforeComment ?. loc . end . line !== comment . loc . end . line
48- } )
41+ source
42+ . getCommentsBefore ( node )
43+ . filter ( comment => ! isShebangComment ( comment ) )
44+ . filter ( comment => {
45+ /**
46+ * `getCommentsBefore` also returns comments that are right after code,
47+ * filter those out
48+ */
49+ let tokenBeforeComment = source . getTokenBefore ( comment )
50+ return tokenBeforeComment ?. loc . end . line !== comment . loc . end . line
51+ } )
52+
53+ let isShebangComment = ( comment : TSESTree . Comment ) : boolean =>
54+ comment . type === ( 'Shebang' as unknown ) ||
55+ comment . type === ( 'Hashbang' as unknown )
0 commit comments