Skip to content

Commit

Permalink
Fix inline component throws error with strip_trailing_whitespace (#2015)
Browse files Browse the repository at this point in the history
* add rstrip failing test case

* simplify failing tests

* final failig  test revision

* fix failing tests

* add: changelog entry

* fix linting errors

* add: inline benchmark

* refcator benchmark

* fix: linting

* Update docs/CHANGELOG.md

---------

Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com>
  • Loading branch information
reeganviljoen and Spone committed Apr 23, 2024
1 parent 7eac9a7 commit 66dcb7c
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Rakefile
Expand Up @@ -30,6 +30,10 @@ task :slots_benchmark do
ruby "./performance/slots_benchmark.rb"
end

task :inline_components_benchmark do
ruby "./performance/inline_benchmark.rb"
end

namespace :coverage do
task :report do
require "simplecov"
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Fixed a bug where inline templates where unable to remove trailing whitespace without throwing an error.

*Reegan Viljoen*

## 3.12.1

* Ensure content is rendered correctly for forwarded slots.
Expand Down
4 changes: 2 additions & 2 deletions lib/view_component/compiler.rb
Expand Up @@ -247,9 +247,9 @@ def variants_from_inline_calls(calls)

def compiled_inline_template(template)
handler = ActionView::Template.handler_for_extension(template.language)
template.rstrip! if component_class.strip_trailing_whitespace?
template = template.source.dup

compile_template(template.source, handler)
compile_template(template, handler)
end

def compiled_template(file_path)
Expand Down
30 changes: 30 additions & 0 deletions performance/components/new_inline_component.rb
@@ -0,0 +1,30 @@
# frozen_string_literal: true

class Performance::NewInlineComponent < ViewComponent::Base
class NestedComponent < ViewComponent::Base
def initialize(name:)
@name = name
end

erb_template <<~ERB
<p>nested hello #{@name}</p>
ERB
end

def initialize(name:)
@name = name
end

erb_template <<~ERB
<h1>hello #{@name}</h1>
<%=
safe_join(
[
content,
50.times.map { render NestedComponent.new(name: @name) }
],
"\n\n"
)
%>
ERB
end
31 changes: 31 additions & 0 deletions performance/inline_benchmark.rb
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# Run `bundle exec rake benchmark` to execute benchmark.
# This is very much a work-in-progress. Please feel free to make/suggest improvements!

require "benchmark/ips"

# Configure Rails Environment
ENV["RAILS_ENV"] = "production"
require File.expand_path("../test/sandbox/config/environment.rb", __dir__)

module Performance
require_relative "components/new_inline_component"
end

class BenchmarksController < ActionController::Base
end

BenchmarksController.view_paths = [File.expand_path("./views", __dir__)]
controller_view = BenchmarksController.new.view_context

Benchmark.ips do |x|
x.time = 10
x.warmup = 2

x.report("inline_component") do
controller_view.render(Performance::NewInlineComponent.new(name: "Reegan"))
end

x.compare!
end
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class InlineTrailingWhitespaceComponent < ViewComponent::Base
strip_trailing_whitespace

erb_template <<~ERB
<h1>Template does not contain any trailing whitespace</h1>
ERB
end
8 changes: 8 additions & 0 deletions test/sandbox/test/rendering_test.rb
Expand Up @@ -1113,4 +1113,12 @@ def test_use_helper
render_inline(UseHelpersComponent.new)
assert_selector ".helper__message", text: "Hello helper method"
end

def test_inline_component_renders_without_trailing_whitespace
without_template_annotations do
render_inline(InlineTrailingWhitespaceComponent.new)
end

refute @rendered_content =~ /\s+\z/, "Rendered component contains trailing whitespace"
end
end

0 comments on commit 66dcb7c

Please sign in to comment.