This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rome_formatter): format import syntax (#1992)
* feat(rome_formatter): initial formatting of module * chore: reduce bloat * feat: format import syntax * chore: extract logic to a new API * test: bare import tests * feat: default import syntax * feat: named import formatting * feat: namespace import * feat: correctly retain line breaks * chore: add formatting to CST * chore: clean code * chore: rebase and update test * code review * rebase * clippy fixes * rebase and fix issues * code review * code review * rebase * remove old code
- Loading branch information
Showing
42 changed files
with
730 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
crates/rome_formatter/src/ts/import/any_import_assertion_entry.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement}; | ||
use rslint_parser::ast::JsAnyImportAssertionEntry; | ||
|
||
impl ToFormatElement for JsAnyImportAssertionEntry { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
match self { | ||
JsAnyImportAssertionEntry::JsImportAssertionEntry(assertion_entry) => { | ||
assertion_entry.to_format_element(formatter) | ||
} | ||
JsAnyImportAssertionEntry::JsUnknownImportAssertionEntry(unknown_assertion_entry) => { | ||
unknown_assertion_entry.to_format_element(formatter) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement}; | ||
use rslint_parser::ast::AnyJsImportClause; | ||
|
||
impl ToFormatElement for AnyJsImportClause { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
match self { | ||
AnyJsImportClause::JsImportBareClause(bare_clause) => { | ||
bare_clause.to_format_element(formatter) | ||
} | ||
AnyJsImportClause::JsImportDefaultClause(e) => e.to_format_element(formatter), | ||
AnyJsImportClause::JsImportNamedClause(named_clause) => { | ||
named_clause.to_format_element(formatter) | ||
} | ||
AnyJsImportClause::JsImportNamespaceClause(e) => e.to_format_element(formatter), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement}; | ||
use rslint_parser::ast::JsAnyNamedImport; | ||
|
||
impl ToFormatElement for JsAnyNamedImport { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
match self { | ||
JsAnyNamedImport::JsNamedImportSpecifiers(e) => e.to_format_element(formatter), | ||
JsAnyNamedImport::JsNamespaceImportSpecifier(e) => e.to_format_element(formatter), | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
crates/rome_formatter/src/ts/import/any_named_import_specifier.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement}; | ||
use rslint_parser::ast::JsAnyNamedImportSpecifier; | ||
|
||
impl ToFormatElement for JsAnyNamedImportSpecifier { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
match self { | ||
JsAnyNamedImportSpecifier::JsNamedImportSpecifier(e) => e.to_format_element(formatter), | ||
JsAnyNamedImportSpecifier::JsShorthandNamedImportSpecifier(e) => { | ||
e.to_format_element(formatter) | ||
} | ||
JsAnyNamedImportSpecifier::JsUnknownNamedImportSpecifier(e) => { | ||
e.to_format_element(formatter) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::{ | ||
empty_element, format_elements, group_elements, if_group_fits_on_single_line, join_elements, | ||
soft_indent, soft_line_break_or_space, space_token, token, FormatElement, FormatResult, | ||
Formatter, ToFormatElement, | ||
}; | ||
use rslint_parser::ast::JsImportAssertion; | ||
use rslint_parser::AstSeparatedList; | ||
|
||
impl ToFormatElement for JsImportAssertion { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let assert_token = formatter.format_token(&self.assert_token()?)?; | ||
let assertions = self.assertions(); | ||
|
||
let space = if assertions.is_empty() { | ||
empty_element() | ||
} else { | ||
if_group_fits_on_single_line(space_token()) | ||
}; | ||
|
||
let result = group_elements(formatter.format_delimited( | ||
&self.l_curly_token()?, | ||
|leading, trailing| { | ||
Ok(format_elements!( | ||
space.clone(), | ||
soft_indent(format_elements![ | ||
leading, | ||
join_elements( | ||
soft_line_break_or_space(), | ||
formatter.format_separated(assertions, || token(","))? | ||
), | ||
trailing | ||
]), | ||
space, | ||
)) | ||
}, | ||
&self.r_curly_token()?, | ||
)?); | ||
|
||
Ok(format_elements![assert_token, space_token(), result]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use crate::{ | ||
format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement, | ||
}; | ||
use rslint_parser::ast::JsImportAssertionEntry; | ||
|
||
impl ToFormatElement for JsImportAssertionEntry { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
Ok(format_elements![ | ||
formatter.format_token(&self.key()?)?, | ||
formatter.format_token(&self.colon_token()?)?, | ||
space_token(), | ||
formatter.format_token(&self.value_token()?)?, | ||
]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::{ | ||
empty_element, format_elements, space_token, FormatElement, FormatResult, Formatter, | ||
ToFormatElement, | ||
}; | ||
use rslint_parser::ast::JsImportBareClause; | ||
|
||
impl ToFormatElement for JsImportBareClause { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let source = formatter.format_node(&self.source()?)?; | ||
let assertion = if let Some(assertion) = self.assertion() { | ||
format_elements![space_token(), formatter.format_node(&assertion)?] | ||
} else { | ||
empty_element() | ||
}; | ||
|
||
Ok(format_elements![source, assertion]) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
crates/rome_formatter/src/ts/import/default_import_specifier.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use crate::{format_elements, FormatElement, FormatResult, Formatter, ToFormatElement}; | ||
use rslint_parser::ast::JsDefaultImportSpecifier; | ||
|
||
impl ToFormatElement for JsDefaultImportSpecifier { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
Ok(format_elements![ | ||
formatter.format_node(&self.local_name()?)?, | ||
formatter.format_token(&self.trailing_comma_token()?)? | ||
]) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
crates/rome_formatter/src/ts/import/import_call_expression.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use crate::{format_elements, FormatElement, FormatResult, Formatter, ToFormatElement}; | ||
use rslint_parser::ast::JsImportCallExpression; | ||
|
||
impl ToFormatElement for JsImportCallExpression { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
Ok(format_elements![ | ||
formatter.format_token(&self.import_token()?)?, | ||
formatter.format_node(&self.arguments()?)?, | ||
]) | ||
} | ||
} |
Oops, something went wrong.