Skip to content

Commit

Permalink
Refactor compiler for clarity (#2091)
Browse files Browse the repository at this point in the history
* add test case

* two tests remaining

* first time all green

* Fix final line endings

* add docs, changelog, test helpers

* Fix final line endings

* streamline compiler method generation

* simplification

* refactor to remove index usage

* clearer control flow

* lint

* md lint

* remove remaining hardcoded formats

* Update lib/view_component/base.rb

Co-authored-by: Blake Williams <blakewilliams@github.com>

* remove unnecessary `inspect`

* remove unused `identifier`

* inline single-use method

* add safe navigation

* consolidate template collision error messages to include format

* add backticks around variant name in error

* use one line syntax

* no need to return false

* inline single-use method

* remove offhand comment that would be better in the compiler, if at all

* inline barely used variable

* consolidate safe_class_name into default_method_name

* use shorter `component` instead of `component_class`

* remove unnecessary conditional

* remove unnecessary nesting

* inline single use method

* remove intermediate compiler methods

* InlineTemplate is now included by default

* move comment to be in correct location

* start to make inline templates like other templates

* continue to consolidate inline template logic

* inline templates are just templates

* move lock to outside conditional

* use same method_name definition

* all templates have line numbers

* move towards single method definition path with source

* template handler is already present, no need to re-calculate

* just pass the template around instead of four params

* use single class_eval to define template methods

* remove unnecessary skipping of iterator

* make template hashes look the same

* move default slot registration to Slotable

* define render_template_for in one place

* remove duplicated method name construction

* sort nils last

* consolidate construction of safe_name

* inline single-use method that at minimum should have been private

* method name should not depend on templates state

* set method name on template hash

* use method name from template hash

* shorten to one-line

* move conditional into existing file template conditional

* simplify `templates

* remove unused formats method

* always set @templates at the same point vs. lazily

* move compiled_template to Template

* clean up method name duplication

* more clearly identify method name

* move compile_to_component to Template

* construct template objects

* use template object in more places

* move html format check to template

* use format accessor on template

* move variant access to template

* remove uses for method name from hash

* use template object in more locations

* use template object in more places

* use existing template object

* add comment for reload functionality

* no more template hash!

* do not define branches if it's not used

* use single code path for safe method definition for templates

* consolidate render_template_for duplication

* consolidate to one-liner

* consolidate to one-liner

* pass redefinition lock to template

* use template object for inline templates in render_template_for

* remove unused method definition

* simplify error check

* use default method name in fewer cases

* remove unreachable code

* remove method name logic from compiler

* simplify lookup of variants

* inline variants accessor

* remove duplicative normalized_variant_name

* add note about allowing inline_call / template collision

* remove usage of inline_calls_defined_on_self

* remove inline_calls_defined_on_self

* inline_calls is only used once

* add todo around tracking template rendering

* simplify conditional to use templates

* simplify conditional

* make one-liner

* gather_template_errors explicitly

* use attr_reader to match templates

* move logic into template error construction

* couple template compilation with render method definition

* define safe method as part of compilation

* extract Template#inline_call?

* inline inline_calls helper

* use select instead of conditonal map+compact

* simplify logic and naming in duplicate detection

* use more specific naming for variant pairs

* inline

* passing around redef lock was messy

* initializer is longer than 120 chars

* let Ruby construct symbol string

* simplify branch construction

* assign body as result of conditionals

* use each_with_object to simplify

* use syntactic sugar

* naming, method privacy

* use default values

* inline inline_calls

* standardrb

* remove out-of-date comment

* shorten lines

* use .first

* remove attr_reader

* simplify compiler mode to be boolean

* remove attr_reader

* extract constant for default format

* split conditional into two lines for readibilty

* rename compiler mode to development mode

* use any? with method extracted to template

* avoid use of _1

* avoid use of _1

* move Template to its own file

* extract simplest case to conditional

* Fix final line endings

* avoid block variable

* avoid more block variables

* standardrb

* add comment

* memoize instance methods lookup

* memoize instance method check

* use single line for each argument

* add TODO for HTML output safety check

* extract and comment format and variant derivation from path

* simpler extension extraction

* add explainer comments for format and variant extraction

* use Hash with Set value to find conflicting variants

* use single loop instead of two

* standardrb

* indicate constant is in internal

* move redefinition lock back to compiler

* remove loop

---------

Co-authored-by: GitHub Actions Bot <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Blake Williams <blakewilliams@github.com>
  • Loading branch information
3 people authored Sep 11, 2024
1 parent 451543a commit ec0434c
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 316 deletions.
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

0 comments on commit ec0434c

Please sign in to comment.