Skip to content
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

Fix inline component throws error with strip_trailing_whitespace #2015

Merged
merged 12 commits into from Apr 23, 2024

Conversation

reeganviljoen
Copy link
Collaborator

@reeganviljoen reeganviljoen commented Apr 13, 2024

closes #2013

What are you trying to accomplish?

Inline components throws

/Users/<redacted>/view_component/lib/view_component/compiler.rb:251:in `compiled_inline_template': undefined method `rstrip!' for an instance of ViewComponent::InlineTemplate::Template (NoMethodError)

template.rstrip! if component_class.strip_trailing_whitespace?

when strip_trailing_whitespace is used, so this is an attempt to make strip_trailing_whitespace behave in inline components as they do for normal components

What approach did you choose and why?

I removed template.rstrip! if component_class.strip_trailing_whitespace? as the template object is a ViewComponent::InlineTemplate::Template and not a string
I then added template = template.source.dup so tat the template string could be unfrozen for compile_template to remove the trailing white space

Performance Imapct

before change

ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin21]
Warming up --------------------------------------
    inline_component   323.000 i/100ms
Calculating -------------------------------------
    inline_component      3.118k (± 4.8%) i/s -     31.331k in  10.075606s

After

ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin21]
Warming up --------------------------------------
strip_trailing_white_space
                       228.000 i/100ms
Calculating -------------------------------------
strip_trailing_white_space
                          3.057k (± 4.5%) i/s -     30.552k in  10.019824s

@reeganviljoen
Copy link
Collaborator Author

@Spone I have simplified the failing test with while keeping the same error

@reeganviljoen
Copy link
Collaborator Author

reeganviljoen commented Apr 14, 2024

it seems strip_trailing_whitespace doesn't work with inline templates, I have created a fix locally that seem to work, I will push it up latter

@Spone
Copy link
Collaborator

Spone commented Apr 14, 2024

Awesome, that's really useful!

@reeganviljoen reeganviljoen changed the title Add rstrip failing test case for #2013 Fix inline component throws error with strip_trailing_whitespace for #2013 Apr 14, 2024
@reeganviljoen reeganviljoen changed the title Fix inline component throws error with strip_trailing_whitespace for #2013 Fix inline component throws error with strip_trailing_whitespace Apr 14, 2024
@reeganviljoen
Copy link
Collaborator Author

@Spone I have updated the description, to reflect the actual bug, I then also fixed the test and have added the fix for the bug

Copy link
Collaborator

@Spone Spone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty elegant solution! Can we make sure that it doesn't affect performance?

@reeganviljoen
Copy link
Collaborator Author

@Spone I write a benchmark by rewriting the old performance inline_componet on a new component and testing that, it seems this is actually faster by a small margin see the description for more details

Copy link
Contributor

@camertron camertron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me, thanks @reeganviljoen!

docs/CHANGELOG.md Outdated Show resolved Hide resolved
@Spone Spone enabled auto-merge (squash) April 23, 2024 23:39
@Spone Spone merged commit 66dcb7c into ViewComponent:main Apr 23, 2024
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

undefined method `rstrip!' for #<struct ViewComponent::InlineTemplate::Template
3 participants