Skip to content

Commit d08e071

Browse files
authored
fix: ignore shebang comments
1 parent 28d2170 commit d08e071

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

test/rules/sort-imports.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3934,6 +3934,34 @@ describe(ruleName, () => {
39343934
},
39353935
)
39363936
})
3937+
3938+
ruleTester.run(`${ruleName}(${type}): ignores shebang comments`, rule, {
3939+
invalid: [
3940+
{
3941+
errors: [
3942+
{
3943+
data: {
3944+
right: 'a',
3945+
left: 'b',
3946+
},
3947+
messageId: 'unexpectedImportsOrder',
3948+
},
3949+
],
3950+
output: dedent`
3951+
#!/usr/bin/node
3952+
import { a } from 'a'
3953+
import { b } from 'b'
3954+
`,
3955+
code: dedent`
3956+
#!/usr/bin/node
3957+
import { b } from 'b'
3958+
import { a } from 'a'
3959+
`,
3960+
options: [options],
3961+
},
3962+
],
3963+
valid: [],
3964+
})
39373965
})
39383966

39393967
describe(`${ruleName}: sorting by natural order`, () => {

utils/get-comments-before.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)