1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Rename 'Explicit Locals to Strict Locals`

Per https://github.com/rails/rails/pull/45602#discussion_r934981516
This commit is contained in:
Joel Hawksley 2022-08-01 17:23:47 -06:00
parent f9f2644b61
commit b7908a62f9
4 changed files with 19 additions and 19 deletions

View file

@ -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:

View file

@ -237,12 +237,12 @@ module ActionView # :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

View file

@ -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
#
@ -129,7 +129,7 @@ module ActionView
@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)
@ -157,7 +157,7 @@ module ActionView
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)
@ -225,16 +225,16 @@ module ActionView
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
@ -287,13 +287,13 @@ module ActionView
# 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
@ -334,10 +334,10 @@ module ActionView
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 =
@ -365,7 +365,7 @@ module ActionView
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.

View file

@ -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: