Skip to content

Commit

Permalink
fix: use service comments when sorting imports
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jun 5, 2023
1 parent dd2730b commit b577ac7
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 12 deletions.
25 changes: 15 additions & 10 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
import { getNodeRange } from '../utils/get-node-range'
import { rangeToDiff } from '../utils/range-to-diff'
import { TSConfig } from '../utils/read-ts-config'
import { getComment } from '../utils/get-comment'
import { SortType, SortOrder } from '../typings'
import { sortNodes } from '../utils/sort-nodes'
import { complete } from '../utils/complete'
Expand Down Expand Up @@ -267,24 +268,28 @@ export default createEslintRule<Options, MESSAGE_ID>({
}

let hasContentBetweenNodes = (
aNode: SortingNode,
bNode: SortingNode,
left: SortingNode,
right: SortingNode,
): boolean =>
!!source.getTokensBetween(aNode.node, bNode.node, {
includeComments: true,
}).length
!!source.getTokensBetween(
left.node,
getComment(right.node, source) || right.node,
{
includeComments: true,
},
).length

let getLinesBetweenImports = (
first: SortingNode,
second: SortingNode,
left: SortingNode,
right: SortingNode,
) => {
if (hasContentBetweenNodes(first, second)) {
if (hasContentBetweenNodes(left, right)) {
return 0
}

let linesBetweenImports = source.lines.slice(
first.node.loc.end.line,
second.node.loc.start.line - 1,
left.node.loc.end.line,
right.node.loc.start.line - 1,
)

return linesBetweenImports.filter(line => !line.trim().length).length
Expand Down
102 changes: 102 additions & 0 deletions test/sort-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,40 @@ describe(RULE_NAME, () => {
invalid: [],
})
})

it(`${RULE_NAME}(${type}): ignores comments for counting lines between imports`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import type { SunshineGirl } from 'weathering-with-you'
// @ts-expect-error missing types
import { hinaAmano } from 'weathering-with-you'
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
'newlines-between': NewlinesBetweenValue.always,
'internal-pattern': ['~/**'],
groups: [
'type',
['builtin', 'external'],
'internal-type',
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'object',
'unknown',
],
},
],
},
],
invalid: [],
})
})
})

describe(`${RULE_NAME}: sorting by natural order`, () => {
Expand Down Expand Up @@ -1322,6 +1356,40 @@ describe(RULE_NAME, () => {
invalid: [],
})
})

it(`${RULE_NAME}(${type}): ignores comments for counting lines between imports`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import type { SunshineGirl } from 'weathering-with-you'
// @ts-expect-error missing types
import { hinaAmano } from 'weathering-with-you'
`,
options: [
{
type: SortType.natural,
order: SortOrder.asc,
'newlines-between': NewlinesBetweenValue.always,
'internal-pattern': ['~/**'],
groups: [
'type',
['builtin', 'external'],
'internal-type',
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'object',
'unknown',
],
},
],
},
],
invalid: [],
})
})
})

describe(`${RULE_NAME}: sorting by line length`, () => {
Expand Down Expand Up @@ -2021,6 +2089,40 @@ describe(RULE_NAME, () => {
invalid: [],
})
})

it(`${RULE_NAME}(${type}): ignores comments for counting lines between imports`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import type { SunshineGirl } from 'weathering-with-you'
// @ts-expect-error missing types
import { hinaAmano } from 'weathering-with-you'
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.desc,
'newlines-between': NewlinesBetweenValue.always,
'internal-pattern': ['~/**'],
groups: [
'type',
['builtin', 'external'],
'internal-type',
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'object',
'unknown',
],
},
],
},
],
invalid: [],
})
})
})

describe(`${RULE_NAME}: misc`, () => {
Expand Down
5 changes: 3 additions & 2 deletions utils/get-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ export let getComment = (
})

if (
previousTokenOrComment?.type === AST_TOKEN_TYPES.Block ||
previousTokenOrComment?.type === AST_TOKEN_TYPES.Line
(previousTokenOrComment?.type === AST_TOKEN_TYPES.Block ||
previousTokenOrComment?.type === AST_TOKEN_TYPES.Line) &&
node.loc.start.line - previousTokenOrComment.loc.end.line <= 1
) {
return previousTokenOrComment
}
Expand Down

0 comments on commit b577ac7

Please sign in to comment.