Skip to content

Commit

Permalink
feat: add ability to disable or enable side effect imports sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Sep 7, 2024
1 parent 4e0e6d8 commit ae02009
Show file tree
Hide file tree
Showing 3 changed files with 421 additions and 5 deletions.
9 changes: 9 additions & 0 deletions docs/content/rules/sort-imports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ Allows you to specify a pattern for identifying internal imports. This is useful

You can use glob patterns to define these internal imports.

### sortSideEffects

<sub>default: `false`</sub>

Specifies whether side effect imports should be sorted. By default, sorting side-effect imports is disabled for security reasons.

- `true` — Sort side effect imports.
- `false` — Do not sort side effect imports.

### newlinesBetween

<sub>default: `'always'`</sub>
Expand Down
24 changes: 19 additions & 5 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Options<T extends string[]> = [
groups: (Group<T>[] | Group<T>)[]
environment: 'node' | 'bun'
internalPattern: string[]
sortSideEffects: boolean
maxLineLength?: number
order: 'desc' | 'asc'
ignoreCase: boolean
Expand Down Expand Up @@ -101,6 +102,11 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
},
type: 'array',
},
sortSideEffects: {
description:
'Controls whether side-effect imports should be sorted.',
type: 'boolean',
},
newlinesBetween: {
description:
'Specifies how new lines should be handled between import groups.',
Expand Down Expand Up @@ -197,6 +203,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
order: 'asc',
ignoreCase: true,
internalPattern: ['~/**'],
sortSideEffects: false,
newlinesBetween: 'always',
maxLineLength: undefined,
groups: [
Expand Down Expand Up @@ -230,6 +237,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
customGroups: { type: {}, value: {} },
internalPattern: ['~/**'],
newlinesBetween: 'always',
sortSideEffects: false,
type: 'alphabetical',
environment: 'node',
ignoreCase: true,
Expand Down Expand Up @@ -524,10 +532,14 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
if (!(groupNum in grouped)) {
grouped[groupNum] = [node]
} else {
grouped[groupNum] = sortNodes(
[...grouped[groupNum], node],
options,
)
if (!options.sortSideEffects && isSideEffectImport(node.node)) {
grouped[groupNum] = [...grouped[groupNum], node]
} else {
grouped[groupNum] = sortNodes(
[...grouped[groupNum], node],
options,
)
}
}
}

Expand Down Expand Up @@ -643,7 +655,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (
!(
isSideEffectImport(left.node) && isSideEffectImport(right.node)
!options.sortSideEffects &&
isSideEffectImport(left.node) &&
isSideEffectImport(right.node)
) &&
!hasContentBetweenNodes(left, right) &&
(leftNum > rightNum ||
Expand Down
Loading

0 comments on commit ae02009

Please sign in to comment.