question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Preserve grouping of imports

See original GitHub issue

Hi Simon,

Thank you for the plugin! I love how it works after simply installing it, without having to configure anything.

I would like to be able to preserve the grouping and order of some imports if possible. Notably, I have one library with tons of named imports, and I would like to preserve how we always choose to have those imports come last:

import { useState } from 'react'
import api from '../shared/api'
import d from '../shared/data-types'

import {
    Button,
    Col,
    Div,
    Flex,
    FlexFix,
    Input,
    makeReactiveUI,
    Margin,
    OnTap,
    Padding,
    Row,
    ScrollCol,
    Span,
    Style,
    TextArea,
    TextView,
} from '../shared/lib/js.ui/dom'

Would you be willing to consider adding the ability to preserve newline-separated groups of imports? My suggestion would be to treat each newline-separated group as a standalone group, and never change their order, but to change the order of import statements within any given group.

Whether you say yes to making that happen or not: thank you for the library!

Cheers, Marcus

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
simonhaenischcommented, Mar 23, 2021

Hi Marcus,

sorry but it’s out of the scope of this package. The plugin simply invokes the TypeScript language service feature (organizeImports) and doesn’t add any other options on top of that. Adding such a feature would mean a completely different approach with the source code that would also be significantly larger. Afaik the organizeImports API doesn’t have any options at the moment, but if the TypeScript team adds some, I’ll make sure to expose them as plugin options. You could open an upstream issue in the TypeScript repo about adding options (or do a search first, I think there are some related ones already).

But IMO the purpose of auto-import-sorting is that you can completely forget about that part of the code and not even really look at it anymore. So my advice would be to stop that convention of wanting some imports in a certain spot. I used to do that, too, but now I don’t care anymore and I think it’s just a waste of time because who reads the import statements anyway? I always skip to the code, and if I want to look sth up, I can use “go to” or “peek definition” and it’ll open the file for me.

That being said, there are also other tools out there to do import sorting/organizing that are configurable and probably can do what you want (but those are also harder to set up 😉 ).

1reaction
gotgenescommented, Jan 9, 2022

@marcuswestin I’m not sure if you’ve already discovered this yourself, but trivago/prettier-plugin-sort-imports looks like it might serve your needs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

disable organize import on specific lines · Issue #41494 ...
I like using the auto organize imports on save feature ( "editor.codeActionsOnSave": {"source.organizeImports": true} on settings.json ) ...
Read more >
Group import/export API - GitLab Documentation
To preserve group-level relationships from imported projects, run Group Import/Export first, to allow project imports into the desired group structure.
Read more >
Auto import | IntelliJ IDEA Documentation
The Optimize Imports feature helps you remove unused imports and organize import statements in the current file or in all files in a...
Read more >
Feature Request: Export/Import Firewall Groups
Rather than having to manually recreate them for each client, it would be very convenient to be able to Export a Group from...
Read more >
Feature Requests - Jamf Nation Community
First of all, thanks to everyone for the feature requests, ... so that you can apply MCX settings to smart groups, or imported...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found