-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Pre process Slim templates embedded in Ruby files
#17336
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
55b1903
add failing test
RobinMalfait 5716e55
handle embedded `slim` template in Ruby files
RobinMalfait 6c1963c
update changelog
RobinMalfait 9a06652
add `fancy-regex`
RobinMalfait 97faf5e
extract lang from `{lang}_template` instead
RobinMalfait 68406f8
added test with multiple occurrences
RobinMalfait 596d531
adjust regex slightly, make captures lazy
RobinMalfait 25e49f6
Merge branch 'main' into fix/issue-17334
RobinMalfait File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,14 @@ | |
| use crate::cursor; | ||
| use crate::extractor::bracket_stack; | ||
| use crate::extractor::pre_processors::pre_processor::PreProcessor; | ||
| use crate::pre_process_input; | ||
| use bstr::ByteSlice; | ||
| use fancy_regex::Regex; | ||
| use std::sync; | ||
|
|
||
| static TEMPLATE_REGEX: sync::LazyLock<Regex> = sync::LazyLock::new(|| { | ||
| Regex::new(r#"\s*(.*?)_template\s*<<[-~]?([A-Z]+?)\n([\s\S]*?)\2"#).unwrap() | ||
|
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. TIL about |
||
| }); | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct Ruby; | ||
|
|
@@ -14,6 +22,20 @@ impl PreProcessor for Ruby { | |
| let mut cursor = cursor::Cursor::new(content); | ||
| let mut bracket_stack = bracket_stack::BracketStack::default(); | ||
|
|
||
| // Extract embedded template languages | ||
| // https://viewcomponent.org/guide/templates.html#interpolations | ||
| let content_as_str = std::str::from_utf8(content).unwrap(); | ||
| for capture in TEMPLATE_REGEX | ||
| .captures_iter(content_as_str) | ||
| .filter_map(Result::ok) | ||
| { | ||
| let lang = capture.get(1).unwrap().as_str(); | ||
| let body = capture.get(3).unwrap().as_str(); | ||
| let replaced = pre_process_input(body.as_bytes(), lang); | ||
| result = result.replace(body, replaced); | ||
| } | ||
|
|
||
| // Ruby extraction | ||
| while cursor.pos < len { | ||
| // Looking for `%w` or `%W` | ||
| if cursor.curr != b'%' && !matches!(cursor.next, b'w' | b'W') { | ||
|
|
@@ -153,4 +175,51 @@ mod tests { | |
| Ruby::test_extract_contains(input, expected); | ||
| } | ||
| } | ||
|
|
||
| // https://github.com/tailwindlabs/tailwindcss/issues/17334 | ||
| #[test] | ||
| fn test_embedded_slim_extraction() { | ||
| let input = r#" | ||
| class QweComponent < ApplicationComponent | ||
| slim_template <<~SLIM | ||
| button.rounded-full.bg-red-500 | ||
| | Some text | ||
| button.rounded-full( | ||
| class="flex" | ||
| ) | ||
| | Some text | ||
| SLIM | ||
| end | ||
| "#; | ||
|
|
||
| Ruby::test_extract_contains(input, vec!["rounded-full", "bg-red-500", "flex"]); | ||
|
|
||
| // Embedded Svelte just to verify that we properly pick up the `{x}_template` | ||
| let input = r#" | ||
| class QweComponent < ApplicationComponent | ||
| svelte_template <<~HTML | ||
| <div class:flex="true"></div> | ||
| HTML | ||
| end | ||
| "#; | ||
|
|
||
| Ruby::test_extract_contains(input, vec!["flex"]); | ||
|
|
||
| // Together in the same file | ||
| let input = r#" | ||
| class QweComponent < ApplicationComponent | ||
| slim_template <<~SLIM | ||
| button.z-1.z-2 | ||
| | Some text | ||
| SLIM | ||
| end | ||
|
|
||
| class QweComponent < ApplicationComponent | ||
| svelte_template <<~HTML | ||
| <div class:z-3="true"></div> | ||
| HTML | ||
| end | ||
| "#; | ||
| Ruby::test_extract_contains(input, vec!["z-1", "z-2", "z-3"]); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm not sure why the
regexlibrary alone is not enough?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.
It's because the

regexcrate doesn't support backreferences:https://github.com/rust-lang/regex?tab=readme-ov-file#:~:text=This%20includes%2C%20but%20is%20not%20limited%20to%2C%20look%2Daround%20andbackreferences.%20In%20exchange%2C%20all%20regex%20searches%20in%20this%20crate%20have%20worst%20caseO(m%20*%20n)%20time%20complexity%2C%20where%20m%20is%20proportional%20to%20the%20size%20of%20the%20regexand%20n%20is%20proportional%20to%20the%20size%20of%20the%20string%20being%20searched.
But luckily
fancy-regexdoes support it. But as theregexcrate also shows is that it is technically much slower (in my testing it's negligible for our use case though)