eslint-plugin-import icon indicating copy to clipboard operation
eslint-plugin-import copied to clipboard

import/order: "type" group is not enough

Open iliubinskii opened this issue 3 years ago • 5 comments

Currently, the rule offers the following groups: "builtin", "external", "internal", "unknown", "parent", "sibling", "index", "object", "type"

To me, having single "type" group for all type imports is not enough.

I can import types from "builtin" modules, from "external" module and so on.

So, it would be logical to have type counterpart for each group, i.e.: "build" -> "built-type" "external" -> "external-type" and so on

iliubinskii avatar Apr 24 '22 09:04 iliubinskii

That's a combinatorial explosion of categories, unfortunately.

I can see some kind of object form of each category that includes types: true - and then, any types not already swept up by another category would be in "type".

ljharb avatar Apr 26 '22 17:04 ljharb

I came to mention a similar thing however I think customizing the type imports might be a bit too much.

Type imports are really their own thing, while every other import group is based on the import source, the Type imports are all categorized based on the import type keyword. I feel like sorting types among themselves with the same ordering that is applied to the rest of the categories would probably be an ideal solution to keep things looking consistent, otherwise internal types which use relative imports will usually be placed above external types, even if you have external types listed first.

csandman avatar Apr 26 '22 18:04 csandman

That's what it does currently, ftr.

ljharb avatar Apr 26 '22 18:04 ljharb

It sorts based on the order of the import groups? That's not what I'm seeing.

These are my settings for the rule:

"import/order": [
  "warn",
  {
    "groups": [
      "builtin",
      "external",
      "internal",
      "parent",
      "sibling",
      "object",
      "type"
    ],
    "newlines-between": "never",
    "alphabetize": {
      "order": "asc",
      "caseInsensitive": false
    }
  }
],

And this is how its sorting my types on a run of eslint --fix:

import { Box } from "@chakra-ui/layout";
import React from "react";
import type { Size, SizeProps } from "../types";
import type { SystemStyleObject } from "@chakra-ui/system";
import type { ReactElement } from "react";
import type {
  ContainerProps,
  GroupBase,
  IndicatorsContainerProps,
  ValueContainerProps,
} from "react-select";

I would expect the type import from "../types" to go to the bottom:

import { Box } from "@chakra-ui/layout";
import React from "react";
import type { SystemStyleObject } from "@chakra-ui/system";
import type { ReactElement } from "react";
import type {
  ContainerProps,
  GroupBase,
  IndicatorsContainerProps,
  ValueContainerProps,
} from "react-select";
import type { Size, SizeProps } from "../types";

csandman avatar Apr 26 '22 18:04 csandman

That's what it should do, then :-) a PR with failing test cases is appreciated.

ljharb avatar Apr 26 '22 19:04 ljharb

It looks like type imports get sorted in reverse order of groups. E.g. I get this:

    import type { Paginator } from "../../utils/NMApi";
    import type NM from "../../utils/NMTypes";
    import type { Actors } from "../TradeWindow.svelte";
    import type { Writable } from "svelte/store";

    import { onDestroy, createEventDispatcher, getContext } from "svelte";
    import { writable } from "svelte/store";
    import NMApi from "../../utils/NMApi";
    import tip from "../actions/tip";
    import Avatar from "../elements/Avatar.svelte";
    import Button from "../elements/Button.svelte";
    import Icon from "../elements/Icon.svelte";
    import FiltersMenu from "./FiltersMenu.svelte";
    import PrintDetails from "./PrintDetails.svelte";

with the following rule options

"import/order": [
    "error",
    {
        groups: [
            "type",
            "builtin",
            "external",
            "internal",
            "parent",
            "sibling",
            "index",
            "object",
        ],
        alphabetize: {
            order: "asc",
            caseInsensitive: true,
        },
        warnOnUnassignedImports: false,
    },
],

7nik avatar Dec 01 '22 19:12 7nik

@7nik would you be able to make a PR with a failing test case?

ljharb avatar Dec 01 '22 19:12 ljharb

add test cases to https://github.com/import-js/eslint-plugin-import/blob/main/tests/src/rules/order.js ? can try in a few days

7nik avatar Dec 01 '22 20:12 7nik

Yep, thanks! Much obliged; I can much more easily add a fix onto a failing PR :-)

ljharb avatar Dec 01 '22 20:12 ljharb