Skip to content

Commit

Permalink
feat(rome_rowan): add has_comments API (rome#2464)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Apr 20, 2022
1 parent 10caea4 commit 7358ffa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
6 changes: 3 additions & 3 deletions crates/rome_js_formatter/src/utils/binary_like_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions crates/rome_js_formatter/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_formatter/src/utils/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
20 changes: 19 additions & 1 deletion crates/rome_js_parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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());
}
10 changes: 8 additions & 2 deletions crates/rome_rowan/src/syntax/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7358ffa

Please sign in to comment.