|
| 1 | +use oxc_ast::ast::Expression; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + Format, |
| 5 | + formatter::{FormatResult, Formatter, prelude::*}, |
| 6 | + generated::ast_nodes::{AstNode, AstNodes}, |
| 7 | + parentheses::NeedsParentheses, |
| 8 | + write, |
| 9 | + write::FormatWrite, |
| 10 | +}; |
| 11 | + |
| 12 | +pub struct FormatExpressionWithoutTrailingComments<'a, 'b>(pub &'b AstNode<'a, Expression<'a>>); |
| 13 | + |
| 14 | +impl<'a> Format<'a> for FormatExpressionWithoutTrailingComments<'a, '_> { |
| 15 | + fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 16 | + let needs_parentheses = self.0.needs_parentheses(f); |
| 17 | + let print_left_paren = |
| 18 | + |f: &mut Formatter<'_, 'a>| write!(f, needs_parentheses.then_some("(")); |
| 19 | + |
| 20 | + match self.0.as_ast_nodes() { |
| 21 | + AstNodes::BooleanLiteral(n) => { |
| 22 | + n.format_leading_comments(f)?; |
| 23 | + print_left_paren(f)?; |
| 24 | + n.write(f) |
| 25 | + } |
| 26 | + AstNodes::NullLiteral(n) => { |
| 27 | + n.format_leading_comments(f)?; |
| 28 | + print_left_paren(f)?; |
| 29 | + n.write(f) |
| 30 | + } |
| 31 | + AstNodes::NumericLiteral(n) => { |
| 32 | + n.format_leading_comments(f)?; |
| 33 | + print_left_paren(f)?; |
| 34 | + n.write(f) |
| 35 | + } |
| 36 | + AstNodes::BigIntLiteral(n) => { |
| 37 | + n.format_leading_comments(f)?; |
| 38 | + print_left_paren(f)?; |
| 39 | + n.write(f) |
| 40 | + } |
| 41 | + AstNodes::RegExpLiteral(n) => { |
| 42 | + n.format_leading_comments(f)?; |
| 43 | + print_left_paren(f)?; |
| 44 | + n.write(f) |
| 45 | + } |
| 46 | + AstNodes::StringLiteral(n) => { |
| 47 | + n.format_leading_comments(f)?; |
| 48 | + print_left_paren(f)?; |
| 49 | + n.write(f) |
| 50 | + } |
| 51 | + AstNodes::TemplateLiteral(n) => { |
| 52 | + n.format_leading_comments(f)?; |
| 53 | + print_left_paren(f)?; |
| 54 | + n.write(f) |
| 55 | + } |
| 56 | + AstNodes::IdentifierReference(n) => { |
| 57 | + n.format_leading_comments(f)?; |
| 58 | + print_left_paren(f)?; |
| 59 | + n.write(f) |
| 60 | + } |
| 61 | + AstNodes::MetaProperty(n) => { |
| 62 | + n.format_leading_comments(f)?; |
| 63 | + print_left_paren(f)?; |
| 64 | + n.write(f) |
| 65 | + } |
| 66 | + AstNodes::Super(n) => { |
| 67 | + n.format_leading_comments(f)?; |
| 68 | + print_left_paren(f)?; |
| 69 | + n.write(f) |
| 70 | + } |
| 71 | + AstNodes::ArrayExpression(n) => { |
| 72 | + n.format_leading_comments(f)?; |
| 73 | + print_left_paren(f)?; |
| 74 | + n.write(f) |
| 75 | + } |
| 76 | + AstNodes::ArrowFunctionExpression(n) => { |
| 77 | + n.format_leading_comments(f)?; |
| 78 | + print_left_paren(f)?; |
| 79 | + n.write(f) |
| 80 | + } |
| 81 | + AstNodes::AssignmentExpression(n) => { |
| 82 | + n.format_leading_comments(f)?; |
| 83 | + print_left_paren(f)?; |
| 84 | + n.write(f) |
| 85 | + } |
| 86 | + AstNodes::AwaitExpression(n) => { |
| 87 | + n.format_leading_comments(f)?; |
| 88 | + print_left_paren(f)?; |
| 89 | + n.write(f) |
| 90 | + } |
| 91 | + AstNodes::BinaryExpression(n) => { |
| 92 | + n.format_leading_comments(f)?; |
| 93 | + print_left_paren(f)?; |
| 94 | + n.write(f) |
| 95 | + } |
| 96 | + AstNodes::CallExpression(n) => { |
| 97 | + n.format_leading_comments(f)?; |
| 98 | + print_left_paren(f)?; |
| 99 | + n.write(f) |
| 100 | + } |
| 101 | + AstNodes::ChainExpression(n) => { |
| 102 | + n.format_leading_comments(f)?; |
| 103 | + print_left_paren(f)?; |
| 104 | + n.write(f) |
| 105 | + } |
| 106 | + AstNodes::Class(n) => { |
| 107 | + n.format_leading_comments(f)?; |
| 108 | + print_left_paren(f)?; |
| 109 | + n.write(f) |
| 110 | + } |
| 111 | + AstNodes::ConditionalExpression(n) => { |
| 112 | + n.format_leading_comments(f)?; |
| 113 | + print_left_paren(f)?; |
| 114 | + n.write(f) |
| 115 | + } |
| 116 | + AstNodes::Function(n) => { |
| 117 | + n.format_leading_comments(f)?; |
| 118 | + print_left_paren(f)?; |
| 119 | + n.write(f) |
| 120 | + } |
| 121 | + AstNodes::ImportExpression(n) => { |
| 122 | + n.format_leading_comments(f)?; |
| 123 | + print_left_paren(f)?; |
| 124 | + n.write(f) |
| 125 | + } |
| 126 | + AstNodes::LogicalExpression(n) => { |
| 127 | + n.format_leading_comments(f)?; |
| 128 | + print_left_paren(f)?; |
| 129 | + n.write(f) |
| 130 | + } |
| 131 | + AstNodes::NewExpression(n) => { |
| 132 | + n.format_leading_comments(f)?; |
| 133 | + print_left_paren(f)?; |
| 134 | + n.write(f) |
| 135 | + } |
| 136 | + AstNodes::ObjectExpression(n) => { |
| 137 | + n.format_leading_comments(f)?; |
| 138 | + print_left_paren(f)?; |
| 139 | + n.write(f) |
| 140 | + } |
| 141 | + AstNodes::ParenthesizedExpression(n) => { |
| 142 | + n.format_leading_comments(f)?; |
| 143 | + print_left_paren(f)?; |
| 144 | + n.write(f) |
| 145 | + } |
| 146 | + AstNodes::SequenceExpression(n) => { |
| 147 | + n.format_leading_comments(f)?; |
| 148 | + print_left_paren(f)?; |
| 149 | + n.write(f) |
| 150 | + } |
| 151 | + AstNodes::TaggedTemplateExpression(n) => { |
| 152 | + n.format_leading_comments(f)?; |
| 153 | + print_left_paren(f)?; |
| 154 | + n.write(f) |
| 155 | + } |
| 156 | + AstNodes::ThisExpression(n) => { |
| 157 | + n.format_leading_comments(f)?; |
| 158 | + print_left_paren(f)?; |
| 159 | + n.write(f) |
| 160 | + } |
| 161 | + AstNodes::UnaryExpression(n) => { |
| 162 | + n.format_leading_comments(f)?; |
| 163 | + print_left_paren(f)?; |
| 164 | + n.write(f) |
| 165 | + } |
| 166 | + AstNodes::UpdateExpression(n) => { |
| 167 | + n.format_leading_comments(f)?; |
| 168 | + print_left_paren(f)?; |
| 169 | + n.write(f) |
| 170 | + } |
| 171 | + AstNodes::YieldExpression(n) => { |
| 172 | + n.format_leading_comments(f)?; |
| 173 | + print_left_paren(f)?; |
| 174 | + n.write(f) |
| 175 | + } |
| 176 | + AstNodes::PrivateInExpression(n) => { |
| 177 | + n.format_leading_comments(f)?; |
| 178 | + print_left_paren(f)?; |
| 179 | + n.write(f) |
| 180 | + } |
| 181 | + AstNodes::JSXElement(n) => { |
| 182 | + n.format_leading_comments(f)?; |
| 183 | + print_left_paren(f)?; |
| 184 | + n.write(f) |
| 185 | + } |
| 186 | + AstNodes::JSXFragment(n) => { |
| 187 | + n.format_leading_comments(f)?; |
| 188 | + print_left_paren(f)?; |
| 189 | + n.write(f) |
| 190 | + } |
| 191 | + AstNodes::TSAsExpression(n) => { |
| 192 | + n.format_leading_comments(f)?; |
| 193 | + print_left_paren(f)?; |
| 194 | + n.write(f) |
| 195 | + } |
| 196 | + AstNodes::TSSatisfiesExpression(n) => { |
| 197 | + n.format_leading_comments(f)?; |
| 198 | + print_left_paren(f)?; |
| 199 | + n.write(f) |
| 200 | + } |
| 201 | + AstNodes::TSTypeAssertion(n) => { |
| 202 | + n.format_leading_comments(f)?; |
| 203 | + print_left_paren(f)?; |
| 204 | + n.write(f) |
| 205 | + } |
| 206 | + AstNodes::TSNonNullExpression(n) => { |
| 207 | + n.format_leading_comments(f)?; |
| 208 | + print_left_paren(f)?; |
| 209 | + n.write(f) |
| 210 | + } |
| 211 | + AstNodes::TSInstantiationExpression(n) => { |
| 212 | + n.format_leading_comments(f)?; |
| 213 | + print_left_paren(f)?; |
| 214 | + n.write(f) |
| 215 | + } |
| 216 | + AstNodes::V8IntrinsicExpression(n) => { |
| 217 | + n.format_leading_comments(f)?; |
| 218 | + print_left_paren(f)?; |
| 219 | + n.write(f) |
| 220 | + } |
| 221 | + AstNodes::StaticMemberExpression(n) => { |
| 222 | + n.format_leading_comments(f)?; |
| 223 | + print_left_paren(f)?; |
| 224 | + n.write(f) |
| 225 | + } |
| 226 | + AstNodes::ComputedMemberExpression(n) => { |
| 227 | + n.format_leading_comments(f)?; |
| 228 | + print_left_paren(f)?; |
| 229 | + n.write(f) |
| 230 | + } |
| 231 | + AstNodes::PrivateFieldExpression(n) => { |
| 232 | + n.format_leading_comments(f)?; |
| 233 | + print_left_paren(f)?; |
| 234 | + n.write(f) |
| 235 | + } |
| 236 | + _ => unreachable!(), |
| 237 | + }?; |
| 238 | + |
| 239 | + if needs_parentheses { |
| 240 | + write!(f, [")"])?; |
| 241 | + } |
| 242 | + |
| 243 | + Ok(()) |
| 244 | + } |
| 245 | +} |
0 commit comments