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

Refactor compiler for clarity #2091

Merged
merged 155 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
155 commits
Select commit Hold shift + click to select a range
698e991
add test case
joelhawksley Aug 23, 2024
71f4adc
two tests remaining
joelhawksley Aug 23, 2024
d356ce8
first time all green
joelhawksley Aug 23, 2024
7c5cde5
Merge branch 'main' into multi-format-component
joelhawksley Aug 26, 2024
10d68ee
Fix final line endings
github-actions[bot] Aug 26, 2024
229aaab
add docs, changelog, test helpers
joelhawksley Aug 26, 2024
9560ca0
Fix final line endings
github-actions[bot] Aug 26, 2024
9640e67
streamline compiler method generation
joelhawksley Aug 26, 2024
4bacfd9
simplification
joelhawksley Aug 26, 2024
eee9aec
refactor to remove index usage
joelhawksley Aug 26, 2024
1331e41
clearer control flow
joelhawksley Aug 26, 2024
ac048cb
lint
joelhawksley Aug 26, 2024
1170071
Merge branch 'main' into multi-format-component
joelhawksley Aug 26, 2024
7bbba51
md lint
joelhawksley Aug 26, 2024
c44b5b3
remove remaining hardcoded formats
joelhawksley Aug 26, 2024
e46c231
Update lib/view_component/base.rb
joelhawksley Aug 27, 2024
955a52e
Merge branch 'main' into multi-format-component
joelhawksley Aug 27, 2024
6fb636a
remove unnecessary `inspect`
joelhawksley Aug 27, 2024
87978c8
remove unused `identifier`
joelhawksley Aug 27, 2024
b55a94a
inline single-use method
joelhawksley Aug 27, 2024
61a6d61
add safe navigation
joelhawksley Aug 27, 2024
e77aecd
consolidate template collision error messages to include format
joelhawksley Sep 3, 2024
9777f38
add backticks around variant name in error
joelhawksley Sep 4, 2024
067f09f
use one line syntax
joelhawksley Aug 29, 2024
b0aaac8
no need to return false
joelhawksley Aug 29, 2024
ce79fe4
inline single-use method
joelhawksley Aug 29, 2024
b327ac2
remove offhand comment that would be better in the compiler, if at all
joelhawksley Aug 29, 2024
e3aeb39
inline barely used variable
joelhawksley Aug 29, 2024
54bd5cd
consolidate safe_class_name into default_method_name
joelhawksley Aug 29, 2024
bd16572
use shorter `component` instead of `component_class`
joelhawksley Sep 3, 2024
6cc7f99
remove unnecessary conditional
joelhawksley Aug 29, 2024
ab90237
remove unnecessary nesting
joelhawksley Aug 29, 2024
e09adf9
inline single use method
joelhawksley Aug 29, 2024
ce15d3f
remove intermediate compiler methods
joelhawksley Aug 29, 2024
fcafca2
InlineTemplate is now included by default
joelhawksley Aug 29, 2024
6a351b4
move comment to be in correct location
joelhawksley Aug 29, 2024
c8f65c2
start to make inline templates like other templates
joelhawksley Aug 29, 2024
08c8798
continue to consolidate inline template logic
joelhawksley Aug 29, 2024
c2e00b2
inline templates are just templates
joelhawksley Aug 29, 2024
fa46d92
move lock to outside conditional
joelhawksley Aug 29, 2024
992c30d
use same method_name definition
joelhawksley Aug 29, 2024
85efee7
all templates have line numbers
joelhawksley Aug 29, 2024
18c7816
move towards single method definition path with source
joelhawksley Aug 30, 2024
7cc3d2b
template handler is already present, no need to re-calculate
joelhawksley Aug 30, 2024
c0c0787
just pass the template around instead of four params
joelhawksley Aug 30, 2024
dcfe939
use single class_eval to define template methods
joelhawksley Aug 30, 2024
1e8715d
remove unnecessary skipping of iterator
joelhawksley Aug 30, 2024
c4c06e9
make template hashes look the same
joelhawksley Aug 30, 2024
fc49c1c
move default slot registration to Slotable
joelhawksley Aug 30, 2024
ba825d8
define render_template_for in one place
joelhawksley Aug 30, 2024
c3d6a37
remove duplicated method name construction
joelhawksley Aug 30, 2024
4c8588a
sort nils last
joelhawksley Aug 30, 2024
f4aa2dd
consolidate construction of safe_name
joelhawksley Aug 30, 2024
70e2f06
inline single-use method that at minimum should have been private
joelhawksley Aug 30, 2024
daf544c
method name should not depend on templates state
joelhawksley Aug 30, 2024
4914d7a
set method name on template hash
joelhawksley Aug 30, 2024
d46e307
use method name from template hash
joelhawksley Aug 30, 2024
b504576
shorten to one-line
joelhawksley Aug 30, 2024
f52acfc
move conditional into existing file template conditional
joelhawksley Aug 30, 2024
82c13ee
simplify `templates
joelhawksley Aug 30, 2024
f7a93ab
remove unused formats method
joelhawksley Aug 30, 2024
2c46082
always set @templates at the same point vs. lazily
joelhawksley Sep 3, 2024
eb4bb63
move compiled_template to Template
joelhawksley Sep 3, 2024
7dc5283
clean up method name duplication
joelhawksley Sep 3, 2024
56be8c2
more clearly identify method name
joelhawksley Sep 3, 2024
7229d1a
move compile_to_component to Template
joelhawksley Sep 3, 2024
aecea67
construct template objects
joelhawksley Sep 4, 2024
bc5144e
use template object in more places
joelhawksley Sep 4, 2024
f560ef8
move html format check to template
joelhawksley Sep 4, 2024
caeb5d7
use format accessor on template
joelhawksley Sep 4, 2024
5e0d3ad
move variant access to template
joelhawksley Sep 4, 2024
cce39bd
remove uses for method name from hash
joelhawksley Sep 4, 2024
514a784
use template object in more locations
joelhawksley Sep 4, 2024
9cd0aeb
use template object in more places
joelhawksley Sep 4, 2024
b1db136
use existing template object
joelhawksley Sep 4, 2024
6409c90
add comment for reload functionality
joelhawksley Sep 4, 2024
1f2f144
no more template hash!
joelhawksley Sep 4, 2024
d6a152a
do not define branches if it's not used
joelhawksley Sep 4, 2024
0894f78
use single code path for safe method definition for templates
joelhawksley Sep 4, 2024
b39874e
consolidate render_template_for duplication
joelhawksley Sep 4, 2024
186b9e2
consolidate to one-liner
joelhawksley Sep 4, 2024
7bb44b2
consolidate to one-liner
joelhawksley Sep 4, 2024
2056ca9
pass redefinition lock to template
joelhawksley Sep 4, 2024
7689673
use template object for inline templates in render_template_for
joelhawksley Sep 4, 2024
0b2cc43
remove unused method definition
joelhawksley Sep 4, 2024
871b62e
simplify error check
joelhawksley Sep 4, 2024
3cb912c
use default method name in fewer cases
joelhawksley Sep 4, 2024
9c3686e
remove unreachable code
joelhawksley Sep 4, 2024
3785905
remove method name logic from compiler
joelhawksley Sep 4, 2024
4b6c787
simplify lookup of variants
joelhawksley Sep 4, 2024
0db8fdb
inline variants accessor
joelhawksley Sep 4, 2024
f122a71
remove duplicative normalized_variant_name
joelhawksley Sep 5, 2024
d99e819
add note about allowing inline_call / template collision
joelhawksley Sep 5, 2024
011fb1c
remove usage of inline_calls_defined_on_self
joelhawksley Sep 5, 2024
a0c7f9e
remove inline_calls_defined_on_self
joelhawksley Sep 5, 2024
e01fc8b
inline_calls is only used once
joelhawksley Sep 5, 2024
bfdcff2
add todo around tracking template rendering
joelhawksley Sep 5, 2024
b39d9f8
simplify conditional to use templates
joelhawksley Sep 5, 2024
24d8a33
simplify conditional
joelhawksley Sep 5, 2024
2456a3a
make one-liner
joelhawksley Sep 5, 2024
8531f11
gather_template_errors explicitly
joelhawksley Sep 5, 2024
c63a414
use attr_reader to match templates
joelhawksley Sep 5, 2024
336077c
move logic into template error construction
joelhawksley Sep 6, 2024
dc25f1e
couple template compilation with render method definition
joelhawksley Sep 6, 2024
1b2e883
define safe method as part of compilation
joelhawksley Sep 6, 2024
ab42dd1
extract Template#inline_call?
joelhawksley Sep 6, 2024
a52524f
inline inline_calls helper
joelhawksley Sep 6, 2024
a42f23b
use select instead of conditonal map+compact
joelhawksley Sep 6, 2024
3935d9b
simplify logic and naming in duplicate detection
joelhawksley Sep 6, 2024
941b38c
use more specific naming for variant pairs
joelhawksley Sep 6, 2024
824735f
inline
joelhawksley Sep 6, 2024
9ce7234
passing around redef lock was messy
joelhawksley Sep 6, 2024
c93f998
initializer is longer than 120 chars
joelhawksley Sep 6, 2024
e1162c3
let Ruby construct symbol string
joelhawksley Sep 6, 2024
8d24cb8
simplify branch construction
joelhawksley Sep 6, 2024
c8ff887
assign body as result of conditionals
joelhawksley Sep 6, 2024
4854fa0
use each_with_object to simplify
joelhawksley Sep 6, 2024
9b3c638
use syntactic sugar
joelhawksley Sep 6, 2024
1569d75
naming, method privacy
joelhawksley Sep 6, 2024
5407eb0
use default values
joelhawksley Sep 6, 2024
87431a9
inline inline_calls
joelhawksley Sep 6, 2024
e3bfb75
standardrb
joelhawksley Sep 6, 2024
03415c3
remove out-of-date comment
joelhawksley Sep 6, 2024
f89456b
shorten lines
joelhawksley Sep 6, 2024
e9151b7
use .first
joelhawksley Sep 6, 2024
e53d8a3
remove attr_reader
joelhawksley Sep 6, 2024
1e71707
simplify compiler mode to be boolean
joelhawksley Sep 6, 2024
b2f23e8
remove attr_reader
joelhawksley Sep 6, 2024
f403700
extract constant for default format
joelhawksley Sep 6, 2024
3717343
Merge branch 'main' into refactor
joelhawksley Sep 6, 2024
a59419e
split conditional into two lines for readibilty
joelhawksley Sep 10, 2024
2b26f1b
rename compiler mode to development mode
joelhawksley Sep 10, 2024
aeb51b0
use any? with method extracted to template
joelhawksley Sep 10, 2024
33af53c
avoid use of _1
joelhawksley Sep 10, 2024
1399874
avoid use of _1
joelhawksley Sep 10, 2024
4819ff7
move Template to its own file
joelhawksley Sep 10, 2024
ccac374
extract simplest case to conditional
joelhawksley Sep 10, 2024
45d351a
Fix final line endings
github-actions[bot] Sep 10, 2024
97dc206
avoid block variable
joelhawksley Sep 10, 2024
34d11c8
avoid more block variables
joelhawksley Sep 10, 2024
a3263bc
standardrb
joelhawksley Sep 10, 2024
e862541
add comment
joelhawksley Sep 10, 2024
a6aaed6
memoize instance methods lookup
joelhawksley Sep 10, 2024
5130214
memoize instance method check
joelhawksley Sep 10, 2024
e10d42c
use single line for each argument
joelhawksley Sep 10, 2024
4882b3a
add TODO for HTML output safety check
joelhawksley Sep 10, 2024
f2b42a6
extract and comment format and variant derivation from path
joelhawksley Sep 10, 2024
34ca335
simpler extension extraction
joelhawksley Sep 10, 2024
570faeb
add explainer comments for format and variant extraction
joelhawksley Sep 10, 2024
a2d2f84
use Hash with Set value to find conflicting variants
joelhawksley Sep 10, 2024
201f6fa
use single loop instead of two
joelhawksley Sep 10, 2024
3d93a49
standardrb
joelhawksley Sep 10, 2024
839fd20
indicate constant is in internal
joelhawksley Sep 11, 2024
0c93a63
move redefinition lock back to compiler
joelhawksley Sep 11, 2024
75e1c13
remove loop
joelhawksley Sep 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require "view_component/preview"
require "view_component/slotable"
require "view_component/slotable_default"
require "view_component/template"
require "view_component/translatable"
require "view_component/with_content_helper"
require "view_component/use_helpers"
Expand All @@ -35,6 +36,7 @@ def config
include ViewComponent::WithContentHelper

RESERVED_PARAMETER = :content
VC_INTERNAL_DEFAULT_FORMAT = :html

# For CSRF authenticity tokens in forms
delegate :form_authenticity_token, :protect_against_forgery?, :config, to: :helpers
Expand Down Expand Up @@ -106,7 +108,6 @@ def render_in(view_context, &block)
before_render

if render?
# Avoid allocating new string when output_preamble and output_postamble are blank
rendered_template =
if compiler.renders_template_for?(@__vc_variant, request&.format&.to_sym)
render_template_for(@__vc_variant, request&.format&.to_sym)
Expand All @@ -116,6 +117,7 @@ def render_in(view_context, &block)
end
end.to_s

# Avoid allocating new string when output_preamble and output_postamble are blank
if output_preamble.blank? && output_postamble.blank?
rendered_template
else
Expand Down Expand Up @@ -562,10 +564,6 @@ def ensure_compiled
compile unless compiled?
end

# Compile templates to instance methods, assuming they haven't been compiled already.
#
# Do as much work as possible in this step, as doing so reduces the amount
# of work done each time a component is rendered.
# @private
def compile(raise_errors: false, force: false)
compiler.compile(raise_errors: raise_errors, force: force)
Expand Down
Loading
Loading