Skip to content

Commit

Permalink
refactor(prettier): add a space!() macro (oxc-project#2348)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored and IWANABETHATGUY committed May 29, 2024
1 parent 4e0b1a0 commit 74ca20e
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 52 deletions.
4 changes: 0 additions & 4 deletions crates/oxc_prettier/src/binaryish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ impl GetPrecedence for BinaryishOperator {
}

impl BinaryishOperator {
pub fn is_binary(self) -> bool {
matches!(self, Self::BinaryOperator(_))
}

pub fn should_flatten(self, parent_op: Self) -> bool {
if self.precedence() != parent_op.precedence() {
return false;
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_prettier/src/comments/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use oxc_span::Span;
use crate::{
array,
doc::{Doc, DocBuilder, Separator},
hardline, line, ss, Prettier,
hardline, line, space, Prettier,
};

use super::{Comment, CommentFlags, DanglingCommentsPrintOptions};
Expand Down Expand Up @@ -97,7 +97,7 @@ impl<'a> Prettier<'a> {
parts.push(line!());
}
} else {
parts.push(ss!(" "));
parts.push(space!());
};
} else {
parts.extend(hardline!());
Expand Down Expand Up @@ -164,7 +164,7 @@ impl<'a> Prettier<'a> {
if !comment.is_block || previous.is_some_and(|c| c.has_line_suffix) {
let suffix = {
let mut parts = self.vec();
parts.push(ss!(" "));
parts.push(space!());
parts.push(printed);
parts
};
Expand All @@ -173,7 +173,7 @@ impl<'a> Prettier<'a> {
return comment.with_line_suffix(true);
}

let doc = array![self, ss!(" "), printed];
let doc = array![self, space!(), printed];
parts.push(doc);
comment.with_line_suffix(false)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_prettier/src/format/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use oxc_ast::{
use crate::{
array,
doc::{Doc, DocBuilder, Group, IndentIfBreak},
group, indent, line, ss, Format, Prettier,
group, indent, line, space, Format, Prettier,
};

use super::{binaryish::should_inline_logical_expression, class::ClassMemberish};
Expand All @@ -25,7 +25,7 @@ pub(super) fn print_assignment_expression<'a>(
p,
AssignmentLikeNode::AssignmentExpression(assignment_expr),
left_doc,
array![p, ss!(" "), Doc::Str(assignment_expr.operator.as_str())],
array![p, space!(), Doc::Str(assignment_expr.operator.as_str())],
Some(&assignment_expr.right),
)
}
Expand Down Expand Up @@ -83,7 +83,7 @@ pub(super) fn print_assignment<'a>(
group!(p, group!(p, left_doc), op, group!(p, indent!(p, line!(), right_doc)))
}
Layout::NeverBreakAfterOperator => {
group!(p, group!(p, left_doc), op, ss!(" "), group!(p, right_doc))
group!(p, group!(p, left_doc), op, space!(), group!(p, right_doc))
}
// First break right-hand side, then after operator
Layout::Fluid => {
Expand All @@ -104,7 +104,7 @@ pub(super) fn print_assignment<'a>(
group!(p, group!(p, left_doc), op, after_op, right_doc)
}
Layout::BreakLhs => {
group!(p, left_doc, op, ss!(" "), group!(p, right_doc))
group!(p, left_doc, op, space!(), group!(p, right_doc))
}
// Parts of assignment chains aren't wrapped in groups.
// Once one of them breaks, the chain breaks too.
Expand Down
38 changes: 24 additions & 14 deletions crates/oxc_prettier/src/format/binaryish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
binaryish::{BinaryishLeft, BinaryishOperator},
comments::CommentFlags,
doc::{Doc, DocBuilder, Group},
group, line, ss, Format, Prettier,
group, line, space, ss, Format, Prettier,
};

pub(super) fn print_binaryish_expression<'a>(
Expand Down Expand Up @@ -38,26 +38,36 @@ fn print_binaryish_expressions<'a>(
parts.push(group!(p, left.format(p)));
}
let should_inline = should_inline_logical_expression(right);
let mut right_parts = p.vec();
right_parts.push(ss!(operator.as_str()));
right_parts.push(if should_inline { ss!(" ") } else { line!() });
right_parts.push(right.format(p));
let line_before_operator = false;

let right = if should_inline {
p.vec()
} else {
let mut parts = p.vec();
if line_before_operator {
parts.push(line!());
}
parts.push(ss!(operator.as_str()));
parts.push(if line_before_operator { space!() } else { line!() });
parts.push(right.format(p));
parts
};

let should_break = p.has_comment(left.span(), CommentFlags::Trailing | CommentFlags::Line);
let should_group = false;

parts.push(ss!(" "));
if should_break {
let group = Doc::Group(Group::new(right_parts, should_break));
parts.push(group);
} else {
parts.push(Doc::Array(right_parts));
if !line_before_operator {
parts.push(space!());
}

if operator.is_binary() {
Doc::Group(Group::new(parts, false))
if should_group {
let group = Doc::Group(Group::new(right, should_break));
parts.push(group);
} else {
Doc::Array(parts)
parts.push(Doc::Array(right));
}

Doc::Array(parts)
}

pub(super) fn should_inline_logical_expression(expr: &Expression) -> bool {
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_prettier/src/format/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
array,
doc::{Doc, DocBuilder},
format::assignment,
hardline, ss, Format, Prettier,
hardline, space, ss, Format, Prettier,
};

use super::assignment::AssignmentLikeNode;
Expand All @@ -15,13 +15,13 @@ pub(super) fn print_class<'a>(p: &mut Prettier<'a>, class: &Class<'a>) -> Doc<'a
parts.push(ss!("class "));
if let Some(id) = &class.id {
parts.push(id.format(p));
parts.push(ss!(" "));
parts.push(space!());
}

if let Some(super_class) = &class.super_class {
parts.push(ss!("extends "));
parts.push(super_class.format(p));
parts.push(ss!(" "));
parts.push(space!());
}

parts.push(class.body.format(p));
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_prettier/src/format/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use oxc_ast::ast::*;
use crate::{
doc::{Doc, DocBuilder},
format::function_parameters::should_group_function_parameters,
group, if_break, indent, softline, ss, Format, Prettier,
group, if_break, indent, softline, space, ss, Format, Prettier,
};

pub(super) fn print_function<'a>(
Expand Down Expand Up @@ -41,7 +41,7 @@ pub(super) fn print_function<'a>(
// Prettier has `returnTypeDoc` to group together, write this for keep same with prettier.
parts.push(group!(p, params_doc));
if let Some(body) = &func.body {
parts.push(ss!(" "));
parts.push(space!());
parts.push(body.format(p));
}
if func.is_ts_declare_function() || func.body.is_none() {
Expand Down Expand Up @@ -94,7 +94,7 @@ fn print_method_value<'a>(p: &mut Prettier<'a>, function: &Function<'a>) -> Doc<
parts.push(group!(p, parameters_doc));

if let Some(body) = &function.body {
parts.push(ss!(" "));
parts.push(space!());
parts.push(body.format(p));
}
Doc::Array(parts)
Expand All @@ -110,7 +110,7 @@ pub(super) fn print_return_or_throw_argument<'a>(
parts.push(ss!(if is_return { "return" } else { "throw" }));

if let Some(argument) = argument {
parts.push(ss!(" "));
parts.push(space!());
parts.push(
if argument.is_binaryish() || matches!(argument, Expression::SequenceExpression(_)) {
group![
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_prettier/src/format/function_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use oxc_ast::{ast::*, AstKind};
use crate::{
comments::CommentFlags,
doc::{Doc, DocBuilder, Group},
hardline, if_break, indent, line, softline, ss, Format, Prettier,
hardline, if_break, indent, line, softline, space, ss, Format, Prettier,
};

pub(super) fn should_hug_the_only_function_parameter(
Expand Down Expand Up @@ -77,7 +77,7 @@ pub(super) fn print_function_parameters<'a>(
}
printed.push(ss!(","));
if should_hug_the_only_function_parameter(p, params) {
printed.push(ss!(" "));
printed.push(space!());
} else if p.is_next_line_empty(param.span) {
printed.extend(hardline!());
printed.extend(hardline!());
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_prettier/src/format/misc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_ast::{ast::*, AstKind};
use oxc_span::Span;

use crate::{array, doc::Doc, indent, line, ss, Prettier};
use crate::{array, doc::Doc, indent, line, space, ss, Prettier};

pub(super) fn adjust_clause<'a>(
p: &Prettier<'a>,
Expand All @@ -14,7 +14,7 @@ pub(super) fn adjust_clause<'a>(
}

if matches!(node, Statement::BlockStatement(_)) || force_space {
return array![p, ss!(" "), clause];
return array![p, space!(), clause];
}

indent![p, line!(), clause]
Expand Down
22 changes: 11 additions & 11 deletions crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use oxc_syntax::identifier::is_identifier_name;
use crate::{
array,
doc::{Doc, DocBuilder, Group, Separator},
format, group, hardline, indent, line, softline, ss, string, wrap, Prettier,
format, group, hardline, indent, line, softline, space, ss, string, wrap, Prettier,
};

use self::{array::Array, object::ObjectLike, template_literal::TemplateLiteralPrinter};
Expand Down Expand Up @@ -165,7 +165,7 @@ impl<'a> Format<'a> for IfStatement<'a> {
if let Some(alternate) = &self.alternate {
let else_on_same_line = matches!(alternate, Statement::BlockStatement(_));
if else_on_same_line {
parts.push(ss!(" "));
parts.push(space!());
} else {
parts.extend(hardline!());
}
Expand Down Expand Up @@ -312,7 +312,7 @@ impl<'a> Format<'a> for DoWhileStatement<'a> {
parts.push(do_body);

if matches!(self.body, Statement::BlockStatement(_)) {
parts.push(ss!(" "));
parts.push(space!());
} else {
parts.extend(hardline!());
}
Expand All @@ -335,7 +335,7 @@ impl<'a> Format<'a> for ContinueStatement {
parts.push(ss!("continue"));

if let Some(label) = &self.label {
parts.push(ss!(" "));
parts.push(space!());
parts.push(format!(p, label));
}

Expand All @@ -349,7 +349,7 @@ impl<'a> Format<'a> for BreakStatement {
parts.push(ss!("break"));

if let Some(label) = &self.label {
parts.push(ss!(" "));
parts.push(space!());
parts.push(format!(p, label));
}

Expand Down Expand Up @@ -434,7 +434,7 @@ impl<'a> Format<'a> for SwitchCase<'a> {
}

if is_only_one_block_statement {
consequent_parts.push(ss!(" "));
consequent_parts.push(space!());
} else {
consequent_parts.extend(hardline!());
}
Expand Down Expand Up @@ -478,7 +478,7 @@ impl<'a> Format<'a> for TryStatement<'a> {
parts.push(ss!("try "));
parts.push(format!(p, self.block));
if let Some(handler) = &self.handler {
parts.push(ss!(" "));
parts.push(space!());
parts.push(format!(p, handler));
}
if let Some(finalizer) = &self.finalizer {
Expand Down Expand Up @@ -584,7 +584,7 @@ impl<'a> Format<'a> for VariableDeclaration<'a> {

let mut parts = p.vec();
parts.push(ss!(kind));
parts.push(ss!(" "));
parts.push(space!());

let is_hardline = !p.parent_kind().is_iteration_statement()
&& self.declarations.iter().all(|decl| decl.init.is_some());
Expand Down Expand Up @@ -1103,7 +1103,7 @@ impl<'a> Format<'a> for ExportNamedDeclaration<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let mut parts = p.vec();
if let Some(decl) = &self.declaration {
parts.push(ss!(" "));
parts.push(space!());
parts.push(decl.format(p));
} else {
parts.push(module::print_module_specifiers(
Expand Down Expand Up @@ -1628,7 +1628,7 @@ impl<'a> Format<'a> for YieldExpression<'a> {
parts.push(ss!("*"));
}
if let Some(argument) = &self.argument {
parts.push(ss!(" "));
parts.push(space!());
parts.push(format!(p, argument));
}
Doc::Array(parts)
Expand All @@ -1654,7 +1654,7 @@ impl<'a> Format<'a> for UnaryExpression<'a> {
let mut parts = p.vec();
parts.push(string!(p, self.operator.as_str()));
if self.operator.is_keyword() {
parts.push(ss!(" "));
parts.push(space!());
}
parts.push(format!(p, self.argument));
Doc::Array(parts)
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_prettier/src/format/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use oxc_ast::ast::*;

use crate::{
doc::{Doc, DocBuilder, Separator},
group, if_break, indent, line, softline, ss, Format, Prettier,
group, if_break, indent, line, softline, space, ss, Format, Prettier,
};

pub(super) fn print_export_declaration<'a>(
Expand Down Expand Up @@ -75,7 +75,7 @@ pub fn print_module_specifiers<'a, T: Format<'a>>(
if specifiers.is_empty() {
parts.push(ss!(" {}"));
} else {
parts.push(ss!(" "));
parts.push(space!());

let mut specifiers_iter: VecDeque<_> = specifiers.iter().collect();
if include_default {
Expand Down Expand Up @@ -113,11 +113,11 @@ pub fn print_module_specifiers<'a, T: Format<'a>>(
} else {
parts.push(ss!("{"));
if p.options.bracket_spacing {
parts.push(ss!(" "));
parts.push(space!());
}
parts.extend(specifiers_iter.iter().map(|s| s.format(p)));
if p.options.bracket_spacing {
parts.push(ss!(" "));
parts.push(space!());
}
parts.push(ss!("}"));
}
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_prettier/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ macro_rules! ss {
}};
}

#[macro_export]
macro_rules! space {
() => {{
Doc::Str(" ")
}};
}

#[macro_export]
macro_rules! string {
($p:ident, $s:expr) => {{
Expand Down

0 comments on commit 74ca20e

Please sign in to comment.