Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type imports #35

Closed
mattiaz9 opened this issue Sep 4, 2022 · 7 comments · Fixed by #44
Closed

Type imports #35

mattiaz9 opened this issue Sep 4, 2022 · 7 comments · Fixed by #44
Assignees

Comments

@mattiaz9
Copy link

mattiaz9 commented Sep 4, 2022

Is your feature request related to a problem?

I'd like to sort all my types into one single group.

Example:

import { a } from 'foo'
import { b } from 'bar'

import type { A } form 'foo'
import type { B } form 'bar'

Describe the solution you'd like

Something like this:
trivago/prettier-plugin-sort-imports#153

Describe alternatives you've considered

None

@IanVS
Copy link
Owner

IanVS commented Sep 4, 2022

@mattiaz9, do you prefer that over this syntax?

import { a, type A } from 'foo';
import { b, type B } from 'bar';

@mattiaz9
Copy link
Author

mattiaz9 commented Sep 5, 2022

@IanVS yes, I prefer to separate them

@tusharf5
Copy link
Contributor

@mattiaz9 you can try https://github.com/serverless-guru/prettier-import-order which is a fork of this project and have this feature.

@mattiaz9
Copy link
Author

Thank you @tusharf5, but I can't put the types where I want, so I rather wait for the feature to be implemented

@IanVS IanVS closed this as completed in #44 Oct 27, 2022
IanVS added a commit that referenced this issue Oct 27, 2022
Closes #35

Adapted from trivago/prettier-plugin-sort-imports#153, by @Xenfo.  

This adds a new special string, `<TYPES>` that can be added to the `importOrder` array.  When used, it will cause type imports to be sorted as specified by its location in the array.  

Notes:
- If it is used, it will disable `importOrderCombineTypeAndValueImports`, throwing a warning if both are used.  This is because:
- We will split apart type and value import declarations if `<TYPES>` is used, so that types can be sorted appropriately.  
- Thinking towards the next breaking change when we remove options, I think the default will be to enable `importOrderCombineTypeAndValueImports`, and this change will give users a good way to opt-out of that behavior if they want, by specifying the location for `<TYPES>`.
@mattiaz9
Copy link
Author

@IanVS I'm trying out the new version, but it doesn't seem to work.
I think it might going in conflict with other imports. For instance "<TYPES>^[./]" is in conflict with "^[./]".

Or maybe I'm doing something wrong. This is my config:

module.exports = {
  importOrder: [
    "^react",
    "^next",
    "<THIRD_PARTY_MODULES>",
    "",
    ".s?css$",
    "^@heroicons",
    "^@/assets",
    "",
    "^[./]",
    "^@/",
    "",
    "<TYPES>^[./]",
    "<TYPES>^react",
    "<TYPES>^next",
    "<TYPES>^@/",
    "<TYPES>",
  ],
  importOrderSeparation: false,
  importOrderMergeDuplicateImports: true,
}

This is an example result of how it sorts the imports:

import React, {  useCallback,  useEffect,  useState } from "react"
import isHotkey from "is-hotkey"
import { createEditor, Transforms, Editor as SlateEditor } from "slate"
import { Slate, Editable, ReactEditor } from "slate-react"

import Block from "./blocks/Block"
import type { BlockProps } from "./blocks/Block"
import Field from "./blocks/Field"
import Image from "./blocks/Image"
import type { LeafProps } from "./blocks/Leaf"
import Leaf from "./blocks/Leaf"
import Page from "./blocks/Page"
import type { PageProps } from "./blocks/Page"
import Table from "./blocks/Table"
import TableCell from "./blocks/TableCell"
import TableRow from "./blocks/TableRow"
import type { FullEditor } from "./plugins/with-plugins"
import withPlugins from "./plugins/with-plugins"
import EditorSidebar from "./sidebar/EditorSidebar"
import Toolbar from "./toolbar/Toolbar"
import { injectFonts } from "@/utils/fonts"
import { DEFAULT_FONT, FONT_FAMILIES, HOTKEYS, toggleMark } from "@/utils/slate"
import type { DescendantExtra, SlateElementExtra } from "@/utils/slate"

@IanVS
Copy link
Owner

IanVS commented Oct 27, 2022

Thanks for trying it out, and for providing the example. I'm looking into it.

IanVS added a commit that referenced this issue Oct 27, 2022
We had a bug, identified in #35 (comment), which was causing the `"^[./]"` group to include both type and value imports, instead of only value imports.

This PR fixes that bug, and expands both the unit and e2e tests, which fail without this change and pass with it.
@IanVS
Copy link
Owner

IanVS commented Oct 27, 2022

@mattiaz9 I've pushed up a fix in https://github.com/IanVS/prettier-plugin-sort-imports/releases/tag/v3.7.1.

Using your config and example, I now get this output:

import React, { useCallback, useEffect, useState } from "react";
import isHotkey from "is-hotkey";
import { createEditor, Transforms, Editor as SlateEditor } from "slate";
import { Slate, Editable, ReactEditor } from "slate-react";

import Block from "./blocks/Block";
import Field from "./blocks/Field";
import Image from "./blocks/Image";
import Leaf from "./blocks/Leaf";
import Page from "./blocks/Page";
import Table from "./blocks/Table";
import TableCell from "./blocks/TableCell";
import TableRow from "./blocks/TableRow";
import withPlugins from "./plugins/with-plugins";
import EditorSidebar from "./sidebar/EditorSidebar";
import Toolbar from "./toolbar/Toolbar";
import { injectFonts } from "@/utils/fonts";
import {
    DEFAULT_FONT,
    FONT_FAMILIES,
    HOTKEYS,
    toggleMark,
} from "@/utils/slate";

import type { BlockProps } from "./blocks/Block";
import type { LeafProps } from "./blocks/Leaf";
import type { PageProps } from "./blocks/Page";
import type { FullEditor } from "./plugins/with-plugins";
import type { DescendantExtra, SlateElementExtra } from "@/utils/slate";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants