-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prettier): Print leading semi for ExpressionStatement (#9194)
Part of #5068
- Loading branch information
Showing
9 changed files
with
244 additions
and
82 deletions.
There are no files selected for viewing
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
92 changes: 92 additions & 0 deletions
92
crates/oxc_prettier/src/format/print/expression_statement.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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use oxc_allocator::Vec; | ||
use oxc_ast::{ast::*, AstKind}; | ||
|
||
use crate::{ | ||
array, format::print::arrow_function, ir::Doc, needs_parens, text, utils, Format, Prettier, | ||
}; | ||
|
||
pub fn print_expression_statement<'a>( | ||
p: &mut Prettier<'a>, | ||
expression_statement: &ExpressionStatement<'a>, | ||
) -> Doc<'a> { | ||
let mut parts = Vec::new_in(p.allocator); | ||
|
||
if should_print_leading_semicolon(p, expression_statement) { | ||
parts.push(text!(";")); | ||
} | ||
|
||
parts.push(expression_statement.expression.format(p)); | ||
|
||
if let Some(semi) = p.semi() { | ||
parts.push(semi); | ||
} | ||
|
||
array!(p, parts) | ||
} | ||
|
||
fn should_print_leading_semicolon<'a>( | ||
p: &mut Prettier<'a>, | ||
expr_statement: &ExpressionStatement<'a>, | ||
) -> bool { | ||
if p.options.semi { | ||
return false; | ||
} | ||
|
||
if !matches!( | ||
p.parent_kind(), | ||
AstKind::Program(_) | ||
| AstKind::BlockStatement(_) | ||
| AstKind::StaticBlock(_) | ||
| AstKind::SwitchCase(_) | ||
| AstKind::TSModuleBlock(_) | ||
) { | ||
return false; | ||
} | ||
|
||
expr_needs_asi_protection(p, &expr_statement.expression) | ||
} | ||
|
||
fn expr_needs_asi_protection<'a>(p: &mut Prettier<'a>, expr: &Expression<'a>) -> bool { | ||
if match expr { | ||
Expression::ParenthesizedExpression(_) | ||
| Expression::ArrayExpression(_) | ||
// TODO: ArrayPattern? | ||
| Expression::TemplateLiteral(_) | ||
| Expression::RegExpLiteral(_) | ||
| Expression::JSXElement(_) | Expression::JSXFragment(_) => true, | ||
Expression::ArrowFunctionExpression(arrow_expr) | ||
if !arrow_function::should_print_params_without_parens(p, arrow_expr) => true, | ||
Expression::UnaryExpression(unary_expr) | ||
if unary_expr.operator.is_arithmetic() => true, | ||
_ => false, | ||
} { | ||
return true; | ||
} | ||
|
||
// PERF: I'm not sure if this is the best way to handle this | ||
let expr = p.alloc(expr); | ||
|
||
let expr_kind = AstKind::from_expression(expr); | ||
|
||
// TODO: Consider this is a temporary hack or the right way to handle | ||
// The current implementation is: | ||
// - parent-child relationship such as `stack`, `current_node()` result are updated only by the `wrap!` macro | ||
// - and the `wrap!` macro is only used with a `Format` trait for each node | ||
// Therefore, at the time this code is executed, the outermost node in `stack` == the current node is `ExpressionStatement`. | ||
// However, `expr_needs_asi_protection()` should be called for `.expression` of `ExpressionStatement`. | ||
// That is, the code inside `need_parens()` should have that `.expression` as the current node, but it is not! | ||
// To resolve this gap, manually update the `stack` then call `need_parens()`. | ||
p.stack.push(expr_kind); | ||
let need_parens = p.need_parens(expr_kind); | ||
p.stack.pop(); | ||
if need_parens { | ||
return true; | ||
} | ||
|
||
if !utils::has_naked_left_side(expr_kind) { | ||
return false; | ||
} | ||
|
||
// Check left side | ||
utils::get_left_side_expression(expr).is_some_and(|e| expr_needs_asi_protection(p, e)) | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use oxc_ast::{ast::*, AstKind}; | ||
|
||
pub fn has_naked_left_side(kind: AstKind<'_>) -> bool { | ||
matches!( | ||
kind, | ||
AstKind::AssignmentExpression(_) | ||
| AstKind::BinaryExpression(_) | ||
| AstKind::LogicalExpression(_) | ||
| AstKind::ConditionalExpression(_) | ||
| AstKind::CallExpression(_) | ||
| AstKind::MemberExpression(_) | ||
| AstKind::SequenceExpression(_) | ||
| AstKind::TaggedTemplateExpression(_) | ||
| AstKind::TSNonNullExpression(_) | ||
| AstKind::ChainExpression(_) | ||
) || matches!(kind, AstKind::UpdateExpression(e) if !e.prefix) | ||
} | ||
|
||
pub fn get_left_side_path_name(kind: AstKind<'_>) -> AstKind<'_> { | ||
match kind { | ||
AstKind::AssignmentExpression(e) => AstKind::AssignmentTarget(&e.left), | ||
AstKind::BinaryExpression(e) => AstKind::from_expression(&e.left), | ||
AstKind::CallExpression(e) => AstKind::from_expression(&e.callee), | ||
AstKind::ChainExpression(e) => match &e.expression { | ||
ChainElement::CallExpression(e) => AstKind::from_expression(&e.callee), | ||
ChainElement::StaticMemberExpression(e) => AstKind::from_expression(&e.object), | ||
ChainElement::ComputedMemberExpression(e) => AstKind::from_expression(&e.object), | ||
ChainElement::PrivateFieldExpression(e) => AstKind::from_expression(&e.object), | ||
ChainElement::TSNonNullExpression(e) => AstKind::from_expression(&e.expression), | ||
}, | ||
AstKind::ConditionalExpression(e) => AstKind::from_expression(&e.test), | ||
AstKind::LogicalExpression(e) => AstKind::from_expression(&e.left), | ||
AstKind::MemberExpression(e) => AstKind::from_expression(e.object()), | ||
AstKind::SequenceExpression(e) => AstKind::from_expression(&e.expressions[0]), | ||
AstKind::TaggedTemplateExpression(e) => AstKind::from_expression(&e.tag), | ||
AstKind::UpdateExpression(e) => AstKind::from_expression( | ||
// TODO: If this causes a panic, make sure to handle `Option` | ||
e.argument.get_expression().expect("UpdateExpression.argument is missing"), | ||
), | ||
AstKind::TSAsExpression(e) => AstKind::from_expression(&e.expression), | ||
AstKind::TSNonNullExpression(e) => AstKind::from_expression(&e.expression), | ||
AstKind::TSSatisfiesExpression(e) => AstKind::from_expression(&e.expression), | ||
_ => unreachable!("get_left_side_path_name(): need to handle {}", kind.debug_name()), | ||
} | ||
} | ||
|
||
pub fn get_left_side_expression<'a>(expr: &'a Expression<'a>) -> Option<&'a Expression<'a>> { | ||
match expr { | ||
Expression::AssignmentExpression(e) => e.left.get_expression(), | ||
Expression::BinaryExpression(e) => Some(&e.left), | ||
Expression::CallExpression(e) => Some(&e.callee), | ||
Expression::ChainExpression(e) => match &e.expression { | ||
ChainElement::CallExpression(e) => Some(&e.callee), | ||
ChainElement::StaticMemberExpression(e) => Some(&e.object), | ||
ChainElement::ComputedMemberExpression(e) => Some(&e.object), | ||
ChainElement::PrivateFieldExpression(e) => Some(&e.object), | ||
ChainElement::TSNonNullExpression(e) => Some(&e.expression), | ||
}, | ||
Expression::ConditionalExpression(e) => Some(&e.test), | ||
Expression::LogicalExpression(e) => Some(&e.left), | ||
Expression::StaticMemberExpression(e) => Some(&e.object), | ||
Expression::ComputedMemberExpression(e) => Some(&e.object), | ||
Expression::PrivateFieldExpression(e) => Some(&e.object), | ||
Expression::SequenceExpression(e) => Some(&e.expressions[0]), | ||
Expression::TaggedTemplateExpression(e) => Some(&e.tag), | ||
Expression::UpdateExpression(e) => e.argument.get_expression(), | ||
Expression::TSAsExpression(e) => Some(&e.expression), | ||
Expression::TSNonNullExpression(e) => Some(&e.expression), | ||
Expression::TSSatisfiesExpression(e) => Some(&e.expression), | ||
_ => unreachable!("get_left_side_expression: need to handle"), | ||
} | ||
} |
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,3 +1,5 @@ | ||
mod ast; | ||
mod document; | ||
|
||
pub use self::document::*; | ||
pub use ast::*; | ||
pub use document::*; |
Oops, something went wrong.