Skip to content

feat(formatter): complete printing ImportExpression #13260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions crates/oxc_formatter/src/write/import_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use oxc_span::GetSpan;
use oxc_syntax::identifier::is_identifier_name;

use crate::{
Format, FormatResult, FormatTrailingCommas, QuoteProperties, TrailingSeparator,
Format, FormatResult, FormatTrailingCommas, QuoteProperties, TrailingSeparator, best_fitting,
format_args,
formatter::{
Formatter, prelude::*, separated::FormatSeparatedIter, trivia::FormatLeadingComments,
},
Expand All @@ -15,20 +16,6 @@ use crate::{

use super::FormatWrite;

impl<'a> FormatWrite<'a> for AstNode<'a, ImportExpression<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
write!(f, ["import"])?;
if let Some(phase) = &self.phase() {
write!(f, [".", phase.as_str()])?;
}
write!(f, ["(", self.source()])?;
if let Some(options) = &self.options() {
write!(f, [",", space(), options])?;
}
write!(f, ")")
}
}

impl<'a> Format<'a> for ImportOrExportKind {
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
if self.is_type() { write!(f, ["type", space()]) } else { Ok(()) }
Expand Down
78 changes: 78 additions & 0 deletions crates/oxc_formatter/src/write/import_expression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use oxc_ast::ast::*;

use crate::{
Format, FormatResult, TrailingSeparator, best_fitting, format_args,
formatter::{Formatter, prelude::*},
generated::ast_nodes::AstNode,
write,
};

use super::FormatWrite;

impl<'a> FormatWrite<'a> for AstNode<'a, ImportExpression<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
write!(f, ["import"])?;
if let Some(phase) = &self.phase() {
write!(f, [".", phase.as_str()])?;
}

// The formatting implementation of `source` and `options` picks from `call_arguments`.
if self.options.is_none()
&& (!matches!(
self.source,
Expression::StringLiteral(_)
| Expression::TemplateLiteral(_)
// Theoretically dynamic import shouldn't have this.
| Expression::TaggedTemplateExpression(_)
) || f.comments().has_comments_before(self.span.end))
{
return write!(
f,
[
"(",
group(&soft_block_indent(&format_once(|f| {
write!(f, [self.source()])?;
if let Some(options) = self.options() {
write!(
f,
[
",",
soft_line_break_or_space(),
group(&options).should_expand(true)
]
)?;
}
Ok(())
}))),
")"
]
);
}

let source = self.source().memoized();
let options = self.options().memoized();

best_fitting![
group(&format_once(|f| {
write!(f, ["(", source])?;
if self.options().is_some() {
write!(f, [",", space(), group(&options).should_expand(true)])?;
}
write!(f, ")")
})),
group(&format_args!(
"(",
&soft_block_indent(&format_once(|f| {
write!(f, [source])?;
if self.options.is_some() {
write!(f, [",", soft_line_break_or_space(), options])?;
}
Ok(())
})),
")"
))
.should_expand(true),
]
.fmt(f)
}
}
1 change: 1 addition & 0 deletions crates/oxc_formatter/src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod decorators;
mod export_declarations;
mod function;
mod import_declaration;
mod import_expression;
mod jsx;
mod member_expression;
mod object_like;
Expand Down
6 changes: 1 addition & 5 deletions tasks/prettier_conformance/snapshots/prettier.js.snap.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
js compatibility: 616/699 (88.13%)
js compatibility: 620/699 (88.70%)

# Failed

| Spec path | Failed or Passed | Match ratio |
| :-------- | :--------------: | :---------: |
| js/arrows/curried.js | 💥💥 | 92.55% |
| js/assignment/sequence.js | 💥 | 71.43% |
| js/class-comment/misc.js | 💥 | 72.73% |
| js/comments/15661.js | 💥💥 | 55.81% |
| js/comments/16398.js | 💥💥 | 80.00% |
| js/comments/blank.js | 💥💥 | 95.24% |
| js/comments/dynamic_imports.js | 💥💥 | 71.43% |
| js/comments/empty-statements.js | 💥💥 | 90.91% |
| js/comments/export.js | 💥💥 | 97.37% |
| js/comments/function-declaration.js | 💥💥 | 92.80% |
Expand Down Expand Up @@ -51,7 +49,6 @@ js compatibility: 616/699 (88.13%)
| js/identifier/parentheses/let.js | 💥💥 | 81.36% |
| js/import-assertions/keyword-detect.js | 💥 | 71.43% |
| js/import-attributes/keyword-detect.js | 💥 | 71.43% |
| js/import-attributes/long-sources.js | 💥 | 75.00% |
| js/label/comment.js | 💥 | 53.33% |
| js/last-argument-expansion/dangling-comment-in-arrow-function.js | 💥 | 22.22% |
| js/logical_expressions/issue-7024.js | 💥 | 66.67% |
Expand Down Expand Up @@ -80,7 +77,6 @@ js compatibility: 616/699 (88.13%)
| js/ternaries/test.js | 💥💥💥💥💥✨✨✨ | 34.24% |
| js/ternaries/parenthesis/await-expression.js | 💥✨ | 14.29% |
| js/test-declarations/angularjs_inject.js | 💥💥 | 91.53% |
| js/trailing-comma/dynamic-import.js | 💥💥💥 | 0.00% |
| jsx/expression-with-types/expression.js | 💥💥💥💥 | 0.00% |
| jsx/fbt/test.js | 💥 | 84.06% |
| jsx/ignore/jsx_ignore.js | 💥 | 82.57% |
Expand Down
Loading