From 65f9bcc4eaea0ff16fca6b61c97eead536175dba Mon Sep 17 00:00:00 2001 From: Jonathan Tran Date: Fri, 21 Mar 2025 16:30:10 -0400 Subject: [PATCH] Fix so that only comments doesn't format to empty --- rust/kcl-lib/src/parsing/mod.rs | 7 ++----- rust/kcl-lib/src/unparser.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/rust/kcl-lib/src/parsing/mod.rs b/rust/kcl-lib/src/parsing/mod.rs index 00e422ba6..d0a81f5cf 100644 --- a/rust/kcl-lib/src/parsing/mod.rs +++ b/rust/kcl-lib/src/parsing/mod.rs @@ -60,11 +60,8 @@ pub fn parse_tokens(mut tokens: TokenStream) -> ParseResult { return Node::::default().into(); } - // Check all the tokens are whitespace or comments. - if tokens - .iter() - .all(|t| t.token_type.is_whitespace() || t.token_type.is_comment()) - { + // Check all the tokens are whitespace. + if tokens.iter().all(|t| t.token_type.is_whitespace()) { return Node::::default().into(); } diff --git a/rust/kcl-lib/src/unparser.rs b/rust/kcl-lib/src/unparser.rs index 11918e20f..e883f3f70 100644 --- a/rust/kcl-lib/src/unparser.rs +++ b/rust/kcl-lib/src/unparser.rs @@ -2039,6 +2039,15 @@ thing = 'foo' ); } + #[test] + fn test_recast_only_line_comments() { + let code = r#"// comment at start +"#; + let program = crate::parsing::top_level_parse(code).unwrap(); + + assert_eq!(program.recast(&Default::default(), 0), code); + } + #[test] fn test_recast_comment_at_start() { let test_program = r#"