Skip to content

Commit

Permalink
feat(prettier): class improvements part 2 (#5838)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix authored Sep 18, 2024
1 parent fd70c4b commit 31da9bc
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 103 deletions.
10 changes: 10 additions & 0 deletions crates/oxc_prettier/src/format/arrow_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@ pub(super) fn print_arrow_function<'a>(
parts.push(ss!("async "));
}

if let Some(type_params) = &expr.type_parameters {
parts.push(type_params.format(p));
}

let parameters = expr.params.format(p);
parts.push(group!(p, parameters));

if let Some(return_type) = &expr.return_type {
parts.push(ss!(": "));
parts.push(return_type.type_annotation.format(p));
}

parts.push(ss!(" => "));

if expr.expression {
Expand Down
18 changes: 14 additions & 4 deletions crates/oxc_prettier/src/format/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ pub(super) fn print_class<'a>(p: &mut Prettier<'a>, class: &Class<'a>) -> Doc<'a
parts.extend(hardline!());
}

if class.declare {
parts.push(ss!("declare "));
}

if class.r#abstract {
parts.push(ss!("abstract "));
}

parts.push(ss!("class "));
if let Some(id) = &class.id {
parts.push(id.format(p));
Expand All @@ -37,6 +42,11 @@ pub(super) fn print_class<'a>(p: &mut Prettier<'a>, class: &Class<'a>) -> Doc<'a
if let Some(super_class) = &class.super_class {
parts.push(ss!("extends "));
parts.push(super_class.format(p));

if let Some(super_type_parameters) = &class.super_type_parameters {
parts.push(super_type_parameters.format(p));
}

parts.push(space!());
}

Expand Down Expand Up @@ -243,6 +253,10 @@ pub(super) fn print_class_property<'a>(
parts.push(ss!("static "));
}

if node.is_abstract() {
parts.push(ss!("abstract "));
}

if node.is_override() {
parts.push(ss!("override "));
}
Expand All @@ -251,10 +265,6 @@ pub(super) fn print_class_property<'a>(
parts.push(ss!("readonly "));
}

if node.is_abstract() {
parts.push(ss!("abstract "));
}

parts.push(node.format_key(p));

if node.is_optional() {
Expand Down
39 changes: 32 additions & 7 deletions crates/oxc_prettier/src/format/function.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use oxc_ast::ast::*;

use crate::{
array,
doc::{Doc, DocBuilder},
format::function_parameters::should_group_function_parameters,
group, if_break, indent, softline, space, ss, Format, Prettier,
Expand All @@ -13,6 +12,11 @@ pub(super) fn print_function<'a>(
property_name: Option<&str>,
) -> Doc<'a> {
let mut parts = p.vec();

if func.declare {
parts.push(ss!("declare "));
}

if func.r#async {
parts.push(ss!("async "));
}
Expand All @@ -28,12 +32,13 @@ pub(super) fn print_function<'a>(
parts.push(p.str(" "));
}

if let Some(type_params) = &func.type_parameters {
parts.push(type_params.format(p));
}
if let Some(id) = &func.id {
parts.push(p.str(id.name.as_str()));
}

if let Some(type_params) = &func.type_parameters {
parts.push(type_params.format(p));
}
// Prettier has `returnTypeDoc` to group together, write this for keep same with prettier.
parts.push(group!(p, {
if should_group_function_parameters(func) {
Expand All @@ -42,6 +47,12 @@ pub(super) fn print_function<'a>(
func.params.format(p)
}
}));

if let Some(return_type) = &func.return_type {
parts.push(ss!(": "));
parts.push(return_type.type_annotation.format(p));
}

if let Some(body) = &func.body {
parts.push(space!());
parts.push(body.format(p));
Expand All @@ -63,12 +74,16 @@ pub(super) fn print_method<'a>(p: &mut Prettier<'a>, method: &MethodDefinition<'
parts.push(space!());
}

if method.r#static {
parts.push(ss!("static "));
}

if matches!(method.r#type, MethodDefinitionType::TSAbstractMethodDefinition) {
parts.push(ss!("abstract "));
}

if method.r#static {
parts.push(ss!("static "));
if method.r#override {
parts.push(ss!("override "));
}

match method.kind {
Expand All @@ -91,6 +106,10 @@ pub(super) fn print_method<'a>(p: &mut Prettier<'a>, method: &MethodDefinition<'

parts.push(method.key.format(p));

if method.optional {
parts.push(ss!("?"));
}

parts.push(print_method_value(p, &method.value));

Doc::Array(parts)
Expand All @@ -102,10 +121,16 @@ fn print_method_value<'a>(p: &mut Prettier<'a>, function: &Function<'a>) -> Doc<
let should_group_parameters = should_group_function_parameters(function);
let parameters_doc =
if should_group_parameters { group!(p, parameters_doc) } else { parameters_doc };

if let Some(type_parameters) = &function.type_parameters {
parts.push(type_parameters.format(p));
}

parts.push(group!(p, parameters_doc));

if let Some(ret_typ) = &function.return_type {
parts.push(array![p, ss!(": "), ret_typ.type_annotation.format(p)]);
parts.push(ss!(": "));
parts.push(ret_typ.type_annotation.format(p));
}

if let Some(body) = &function.body {
Expand Down
13 changes: 13 additions & 0 deletions crates/oxc_prettier/src/format/function_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ pub(super) fn print_function_parameters<'a>(
let len = params.items.len();
let has_rest = params.rest.is_some();
for (i, param) in params.items.iter().enumerate() {
if let Some(accessibility) = &param.accessibility {
printed.push(ss!(accessibility.as_str()));
printed.push(space!());
}

if param.r#override {
printed.push(ss!("override "));
}

if param.readonly {
printed.push(ss!("readonly "));
}

printed.push(param.format(p));
if i == len - 1 && !has_rest {
break;
Expand Down
78 changes: 61 additions & 17 deletions crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,11 @@ impl<'a> Format<'a> for VariableDeclaration<'a> {
let kind = self.kind.as_str();

let mut parts = p.vec();

if self.declare {
parts.push(ss!("declare "));
}

parts.push(ss!(kind));
parts.push(space!());

Expand Down Expand Up @@ -806,7 +811,36 @@ impl<'a> Format<'a> for TSConstructorType<'a> {

impl<'a> Format<'a> for TSFunctionType<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
line!()
let mut parts = p.vec();

if let Some(type_parameters) = &self.type_parameters {
parts.push(type_parameters.format(p));
}

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

parts.push(self.params.format(p));

parts.push(ss!(" => "));
parts.push(self.return_type.type_annotation.format(p));

Doc::Array(parts)
}
}
impl<'a> Format<'a> for TSThisParameter<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let mut parts = p.vec();
parts.push(ss!("this"));

if let Some(type_annotation) = &self.type_annotation {
parts.push(ss!(": "));
parts.push(type_annotation.type_annotation.format(p));
}

Doc::Array(parts)
}
}

Expand Down Expand Up @@ -919,7 +953,7 @@ impl<'a> Format<'a> for TSNamedTupleMember<'a> {

impl<'a> Format<'a> for TSQualifiedName<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
line!()
array!(p, self.left.format(p), ss!("."), self.right.format(p))
}
}

Expand Down Expand Up @@ -984,7 +1018,25 @@ impl<'a> Format<'a> for TSTypePredicate<'a> {

impl<'a> Format<'a> for TSTypeQuery<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
line!()
let mut parts = p.vec();

parts.push(ss!("typeof "));

match &self.expr_name {
TSTypeQueryExprName::TSImportType(import_type) => parts.push(import_type.format(p)),
TSTypeQueryExprName::IdentifierReference(identifier_reference) => {
parts.push(identifier_reference.format(p));
}
TSTypeQueryExprName::QualifiedName(qualified_name) => {
parts.push(qualified_name.format(p));
}
}

if let Some(type_parameters) = &self.type_parameters {
parts.push(type_parameters.format(p));
}

Doc::Array(parts)
}
}

Expand Down Expand Up @@ -1057,6 +1109,10 @@ impl<'a> Format<'a> for TSInterfaceDeclaration<'a> {
parts.push(type_parameters.format(p));
}

if let Some(type_parameters) = &self.type_parameters {
parts.push(type_parameters.format(p));
}

parts.push(space!());

if let Some(extends) = &self.extends {
Expand Down Expand Up @@ -1278,7 +1334,7 @@ impl<'a> Format<'a> for TSTypeParameter<'a> {
parts.push(self.name.format(p));

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

Expand All @@ -1296,10 +1352,6 @@ impl<'a> Format<'a> for TSTypeParameterDeclaration<'a> {
let mut parts = p.vec();
let mut print_comma = false;

if self.params.len() == 0 {
return Doc::Array(parts);
}

parts.push(ss!("<"));

for param in &self.params {
Expand All @@ -1323,10 +1375,6 @@ impl<'a> Format<'a> for TSTypeParameterInstantiation<'a> {
let mut parts = p.vec();
let mut print_comma = false;

if self.params.len() == 0 {
return Doc::Array(parts);
}

parts.push(ss!("<"));

for param in &self.params {
Expand Down Expand Up @@ -2179,11 +2227,7 @@ impl<'a> Format<'a> for AssignmentTargetPropertyIdentifier<'a> {

impl<'a> Format<'a> for AssignmentTargetPropertyProperty<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let mut parts = p.vec();
parts.push(self.name.format(p));
parts.push(ss!(": "));
parts.push(self.binding.format(p));
Doc::Array(parts)
array!(p, self.name.format(p), ss!(": "), self.binding.format(p))
}
}

Expand Down
Loading

0 comments on commit 31da9bc

Please sign in to comment.