mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(prettier): wrap return statements with parentheses (#1583)
This commit is contained in:
parent
b8ce6266ad
commit
c50fcececa
4 changed files with 25 additions and 6 deletions
|
|
@ -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> {
|
||||
|
|
|
|||
|
|
@ -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(";"));
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue