Skip to content

Commit

Permalink
fix: disallow to sort default import specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jun 5, 2023
1 parent de061ff commit 60044c6
Show file tree
Hide file tree
Showing 2 changed files with 290 additions and 2 deletions.
10 changes: 8 additions & 2 deletions rules/sort-named-imports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { SortingNode } from '../typings'

import { AST_NODE_TYPES } from '@typescript-eslint/types'

import { createEslintRule } from '../utils/create-eslint-rule'
import { rangeToDiff } from '../utils/range-to-diff'
import { SortType, SortOrder } from '../typings'
Expand Down Expand Up @@ -67,7 +69,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
],
create: context => ({
ImportDeclaration: node => {
if (node.specifiers.length > 1) {
let specifiers = node.specifiers.filter(
({ type }) => type === AST_NODE_TYPES.ImportSpecifier,
)

if (specifiers.length > 1) {
let options = complete(context.options.at(0), {
type: SortType.alphabetical,
'ignore-case': false,
Expand All @@ -76,7 +82,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

let source = context.getSourceCode()

let nodes: SortingNode[] = node.specifiers.map(specifier => ({
let nodes: SortingNode[] = specifiers.map(specifier => ({
size: rangeToDiff(specifier.range),
name: specifier.local.name,
node: specifier,
Expand Down
282 changes: 282 additions & 0 deletions test/sort-named-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,100 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}: not sorts default specifiers`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import spiritedAway, { protagonist as chihiro } from 'spirited-away'
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
},
],
invalid: [],
})
})

it(`${RULE_NAME}: sorts with import aliases`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import titan, {
femaleTitan as annieLeonhart,
colossusTitan,
attackTitan as erenYeager,
armoredTitan as reinerBraun,
beastTitan as zekeYeager,
} from '~/titans'
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
},
],
invalid: [
{
code: dedent`
import titan, {
armoredTitan as reinerBraun,
colossusTitan,
beastTitan as zekeYeager,
attackTitan as erenYeager,
femaleTitan as annieLeonhart,
} from '~/titans'
`,
output: dedent`
import titan, {
femaleTitan as annieLeonhart,
colossusTitan,
attackTitan as erenYeager,
armoredTitan as reinerBraun,
beastTitan as zekeYeager,
} from '~/titans'
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedNamedImportsOrder',
data: {
first: 'reinerBraun',
second: 'colossusTitan',
},
},
{
messageId: 'unexpectedNamedImportsOrder',
data: {
first: 'zekeYeager',
second: 'erenYeager',
},
},
{
messageId: 'unexpectedNamedImportsOrder',
data: {
first: 'erenYeager',
second: 'annieLeonhart',
},
},
],
},
],
})
})
})

describe(`${RULE_NAME}: sorting by natural order`, () => {
Expand Down Expand Up @@ -382,6 +476,100 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}: not sorts default specifiers`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import spiritedAway, { protagonist as chihiro } from 'spirited-away'
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.asc,
},
],
},
],
invalid: [],
})
})

it(`${RULE_NAME}: sorts with import aliases`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import titan, {
femaleTitan as annieLeonhart,
colossusTitan,
attackTitan as erenYeager,
armoredTitan as reinerBraun,
beastTitan as zekeYeager,
} from '~/titans'
`,
options: [
{
type: SortType.natural,
order: SortOrder.asc,
},
],
},
],
invalid: [
{
code: dedent`
import titan, {
armoredTitan as reinerBraun,
colossusTitan,
beastTitan as zekeYeager,
attackTitan as erenYeager,
femaleTitan as annieLeonhart,
} from '~/titans'
`,
output: dedent`
import titan, {
femaleTitan as annieLeonhart,
colossusTitan,
attackTitan as erenYeager,
armoredTitan as reinerBraun,
beastTitan as zekeYeager,
} from '~/titans'
`,
options: [
{
type: SortType.natural,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedNamedImportsOrder',
data: {
first: 'reinerBraun',
second: 'colossusTitan',
},
},
{
messageId: 'unexpectedNamedImportsOrder',
data: {
first: 'zekeYeager',
second: 'erenYeager',
},
},
{
messageId: 'unexpectedNamedImportsOrder',
data: {
first: 'erenYeager',
second: 'annieLeonhart',
},
},
],
},
],
})
})
})

describe(`${RULE_NAME}: sorting by line length`, () => {
Expand Down Expand Up @@ -562,6 +750,100 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}: not sorts default specifiers`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import spiritedAway, { protagonist as chihiro } from 'spirited-away'
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.desc,
},
],
},
],
invalid: [],
})
})

it(`${RULE_NAME}: sorts with import aliases`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
import titan, {
femaleTitan as annieLeonhart,
armoredTitan as reinerBraun,
attackTitan as erenYeager,
beastTitan as zekeYeager,
colossusTitan,
} from '~/titans'
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.desc,
},
],
},
],
invalid: [
{
code: dedent`
import titan, {
armoredTitan as reinerBraun,
colossusTitan,
beastTitan as zekeYeager,
attackTitan as erenYeager,
femaleTitan as annieLeonhart,
} from '~/titans'
`,
output: dedent`
import titan, {
femaleTitan as annieLeonhart,
armoredTitan as reinerBraun,
attackTitan as erenYeager,
beastTitan as zekeYeager,
colossusTitan,
} from '~/titans'
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.desc,
},
],
errors: [
{
messageId: 'unexpectedNamedImportsOrder',
data: {
first: 'colossusTitan',
second: 'zekeYeager',
},
},
{
messageId: 'unexpectedNamedImportsOrder',
data: {
first: 'zekeYeager',
second: 'erenYeager',
},
},
{
messageId: 'unexpectedNamedImportsOrder',
data: {
first: 'erenYeager',
second: 'annieLeonhart',
},
},
],
},
],
})
})
})

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

0 comments on commit 60044c6

Please sign in to comment.