-
Notifications
You must be signed in to change notification settings - Fork 21
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
<TYPES> & <BUILTIN_MODULES> #141
Comments
Thanks for the issue. Could you provide a concrete example we can use as a test case? i.e. Your config, the code before the sort, the result you want, and the result you're getting? |
Thanks for the speedy response! Given this config importOrder: [
"<TYPES><BUILTIN_MODULES>",
"<BUILTIN_MODULES>",
"",
"<TYPES>",
"<THIRD_PARTY_MODULES>",
"",
"<TYPES>^[.]",
"^[.]",
] I would expect the following imports: import path from "node:path";
import type fs from "node:fs";
import type { RunResult } from "better-sqlite3";
import { Do, Option, Result } from "ftld";
import { DomainError } from "../lib/domain-error";
import { msTimestamp } from "./utils"; to be formatted as such: import type fs from "node:fs";
import path from "node:path";
import type { RunResult } from "better-sqlite3";
import { Do, Option, Result } from "ftld";
import { DomainError } from "../lib/domain-error";
import { msTimestamp } from "./utils"; but it is instead formatted as such: import path from "node:path";
import type { RunResult } from "better-sqlite3";
import type fs from "node:fs";
import { Do, Option, Result } from "ftld";
import { DomainError } from "../lib/domain-error";
import { msTimestamp } from "./utils"; |
But also, I suppose an alternative solution for my purposes would be to expose |
@cevr what you demonstrated in the test case I consider a bug in our current implementation — but the more I look at it, the more I realize why it happens, based on our implementation. For complicated parsing reasons, the This may be fixable, I’m not sure, but I expect it to be a challenge! — definitely thank you for filing the ticket!
We don’t want to expose more options for configuration than we have, because it’s both against the “opinionated formatter” ethos of prettier, and because it significantly complicates the implementation, test cases, documentation, and plugin usability overall, and indeed in recent major versions we pared down the options provided by this plugin. I hope this is convincing! |
@cevr if the I believe that code partially happens here: prettier-plugin-sort-imports/src/utils/get-sorted-nodes.ts Lines 72 to 77 in 539e23d
|
When presented with import type fs from 'node:fs'; The code has to make a choice: is this a Currently the code says “if you use the
I’m less convinced this is an obvious bug now, and rather think it’s an unexpected consequence of an implicit ranking that the I could be convinced that the Additionally this code is complicated by the fact that types can be a prefix on another pattern, |
I need to give it some more thought, and it's been a while since I looked at the code so I can't promise it can be done, but I wouldn't be entirely opposed to adding a |
I can't comment on the best solution, though I would argue that breaking types from non types is always better since typescript will remove type import statements completely, but not inline type specifiers. If the goal is to be opinionated and simple, perhaps that could be a simple solution. Otherwise, adding another keyword would work! Though I would say it might make sense to make a keyword for third party modules as well for consistency. Thank you for the responses! |
@cevr I gave this a little more thought, and I think that I found a solution. You should be able to add Here's an example showing the syntax above in action: e414d30#diff-972a783a7dfae730fe457ea5acd569db03093756f54f1bf794ea521be431f57fR5 Could this be a good-enough solution for you, @cevr? I can also add an example to the README in case this is something others are interested in doing, since it's not immediately obvious it can be done. |
@IanVS Thank you! That's a perfectl solution. Would this work for built ins of other platforms like bun? |
Sure, I don't see why not, you just need some kind of regex pattern to match off of. |
Your Environment
Describe the bug
Currently there is a way to delineate between sorting local type imports, and third party type imports. However, there is no way to delineate between third party and builtin type imports which breaks the sort order if you have those specificed.
To Reproduce
Using a importOrder as one above, if you import a type from a builtin module, since there is no way to specify something like
<TYPES><BUILTIN_MODULES>
, it breaks the sort order.Expected behavior
<TYPES><BUILTIN_MODULES>
should be possible.Contribute to @ianvs/prettier-plugin-sort-imports
The text was updated successfully, but these errors were encountered: