|
| 1 | +use crate::cursor; |
| 2 | +use crate::extractor::pre_processors::pre_processor::PreProcessor; |
| 3 | +use bstr::ByteSlice; |
| 4 | + |
| 5 | +#[derive(Debug, Default)] |
| 6 | +pub struct Clojure; |
| 7 | + |
| 8 | +impl PreProcessor for Clojure { |
| 9 | + fn process(&self, content: &[u8]) -> Vec<u8> { |
| 10 | + let content = content |
| 11 | + .replace(":class", " ") |
| 12 | + .replace(":className", " "); |
| 13 | + let len = content.len(); |
| 14 | + let mut result = content.to_vec(); |
| 15 | + let mut cursor = cursor::Cursor::new(&content); |
| 16 | + |
| 17 | + while cursor.pos < len { |
| 18 | + match cursor.curr { |
| 19 | + // Consume strings as-is |
| 20 | + b'"' => { |
| 21 | + cursor.advance(); |
| 22 | + |
| 23 | + while cursor.pos < len { |
| 24 | + match cursor.curr { |
| 25 | + // Escaped character, skip ahead to the next character |
| 26 | + b'\\' => cursor.advance_twice(), |
| 27 | + |
| 28 | + // End of the string |
| 29 | + b'"' => break, |
| 30 | + |
| 31 | + // Everything else is valid |
| 32 | + _ => cursor.advance(), |
| 33 | + }; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // Consume comments as-is until the end of the line. |
| 38 | + // Comments start with `;;` |
| 39 | + b';' if matches!(cursor.next, b';') => { |
| 40 | + while cursor.pos < len && cursor.curr != b'\n' { |
| 41 | + cursor.advance(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + b':' | b'.' => { |
| 46 | + result[cursor.pos] = b' '; |
| 47 | + } |
| 48 | + |
| 49 | + // Consume everything else |
| 50 | + _ => {} |
| 51 | + }; |
| 52 | + |
| 53 | + cursor.advance(); |
| 54 | + } |
| 55 | + |
| 56 | + result |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(test)] |
| 61 | +mod tests { |
| 62 | + use super::Clojure; |
| 63 | + use crate::extractor::pre_processors::pre_processor::PreProcessor; |
| 64 | + |
| 65 | + #[test] |
| 66 | + fn test_clojure_pre_processor() { |
| 67 | + for (input, expected) in [ |
| 68 | + (":div.flex-1.flex-2", " div flex-1 flex-2"), |
| 69 | + ( |
| 70 | + ":.flex-3.flex-4 ;defaults to div", |
| 71 | + " flex-3 flex-4 ;defaults to div", |
| 72 | + ), |
| 73 | + ("{:class :flex-5.flex-6", "{ flex-5 flex-6"), |
| 74 | + (r#"{:class "flex-7 flex-8"}"#, r#"{ "flex-7 flex-8"}"#), |
| 75 | + ( |
| 76 | + r#"{:class ["flex-9" :flex-10]}"#, |
| 77 | + r#"{ ["flex-9" flex-10]}"#, |
| 78 | + ), |
| 79 | + ( |
| 80 | + r#"(dom/div {:class "flex-11 flex-12"})"#, |
| 81 | + r#"(dom/div { "flex-11 flex-12"})"#, |
| 82 | + ), |
| 83 | + ("(dom/div :.flex-13.flex-14", "(dom/div flex-13 flex-14"), |
| 84 | + ] { |
| 85 | + Clojure::test(input, expected); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn test_extract_candidates() { |
| 91 | + // https://github.com/luckasRanarison/tailwind-tools.nvim/issues/68#issuecomment-2660951258 |
| 92 | + let input = r#" |
| 93 | + :div.c1.c2 |
| 94 | + :.c3.c4 ;defaults to div |
| 95 | + {:class :c5.c6 |
| 96 | + {:class "c7 c8"} |
| 97 | + {:class ["c9" :c10]} |
| 98 | + (dom/div {:class "c11 c12"}) |
| 99 | + (dom/div :.c13.c14 |
| 100 | + {:className :c15.c16 |
| 101 | + {:className "c17 c18"} |
| 102 | + {:className ["c19" :c20]} |
| 103 | + (dom/div {:className "c21 c22"}) |
| 104 | + "#; |
| 105 | + |
| 106 | + Clojure::test_extract_contains( |
| 107 | + input, |
| 108 | + vec![ |
| 109 | + "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11", "c12", "c13", |
| 110 | + "c14", "c15", "c16", "c17", "c18", "c19", "c20", "c21", "c22", |
| 111 | + ], |
| 112 | + ); |
| 113 | + |
| 114 | + // Similar structure but using real classes |
| 115 | + let input = r#" |
| 116 | + :div.flex-1.flex-2 |
| 117 | + :.flex-3.flex-4 ;defaults to div |
| 118 | + {:class :flex-5.flex-6 |
| 119 | + {:class "flex-7 flex-8"} |
| 120 | + {:class ["flex-9" :flex-10]} |
| 121 | + (dom/div {:class "flex-11 flex-12"}) |
| 122 | + (dom/div :.flex-13.flex-14 |
| 123 | + {:className :flex-15.flex-16 |
| 124 | + {:className "flex-17 flex-18"} |
| 125 | + {:className ["flex-19" :flex-20]} |
| 126 | + (dom/div {:className "flex-21 flex-22"}) |
| 127 | + "#; |
| 128 | + |
| 129 | + Clojure::test_extract_contains( |
| 130 | + input, |
| 131 | + vec![ |
| 132 | + "flex-1", "flex-2", "flex-3", "flex-4", "flex-5", "flex-6", "flex-7", "flex-8", |
| 133 | + "flex-9", "flex-10", "flex-11", "flex-12", "flex-13", "flex-14", "flex-15", |
| 134 | + "flex-16", "flex-17", "flex-18", "flex-19", "flex-20", "flex-21", "flex-22", |
| 135 | + ], |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_special_characters_are_valid_in_strings() { |
| 141 | + // In this case the `:` and `.` should not be replaced by ` ` because they are inside a |
| 142 | + // string. |
| 143 | + let input = r#" |
| 144 | + (dom/div {:class "hover:flex px-1.5"}) |
| 145 | + "#; |
| 146 | + |
| 147 | + Clojure::test_extract_contains(input, vec!["hover:flex", "px-1.5"]); |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn test_ignore_comments_with_invalid_strings() { |
| 152 | + let input = r#" |
| 153 | + ;; This is an unclosed string: " |
| 154 | + (dom/div {:class "hover:flex px-1.5"}) |
| 155 | + "#; |
| 156 | + |
| 157 | + Clojure::test_extract_contains(input, vec!["hover:flex", "px-1.5"]); |
| 158 | + } |
| 159 | +} |
0 commit comments