feat(rome_rowan): add `has_comments` API
MODIFY
crates/rome_js_formatter/src/utils/binary_like_expression.rs
crates/rome_js_formatter/src/utils/binary_like_expression.rs
@@ -125,7 +125,7 @@ pub(crate) fn format_binary_like_expression(
let left = parent.left()?;
let formatted = left.format(formatter)?;
- let has_comments = left.syntax().contains_comments();
+ let has_comments = left.syntax().has_comments_direct();
flatten_items.items.push(FlattenItem::regular(
formatted,
@@ -210,7 +210,7 @@ fn format_with_or_without_parenthesis(
};
let result = if operation_is_higher {
- let formatted = if node.contains_comments() {
+ let formatted = if node.has_comments_direct() {
let (leading, content, trailing) = formatted_node.split_trivia();
format_elements![
leading,
@@ -322,7 +322,7 @@ impl FlattenItems {
formatter: &Formatter,
) -> FormatResult<()> {
let right = binary_like_expression.right()?;
- let has_comments = right.syntax().contains_comments();
+ let has_comments = right.syntax().has_comments_direct();
let right_formatted = right.format(formatter)?;
let (formatted_node, _) = format_with_or_without_parenthesis(

MODIFY
crates/rome_js_formatter/src/utils/mod.rs
crates/rome_js_formatter/src/utils/mod.rs
@@ -505,8 +505,12 @@ impl TemplateElement {
fn has_comments(&self) -> bool {
match self {
- TemplateElement::Js(template_element) => template_element.syntax().contains_comments(),
- TemplateElement::Ts(template_element) => template_element.syntax().contains_comments(),
+ TemplateElement::Js(template_element) => {
+ template_element.syntax().has_comments_descendants()
+ }
+ TemplateElement::Ts(template_element) => {
+ template_element.syntax().has_comments_descendants()
+ }
}
}
}

MODIFY
crates/rome_js_formatter/src/utils/simple.rs
crates/rome_js_formatter/src/utils/simple.rs
@@ -83,7 +83,7 @@ pub(crate) fn is_simple_function_expression(func: JsAnyFunction) -> SyntaxResult
}
if let Some(id) = func.id()? {
- if id.syntax().contains_comments() {
+ if id.syntax().has_comments_direct() {
return Ok(false);
}
}

MODIFY
crates/rome_js_parser/src/tests.rs
crates/rome_js_parser/src/tests.rs
@@ -318,7 +318,7 @@ pub fn node_contains_comments() {
let root = parse_module(text, 0);
let syntax = root.syntax();
- assert!(syntax.contains_comments());
+ assert!(syntax.has_comments_descendants());
}
#[test]
@@ -369,3 +369,21 @@ pub fn node_contains_leading_comments() {
assert!(right.syntax().has_leading_comments());
assert!(!right.syntax().has_trailing_comments());
}
+
+#[test]
+pub fn node_has_comments() {
+ let text = r"true &&
+// comment
+(3 - 2 == 0)";
+ let root = parse_module(text, 0);
+ let syntax = root.syntax();
+ let node = syntax
+ .descendants()
+ .find(|n| n.kind() == JsSyntaxKind::JS_LOGICAL_EXPRESSION)
+ .unwrap();
+
+ let logical_expression = JsLogicalExpression::cast(node).unwrap();
+ let right = logical_expression.right().unwrap();
+
+ assert!(right.syntax().has_comments_direct());
+}

MODIFY
crates/rome_rowan/src/syntax/node.rs
crates/rome_rowan/src/syntax/node.rs
@@ -374,12 +374,18 @@ impl<L: Language> SyntaxNode<L> {
SyntaxList::new(self)
}
- /// Whether the node contains any comments.
- pub fn contains_comments(&self) -> bool {
+ /// Whether the node contains any comments. This function checks
+ /// **all the descendants** of the current node.
+ pub fn has_comments_descendants(&self) -> bool {
self.descendants_tokens()
.any(|tok| tok.has_trailing_comments() || tok.has_leading_comments())
}
+ /// It checks if the current node has trailing or leading trivia
+ pub fn has_comments_direct(&self) -> bool {
+ self.has_trailing_comments() || self.has_leading_comments()
+ }
+
/// Whether the node contains trailing comments.
pub fn has_trailing_comments(&self) -> bool {
self.last_token()

feat(rome_rowan): add `has_comments` API (#2464)