mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(prettier): support format function for ImportDefaultSpecifier (#1424)
This commit is contained in:
parent
d6b913e46a
commit
b945307ab3
3 changed files with 18 additions and 7 deletions
|
|
@ -896,7 +896,10 @@ impl<'a> Format<'a> for ImportDeclaration<'a> {
|
|||
parts.push(ss!(" type"));
|
||||
}
|
||||
if let Some(specifiers) = &self.specifiers {
|
||||
parts.push(module::print_module_specifiers(p, specifiers));
|
||||
let is_default = specifiers.get(0).is_some_and(|x| {
|
||||
matches!(x, ImportDeclarationSpecifier::ImportDefaultSpecifier(_))
|
||||
});
|
||||
parts.push(module::print_module_specifiers(p, specifiers, is_default));
|
||||
}
|
||||
parts.push(ss!(" from "));
|
||||
parts.push(self.source.format(p));
|
||||
|
|
@ -929,7 +932,7 @@ impl<'a> Format<'a> for ImportSpecifier {
|
|||
|
||||
impl<'a> Format<'a> for ImportDefaultSpecifier {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
self.local.format(p)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -954,7 +957,7 @@ impl<'a> Format<'a> for ImportAttribute {
|
|||
impl<'a> Format<'a> for ExportNamedDeclaration<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
let mut parts = p.vec();
|
||||
parts.push(module::print_module_specifiers(p, &self.specifiers));
|
||||
parts.push(module::print_module_specifiers(p, &self.specifiers, false));
|
||||
if let Some(decl) = &self.declaration {
|
||||
parts.push(decl.format(p));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,6 +65,7 @@ fn print_semicolon_after_export_declaration<'a>(
|
|||
pub fn print_module_specifiers<'a, T: Format<'a>>(
|
||||
p: &mut Prettier<'a>,
|
||||
specifiers: &Vec<'a, T>,
|
||||
include_default: bool,
|
||||
) -> Doc<'a> {
|
||||
let mut parts = p.vec();
|
||||
if specifiers.is_empty() {
|
||||
|
|
@ -74,8 +75,16 @@ pub fn print_module_specifiers<'a, T: Format<'a>>(
|
|||
|
||||
let can_break = specifiers.len() > 1;
|
||||
|
||||
let mut specifiers_iter = specifiers.iter();
|
||||
if include_default {
|
||||
parts.push(specifiers_iter.next().unwrap().format(p));
|
||||
if can_break {
|
||||
parts.push(p.str(", "));
|
||||
}
|
||||
}
|
||||
|
||||
if can_break {
|
||||
let docs = specifiers.iter().map(|s| s.format(p)).collect::<std::vec::Vec<_>>();
|
||||
let docs = specifiers_iter.map(|s| s.format(p)).collect::<std::vec::Vec<_>>();
|
||||
parts.push(group![
|
||||
p,
|
||||
ss!("{"),
|
||||
|
|
@ -88,12 +97,12 @@ pub fn print_module_specifiers<'a, T: Format<'a>>(
|
|||
if p.options.bracket_spacing { line!() } else { softline!() },
|
||||
ss!("}"),
|
||||
]);
|
||||
} else {
|
||||
} else if !include_default {
|
||||
parts.push(ss!("{"));
|
||||
if p.options.bracket_spacing {
|
||||
parts.push(ss!(" "));
|
||||
}
|
||||
parts.extend(specifiers.iter().map(|s| s.format(p)));
|
||||
parts.extend(specifiers_iter.map(|s| s.format(p)));
|
||||
if p.options.bracket_spacing {
|
||||
parts.push(ss!(" "));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -588,7 +588,6 @@ Compatibility: 109/838 (13.01%)
|
|||
* import-reflection/valid-default-import.mjs
|
||||
* import-reflection/valid-from-as-default-module-binding-escaped.mjs
|
||||
* import-reflection/valid-from-as-default-module-binding.mjs
|
||||
* import-reflection/valid-module-as-default-binding-2.mjs
|
||||
* import-reflection/valid-module-as-default-binding.mjs
|
||||
|
||||
### label
|
||||
|
|
|
|||
Loading…
Reference in a new issue