feat(prettier): wrap return statements with parentheses (#1583)

This commit is contained in:
Boshen 2023-11-29 19:31:55 +08:00 committed by GitHub
parent b8ce6266ad
commit c50fcececa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 6 deletions

View file

@ -225,6 +225,10 @@ impl<'a> Expression<'a> {
matches!(self, Expression::FunctionExpression(_) | Expression::ArrowExpression(_))
}
pub fn is_binaryish(&self) -> bool {
matches!(self, Expression::BinaryExpression(_) | Expression::LogicalExpression(_))
}
/// Returns literal's value converted to the Boolean type
/// returns `true` when node is truthy, `false` when node is falsy, `None` when it cannot be determined.
pub fn get_boolean_value(&self) -> Option<bool> {

View file

@ -2,7 +2,7 @@ use oxc_ast::ast::*;
use crate::{
doc::{Doc, DocBuilder},
group, ss, Format, Prettier,
group, if_break, indent, softline, ss, Format, Prettier,
};
pub(super) fn print_function<'a>(
@ -105,7 +105,19 @@ pub(super) fn print_return_or_throw_argument<'a>(
if let Some(argument) = argument {
parts.push(ss!(" "));
parts.push(argument.format(p));
parts.push(
if argument.is_binaryish() || matches!(argument, Expression::SequenceExpression(_)) {
group![
p,
if_break!(p, "("),
indent!(p, softline!(), argument.format(p)),
softline!(),
if_break!(p, ")"),
]
} else {
argument.format(p)
},
);
}
parts.push(p.str(";"));

View file

@ -108,6 +108,12 @@ macro_rules! if_break {
group_id: $group_id,
})
}};
($p:ident, $s:expr, $flat:expr) => {{
if_break!($p, $s, $flat, None)
}};
($p:ident, $s:expr) => {{
if_break!($p, $s, "", None)
}};
}
#[macro_export]

View file

@ -1,4 +1,4 @@
Compatibility: 200/578 (34.60%)
Compatibility: 201/578 (34.78%)
# Failed
@ -457,9 +457,6 @@ Compatibility: 200/578 (34.60%)
* return/binaryish.js
* return/comment.js
### return-outside-function
* return-outside-function/return-outside-function.js
### sequence-break
* sequence-break/break.js