Skip to content

Commit

Permalink
Rename 'Explicit Locals to Strict Locals`
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhawksley committed Aug 1, 2022
1 parent f9f2644 commit b7908a6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion actionview/CHANGELOG.md
@@ -1,4 +1,4 @@
* Allow templates to set explicit locals.
* Allow templates to set strict `locals`.

By default, templates will accept any `locals` as keyword arguments. To define what `locals` a template accepts, add a `locals` magic comment:

Expand Down
4 changes: 2 additions & 2 deletions actionview/lib/action_view/base.rb
Expand Up @@ -237,12 +237,12 @@ def initialize(lookup_context, assigns, controller) # :nodoc:
_prepare_context
end

def _run(method, template, locals, buffer, add_to_stack: true, has_explicit_locals: false, &block)
def _run(method, template, locals, buffer, add_to_stack: true, has_strict_locals: false, &block)
_old_output_buffer, _old_virtual_path, _old_template = @output_buffer, @virtual_path, @current_template
@current_template = template if add_to_stack
@output_buffer = buffer

if has_explicit_locals
if has_strict_locals
begin
public_send(method, buffer, **locals, &block)
rescue ArgumentError => argument_error
Expand Down
30 changes: 15 additions & 15 deletions actionview/lib/action_view/template.rb
Expand Up @@ -8,7 +8,7 @@ module ActionView
class Template
extend ActiveSupport::Autoload

EXPLICIT_LOCALS_REGEX = /\#\s+locals:\s+\((.*)\)/
STRICT_LOCALS_REGEX = /\#\s+locals:\s+\((.*)\)/

# === Encodings in ActionView::Template
#
Expand Down Expand Up @@ -129,7 +129,7 @@ def initialize(source, identifier, handler, locals:, format: nil, variant: nil,
@compiled = false
@locals = locals
@virtual_path = virtual_path
@explicit_locals = nil
@strict_locals = nil

@variable = if @virtual_path
base = @virtual_path.end_with?("/") ? "" : ::File.basename(@virtual_path)
Expand Down Expand Up @@ -157,7 +157,7 @@ def supports_streaming?
def render(view, locals, buffer = ActionView::OutputBuffer.new, add_to_stack: true, &block)
instrument_render_template do
compile!(view)
view._run(method_name, self, locals, buffer, add_to_stack: add_to_stack, has_explicit_locals: @explicit_locals.present?, &block)
view._run(method_name, self, locals, buffer, add_to_stack: add_to_stack, has_strict_locals: @strict_locals.present?, &block)
end
rescue => e
handle_render_error(view, e)
Expand Down Expand Up @@ -225,16 +225,16 @@ def encode!
end
end

# This method is responsible for marking a template as having explicit locals
# This method is responsible for marking a template as having strict locals
# and extracting any arguments declared in the format
# locals: (message:, label: "My Message")
def explicit_locals!
self.source.sub!(EXPLICIT_LOCALS_REGEX, "")
@explicit_locals = $1
def strict_locals!
self.source.sub!(STRICT_LOCALS_REGEX, "")
@strict_locals = $1

return if @explicit_locals.nil? # Magic comment not found
return if @strict_locals.nil? # Magic comment not found

@explicit_locals = "**nil" if @explicit_locals.blank?
@strict_locals = "**nil" if @strict_locals.blank?
end

# Exceptions are marshalled when using the parallel test runner with DRb, so we need
Expand Down Expand Up @@ -287,13 +287,13 @@ def compile!(view)
# In general, this means that templates will be UTF-8 inside of Rails,
# regardless of the original source encoding.
def compile(mod)
explicit_locals!
strict_locals!
source = encode!
code = @handler.call(self, source)

method_arguments =
if @explicit_locals.present?
"output_buffer, #{@explicit_locals}"
if @strict_locals.present?
"output_buffer, #{@strict_locals}"
else
"local_assigns, output_buffer"
end
Expand Down Expand Up @@ -334,10 +334,10 @@ def #{method_name}(#{method_arguments})
raise SyntaxErrorInTemplate.new(self, original_source)
end

return unless @explicit_locals.present?
return unless @strict_locals.present?

# Check compiled method parameters to ensure that only kwargs
# were provided as explicit locals, preventing `locals: (foo, *foo)` etc
# were provided as strict locals, preventing `locals: (foo, *foo)` etc
# and allowing `locals: (foo:)`.

non_kwarg_parameters =
Expand Down Expand Up @@ -365,7 +365,7 @@ def handle_render_error(view, e)
end

def locals_code
return "" if @explicit_locals.present?
return "" if @strict_locals.present?

# Only locals with valid variable names get set directly. Others will
# still be available in local_assigns.
Expand Down
2 changes: 1 addition & 1 deletion guides/source/action_view_overview.md
Expand Up @@ -318,7 +318,7 @@ You can also specify a second partial to be rendered between instances of the ma

Rails will render the `_product_ruler` partial (with no data passed to it) between each pair of `_product` partials.

#### Explicit Locals
#### Strict Locals

By default, templates will accept any `locals` as keyword arguments. To define what `locals` a template accepts, add a `locals` magic comment:

Expand Down

0 comments on commit b7908a6

Please sign in to comment.