mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(pretter): start formatting ModuleDeclaration and ArrowExpression (#1354)
This commit is contained in:
parent
40be405a57
commit
9754ef0d95
5 changed files with 83 additions and 13 deletions
|
|
@ -1848,15 +1848,14 @@ impl<'a> ModuleDeclaration<'a> {
|
|||
matches!(self, Self::ImportDeclaration(_))
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub fn is_export(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::ExportAllDeclaration(_)
|
||||
| Self::ExportDefaultDeclaration(_)
|
||||
| Self::ExportNamedDeclaration(_)
|
||||
| Self::TSExportAssignment(_)
|
||||
| Self::TSNamespaceExportDeclaration(_)
|
||||
)
|
||||
matches!(self, Self::ExportAllDeclaration(_) | Self::ExportDefaultDeclaration(_) | Self::ExportNamedDeclaration(_)
|
||||
| Self::TSExportAssignment(_) | Self::TSNamespaceExportDeclaration(_))
|
||||
}
|
||||
|
||||
pub fn is_default_export(&self) -> bool {
|
||||
matches!(self, Self::ExportDefaultDeclaration(_))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1977,6 +1976,10 @@ impl<'a> ExportNamedDeclaration<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Export Default Declaration
|
||||
/// export default HoistableDeclaration
|
||||
/// export default ClassDeclaration
|
||||
/// export default AssignmentExpression
|
||||
#[derive(Debug, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))]
|
||||
pub struct ExportDefaultDeclaration<'a> {
|
||||
|
|
|
|||
15
crates/oxc_prettier/src/format/arrow_function.rs
Normal file
15
crates/oxc_prettier/src/format/arrow_function.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_ast::ast::*;
|
||||
|
||||
use crate::{doc::Doc, ss, Format, Prettier};
|
||||
|
||||
impl<'a> Prettier<'a> {
|
||||
pub(super) fn print_arrow_function(&mut self, expr: &ArrowExpression<'a>) -> Doc<'a> {
|
||||
let mut parts = self.vec();
|
||||
|
||||
parts.push(ss!("() => "));
|
||||
parts.push(expr.body.format(self));
|
||||
|
||||
Doc::Array(parts)
|
||||
}
|
||||
}
|
||||
|
|
@ -9,11 +9,13 @@ use oxc_allocator::{Box, Vec};
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_ast::ast::*;
|
||||
|
||||
mod arrow_function;
|
||||
mod binaryish;
|
||||
mod block;
|
||||
mod call_expression;
|
||||
mod function;
|
||||
mod function_parameters;
|
||||
mod module;
|
||||
mod object;
|
||||
mod statement;
|
||||
mod ternary;
|
||||
|
|
@ -341,7 +343,11 @@ impl<'a> Format<'a> for DebuggerStatement {
|
|||
|
||||
impl<'a> Format<'a> for ModuleDeclaration<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
if let ModuleDeclaration::ImportDeclaration(decl) = self {
|
||||
decl.format(p)
|
||||
} else {
|
||||
p.print_export_declaration(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -488,6 +494,18 @@ impl<'a> Format<'a> for ExportNamedDeclaration<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Format<'a> for TSExportAssignment<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Format<'a> for TSNamespaceExportDeclaration {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Format<'a> for ExportSpecifier {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
|
|
@ -508,12 +526,18 @@ impl<'a> Format<'a> for ExportAllDeclaration<'a> {
|
|||
|
||||
impl<'a> Format<'a> for ExportDefaultDeclaration<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
self.declaration.format(p)
|
||||
}
|
||||
}
|
||||
impl<'a> Format<'a> for ExportDefaultDeclarationKind<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
match self {
|
||||
Self::Expression(expr) => expr.format(p),
|
||||
Self::FunctionDeclaration(decl) => decl.format(p),
|
||||
Self::ClassDeclaration(decl) => decl.format(p),
|
||||
Self::TSInterfaceDeclaration(decl) => decl.format(p),
|
||||
Self::TSEnumDeclaration(decl) => decl.format(p),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -799,7 +823,7 @@ impl<'a> Format<'a> for PropertyKey<'a> {
|
|||
|
||||
impl<'a> Format<'a> for ArrowExpression<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
p.print_arrow_function(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
28
crates/oxc_prettier/src/format/module.rs
Normal file
28
crates/oxc_prettier/src/format/module.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_ast::ast::*;
|
||||
|
||||
use crate::{doc::Doc, ss, Format, Prettier};
|
||||
|
||||
impl<'a> Prettier<'a> {
|
||||
pub(super) fn print_export_declaration(&mut self, decl: &ModuleDeclaration<'a>) -> Doc<'a> {
|
||||
debug_assert!(decl.is_export());
|
||||
|
||||
let mut parts = self.vec();
|
||||
parts.push(ss!("export"));
|
||||
|
||||
if decl.is_default_export() {
|
||||
parts.push(ss!(" default "));
|
||||
}
|
||||
|
||||
parts.push(match decl {
|
||||
ModuleDeclaration::ImportDeclaration(decl) => unreachable!(),
|
||||
ModuleDeclaration::ExportAllDeclaration(decl) => decl.format(self),
|
||||
ModuleDeclaration::ExportDefaultDeclaration(decl) => decl.format(self),
|
||||
ModuleDeclaration::ExportNamedDeclaration(decl) => decl.format(self),
|
||||
ModuleDeclaration::TSExportAssignment(decl) => decl.format(self),
|
||||
ModuleDeclaration::TSNamespaceExportDeclaration(decl) => decl.format(self),
|
||||
});
|
||||
|
||||
Doc::Array(parts)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
Compatibility: 9/173 (5.20%)
|
||||
Compatibility: 10/173 (5.78%)
|
||||
|
||||
# Failed
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue