Repository: seaofvoices/darklua
Pull request #167 (2023-12-16 19:22:24)

Fix `append_text_comment` rule to support multiline comments


- [x] add entry to the changelog


github-actions[bot]: Coverage after merging <b>fix-append-multiline-comment</b> into <b>main</b> will be<table><tbody><tr><th>87.07%</th></tr></tbody></table>

<details><summary>Coverage Report for Changed Files</summary><table><tbody><tr><th>File</th><th>Stmts</th><th>Branches</th><th>Funcs</th><th>Lines</th><th>Uncovered Lines</th></tr><tr><td colspan='6'><b>src/rules</b></td></tr><tr><td>&nbsp; &nbsp;<a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs'>append_text_comment.rs</a></td><td><b>51.58%</b></td><td>100%</td><td><b>62.86%</b></td><td><b>50.68%</b></td><td><a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L119-L126'>119&ndash;126</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L142-L155'>142&ndash;155</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L161-L172'>161&ndash;172</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L178-L187'>178&ndash;187</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L193-L202'>193&ndash;202</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L208-L224'>208&ndash;224</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L230-L241'>230&ndash;241</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L247-L254'>247&ndash;254</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L260-L268'>260&ndash;268</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L283-L301'>283&ndash;301</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L322-L329'>322&ndash;329</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L332-L334'>332&ndash;334</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L336-L338'>336&ndash;338</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L348-L349'>348&ndash;349</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L351-L352'>351&ndash;352</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L361-L363'>361&ndash;363</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L367-L369'>367&ndash;369</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L37'>37</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L371-L372'>371&ndash;372</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L38'>38</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L380-L381'>380&ndash;381</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L386-L389'>386&ndash;389</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L39'>39</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L390-L394'>390&ndash;394</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L40'>40</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L407-L408'>407&ndash;408</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L41'>41</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L414-L419'>414&ndash;419</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L42'>42</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L420-L421'>420&ndash;421</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L43'>43</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L451-L456'>451&ndash;456</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L463'>463</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L476'>476</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L484-L499'>484&ndash;499</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L506-L508'>506&ndash;508</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L54'>54</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L56-L59'>56&ndash;59</a>, <a href='https://github.com/seaofvoices/darklua/blob/a78fc65dcf43f41c608fca918b5ced1a89c1acaf/src/rules/append_text_comment.rs#L73-L74'>73&ndash;74</a></td></tr></tbody></table></details>

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4f67e7a..fd2021a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,8 @@
 
 ## Unreleased
 
+* fix `append_text_comment` rule to support multiline comments ([#167](https://github.com/seaofvoices/darklua/pull/167))
+
 ## 0.12.0
 
 * fix relative parent paths when working with sourcemaps ([#164](https://github.com/seaofvoices/darklua/pull/164))
diff --git a/src/rules/append_text_comment.rs b/src/rules/append_text_comment.rs
index 44ec0e2..6e48439 100644
--- a/src/rules/append_text_comment.rs
+++ b/src/rules/append_text_comment.rs
@@ -75,8 +75,9 @@ impl AppendTextComment {
                         };
 
                         format!(
-                            "--[{}[\ncontent\n{}",
+                            "--[{}[\n{}\n{}",
                             "=".repeat(equal_count),
+                            content,
                             close_comment
                         )
                     } else {


Test patch:
diff --git a/tests/rule_tests/append_text_comment.rs b/tests/rule_tests/append_text_comment.rs
index 6a1fc4a..1b6ae23 100644
--- a/tests/rule_tests/append_text_comment.rs
+++ b/tests/rule_tests/append_text_comment.rs
@@ -6,7 +6,7 @@ test_rule_with_tokens!(
         rule: 'append_text_comment',
         text: 'hello',
     }"#).unwrap(),
-    empty_function("do end") => "-- hello\ndo end",
+    empty_do("do end") => "-- hello\ndo end",
     local_assign("local a") => "-- hello\nlocal a",
     local_assign_with_value("local var = true") => "-- hello\nlocal var = true",
     assign_variable("var = true") => "-- hello\nvar = true",
@@ -29,6 +29,15 @@ test_rule_with_tokens!(
     return_one_value_statement("return 1") => "-- hello\nreturn 1",
 );
 
+test_rule_with_tokens!(
+    append_text_comment_multiline,
+    json5::from_str::<Box<dyn Rule>>(r#"{
+        rule: 'append_text_comment',
+        text: '1\n2',
+    }"#).unwrap(),
+    empty_do("do end") => "--[[\n1\n2\n]]\ndo end",
+);
+
 test_rule_without_effects!(
     json5::from_str::<Box<dyn Rule>>(
         r#"{
diff --git a/tests/rule_tests/mod.rs b/tests/rule_tests/mod.rs
index b868734..bd12b00 100644
--- a/tests/rule_tests/mod.rs
+++ b/tests/rule_tests/mod.rs
@@ -6,7 +6,7 @@ macro_rules! test_rule_with_generator {
         $resources:expr,
         $generator:expr,
         $parser:expr,
-        $parsing_tokens:expr,
+        $compare_with_tokens:expr,
         $test_file_name:literal,
         $name:ident,
         $input:literal,
@@ -18,7 +18,14 @@ macro_rules! test_rule_with_generator {
 
             // $crate::utils::setup_logger(log::LevelFilter::Trace);
 
-            let expect_block = $crate::utils::parse_input($output);
+            let expect_block = if $compare_with_tokens {
+                darklua_core::Parser::default()
+                    .preserve_tokens()
+                    .parse($output)
+                    .expect("unable to parse expected code")
+            } else {
+                $crate::utils::parse_input($output)
+            };
 
             let parser = $parser;
             let mut block = parser.parse($input).unwrap_or_else(|error| {
@@ -41,17 +48,17 @@ macro_rules! test_rule_with_generator {
             generator.write_block(&block);
             let lua_code = generator.into_string();
 
-            pretty_assertions::assert_eq!(
-                if $parsing_tokens {
-                    $crate::utils::parse_input(&lua_code)
-                } else {
-                    block
-                },
-                expect_block,
-                "\nexpected code:\n{}\nbut received:\n{}",
-                $output,
-                lua_code
-            );
+            if $compare_with_tokens {
+                pretty_assertions::assert_eq!($output, lua_code,);
+            } else {
+                pretty_assertions::assert_eq!(
+                    $crate::utils::parse_input(&lua_code),
+                    expect_block,
+                    "\nexpected code:\n{}\nbut received:\n{}",
+                    $output,
+                    lua_code
+                );
+            }
         }
     };
     ($rule:expr, $resources:expr, $generator:expr, $test_file_name:literal, $name:ident, $input:literal, $output:literal) => {
@@ -195,7 +202,7 @@ macro_rules! test_rule {
                 $resources,
                 |input| darklua_core::generator::TokenBasedLuaGenerator::new(input),
                 darklua_core::Parser::default().preserve_tokens(),
-                true,
+                false,
                 $test_file_name,
                 $name,
                 $input,