Skip to content

Commit

Permalink
feat: add side-effect group to sort the imports
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jun 9, 2023
1 parent 05bf0f7 commit 02f51fb
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/rules/sort-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type Group =
| 'internal'
| 'parent'
| 'sibling'
| 'side-effect'
| 'index'
| 'object'
| 'style'
Expand Down Expand Up @@ -155,6 +156,8 @@ import Button from '~/components/Button'
import formatNumber from '../utils/format-number'
// siblings - Modules from the same directory
import config from './config'
// side-effect - Side effect imports
import './set-production-env.js'
// index - Main file from the current directory
import main from '.'
// object - TypeScript object-imports
Expand Down Expand Up @@ -233,6 +236,7 @@ If your project is written in TypeScript, you can read `tsconfig.json` and use `
"internal",
["parent-type", "sibling-type", "index-type"],
["parent", "sibling", "index"],
"side-effect",
"style",
"object",
"unknown"
Expand Down Expand Up @@ -273,6 +277,7 @@ export default [
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'side-effect'
'style',
'object',
'unknown',
Expand Down
4 changes: 4 additions & 0 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
defineGroup('style')
}

if (node.specifiers.length === 0) {
defineGroup('side-effect')
}

if (isIndex(node.source.value)) {
defineGroup('index')
}
Expand Down
108 changes: 108 additions & 0 deletions test/sort-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,42 @@ describe(RULE_NAME, () => {
invalid: [],
})
})

it(`${RULE_NAME}(${type}): separates side effect imports from the rest`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import { Sh艒yaIshida } from '../edu/kuise-hairdressing-school'
import { ShoukoNishimiya } from './salon-stray-cat'
import '../edu/prepare-students.js'
import './load-memories'
`,
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'],
'side-effect',
'object',
'unknown',
],
},
],
},
],
invalid: [],
})
})
})

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

it(`${RULE_NAME}(${type}): separates side effect imports from the rest`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import { Sh艒yaIshida } from '../edu/kuise-hairdressing-school'
import { ShoukoNishimiya } from './salon-stray-cat'
import '../edu/prepare-students.js'
import './load-memories'
`,
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'],
'side-effect',
'object',
'unknown',
],
},
],
},
],
invalid: [],
})
})
})

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

it(`${RULE_NAME}(${type}): separates side effect imports from the rest`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import { Sh艒yaIshida } from '../edu/kuise-hairdressing-school'
import { ShoukoNishimiya } from './salon-stray-cat'
import '../edu/prepare-students.js'
import './load-memories'
`,
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'],
'side-effect',
'object',
'unknown',
],
},
],
},
],
invalid: [],
})
})
})

describe(`${RULE_NAME}: misc`, () => {
Expand Down

0 comments on commit 02f51fb

Please sign in to comment.