-
Notifications
You must be signed in to change notification settings - Fork 25
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
[Feature] Combine type and value imports #4
Comments
Hmm, I'd say it fits with the theme of normalizing imports (of which ordering is a part). Given the name of this plugin, it does sound like it should be another plugin. But we also need to consider that prettier doesn't have an API for these kind of AST changing plugins, so another plugin would have to parse the entire file again. If we do this, I think we should not limit that to combining type and value imports, but combining duplicate imports in general, so that this would become one import. import {a} from "./a";
import {type b} from "./a";
import {c} from "./a"; One complication I can think of is how to decide what is the same import? // Are all these the same? What would users expect?
import ... from "./index";
import ... from "./index.js";
import ... from "../model/index";
import ... from "../model";
import ... from "../node_modules/orm/lib/index.js";
import ... from "orm"; I don't think we should try to normalize these paths since that can be specific to the OS and build system, but I feel users would expect at least some logical equivalent paths to be treated the same. How does Related:
And yes, I'd also like to stick to prettier's philosophy of keeping the number of options limited, one option for enabling or disabling this is OK imo, but not e.g. fine-tuned settings for how to order items. Personally, I care that imports have a defined order, I don't really care which one it is exactly (since I'll almost never write imports directly). In that sense, I feel this plugin already has too many options (though we shouldn't just remove those now). |
We'd like to use this syntax. Currently the plugin errors out on |
I believe that means that either your typescript version or prettier version is not high enough. Would you mind opening an issue with the details of what you're seeing, and what versions you are using? |
Sorry, I missed this question earlier. Currently, the codemod only looks at the string literal value, it does not try to do any normalizing at all, since that seemed to be the safest approach.
Maybe. Although, it's also pretty easy to prevent that situation using an auto-fixable eslint rule: https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-duplicates.md. But, it's easy enough to support that here too, I think. |
@blutorange I don't think we should be responsible for normalizing imports that are spelled differently: import ... from "./index";
import ... from "./index.js";
import ... from "../model/index";
import ... from "../model";
import ... from "../node_modules/orm/lib/index.js";
import ... from "orm"; I do however think we should remove duplicate imports for things that are spelled identically! I'm not an expert at AST modification, but it feels like a comparatively straightforward (not simple, exactly) AST modification "take children of this node A, and add them as children of node B, then finally delete node A (and then chain to our sorting mutation)". As I understand it, that should be about as complicated as "sort all the children of this node" I may take a look at this operation to get familiar with the codebase |
@fbartho Yes, I don't think implementing this should be too hard. Feel free to take a look if you want to. You can start here https://github.com/IanVS/prettier-plugin-sort-imports/blob/main/src/utils/get-sorted-nodes.ts where it builds the https://astexplorer.net/ is pretty helpful if you want to visualize the AST. Some things to consider that also need appropriate tests
|
|
I realize now that the headliner feature on this ticket was actually to combine type & value imports -- for some reason my brain was stuck on just the classic duplicate imports problem. -- I've implemented that in #19 I'm happy to add an additional feature now to combine type & value imports. |
👍 To be clear, I'm not proposing converting from |
Fixes: #4 Allows the ability for users to convert `import type` expressions into `import {type …}` expressions via a boolean flag. Should this be controlled via a string parameter?
Is your feature request related to a problem?
Not so much a problem, but an enhancement
Describe the solution you'd like
I'm not positive this fits in to the scope of this project, but thought I'd suggest it anyway. I recently created https://github.com/IanVS/type-import-codemod, which combines type and value imports into a single import statement, using a new feature of Typescript 4.5:
import {foo, type Foo} from './foo';
. If desired, I think it could be adapted or used within this prettier plugin.If we did this, it would probably either need to be behind an option, or ideally, the version of typescript would be checked to find out whether it supports the new syntax, perhaps with an option to disable it. I normally want to avoid options, but since this feature isn't directly related to sorting imports, there should be a way to disable it, if we even include the functionality at all.
Describe alternatives you've considered
I could see creating a different prettier plugin just to combine those imports. That's honestly probably the better approach than trying to shim it into this one.
Additional context
I'm opening this issue to see if anyone else has thoughts about whether this would be useful.
The text was updated successfully, but these errors were encountered: