|
| 1 | +use oxc_ast::ast::*; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + JsLabels, format_args, |
| 5 | + formatter::{ |
| 6 | + Buffer, Format, FormatResult, Formatter, prelude::*, trivia::format_dangling_comments, |
| 7 | + }, |
| 8 | + generated::ast_nodes::{AstNode, AstNodes}, |
| 9 | + options::Expand, |
| 10 | + write, |
| 11 | +}; |
| 12 | + |
| 13 | +use super::FormatWrite; |
| 14 | + |
| 15 | +impl<'a> FormatWrite<'a> for AstNode<'a, ComputedMemberExpression<'a>> { |
| 16 | + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 17 | + write!(f, self.object())?; |
| 18 | + |
| 19 | + if matches!(self.expression, Expression::NumericLiteral(_)) { |
| 20 | + write!(f, [self.optional().then_some("?."), "[", self.expression(), "]"]) |
| 21 | + } else { |
| 22 | + write!( |
| 23 | + f, |
| 24 | + group(&format_args!( |
| 25 | + self.optional().then_some("?."), |
| 26 | + "[", |
| 27 | + soft_block_indent(self.expression()), |
| 28 | + "]" |
| 29 | + )) |
| 30 | + ) |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl<'a> FormatWrite<'a> for AstNode<'a, StaticMemberExpression<'a>> { |
| 36 | + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 37 | + let is_member_chain = { |
| 38 | + let mut recording = f.start_recording(); |
| 39 | + write!(recording, [self.object()])?; |
| 40 | + recording.stop().has_label(LabelId::of(JsLabels::MemberChain)) |
| 41 | + }; |
| 42 | + |
| 43 | + match layout(self, is_member_chain) { |
| 44 | + StaticMemberLayout::NoBreak => { |
| 45 | + let format_no_break = |
| 46 | + format_with(|f| write!(f, [operator_token(self.optional()), self.property()])); |
| 47 | + |
| 48 | + if is_member_chain { |
| 49 | + write!(f, [labelled(LabelId::of(JsLabels::MemberChain), &format_no_break)]) |
| 50 | + } else { |
| 51 | + write!(f, [format_no_break]) |
| 52 | + } |
| 53 | + } |
| 54 | + StaticMemberLayout::BreakAfterObject => { |
| 55 | + write!( |
| 56 | + f, |
| 57 | + [group(&indent(&format_args!( |
| 58 | + soft_line_break(), |
| 59 | + operator_token(self.optional()), |
| 60 | + self.property(), |
| 61 | + )))] |
| 62 | + ) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Debug, Copy, Clone)] |
| 69 | +enum StaticMemberLayout { |
| 70 | + /// Forces that there's no line break between the object, operator, and member |
| 71 | + NoBreak, |
| 72 | + |
| 73 | + /// Breaks the static member expression after the object if the whole expression doesn't fit on a single line |
| 74 | + BreakAfterObject, |
| 75 | +} |
| 76 | + |
| 77 | +fn operator_token(optional: bool) -> &'static str { |
| 78 | + if optional { "?." } else { "." } |
| 79 | +} |
| 80 | + |
| 81 | +fn layout<'a>( |
| 82 | + node: &AstNode<'a, StaticMemberExpression<'a>>, |
| 83 | + is_member_chain: bool, |
| 84 | +) -> StaticMemberLayout { |
| 85 | + let parent = node.parent; |
| 86 | + let object = &node.object; |
| 87 | + |
| 88 | + let is_nested = match parent { |
| 89 | + AstNodes::AssignmentExpression(_) | AstNodes::VariableDeclarator(_) => { |
| 90 | + let no_break = match object { |
| 91 | + Expression::CallExpression(call_expression) => { |
| 92 | + !call_expression.arguments.is_empty() |
| 93 | + } |
| 94 | + Expression::TSNonNullExpression(non_null_assertion) => { |
| 95 | + match &non_null_assertion.expression { |
| 96 | + Expression::CallExpression(call_expression) => { |
| 97 | + !call_expression.arguments.is_empty() |
| 98 | + } |
| 99 | + _ => false, |
| 100 | + } |
| 101 | + } |
| 102 | + _ => false, |
| 103 | + }; |
| 104 | + |
| 105 | + if no_break || is_member_chain { |
| 106 | + return StaticMemberLayout::NoBreak; |
| 107 | + } |
| 108 | + true |
| 109 | + } |
| 110 | + AstNodes::StaticMemberExpression(_) | AstNodes::ComputedMemberExpression(_) => true, |
| 111 | + _ => false, |
| 112 | + }; |
| 113 | + |
| 114 | + if !is_nested && matches!(object, Expression::Identifier(_)) { |
| 115 | + return StaticMemberLayout::NoBreak; |
| 116 | + } |
| 117 | + |
| 118 | + let mut first_non_static_member_ancestor = parent; |
| 119 | + while matches!( |
| 120 | + first_non_static_member_ancestor, |
| 121 | + AstNodes::StaticMemberExpression(_) | AstNodes::ComputedMemberExpression(_) |
| 122 | + ) { |
| 123 | + first_non_static_member_ancestor = first_non_static_member_ancestor.parent(); |
| 124 | + } |
| 125 | + |
| 126 | + match first_non_static_member_ancestor { |
| 127 | + AstNodes::NewExpression(_) => StaticMemberLayout::NoBreak, |
| 128 | + AstNodes::AssignmentExpression(assignment) => { |
| 129 | + if matches!(assignment.left, AssignmentTarget::AssignmentTargetIdentifier(_)) { |
| 130 | + StaticMemberLayout::BreakAfterObject |
| 131 | + } else { |
| 132 | + StaticMemberLayout::NoBreak |
| 133 | + } |
| 134 | + } |
| 135 | + _ => StaticMemberLayout::BreakAfterObject, |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +impl<'a> FormatWrite<'a> for AstNode<'a, PrivateFieldExpression<'a>> { |
| 140 | + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 141 | + write!(f, [self.object(), self.optional().then_some("?"), ".", self.field()]) |
| 142 | + } |
| 143 | +} |
0 commit comments