@@ -77,8 +77,74 @@ impl PreProcessor for Ruby {
7777
7878 // Ruby extraction
7979 while cursor. pos < len {
80- // Looking for `%w` or `%W`
81- if cursor. curr != b'%' && !matches ! ( cursor. next, b'w' | b'W' ) {
80+ match cursor. curr {
81+ b'"' => {
82+ cursor. advance ( ) ;
83+
84+ while cursor. pos < len {
85+ match cursor. curr {
86+ // Escaped character, skip ahead to the next character
87+ b'\\' => cursor. advance_twice ( ) ,
88+
89+ // End of the string
90+ b'"' => break ,
91+
92+ // Everything else is valid
93+ _ => cursor. advance ( ) ,
94+ } ;
95+ }
96+
97+ cursor. advance ( ) ;
98+ continue ;
99+ }
100+
101+ b'\'' => {
102+ cursor. advance ( ) ;
103+
104+ while cursor. pos < len {
105+ match cursor. curr {
106+ // Escaped character, skip ahead to the next character
107+ b'\\' => cursor. advance_twice ( ) ,
108+
109+ // End of the string
110+ b'\'' => break ,
111+
112+ // Everything else is valid
113+ _ => cursor. advance ( ) ,
114+ } ;
115+ }
116+
117+ cursor. advance ( ) ;
118+ continue ;
119+ }
120+
121+ // Replace comments in Ruby files
122+ b'#' => {
123+ result[ cursor. pos ] = b' ' ;
124+ cursor. advance ( ) ;
125+
126+ while cursor. pos < len {
127+ match cursor. curr {
128+ // End of the comment
129+ b'\n' => break ,
130+
131+ // Everything else is part of the comment and replaced
132+ _ => {
133+ result[ cursor. pos ] = b' ' ;
134+ cursor. advance ( ) ;
135+ }
136+ } ;
137+ }
138+
139+ cursor. advance ( ) ;
140+ continue ;
141+ }
142+
143+ _ => { }
144+ }
145+
146+ // Looking for `%w`, `%W`, or `%p`
147+ if cursor. curr != b'%' || !matches ! ( cursor. next, b'w' | b'W' | b'p' ) {
82148 cursor. advance ( ) ;
83149 continue ;
84150 }
@@ -90,6 +156,8 @@ impl PreProcessor for Ruby {
90156 b'[' => b']' ,
91157 b'(' => b')' ,
92158 b'{' => b'}' ,
159+ b'#' => b'#' ,
160+ b' ' => b'\n' ,
93161 _ => {
94162 cursor. advance ( ) ;
95163 continue ;
@@ -131,7 +199,10 @@ impl PreProcessor for Ruby {
131199
132200 // End of the pattern, replace the boundary character with a space
133201 _ if cursor. curr == boundary => {
134- result[ cursor. pos ] = b' ' ;
202+ if boundary != b'\n' {
203+ result[ cursor. pos ] = b' ' ;
204+ }
205+
135206 break ;
136207 }
137208
@@ -173,12 +244,51 @@ mod tests {
173244 "%w(flex data-[state=pending]:bg-(--my-color) flex-col)" ,
174245 "%w flex data-[state=pending]:bg-(--my-color) flex-col " ,
175246 ) ,
247+
248+ // %w …\n
249+ ( "%w flex px-2.5\n " , "%w flex px-2.5\n " ) ,
250+
176251 // Use backslash to embed spaces in the strings.
177252 ( r#"%w[foo\ bar baz\ bat]"# , r#"%w foo bar baz bat "# ) ,
178253 ( r#"%W[foo\ bar baz\ bat]"# , r#"%W foo bar baz bat "# ) ,
254+
179255 // The nested delimiters evaluated to a flat array of strings
180256 // (not nested array).
181257 ( r#"%w[foo[bar baz]qux]"# , r#"%w foo[bar baz]qux "# ) ,
258+
259+ (
260+ "# test\n # test\n # {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!]\n %w[flex px-2.5]" ,
261+ " \n \n \n %w flex px-2.5 "
262+ ) ,
263+
264+ ( r#""foo # bar""# , r#""foo # bar""# ) ,
265+ ( r#"'foo # bar'"# , r#"'foo # bar'"# ) ,
266+ (
267+ r#"def call = tag.span "Foo", class: %w[rounded-full h-0.75 w-0.75]"# ,
268+ r#"def call = tag.span "Foo", class: %w rounded-full h-0.75 w-0.75 "#
269+ ) ,
270+
271+ ( r#"%w[foo ' bar]"# , r#"%w foo ' bar "# ) ,
272+ ( r#"%w[foo " bar]"# , r#"%w foo " bar "# ) ,
273+ ( r#"%W[foo ' bar]"# , r#"%W foo ' bar "# ) ,
274+ ( r#"%W[foo " bar]"# , r#"%W foo " bar "# ) ,
275+
276+ ( r#"%p foo ' bar "# , r#"%p foo ' bar "# ) ,
277+ ( r#"%p foo " bar "# , r#"%p foo " bar "# ) ,
278+
279+ (
280+ "%p has a ' quote\n # this should be removed\n %p has a ' quote" ,
281+ "%p has a ' quote\n \n %p has a ' quote"
282+ ) ,
283+ (
284+ "%p has a \" quote\n # this should be removed\n %p has a \" quote" ,
285+ "%p has a \" quote\n \n %p has a \" quote"
286+ ) ,
287+
288+ (
289+ "%w#this text is kept# # this text is not" ,
290+ "%w this text is kept " ,
291+ ) ,
182292 ] {
183293 Ruby :: test ( input, expected) ;
184294 }
@@ -211,6 +321,16 @@ mod tests {
211321 "%w(flex data-[state=pending]:bg-(--my-color) flex-col)" ,
212322 vec ! [ "flex" , "data-[state=pending]:bg-(--my-color)" , "flex-col" ] ,
213323 ) ,
324+
325+ (
326+ "# test\n # test\n # {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!]\n %w[flex px-2.5]" ,
327+ vec ! [ "flex" , "px-2.5" ] ,
328+ ) ,
329+
330+ ( r#""foo # bar""# , vec ! [ "foo" , "bar" ] ) ,
331+ ( r#"'foo # bar'"# , vec ! [ "foo" , "bar" ] ) ,
332+
333+ ( r#"%w[foo ' bar]"# , vec ! [ "foo" , "bar" ] ) ,
214334 ] {
215335 Ruby :: test_extract_contains ( input, expected) ;
216336 }
0 commit comments