Skip to content

Commit b554d8f

Browse files
committed
feat(formatter): complete printing ImportExpression
1 parent d9cf44e commit b554d8f

File tree

4 files changed

+82
-20
lines changed

4 files changed

+82
-20
lines changed

crates/oxc_formatter/src/write/import_declaration.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use oxc_span::GetSpan;
44
use oxc_syntax::identifier::is_identifier_name;
55

66
use crate::{
7-
Format, FormatResult, FormatTrailingCommas, QuoteProperties, TrailingSeparator,
7+
Format, FormatResult, FormatTrailingCommas, QuoteProperties, TrailingSeparator, best_fitting,
8+
format_args,
89
formatter::{
910
Formatter, prelude::*, separated::FormatSeparatedIter, trivia::FormatLeadingComments,
1011
},
@@ -15,20 +16,6 @@ use crate::{
1516

1617
use super::FormatWrite;
1718

18-
impl<'a> FormatWrite<'a> for AstNode<'a, ImportExpression<'a>> {
19-
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
20-
write!(f, ["import"])?;
21-
if let Some(phase) = &self.phase() {
22-
write!(f, [".", phase.as_str()])?;
23-
}
24-
write!(f, ["(", self.source()])?;
25-
if let Some(options) = &self.options() {
26-
write!(f, [",", space(), options])?;
27-
}
28-
write!(f, ")")
29-
}
30-
}
31-
3219
impl<'a> Format<'a> for ImportOrExportKind {
3320
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
3421
if self.is_type() { write!(f, ["type", space()]) } else { Ok(()) }
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use oxc_ast::ast::*;
2+
3+
use crate::{
4+
Format, FormatResult, TrailingSeparator, best_fitting, format_args,
5+
formatter::{Formatter, prelude::*},
6+
generated::ast_nodes::AstNode,
7+
write,
8+
};
9+
10+
use super::FormatWrite;
11+
12+
impl<'a> FormatWrite<'a> for AstNode<'a, ImportExpression<'a>> {
13+
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
14+
write!(f, ["import"])?;
15+
if let Some(phase) = &self.phase() {
16+
write!(f, [".", phase.as_str()])?;
17+
}
18+
19+
// The formatting implementation of `source` and `options` picks from `call_arguments`.
20+
if self.options.is_none()
21+
&& (!matches!(
22+
self.source,
23+
Expression::StringLiteral(_)
24+
| Expression::TemplateLiteral(_)
25+
// Theoretically dynamic import shouldn't have this.
26+
| Expression::TaggedTemplateExpression(_)
27+
) || f.comments().has_comments_before(self.span.end))
28+
{
29+
return write!(
30+
f,
31+
[
32+
"(",
33+
group(&soft_block_indent(&format_once(|f| {
34+
write!(f, [self.source()])?;
35+
if let Some(options) = self.options() {
36+
write!(
37+
f,
38+
[
39+
",",
40+
soft_line_break_or_space(),
41+
group(&options).should_expand(true)
42+
]
43+
)?;
44+
}
45+
Ok(())
46+
}))),
47+
")"
48+
]
49+
);
50+
}
51+
52+
let source = self.source().memoized();
53+
let options = self.options().memoized();
54+
55+
best_fitting![
56+
group(&format_once(|f| {
57+
write!(f, ["(", source])?;
58+
if self.options().is_some() {
59+
write!(f, [",", space(), group(&options).should_expand(true)])?;
60+
}
61+
write!(f, ")")
62+
})),
63+
group(&format_args!(
64+
"(",
65+
&soft_block_indent(&format_once(|f| {
66+
write!(f, [source])?;
67+
if self.options.is_some() {
68+
write!(f, [",", soft_line_break_or_space(), options])?;
69+
}
70+
Ok(())
71+
})),
72+
")"
73+
))
74+
.should_expand(true),
75+
]
76+
.fmt(f)
77+
}
78+
}

crates/oxc_formatter/src/write/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod decorators;
1111
mod export_declarations;
1212
mod function;
1313
mod import_declaration;
14+
mod import_expression;
1415
mod jsx;
1516
mod member_expression;
1617
mod object_like;

tasks/prettier_conformance/snapshots/prettier.js.snap.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
js compatibility: 616/699 (88.13%)
1+
js compatibility: 620/699 (88.70%)
22

33
# Failed
44

55
| Spec path | Failed or Passed | Match ratio |
66
| :-------- | :--------------: | :---------: |
7-
| js/arrows/curried.js | 💥💥 | 92.55% |
87
| js/assignment/sequence.js | 💥 | 71.43% |
98
| js/class-comment/misc.js | 💥 | 72.73% |
109
| js/comments/15661.js | 💥💥 | 55.81% |
1110
| js/comments/16398.js | 💥💥 | 80.00% |
1211
| js/comments/blank.js | 💥💥 | 95.24% |
13-
| js/comments/dynamic_imports.js | 💥💥 | 71.43% |
1412
| js/comments/empty-statements.js | 💥💥 | 90.91% |
1513
| js/comments/export.js | 💥💥 | 97.37% |
1614
| js/comments/function-declaration.js | 💥💥 | 92.80% |
@@ -51,7 +49,6 @@ js compatibility: 616/699 (88.13%)
5149
| js/identifier/parentheses/let.js | 💥💥 | 81.36% |
5250
| js/import-assertions/keyword-detect.js | 💥 | 71.43% |
5351
| js/import-attributes/keyword-detect.js | 💥 | 71.43% |
54-
| js/import-attributes/long-sources.js | 💥 | 75.00% |
5552
| js/label/comment.js | 💥 | 53.33% |
5653
| js/last-argument-expansion/dangling-comment-in-arrow-function.js | 💥 | 22.22% |
5754
| js/logical_expressions/issue-7024.js | 💥 | 66.67% |
@@ -80,7 +77,6 @@ js compatibility: 616/699 (88.13%)
8077
| js/ternaries/test.js | 💥💥💥💥💥✨✨✨ | 34.24% |
8178
| js/ternaries/parenthesis/await-expression.js | 💥✨ | 14.29% |
8279
| js/test-declarations/angularjs_inject.js | 💥💥 | 91.53% |
83-
| js/trailing-comma/dynamic-import.js | 💥💥💥 | 0.00% |
8480
| jsx/expression-with-types/expression.js | 💥💥💥💥 | 0.00% |
8581
| jsx/fbt/test.js | 💥 | 84.06% |
8682
| jsx/ignore/jsx_ignore.js | 💥 | 82.57% |

0 commit comments

Comments
 (0)