mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(prettier): support format function for ImportNamespaceSpecifier (#1443)
This commit is contained in:
parent
6892a298cf
commit
ca06126d50
3 changed files with 56 additions and 31 deletions
|
|
@ -955,7 +955,15 @@ impl<'a> Format<'a> for ImportDeclaration<'a> {
|
|||
let is_default = specifiers.get(0).is_some_and(|x| {
|
||||
matches!(x, ImportDeclarationSpecifier::ImportDefaultSpecifier(_))
|
||||
});
|
||||
parts.push(module::print_module_specifiers(p, specifiers, is_default));
|
||||
|
||||
let validate_namespace = |x: &ImportDeclarationSpecifier| {
|
||||
matches!(x, ImportDeclarationSpecifier::ImportNamespaceSpecifier(_))
|
||||
};
|
||||
|
||||
let is_namespace = specifiers.get(0).is_some_and(validate_namespace)
|
||||
|| specifiers.get(1).is_some_and(validate_namespace);
|
||||
|
||||
parts.push(module::print_module_specifiers(p, specifiers, is_default, is_namespace));
|
||||
}
|
||||
parts.push(ss!(" from "));
|
||||
parts.push(self.source.format(p));
|
||||
|
|
@ -994,7 +1002,7 @@ impl<'a> Format<'a> for ImportDefaultSpecifier {
|
|||
|
||||
impl<'a> Format<'a> for ImportNamespaceSpecifier {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
array!(p, ss!("* as "), self.local.format(p))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1013,7 +1021,12 @@ 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, false));
|
||||
parts.push(module::print_module_specifiers(
|
||||
p,
|
||||
&self.specifiers,
|
||||
/* include_default */ false,
|
||||
/* include_namespace */ false,
|
||||
));
|
||||
if let Some(decl) = &self.declaration {
|
||||
parts.push(decl.format(p));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use oxc_allocator::Vec;
|
||||
use oxc_ast::ast::*;
|
||||
|
||||
|
|
@ -66,6 +68,7 @@ pub fn print_module_specifiers<'a, T: Format<'a>>(
|
|||
p: &mut Prettier<'a>,
|
||||
specifiers: &Vec<'a, T>,
|
||||
include_default: bool,
|
||||
include_namespace: bool,
|
||||
) -> Doc<'a> {
|
||||
let mut parts = p.vec();
|
||||
if specifiers.is_empty() {
|
||||
|
|
@ -73,40 +76,50 @@ pub fn print_module_specifiers<'a, T: Format<'a>>(
|
|||
} else {
|
||||
parts.push(ss!(" "));
|
||||
|
||||
let can_break = specifiers.len() > 1;
|
||||
|
||||
let mut specifiers_iter = specifiers.iter();
|
||||
let mut specifiers_iter: VecDeque<_> = specifiers.iter().collect();
|
||||
if include_default {
|
||||
parts.push(specifiers_iter.next().unwrap().format(p));
|
||||
if can_break {
|
||||
parts.push(specifiers_iter.pop_front().unwrap().format(p));
|
||||
if !specifiers_iter.is_empty() {
|
||||
parts.push(p.str(", "));
|
||||
}
|
||||
}
|
||||
|
||||
if can_break {
|
||||
let docs = specifiers_iter.map(|s| s.format(p)).collect::<std::vec::Vec<_>>();
|
||||
parts.push(group![
|
||||
p,
|
||||
ss!("{"),
|
||||
indent![
|
||||
if include_namespace {
|
||||
parts.push(specifiers_iter.pop_front().unwrap().format(p));
|
||||
if !specifiers_iter.is_empty() {
|
||||
parts.push(p.str(", "));
|
||||
}
|
||||
}
|
||||
|
||||
if !specifiers_iter.is_empty() {
|
||||
let can_break = specifiers.len() > 1;
|
||||
|
||||
if can_break {
|
||||
let docs =
|
||||
specifiers_iter.iter().map(|s| s.format(p)).collect::<std::vec::Vec<_>>();
|
||||
parts.push(group![
|
||||
p,
|
||||
ss!("{"),
|
||||
indent![
|
||||
p,
|
||||
if p.options.bracket_spacing { line!() } else { softline!() },
|
||||
Doc::Array(p.join(Separator::CommaLine, docs))
|
||||
],
|
||||
if_break!(p, if p.should_print_es5_comma() { "," } else { "" }),
|
||||
if p.options.bracket_spacing { line!() } else { softline!() },
|
||||
Doc::Array(p.join(Separator::CommaLine, docs))
|
||||
],
|
||||
if_break!(p, if p.should_print_es5_comma() { "," } else { "" }),
|
||||
if p.options.bracket_spacing { line!() } else { softline!() },
|
||||
ss!("}"),
|
||||
]);
|
||||
} else if !include_default {
|
||||
parts.push(ss!("{"));
|
||||
if p.options.bracket_spacing {
|
||||
parts.push(ss!(" "));
|
||||
ss!("}"),
|
||||
]);
|
||||
} else {
|
||||
parts.push(ss!("{"));
|
||||
if p.options.bracket_spacing {
|
||||
parts.push(ss!(" "));
|
||||
}
|
||||
parts.extend(specifiers_iter.iter().map(|s| s.format(p)));
|
||||
if p.options.bracket_spacing {
|
||||
parts.push(ss!(" "));
|
||||
}
|
||||
parts.push(ss!("}"));
|
||||
}
|
||||
parts.extend(specifiers_iter.map(|s| s.format(p)));
|
||||
if p.options.bracket_spacing {
|
||||
parts.push(ss!(" "));
|
||||
}
|
||||
parts.push(ss!("}"));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
Compatibility: 113/601 (18.80%)
|
||||
Compatibility: 114/601 (18.97%)
|
||||
|
||||
# Failed
|
||||
|
||||
|
|
@ -408,7 +408,6 @@ Compatibility: 113/601 (18.80%)
|
|||
### import
|
||||
* import/comments.js
|
||||
* import/inline.js
|
||||
* import/multiple_standalones.js
|
||||
* import/same-local-and-imported.js
|
||||
|
||||
### label
|
||||
|
|
|
|||
Loading…
Reference in a new issue