diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 425f42890..04f45d246 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -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> { diff --git a/crates/oxc_prettier/src/format/arrow_function.rs b/crates/oxc_prettier/src/format/arrow_function.rs new file mode 100644 index 000000000..a1da17091 --- /dev/null +++ b/crates/oxc_prettier/src/format/arrow_function.rs @@ -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) + } +} diff --git a/crates/oxc_prettier/src/format/mod.rs b/crates/oxc_prettier/src/format/mod.rs index 51753ab91..7016dbfdb 100644 --- a/crates/oxc_prettier/src/format/mod.rs +++ b/crates/oxc_prettier/src/format/mod.rs @@ -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) } } diff --git a/crates/oxc_prettier/src/format/module.rs b/crates/oxc_prettier/src/format/module.rs new file mode 100644 index 000000000..0a1d1eba6 --- /dev/null +++ b/crates/oxc_prettier/src/format/module.rs @@ -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) + } +} diff --git a/tasks/prettier_conformance/prettier.snap.md b/tasks/prettier_conformance/prettier.snap.md index 57c3abf25..7ef4c20fd 100644 --- a/tasks/prettier_conformance/prettier.snap.md +++ b/tasks/prettier_conformance/prettier.snap.md @@ -1,4 +1,4 @@ -Compatibility: 9/173 (5.20%) +Compatibility: 10/173 (5.78%) # Failed