feat(printer) Print RegExpLiteral (#1269)

This commit is contained in:
Cameron 2023-11-13 15:06:59 +00:00 committed by GitHub
parent 1cc003f194
commit 1fb12d0088
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -481,7 +481,12 @@ impl<'a> Format<'a> for BigintLiteral {
impl<'a> Format<'a> for RegExpLiteral {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
Doc::Line
let mut parts = p.vec();
parts.push(p.str("/"));
parts.push(p.str(self.regex.pattern.as_str()));
parts.push(p.str("/"));
parts.push(format!(p, self.regex.flags));
Doc::Array(parts)
}
}
@ -1011,3 +1016,35 @@ impl<'a> Format<'a> for AssignmentPattern<'a> {
array![p, format!(p, self.left), string!(p, " = "), format!(p, self.right)]
}
}
impl<'a> Format<'a> for RegExpFlags {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let mut str = vec![];
if self.contains(Self::D) {
str.push('d');
}
if self.contains(Self::G) {
str.push('g');
}
if self.contains(Self::I) {
str.push('i');
}
if self.contains(Self::M) {
str.push('m');
}
if self.contains(Self::S) {
str.push('s');
}
if self.contains(Self::V) {
str.push('v');
}
if self.contains(Self::U) {
str.push('u');
}
if self.contains(Self::Y) {
str.push('y');
}
p.str(str.iter().collect::<String>().as_str())
}
}