azat-io / eslint-plugin-perfectionist

🦄 ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.
https://perfectionist.dev
MIT License
1.72k stars 35 forks source link

Feature: Grouping imports together #157

Closed Ericlm closed 1 week ago

Ericlm commented 1 month ago

Describe the rule

The rule would check that all imports are at the top of the file, to ensure that:

Code example

This is the code that should be reported (from a Vue project) :

import { storeToRefs } from 'pinia'
import { computed, onMounted } from 'vue'

const val = 0

import { RouterView, useRoute } from 'vue-router' // ⚠️ does not go to the beauty salon

Additional comments

Validations

Ericlm commented 2 weeks ago

Looking at the rule, it looks like there is a hasContentBetweenNodes function that build groups of imports to be sorted later. That allows the rule to handle multiple import groups (not the user-defined groups, but groups defined by the presence of content around them) inside the same file.

import { } from 'b' // Group with single import statement

const val = 2

// Group with two import statements, they will be sorted
import { } from 'a' // Not required to be on top of 'b'
import { } from 'c' // Cannot be on top of 'a'

I think it would be great to ignore statements between import so that imports are always sorted in one group. And then that's up to the dev to chose where this block goes (very often at the top I think.)

Ericlm commented 1 week ago

I'm gonna close it as it is more something like "put imports first" :)