Skip to content

Commit 01da88a

Browse files
committed
fix: disable sorting side-effect imports
1 parent 90feb82 commit 01da88a

2 files changed

Lines changed: 91 additions & 71 deletions

File tree

rules/sort-imports.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
182182

183183
let nodes: SortingNodeWithGroup<string[]>[] = []
184184

185+
let isSideEffectImport = (node: TSESTree.Node) =>
186+
node.type === AST_NODE_TYPES.ImportDeclaration &&
187+
node.specifiers.length === 0
188+
185189
let computeGroup = (node: ModuleDeclaration): Group<string[]> => {
186190
let group: Group<string[]> | undefined
187191

@@ -282,7 +286,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
282286
defineGroup('style')
283287
}
284288

285-
if (node.specifiers.length === 0) {
289+
if (isSideEffectImport(node)) {
286290
defineGroup('side-effect')
287291
}
288292

@@ -496,6 +500,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
496500
let numberOfEmptyLinesBetween = getLinesBetweenImports(left, right)
497501

498502
if (
503+
!(
504+
isSideEffectImport(left.node) && isSideEffectImport(right.node)
505+
) &&
499506
!hasContentBetweenNodes(left, right) &&
500507
(leftNum > rightNum ||
501508
(leftNum === rightNum && compare(left, right, options)))

test/sort-imports.test.ts

Lines changed: 83 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3260,81 +3260,94 @@ describe(RULE_NAME, () => {
32603260
],
32613261
})
32623262
})
3263-
})
32643263

3265-
it(`${RULE_NAME}: allow to use paths from tsconfig.json`, () => {
3266-
vi.mock('../utils/read-ts-config', () => ({
3267-
TSConfig: {
3268-
get: () => ({
3269-
compilerOptions: {
3270-
moduleResolution: 'bundler',
3271-
verbatimModuleSyntax: true,
3272-
resolveJsonModule: true,
3273-
lib: ['ESNext', 'DOM'],
3274-
esModuleInterop: true,
3275-
skipLibCheck: true,
3276-
module: 'esnext',
3277-
target: 'es2020',
3278-
paths: {
3279-
'@/components/*': './src/components/*',
3264+
it(`${RULE_NAME}: allow to use paths from tsconfig.json`, () => {
3265+
vi.mock('../utils/read-ts-config', () => ({
3266+
TSConfig: {
3267+
get: () => ({
3268+
compilerOptions: {
3269+
moduleResolution: 'bundler',
3270+
verbatimModuleSyntax: true,
3271+
resolveJsonModule: true,
3272+
lib: ['ESNext', 'DOM'],
3273+
esModuleInterop: true,
3274+
skipLibCheck: true,
3275+
module: 'esnext',
3276+
target: 'es2020',
3277+
paths: {
3278+
'@/components/*': './src/components/*',
3279+
},
32803280
},
3281-
},
3282-
}),
3283-
},
3284-
}))
3285-
3286-
ruleTester.run(RULE_NAME, rule, {
3287-
valid: [],
3288-
invalid: [
3289-
{
3290-
code: dedent`
3291-
import { MikuruAsahina } from '@/components/mikuru'
3292-
import { HaruhiSuzumiya } from '@melancholy/haruhi-suzumiya'
3293-
import { YukiNagato } from '~/data/yuki'
3294-
`,
3295-
output: dedent`
3296-
import { HaruhiSuzumiya } from '@melancholy/haruhi-suzumiya'
3281+
}),
3282+
},
3283+
}))
32973284

3298-
import { MikuruAsahina } from '@/components/mikuru'
3299-
import { YukiNagato } from '~/data/yuki'
3300-
`,
3301-
options: [
3302-
{
3303-
type: SortType['line-length'],
3304-
order: SortOrder.desc,
3305-
'newlines-between': NewlinesBetweenValue.always,
3306-
'internal-pattern': ['~/**'],
3307-
groups: [
3308-
'type',
3309-
['builtin', 'external'],
3310-
'internal-type',
3311-
'internal',
3312-
['parent-type', 'sibling-type', 'index-type'],
3313-
['parent', 'sibling', 'index'],
3314-
'object',
3315-
'unknown',
3316-
],
3317-
'read-tsconfig': true,
3318-
},
3319-
],
3320-
errors: [
3321-
{
3322-
messageId: 'unexpectedImportsOrder',
3323-
data: {
3324-
left: '@/components/mikuru',
3325-
right: '@melancholy/haruhi-suzumiya',
3285+
ruleTester.run(RULE_NAME, rule, {
3286+
valid: [],
3287+
invalid: [
3288+
{
3289+
code: dedent`
3290+
import { MikuruAsahina } from '@/components/mikuru'
3291+
import { HaruhiSuzumiya } from '@melancholy/haruhi-suzumiya'
3292+
import { YukiNagato } from '~/data/yuki'
3293+
`,
3294+
output: dedent`
3295+
import { HaruhiSuzumiya } from '@melancholy/haruhi-suzumiya'
3296+
3297+
import { MikuruAsahina } from '@/components/mikuru'
3298+
import { YukiNagato } from '~/data/yuki'
3299+
`,
3300+
options: [
3301+
{
3302+
type: SortType['line-length'],
3303+
order: SortOrder.desc,
3304+
'newlines-between': NewlinesBetweenValue.always,
3305+
'internal-pattern': ['~/**'],
3306+
groups: [
3307+
'type',
3308+
['builtin', 'external'],
3309+
'internal-type',
3310+
'internal',
3311+
['parent-type', 'sibling-type', 'index-type'],
3312+
['parent', 'sibling', 'index'],
3313+
'object',
3314+
'unknown',
3315+
],
3316+
'read-tsconfig': true,
33263317
},
3327-
},
3328-
{
3329-
messageId: 'missedSpacingBetweenImports',
3330-
data: {
3331-
left: '@melancholy/haruhi-suzumiya',
3332-
right: '~/data/yuki',
3318+
],
3319+
errors: [
3320+
{
3321+
messageId: 'unexpectedImportsOrder',
3322+
data: {
3323+
left: '@/components/mikuru',
3324+
right: '@melancholy/haruhi-suzumiya',
3325+
},
33333326
},
3334-
},
3335-
],
3336-
},
3337-
],
3327+
{
3328+
messageId: 'missedSpacingBetweenImports',
3329+
data: {
3330+
left: '@melancholy/haruhi-suzumiya',
3331+
right: '~/data/yuki',
3332+
},
3333+
},
3334+
],
3335+
},
3336+
],
3337+
})
3338+
})
3339+
3340+
it(`${RULE_NAME}: doesn't sort imports with side effects`, () => {
3341+
ruleTester.run(RULE_NAME, rule, {
3342+
valid: [
3343+
dedent`
3344+
import './index.css'
3345+
import './animate.css'
3346+
import './reset.css'
3347+
`,
3348+
],
3349+
invalid: [],
3350+
})
33383351
})
33393352
})
33403353
})

0 commit comments

Comments
 (0)