-
-
Notifications
You must be signed in to change notification settings - Fork 695
feat: support configureable sorting mode for imports, keys and attributes #6506
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
Conversation
🦋 Changeset detectedLatest commit: e2e4556 The changes in this PR will be included in the next version bump. This PR includes changesets to release 14 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Thanks for the PR! The scope of this PR is pretty big. IMO, I would prefer if a discussion was opened first so that we could gather community feedback on the feature to see if there's enough demand for it, and to agree and align on scope. I'm personally on the fence about how useful this would realistically be -- the value add feels negligible. Implementation wise, it's a bit unusual to add a formatter option where it's mostly used in assists. We have a pretty high bar for adding formatter options. It would make a lot more sense (at least for now) to have the option be per-rule. |
I feel that value addition is particularly based on the nature of project. The moment I upgrade Biome, it was a clear that my team will not go along this kind of import sorting. And there is also a demand building up from the community #6122, and from that you can see different impact of selecting sorting logic. If you feel I can open open discussion in Github/Discord anywhere you want to gather more feedback.
Yes, I started PR earlier by setting up option per assist, but later observed that this is actually a formatter option like |
Agreed a little more discussion might've helped, but it seems indeed #6122 makes a good case for why we want this functionality. Also agreed that the setting should be an assist option rather than a formatter option though. The formatter itself doesn't apply sorting/ordering at all, and having the option there would give the impression that it does, which I think is a bit misleading. And who knows, maybe some people want different orderings for keys than they want for imports, for instance, so per assist makes it more flexible too. I think if you make that change indeed, we can accept this PR. |
I am not sure to see the relation between The natural order provided by Biome is really close to an alphabetical order. The main difference is number sorting. |
Signaling support for this feature 🚀 |
9a41999
to
89f8af5
Compare
@arendjr I had updated the PR with options specific to the assists rules. And kept schema configuration in a way that it not breaking any change in previous release. Though having one last error now.
And this is because in configuration crate we are picking biome/crates/biome_configuration/src/analyzer/assist/actions.rs Lines 194 to 195 in 5f42630
Please suggest how to fix it? Is there a crate which is usually used in such cases to have shared structs? |
For shared options, no not yet. There's an open PR for it #5543 |
So I think for now you will have to duplicate the options. Would that work for you? |
I don't think so. The problem comes from there being 2 rules, one for each language, so the name conflicts. |
Yes two same name rules for two languages. One for json and other for js, but from configuration exported as json options. The assert logic want it to be exact same reference of struct. So it works for json but not for js. Good approach would be to assert if the options are same not the exact object reference. |
Well, no, we do want it to be the same struct because we want to keep the options in sync, and that's the easiest way to do that. Feel free to copy the approach in that PR I linked. I'm on mobile right now so I can't give you links, but IIRC that PR moves options into the common biome_analyze crate. |
89f8af5
to
2354197
Compare
I tried to only have methods inside the compareable_token now in reference to the not put the sort_mode into every token, that was redundant and overwhelming for memory. Please review now @dyc3 @arendjr Related to the error for cross crate options for similar rules, it seems to have following options for us.
Please review the code and suggest the next steps. |
CodSpeed Performance ReportMerging #6506 will degrade performances by 61.19%Comparing 🎉 Hooray!
|
Benchmark | BASE |
HEAD |
Change | |
---|---|---|---|---|
❌ | js_analyzer[index_3894593175024091846.js] |
49.4 ms | 52.9 ms | -6.61% |
❌ | js_analyzer[parser_13571644119461115204.ts] |
79.4 ms | 85.5 ms | -7.11% |
🆕 | module_graph[@next/font/google/index.d.ts] |
N/A | 290.3 ms | N/A |
⚡ | deserialize_from_json_str[package.json] |
1,038.3 µs | 930.8 µs | +11.55% |
❌ | deserialize_from_json_str[tsconfig.json] |
218 µs | 561.8 µs | -61.19% |
Footnotes
It seems #5543 is almost ready to be merged. Is it okay we keep this on hold until it is ready? You'll probably need a rebase after that, but it should solve the issues with the codegen/schema. |
@arendjr I had fixed the lint/fmt issues and also added the changeset. For the website update, is it possible to open a PR there now or have to wait for this one to be merged first? The instructions are not that much clear for understanding. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Yes, you can create the PR there now, I'll clarify the docs.
Co-authored-by: Arend van Beelen jr. <[email protected]>
Wait, I may be leading you astray with the suggestion to open a PR against the website: Maybe the docs should simply be updated by adding docs to |
Yeah, apologise for the lack of proper directions regarding this. I will make sure to address it in the coming days. For docs, even if they are shared, we must document them in the rule/action itself. When you document the option like this struct UseSortedAttributesOptions {
// i am documentation
foo_bar: bool
} The documentation will only appear as a description of the schema, so we should keep it generic and it's applicable to all rules that will consume that option. However, we also want to update the docs of the rule/action too so we can create an example: ## Options
The action accepts the following options
### `nameOfTheOption`
This option is amazing and does this. In the following example, the options makes the code look differently beauftilf
```json,options
{
"options" {
"nameOfTheOption": true
}
}
```
```js,use_options,expect_diagnostics
function fn() {}
``` Of course, the code block has to change to meet the expectations of the option. In this case, we need to use |
… into nh/import-sort-config
@ematipico I added doc comments with updated options. Please review and suggest if there is nay change needed. |
@@ -618,6 +618,23 @@ declare_source_rule! { | |||
/// } | |||
/// ``` | |||
/// | |||
/// ## Options |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems my suggestion was too literal 😅 as you can see, this action uses its own format for documenting the options, I'd we should stick with it.
/// ## Options | |
/// ## Change the sorting of the identifiers |
/// This actions accepts following options | ||
/// | ||
/// ### `groups` | ||
/// The usage of `groups` is explained in detail above. | ||
/// | ||
/// ### `identifierOrder` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// This actions accepts following options | |
/// | |
/// ### `groups` | |
/// The usage of `groups` is explained in detail above. | |
/// | |
/// ### `identifierOrder` |
/// } | ||
/// } | ||
/// ``` | ||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, you want to add a snippet that uses the options you defined at line 631. Create a js
code block:
```js,use_options,expect_diagnostic
// code here
```
Of course, you want to use a code that shows how the natural order is applied
/// } | ||
/// } | ||
/// ``` | ||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too, we want to add a snippet that uses the options. Please refer to our contribution guide https://github.com/biomejs/biome/blob/main/crates/biome_analyze/CONTRIBUTING.md#using-rule-options
/// } | ||
/// } | ||
/// ``` | ||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too, let's add a snippet
@ematipico Thanks for your feedback. There were no invalid/valid cases for the assists I believe so I added examples which shows what diagnostic after sorting. Let me know if that's good or suggest any changes. |
@nazarhussain the PR is still failing, can you please address it? It seems that the code blocks you added in the docs have syntax errors. |
Seems issue happens from base branch, as lint passing for me locally.
I will try to rebase or update the branch. |
@ematipico Please rerun the CI and re-review this PR. |
…utes (#6506) Co-authored-by: Arend van Beelen jr. <[email protected]> Co-authored-by: dyc3 <[email protected]> Co-authored-by: arendjr <[email protected]> Co-authored-by: Conaclos <[email protected]> Co-authored-by: matthewkeil <[email protected]> Co-authored-by: ematipico <[email protected]>
Summary
Add a configuration for the formatter
sortMode
with eitheralphabetical
ornatural
(default) options. This sorting mode applies to the import sorting, sorting keys and sorting attributes.Motivation
Not all projects and teams have same preferences when it comes to code formatting, and it's much inclusive approach to give them option rather than enforcing one format. In addition:
Test Plan
Added the spec tests
NOTE: