mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(prettier): add print_function and print_function_parameters (#1346)
This commit is contained in:
parent
147b4bc530
commit
66aae99c63
3 changed files with 65 additions and 34 deletions
33
crates/oxc_prettier/src/format/function.rs
Normal file
33
crates/oxc_prettier/src/format/function.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_ast::ast::*;
|
||||
|
||||
use crate::{doc::Doc, ss, Format, Prettier};
|
||||
|
||||
impl<'a> Prettier<'a> {
|
||||
pub(super) fn print_function(&mut self, func: &Function<'a>) -> Doc<'a> {
|
||||
let mut parts = self.vec();
|
||||
if let Some(comments) = self.print_leading_comments(func.span) {
|
||||
parts.push(comments);
|
||||
}
|
||||
if func.r#async {
|
||||
parts.push(ss!("async "));
|
||||
}
|
||||
if func.generator {
|
||||
parts.push(ss!("function* "));
|
||||
} else {
|
||||
parts.push(ss!("function "));
|
||||
}
|
||||
if let Some(type_params) = &func.type_parameters {
|
||||
parts.push(type_params.format(self));
|
||||
}
|
||||
if let Some(id) = &func.id {
|
||||
parts.push(self.str(id.name.as_str()));
|
||||
}
|
||||
parts.push(func.params.format(self));
|
||||
if let Some(body) = &func.body {
|
||||
parts.push(ss!(" "));
|
||||
parts.push(body.format(self));
|
||||
}
|
||||
Doc::Array(parts)
|
||||
}
|
||||
}
|
||||
26
crates/oxc_prettier/src/format/function_parameters.rs
Normal file
26
crates/oxc_prettier/src/format/function_parameters.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_ast::ast::*;
|
||||
|
||||
use crate::{doc::Doc, ss, Format, Prettier};
|
||||
|
||||
impl<'a> Prettier<'a> {
|
||||
pub(super) fn print_function_parameters(&mut self, params: &FormalParameters<'a>) -> Doc<'a> {
|
||||
let mut parts = self.vec();
|
||||
parts.push(ss!("("));
|
||||
|
||||
for (i, param) in params.items.iter().enumerate() {
|
||||
parts.push(param.format(self));
|
||||
if i < params.items.len() - 1 {
|
||||
parts.push(ss!(", "));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(rest) = ¶ms.rest {
|
||||
parts.push(ss!(", "));
|
||||
parts.push(rest.format(self));
|
||||
}
|
||||
|
||||
parts.push(ss!(")"));
|
||||
Doc::Array(parts)
|
||||
}
|
||||
}
|
||||
|
|
@ -12,6 +12,8 @@ use oxc_ast::ast::*;
|
|||
mod binaryish;
|
||||
mod block;
|
||||
mod call_expression;
|
||||
mod function;
|
||||
mod function_parameters;
|
||||
mod object;
|
||||
mod statement;
|
||||
mod ternary;
|
||||
|
|
@ -459,30 +461,7 @@ impl<'a> Format<'a> for VariableDeclarator<'a> {
|
|||
|
||||
impl<'a> Format<'a> for Function<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
let mut parts = p.vec();
|
||||
if let Some(comments) = p.print_leading_comments(self.span) {
|
||||
parts.push(comments);
|
||||
}
|
||||
if self.r#async {
|
||||
parts.push(ss!("async "));
|
||||
}
|
||||
if self.generator {
|
||||
parts.push(ss!("function* "));
|
||||
} else {
|
||||
parts.push(ss!("function "));
|
||||
}
|
||||
if let Some(type_params) = &self.type_parameters {
|
||||
parts.push(format!(p, type_params));
|
||||
}
|
||||
if let Some(id) = &self.id {
|
||||
parts.push(p.str(id.name.as_str()));
|
||||
}
|
||||
parts.push(self.params.format(p));
|
||||
if let Some(body) = &self.body {
|
||||
parts.push(ss!(" "));
|
||||
parts.push(body.format(p));
|
||||
}
|
||||
Doc::Array(parts)
|
||||
p.print_function(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -494,20 +473,13 @@ impl<'a> Format<'a> for FunctionBody<'a> {
|
|||
|
||||
impl<'a> Format<'a> for FormalParameters<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
let mut parts = p.vec();
|
||||
parts.push(ss!("("));
|
||||
parts.extend(self.items.iter().map(|stmt| stmt.format(p)));
|
||||
if let Some(rest) = &self.rest {
|
||||
parts.push(rest.format(p));
|
||||
}
|
||||
parts.push(ss!(")"));
|
||||
Doc::Array(parts)
|
||||
p.print_function_parameters(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Format<'a> for FormalParameter<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
self.pattern.format(p)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1300,7 +1272,7 @@ impl<'a> Format<'a> for BindingProperty<'a> {
|
|||
|
||||
impl<'a> Format<'a> for RestElement<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
array!(p, ss!("..."), format!(p, self.argument))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue