-
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
challenge(formatter): predictable order of type parameter modifiers (#…
…719)
- Loading branch information
Showing
5 changed files
with
41 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 16 additions & 2 deletions
18
crates/biome_js_formatter/src/ts/lists/type_parameter_modifier_list.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,28 @@ | ||
use crate::prelude::*; | ||
use biome_js_syntax::TsTypeParameterModifierList; | ||
use biome_js_syntax::{ | ||
AnyTsTypeParameterModifier, TsTypeParameterModifierList, TypeParameterModifiers, | ||
}; | ||
use biome_rowan::AstNodeList; | ||
use smallvec::SmallVec; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatTsTypeParameterModifierList; | ||
|
||
impl FormatRule<TsTypeParameterModifierList> for FormatTsTypeParameterModifierList { | ||
type Context = JsFormatContext; | ||
fn fmt(&self, node: &TsTypeParameterModifierList, f: &mut JsFormatter) -> FormatResult<()> { | ||
let modifiers = sort_modifiers_by_precedence(node); | ||
f.join_with(&space()) | ||
.entries(node.iter().formatted()) | ||
.entries(modifiers.into_iter().formatted()) | ||
.finish() | ||
} | ||
} | ||
|
||
/// This function consumes a list of modifiers and applies a predictable sorting. | ||
fn sort_modifiers_by_precedence( | ||
list: &TsTypeParameterModifierList, | ||
) -> SmallVec<[AnyTsTypeParameterModifier; 3]> { | ||
let mut result = list.iter().collect::<SmallVec<_>>(); | ||
result.sort_unstable_by_key(|node| TypeParameterModifiers::from(node)); | ||
result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters