From 5fd2ac85ab97e22b2e68a60223434f93423f1b6b Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Tue, 30 Aug 2022 11:24:03 +0200 Subject: [PATCH] Add tests for `is_doc_comment` --- crates/rome_js_formatter/src/comments.rs | 46 ++++++++++++++++-------- crates/rome_js_formatter/src/lib.rs | 2 +- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/crates/rome_js_formatter/src/comments.rs b/crates/rome_js_formatter/src/comments.rs index 03fe147c8ac..711350197d5 100644 --- a/crates/rome_js_formatter/src/comments.rs +++ b/crates/rome_js_formatter/src/comments.rs @@ -56,30 +56,48 @@ impl FormatRule> for FormatJsLeadingComment { /// /// # Examples /// -/// ## Doc Comments +/// ``` +/// # use rome_js_parser::parse_module; +/// # use rome_js_syntax::JsLanguage; +/// # use rome_rowan::{Direction, SyntaxTriviaPieceComments}; +/// use rome_js_formatter::comments::is_doc_comment; /// -/// ```javascript -/// /** -/// * Multiline doc comment -/// */ +/// # fn parse_comment(source: &str) -> SyntaxTriviaPieceComments { +/// # let root = parse_module(source, 0).tree(); +/// # root +/// # .eof_token() +/// # .expect("Root to have an EOF token") +/// # .leading_trivia() +/// # .pieces() +/// # .filter_map(|piece| piece.as_comments()) +/// # .next() +/// # .expect("Source to contain a comment.") +/// # } /// -/// /* -/// * With single star -/// */ -/// ``` +/// assert!(is_doc_comment(&parse_comment(r#" +/// /** +/// * Multiline doc comment +/// */ +/// "#))); /// -/// ## Non Doc Comments +/// assert!(is_doc_comment(&parse_comment(r#" +/// /* +/// * Single star +/// */ +/// "#))); /// -/// ```javascript -/// /** has no line break */ /// +/// // Non doc-comments +/// assert!(!is_doc_comment(&parse_comment(r#"/** has no line break */"#))); +/// +/// assert!(!is_doc_comment(&parse_comment(r#" /// /* /// * /// this line doesn't start with a star /// */ +/// "#))); /// ``` -/// -fn is_doc_comment(comment: &SyntaxTriviaPieceComments) -> bool { +pub fn is_doc_comment(comment: &SyntaxTriviaPieceComments) -> bool { if !comment.has_newline() { return false; } diff --git a/crates/rome_js_formatter/src/lib.rs b/crates/rome_js_formatter/src/lib.rs index c0e287ea682..f411a369032 100644 --- a/crates/rome_js_formatter/src/lib.rs +++ b/crates/rome_js_formatter/src/lib.rs @@ -253,7 +253,7 @@ mod check_reformat; #[rustfmt::skip] mod generated; pub(crate) mod builders; -mod comments; +pub mod comments; pub mod context; mod parentheses; pub(crate) mod separated;