feat(rome_formatter): initial formatting of module
MODIFY
crates/rome_formatter/src/cst.rs
crates/rome_formatter/src/cst.rs
@@ -5,7 +5,7 @@ use rslint_parser::ast::{
JsClassStatement, JsConstructorParameters, JsContinueStatement, JsDebuggerStatement,
JsDefaultClause, JsDoWhileStatement, JsEmptyStatement, JsExpressionStatement, JsFinallyClause,
JsForInStatement, JsForStatement, JsFunctionStatement, JsGetterClassMember,
- JsIdentifierBinding, JsIdentifierExpression, JsIfStatement, JsLabeledStatement,
+ JsIdentifierBinding, JsIdentifierExpression, JsIfStatement, JsLabeledStatement, JsModule,
JsNullLiteralExpression, JsNumberLiteralExpression, JsObjectExpression, JsParameters,
JsPropertyClassMember, JsPropertyObjectMember, JsReturnStatement, JsScript,
JsSequenceExpression, JsSetterClassMember, JsShorthandPropertyObjectMember, JsSpread,
@@ -57,6 +57,9 @@ impl ToFormatElement for SyntaxNode {
JsSyntaxKind::JS_SCRIPT => JsScript::cast(self.clone())
.unwrap()
.to_format_element(formatter),
+ JsSyntaxKind::JS_MODULE => JsModule::cast(self.clone())
+ .unwrap()
+ .to_format_element(formatter),
JsSyntaxKind::JS_SPREAD => JsSpread::cast(self.clone())
.unwrap()
.to_format_element(formatter),

MODIFY
crates/rome_formatter/src/ts/mod.rs
crates/rome_formatter/src/ts/mod.rs
@@ -10,7 +10,7 @@ mod ident;
mod import;
mod object_members;
mod parameter_list;
-mod script;
+mod root;
mod statements;
mod template;
mod unknown;

ADD
crates/rome_formatter/src/ts/root/any_module_item.rs
+use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
+use rslint_parser::ast::JsAnyModuleItem;
+
+impl ToFormatElement for JsAnyModuleItem {
+ fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
+ match self {
+ JsAnyModuleItem::JsAnyStatement(any_statement) => {
+ any_statement.to_format_element(formatter)
+ }
+ JsAnyModuleItem::JsExport(_) => todo!(),
+ JsAnyModuleItem::JsImport(_) => todo!(),
+ }
+ }
+}

ADD
crates/rome_formatter/src/ts/root/mod.rs
+use crate::{hard_line_break, join_elements, FormatElement, Formatter};
+use rslint_parser::ast::JsModuleItemList;
+use rslint_parser::AstNode;
+use rslint_parser::AstNodeList;
+
+mod any_module_item;
+mod module;
+mod script;
+
+pub fn format_module_list(list: JsModuleItemList, formatter: &Formatter) -> FormatElement {
+ join_elements(
+ hard_line_break(),
+ list.iter().map(|module_item| {
+ let snapshot = formatter.snapshot();
+ match formatter.format_node(module_item.clone()) {
+ Ok(result) => result,
+ Err(_) => {
+ formatter.restore(snapshot);
+ formatter
+ .format_verbatim(module_item.syntax())
+ .trim_start()
+ .trim_end()
+ }
+ }
+ }),
+ )
+}

ADD
crates/rome_formatter/src/ts/root/module.rs
+use crate::ts::directives::format_directives;
+use crate::ts::root::format_module_list;
+use crate::{
+ format_elements, hard_line_break, FormatElement, FormatResult, Formatter, ToFormatElement,
+};
+use rslint_parser::ast::JsModule;
+use rslint_parser::AstNodeList;
+
+impl ToFormatElement for JsModule {
+ fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
+ let mut elements = vec![];
+
+ if let Some(interpreter) = self.interpreter_token() {
+ elements.push(formatter.format_token(&interpreter)?);
+ elements.push(hard_line_break());
+ }
+
+ let directives = self.directives();
+ if directives.len() > 0 {
+ elements.push(format_directives(directives, formatter));
+ elements.push(hard_line_break());
+ }
+
+ elements.push(format_module_list(self.items(), formatter));
+
+ elements.push(formatter.format_token(&self.eof_token()?)?);
+
+ Ok(format_elements![
+ concat_elements(elements),
+ hard_line_break()
+ ])
+ }
+}

RENAME
crates/rome_formatter/src/ts/script.rs
crates/rome_formatter/src/ts/root/script.rs
feat(rome_formatter): initial formatting of module (#1987)