feat(prettier): sort regex flags (#1372)

This commit is contained in:
Boshen 2023-11-17 17:08:57 +08:00 committed by GitHub
parent 5a6c83b0cc
commit 6d8fa7ff36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 13 deletions

View file

@ -170,8 +170,6 @@ impl TryFrom<char> for RegExpFlags {
}
}
// TODO: should we implement TryFrom<&str> too?
impl fmt::Display for RegExpFlags {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.contains(Self::G) {

View file

@ -1363,33 +1363,33 @@ impl<'a> Format<'a> for AssignmentPattern<'a> {
impl<'a> Format<'a> for RegExpFlags {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let mut str = vec![];
let mut string = std::vec::Vec::with_capacity(self.iter().count());
if self.contains(Self::D) {
str.push('d');
string.push('d');
}
if self.contains(Self::G) {
str.push('g');
string.push('g');
}
if self.contains(Self::I) {
str.push('i');
string.push('i');
}
if self.contains(Self::M) {
str.push('m');
string.push('m');
}
if self.contains(Self::S) {
str.push('s');
string.push('s');
}
if self.contains(Self::V) {
str.push('v');
string.push('v');
}
if self.contains(Self::U) {
str.push('u');
string.push('u');
}
if self.contains(Self::Y) {
str.push('y');
string.push('y');
}
p.str(str.iter().collect::<String>().as_str())
string.sort_unstable();
p.str(&string.iter().collect::<String>())
}
}