-
-
Notifications
You must be signed in to change notification settings - Fork 489
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_analyzer): useJsxSortProps #2652
Changes from all commits
75ac574
7a4d280
37f67c8
6d842eb
53af54c
49ad1ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
use std::{borrow::Cow, cmp::Ordering, iter::zip}; | ||
|
||
use biome_analyze::{ | ||
context::RuleContext, declare_source_rule, ActionCategory, Ast, Rule, RuleAction, RuleSource, | ||
RuleSourceKind, SourceActionKind, | ||
}; | ||
use biome_console::markup; | ||
use biome_diagnostics::Applicability; | ||
use biome_js_syntax::{AnyJsxAttribute, JsxAttribute, JsxAttributeList}; | ||
use biome_rowan::{AstNode, BatchMutationExt}; | ||
|
||
use crate::JsRuleAction; | ||
|
||
declare_source_rule! { | ||
/// Enforce props sorting in JSX elements. | ||
/// | ||
/// This rule checks if the JSX props are sorted in a consistent way. | ||
/// Props are sorted alphabetically. | ||
/// This rule will not consider spread props as sortable. | ||
/// Instead, whenever it encounters a spread prop, it will sort all the | ||
/// previous non spread props up until the nearest spread prop, if one | ||
/// exist. | ||
/// This prevents breaking the override of certain props using spread | ||
/// props. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// <Hello lastName="Smith" firstName="John" />; | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// <Hello firstName="John" lastName="Smith" />; | ||
/// <Hello lastName="Smith" {...this.props} firstName="John" />; | ||
/// <Hello tel={5555555} {...this.props} firstName="John" {...another.props} lastName="Smith" />; | ||
/// ``` | ||
/// | ||
pub SortJsxProps { | ||
version: "next", | ||
name: "sortJsxProps", | ||
language: "js", | ||
recommended: false, | ||
sources: &[RuleSource::EslintReact("jsx-sort-props")], | ||
source_kind: RuleSourceKind::SameLogic, | ||
} | ||
} | ||
|
||
impl Rule for SortJsxProps { | ||
type Query = Ast<JsxAttributeList>; | ||
type State = PropGroup; | ||
type Signals = Vec<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let props = ctx.query().clone(); | ||
let mut current_prop_group = PropGroup::default(); | ||
let mut prop_groups = Vec::new(); | ||
for prop in props.clone() { | ||
match prop { | ||
AnyJsxAttribute::JsxAttribute(attr) => { | ||
current_prop_group.props.push(PropElement { prop: attr }); | ||
} | ||
// spread prop reset sort order | ||
AnyJsxAttribute::JsxSpreadAttribute(_) => { | ||
prop_groups.push(current_prop_group); | ||
current_prop_group = PropGroup::default(); | ||
} | ||
} | ||
} | ||
prop_groups.push(current_prop_group); | ||
prop_groups | ||
Comment on lines
+59
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This really looks cleaner after being split to groups |
||
} | ||
|
||
fn action(ctx: &RuleContext<Self>, state: &Self::State) -> Option<JsRuleAction> { | ||
if state.is_sorted() { | ||
return None; | ||
} | ||
let mut mutation = ctx.root().begin(); | ||
|
||
for (PropElement { prop }, PropElement { prop: sorted_prop }) in | ||
zip(state.props.clone(), state.get_sorted_props()) | ||
{ | ||
mutation.replace_node(prop, sorted_prop); | ||
} | ||
|
||
Some(RuleAction::new( | ||
rule_action_category!(), | ||
Applicability::Always, | ||
markup! { "Sort the JSX props." }, | ||
mutation, | ||
)) | ||
} | ||
} | ||
|
||
#[derive(PartialEq, Eq, Clone)] | ||
pub struct PropElement { | ||
prop: JsxAttribute, | ||
} | ||
|
||
impl Ord for PropElement { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
let (Ok(self_name), Ok(other_name)) = (self.prop.name(), other.prop.name()) else { | ||
return Ordering::Equal; | ||
}; | ||
let (a_name, b_name) = (self_name.text(), other_name.text()); | ||
|
||
a_name.cmp(&b_name) | ||
} | ||
} | ||
|
||
impl PartialOrd for PropElement { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
#[derive(Clone, Default)] | ||
pub struct PropGroup { | ||
props: Vec<PropElement>, | ||
} | ||
|
||
impl PropGroup { | ||
fn is_sorted(&self) -> bool { | ||
let mut new_props = self.props.clone(); | ||
new_props.sort(); | ||
new_props == self.props | ||
} | ||
|
||
fn get_sorted_props(&self) -> Vec<PropElement> { | ||
let mut new_props = self.props.clone(); | ||
new_props.sort(); | ||
new_props | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<Hello firstName="John" lastName="Smith" />; | ||
<Hello tel={5555555} {...this.props} firstName="John" lastName="Smith" />; | ||
<Hello onClick={this._handleClick} tel={5555555} /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: sorted.jsx | ||
--- | ||
# Input | ||
```jsx | ||
<Hello firstName="John" lastName="Smith" />; | ||
<Hello tel={5555555} {...this.props} firstName="John" lastName="Smith" />; | ||
<Hello onClick={this._handleClick} tel={5555555} /> | ||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<Hello lastName="Smith" firstName="John" />; | ||
<Hello tel={5555555} address="NY" {...this.props} lastName="Smith" firstName="John" />; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: unsorted.jsx | ||
--- | ||
# Input | ||
```jsx | ||
<Hello lastName="Smith" firstName="John" />; | ||
<Hello tel={5555555} address="NY" {...this.props} lastName="Smith" firstName="John" />; | ||
|
||
``` | ||
|
||
# Actions | ||
```diff | ||
@@ -1,2 +1,2 @@ | ||
-<Hello lastName="Smith" firstName="John" />; | ||
+<Hello firstName="John" lastName="Smith" />; | ||
<Hello tel={5555555} address="NY" {...this.props} lastName="Smith" firstName="John" />; | ||
|
||
``` | ||
|
||
```diff | ||
@@ -1,2 +1,2 @@ | ||
<Hello lastName="Smith" firstName="John" />; | ||
-<Hello tel={5555555} address="NY" {...this.props} lastName="Smith" firstName="John" />; | ||
+<Hello address="NY" tel={5555555} {...this.props} lastName="Smith" firstName="John" />; | ||
|
||
``` | ||
|
||
```diff | ||
@@ -1,2 +1,2 @@ | ||
<Hello lastName="Smith" firstName="John" />; | ||
-<Hello tel={5555555} address="NY" {...this.props} lastName="Smith" firstName="John" />; | ||
+<Hello tel={5555555} address="NY" {...this.props} firstName="John" lastName="Smith" />; | ||
|
||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 definitely makes sense to add an example with spread props, so users can understand how it works. Maybe two spread props should be enough