-
-
Notifications
You must be signed in to change notification settings - Fork 529
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
feat(biome_js_analyze): add useSortedKeys assist rule #5079
base: main
Are you sure you want to change the base?
Conversation
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, Deserializable)] | ||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] | ||
#[serde(rename_all = "camelCase", deny_unknown_fields, default)] | ||
pub struct UseSortedPropertiesOptions { | ||
/// Partition the object properties by a newline. | ||
pub partion_by_new_line: bool, | ||
/// Partition the object properties by a comment. | ||
pub partion_by_comment: bool, | ||
} |
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.
As a general rule of thumb, we don't implement options for new rules on the first iteration to avoid unnecessary complexity. Lets leave these out for now.
CodSpeed Performance ReportMerging #5079 will not alter performanceComparing Summary
|
16753e7
to
1f56f09
Compare
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.
Blocking because this PR should go against next
and match the new contribution guidelines. The changeset is missing
5b133c8
to
3770bfd
Compare
I updated the PR to be based on the |
crates/biome_js_analyze/src/assist/source/use_sorted_object_properties.rs
Outdated
Show resolved
Hide resolved
Biome linter is particular, and the same rule can be applied to multiple languages. If the intent of the rule is the same, we should implement the same rule for new languages. Also, there's already a rule called |
It makes sense, I tried to rename to biome/packages/@biomejs/biome/configuration_schema.json Lines 3903 to 3940 in a41878d
crates/biome_configuration/src/analyzer/assist/actions.rs . The same for useSortedProperties , it already exists here
|
That should be fine. Is the codegen throwing any errors for you? It works fine on my machine. |
The current commit is fine, I was about to rename that property from |
We do this with other rules, like |
5562edf
to
4c7e012
Compare
Co-authored-by: Carson McManus <dyc3@users.noreply.github.com>
4c7e012
to
c3ebe18
Compare
Summary
Add a new assist rule -
useSortedKeys
which enforces ordering of a JS object properties.This rule will consider spread/calculated keys e.g
[k]: 1
as non-sortable.Instead, whenever it encounters a non-sortable key, it will sort all the
previous sortable keys up until the nearest non-sortable key, if one exist.
This prevents breaking the override of certain keys using spread keys.
Source: https://perfectionist.dev/rules/sort-objects
How it is implemented
The
run
method of the rule queriesJSObjectMemberList
and collects all the members toVec<Vec<ObjectMember>>
state which is a list of groups, each group is separated by a spread or a computed key. Then, in the action we check if the current state is sorted (Ord
was implemented forObjectMember
usingnatord
for natural ordering) and if not - iterate through sorted - non sorted pairs (withzip
) and replace the nodes.The logic is very similar to the existing
useSortedAttributes
rule for JSX properties.Test plan
Includes 2 snapshot tests - sorted and unsorted.