-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Skip comments in Ruby files when checking for class names #19243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c29506b
fa2be75
128775d
1ed473a
41d3f41
bff4739
ef3a8cc
c19d255
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,8 +77,74 @@ impl PreProcessor for Ruby { | |
|
|
||
| // Ruby extraction | ||
| while cursor.pos < len { | ||
| // Looking for `%w` or `%W` | ||
| if cursor.curr != b'%' && !matches!(cursor.next, b'w' | b'W') { | ||
| match cursor.curr { | ||
| b'"' => { | ||
| cursor.advance(); | ||
|
|
||
| while cursor.pos < len { | ||
| match cursor.curr { | ||
| // Escaped character, skip ahead to the next character | ||
| b'\\' => cursor.advance_twice(), | ||
|
|
||
| // End of the string | ||
| b'"' => break, | ||
|
|
||
| // Everything else is valid | ||
| _ => cursor.advance(), | ||
| }; | ||
| } | ||
|
|
||
| cursor.advance(); | ||
| continue; | ||
| } | ||
|
|
||
| b'\'' => { | ||
| cursor.advance(); | ||
|
|
||
| while cursor.pos < len { | ||
| match cursor.curr { | ||
| // Escaped character, skip ahead to the next character | ||
| b'\\' => cursor.advance_twice(), | ||
|
|
||
| // End of the string | ||
| b'\'' => break, | ||
|
|
||
| // Everything else is valid | ||
| _ => cursor.advance(), | ||
| }; | ||
| } | ||
|
|
||
| cursor.advance(); | ||
| continue; | ||
| } | ||
|
Comment on lines
+81
to
+119
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add some tests where we handle quotes? We have to be careful with these because it could be that you start a sentence with a %p a quote here can't exist, or can it
-#
This won't be displayed (there is a quote here as well)
Nor will this
Nor will this.
%p a quote here can't exist, or can it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added tests and updated the implementation a bit |
||
|
|
||
| // Replace comments in Ruby files | ||
| b'#' => { | ||
| result[cursor.pos] = b' '; | ||
| cursor.advance(); | ||
|
|
||
| while cursor.pos < len { | ||
| match cursor.curr { | ||
| // End of the comment | ||
| b'\n' => break, | ||
|
|
||
| // Everything else is part of the comment and replaced | ||
| _ => { | ||
| result[cursor.pos] = b' '; | ||
| cursor.advance(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| cursor.advance(); | ||
| continue; | ||
| } | ||
|
|
||
| _ => {} | ||
| } | ||
|
|
||
| // Looking for `%w`, `%W`, or `%p` | ||
| if cursor.curr != b'%' || !matches!(cursor.next, b'w' | b'W' | b'p') { | ||
| cursor.advance(); | ||
| continue; | ||
| } | ||
|
|
@@ -90,6 +156,7 @@ impl PreProcessor for Ruby { | |
| b'[' => b']', | ||
| b'(' => b')', | ||
| b'{' => b'}', | ||
| b' ' => b'\n', | ||
| _ => { | ||
| cursor.advance(); | ||
| continue; | ||
|
|
@@ -131,7 +198,10 @@ impl PreProcessor for Ruby { | |
|
|
||
| // End of the pattern, replace the boundary character with a space | ||
| _ if cursor.curr == boundary => { | ||
| result[cursor.pos] = b' '; | ||
| if boundary != b'\n' { | ||
| result[cursor.pos] = b' '; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
|
|
||
|
|
@@ -173,12 +243,46 @@ mod tests { | |
| "%w(flex data-[state=pending]:bg-(--my-color) flex-col)", | ||
| "%w flex data-[state=pending]:bg-(--my-color) flex-col ", | ||
| ), | ||
|
|
||
| // %w …\n | ||
| ("%w flex px-2.5\n", "%w flex px-2.5\n"), | ||
|
|
||
| // Use backslash to embed spaces in the strings. | ||
| (r#"%w[foo\ bar baz\ bat]"#, r#"%w foo bar baz bat "#), | ||
| (r#"%W[foo\ bar baz\ bat]"#, r#"%W foo bar baz bat "#), | ||
|
|
||
| // The nested delimiters evaluated to a flat array of strings | ||
| // (not nested array). | ||
| (r#"%w[foo[bar baz]qux]"#, r#"%w foo[bar baz]qux "#), | ||
|
|
||
| ( | ||
| "# test\n# test\n# {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!]\n%w[flex px-2.5]", | ||
| " \n \n \n%w flex px-2.5 " | ||
| ), | ||
|
|
||
| (r#""foo # bar""#, r#""foo # bar""#), | ||
| (r#"'foo # bar'"#, r#"'foo # bar'"#), | ||
| ( | ||
| r#"def call = tag.span "Foo", class: %w[rounded-full h-0.75 w-0.75]"#, | ||
| r#"def call = tag.span "Foo", class: %w rounded-full h-0.75 w-0.75 "# | ||
| ), | ||
|
|
||
| (r#"%w[foo ' bar]"#, r#"%w foo ' bar "#), | ||
| (r#"%w[foo " bar]"#, r#"%w foo " bar "#), | ||
| (r#"%W[foo ' bar]"#, r#"%W foo ' bar "#), | ||
| (r#"%W[foo " bar]"#, r#"%W foo " bar "#), | ||
|
|
||
| (r#"%p foo ' bar "#, r#"%p foo ' bar "#), | ||
| (r#"%p foo " bar "#, r#"%p foo " bar "#), | ||
|
|
||
| ( | ||
| "%p has a ' quote\n# this should be removed\n%p has a ' quote", | ||
| "%p has a ' quote\n \n%p has a ' quote" | ||
| ), | ||
| ( | ||
| "%p has a \" quote\n# this should be removed\n%p has a \" quote", | ||
| "%p has a \" quote\n \n%p has a \" quote" | ||
| ), | ||
| ] { | ||
| Ruby::test(input, expected); | ||
| } | ||
|
|
@@ -211,6 +315,16 @@ mod tests { | |
| "%w(flex data-[state=pending]:bg-(--my-color) flex-col)", | ||
| vec!["flex", "data-[state=pending]:bg-(--my-color)", "flex-col"], | ||
| ), | ||
|
|
||
| ( | ||
| "# test\n# test\n# {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!]\n%w[flex px-2.5]", | ||
| vec!["flex", "px-2.5"], | ||
| ), | ||
|
|
||
| (r#""foo # bar""#, vec!["foo", "bar"]), | ||
| (r#"'foo # bar'"#, vec!["foo", "bar"]), | ||
|
|
||
| (r#"%w[foo ' bar]"#, vec!["foo", "bar"]), | ||
| ] { | ||
| Ruby::test_extract_contains(input, expected); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle
!importantcase-insensitively.starts_with(b"!important]")now rejects valid ASCII-insensitive variants like[color:red!IMPORTANT], which CSS accepts and the prior implementation extracted. Please keep the comparison case-insensitive so we don’t regress these inputs.Apply this diff to make the comparison ASCII-insensitive:
🤖 Prompt for AI Agents