Skip to content

Commit

Permalink
feat: add style 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 65fe6c7 commit 05bf0f7
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 9 deletions.
5 changes: 5 additions & 0 deletions docs/rules/sort-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type Group =
| 'sibling'
| 'index'
| 'object'
| 'style'
| 'type'
| 'internal-type'
| 'parent-type'
Expand Down Expand Up @@ -158,6 +159,8 @@ import config from './config'
import main from '.'
// object - TypeScript object-imports
import log = console.log
// style - Styles
import styles from './index.module.css'
// type - TypeScript type imports
import type { FC } from 'react'
// internal-type - TypeScript type imports from your internal modules
Expand Down Expand Up @@ -230,6 +233,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"],
"style",
"object",
"unknown"
],
Expand Down Expand Up @@ -269,6 +273,7 @@ export default [
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'style',
'object',
'unknown',
],
Expand Down
43 changes: 34 additions & 9 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export enum NewlinesBetweenValue {
type Group =
| 'internal-type'
| 'sibling-type'
| 'side-effect'
| 'parent-type'
| 'index-type'
| 'external'
Expand All @@ -41,6 +42,7 @@ type Group =
| 'sibling'
| 'object'
| 'parent'
| 'style'
| 'index'
| 'type'

Expand Down Expand Up @@ -161,6 +163,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
let computeGroup = (node: ModuleDeclaration): Group => {
let group: undefined | Group

let isStyle = (value: string) =>
['.less', '.scss', '.sass', '.pcss', '.css', '.sss'].some(extension =>
value.endsWith(extension),
)

let isIndex = (value: string) =>
[
'./index.d.js',
Expand Down Expand Up @@ -193,11 +200,17 @@ export default createEslintRule<Options, MESSAGE_ID>({
if (node.type === AST_NODE_TYPES.ImportDeclaration) {
if (isInternal(node)) {
defineGroup('internal-type')
} else if (isIndex(node.source.value)) {
}

if (isIndex(node.source.value)) {
defineGroup('index-type')
} else if (isParent(node.source.value)) {
}

if (isParent(node.source.value)) {
defineGroup('parent-type')
} else if (isSibling(node.source.value)) {
}

if (isSibling(node.source.value)) {
defineGroup('sibling-type')
}
}
Expand All @@ -208,17 +221,29 @@ export default createEslintRule<Options, MESSAGE_ID>({
if (!group && node.type === AST_NODE_TYPES.ImportDeclaration) {
if (isCoreModule(node.source.value)) {
defineGroup('builtin')
} else if (isInternal(node)) {
}

if (isInternal(node)) {
defineGroup('internal')
} else if (isIndex(node.source.value)) {
}

if (isStyle(node.source.value)) {
defineGroup('style')
}

if (isIndex(node.source.value)) {
defineGroup('index')
} else if (isParent(node.source.value)) {
}

if (isParent(node.source.value)) {
defineGroup('parent')
} else if (isSibling(node.source.value)) {
}

if (isSibling(node.source.value)) {
defineGroup('sibling')
} else {
defineGroup('external')
}

defineGroup('external')
}

return group ?? 'unknown'
Expand Down
105 changes: 105 additions & 0 deletions test/sort-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,41 @@ describe(RULE_NAME, () => {
invalid: [],
})
})

it(`${RULE_NAME}(${type}): separates style imports from the rest`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import { MatsuriShihou, AonoMorimiya } from 'sola'
import styles from '../sunrise.css'
import './sky.css'
`,
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'],
'style',
'object',
'unknown',
],
},
],
},
],
invalid: [],
})
})
})

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

it(`${RULE_NAME}(${type}): separates style imports from the rest`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import { MatsuriShihou, AonoMorimiya } from 'sola'
import styles from '../sunrise.css'
import './sky.css'
`,
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'],
'style',
'object',
'unknown',
],
},
],
},
],
invalid: [],
})
})
})

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

it(`${RULE_NAME}(${type}): separates style imports from the rest`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import { MatsuriShihou, AonoMorimiya } from 'sola'
import styles from '../sunrise.css'
import './sky.css'
`,
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'],
'style',
'object',
'unknown',
],
},
],
},
],
invalid: [],
})
})
})

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

0 comments on commit 05bf0f7

Please sign in to comment.