mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(prettier): print module specifiers (#1383)
This commit is contained in:
parent
001a8d8fe9
commit
f8b9679d74
3 changed files with 42 additions and 30 deletions
|
|
@ -752,17 +752,7 @@ impl<'a> Format<'a> for ImportDeclaration<'a> {
|
|||
parts.push(ss!(" type"));
|
||||
}
|
||||
if let Some(specifiers) = &self.specifiers {
|
||||
parts.push(ss!(" {"));
|
||||
if !specifiers.is_empty() {
|
||||
parts.push(ss!(" "));
|
||||
}
|
||||
for (i, specifier) in specifiers.iter().enumerate() {
|
||||
if i != 0 {
|
||||
parts.push(ss!(", "));
|
||||
}
|
||||
parts.push(specifier.format(p));
|
||||
}
|
||||
parts.push(ss!(" }"));
|
||||
parts.push(module::print_module_specifiers(p, specifiers));
|
||||
}
|
||||
parts.push(ss!(" from "));
|
||||
parts.push(self.source.format(p));
|
||||
|
|
@ -816,7 +806,13 @@ impl<'a> Format<'a> for ImportAttribute {
|
|||
|
||||
impl<'a> Format<'a> for ExportNamedDeclaration<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
let mut parts = p.vec();
|
||||
parts.push(ss!(" "));
|
||||
parts.push(module::print_module_specifiers(p, &self.specifiers));
|
||||
if let Some(decl) = &self.declaration {
|
||||
parts.push(decl.format(p));
|
||||
}
|
||||
Doc::Array(parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use oxc_allocator::Vec;
|
||||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_ast::ast::*;
|
||||
|
||||
|
|
@ -45,13 +46,37 @@ fn print_semicolon_after_export_declaration<'a>(
|
|||
return None;
|
||||
}
|
||||
|
||||
let ModuleDeclaration::ExportDefaultDeclaration(decl) = decl else { return None };
|
||||
|
||||
match decl.declaration {
|
||||
ExportDefaultDeclarationKind::Expression(_) => Some(ss!(";")),
|
||||
ExportDefaultDeclarationKind::FunctionDeclaration(_)
|
||||
| ExportDefaultDeclarationKind::ClassDeclaration(_)
|
||||
| ExportDefaultDeclarationKind::TSInterfaceDeclaration(_)
|
||||
| ExportDefaultDeclarationKind::TSEnumDeclaration(_) => None,
|
||||
match decl {
|
||||
ModuleDeclaration::ExportDefaultDeclaration(decl) => match decl.declaration {
|
||||
ExportDefaultDeclarationKind::Expression(_) => Some(ss!(";")),
|
||||
ExportDefaultDeclarationKind::FunctionDeclaration(_)
|
||||
| ExportDefaultDeclarationKind::ClassDeclaration(_)
|
||||
| ExportDefaultDeclarationKind::TSInterfaceDeclaration(_)
|
||||
| ExportDefaultDeclarationKind::TSEnumDeclaration(_) => None,
|
||||
},
|
||||
ModuleDeclaration::ExportAllDeclaration(_) => Some(ss!(";")),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_module_specifiers<'a, T: Format<'a>>(
|
||||
p: &mut Prettier<'a>,
|
||||
specifiers: &Vec<'a, T>,
|
||||
) -> Doc<'a> {
|
||||
let mut parts = p.vec();
|
||||
parts.push(ss!("{"));
|
||||
|
||||
if !specifiers.is_empty() {
|
||||
parts.push(ss!(" "));
|
||||
|
||||
for (i, specifier) in specifiers.iter().enumerate() {
|
||||
if i != 0 {
|
||||
parts.push(ss!(", "));
|
||||
}
|
||||
parts.push(specifier.format(p));
|
||||
}
|
||||
}
|
||||
|
||||
parts.push(ss!("}"));
|
||||
Doc::Array(parts)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
Compatibility: 52/881 (5.90%)
|
||||
Compatibility: 59/881 (6.70%)
|
||||
|
||||
# Failed
|
||||
|
||||
|
|
@ -522,14 +522,6 @@ Compatibility: 52/881 (5.90%)
|
|||
### export-default/export-default-from
|
||||
* export-default/export-default-from/export.js
|
||||
|
||||
### export-star
|
||||
* export-star/export-star-as-default.js
|
||||
* export-star/export-star-as-reserved-word.js
|
||||
* export-star/export-star-as-string.js
|
||||
* export-star/export-star-as-string2.js
|
||||
* export-star/export-star-as.js
|
||||
* export-star/export-star.js
|
||||
|
||||
### expression_statement
|
||||
* expression_statement/no_regression.js
|
||||
|
||||
|
|
@ -635,7 +627,6 @@ Compatibility: 52/881 (5.90%)
|
|||
|
||||
### import-assertions/bracket-spacing
|
||||
* import-assertions/bracket-spacing/dynamic-import.js
|
||||
* import-assertions/bracket-spacing/empty.js
|
||||
* import-assertions/bracket-spacing/re-export.js
|
||||
* import-assertions/bracket-spacing/static-import.js
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue