Skip to content

Commit bf2e2fe

Browse files
authored
Extract classes from interpolated expressions in Ruby (tailwindlabs#19730)
This PR ensures that interpolated expressions in Ruby syntax are correctly extracted. The issue was that we ignore comments in Ruby syntax (which start with `#`). We already made an exception for locals (`<%# locals: … %>`), but we also need to handle interpolated expressions (`#{ … }`) in the same way because they are not comments. Fixes: tailwindlabs#19728 ## Test plan 1. Existing tests pass 2. Added a regression test for this scenario 3. Tested using the extractor on the given code snippet: <img width="1461" height="1856" alt="image" src="https://github.com/user-attachments/assets/b48f7042-ca9d-4133-87ae-4c37c633c073" /> Notice that the `w-100` gets extracted now.
1 parent 9ded4a2 commit bf2e2fe

File tree

1 file changed

+17
-1
lines changed
  • crates/oxide/src/extractor/pre_processors

1 file changed

+17
-1
lines changed

crates/oxide/src/extractor/pre_processors/ruby.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ impl PreProcessor for Ruby {
124124
// Except for strict locals, these are defined in a `<%# locals: … %>`. Checking if
125125
// the comment is preceded by a `%` should be enough without having to perform more
126126
// parsing logic. Worst case we _do_ scan a few comments.
127-
b'#' if !matches!(cursor.prev(), b'%') => {
127+
//
128+
// We also want to skip interpolation syntax, which look like `#{…}`.
129+
b'#' if !matches!(cursor.prev(), b'%') && !matches!(cursor.next(), b'{') => {
128130
result[cursor.pos] = b' ';
129131
cursor.advance();
130132

@@ -388,6 +390,20 @@ mod tests {
388390
Ruby::test_extract_contains(input, vec!["z-1", "z-2", "z-3"]);
389391
}
390392

393+
// https://github.com/tailwindlabs/tailwindcss/issues/19728
394+
#[test]
395+
fn test_interpolated_expressions() {
396+
let input = r#"
397+
def width_class(width = nil)
398+
<<~STYLE_CLASS
399+
#{width || 'w-100'}
400+
STYLE_CLASS
401+
end
402+
"#;
403+
404+
Ruby::test_extract_contains(input, vec!["w-100"]);
405+
}
406+
391407
// https://github.com/tailwindlabs/tailwindcss/issues/19239
392408
#[test]
393409
fn test_skip_comments() {

0 commit comments

Comments
 (0)