Skip to content

Commit 5026aba

Browse files
Refactor compiled source code into method
Moves the part of `compile!` that compiles the template source into it's own method. We need this for future work in improving exceptions for ERB templates to pass to ErrorHighlight. Co-authored-by: Aaron Patterson <[email protected]>
1 parent a1367da commit 5026aba

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

actionview/lib/action_view/template.rb

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -317,19 +317,10 @@ def compile!(view)
317317
end
318318
end
319319

320-
# Among other things, this method is responsible for properly setting
321-
# the encoding of the compiled template.
322-
#
323-
# If the template engine handles encodings, we send the encoded
324-
# String to the engine without further processing. This allows
325-
# the template engine to support additional mechanisms for
326-
# specifying the encoding. For instance, ERB supports <%# encoding: %>
327-
#
328-
# Otherwise, after we figure out the correct encoding, we then
329-
# encode the source into <tt>Encoding.default_internal</tt>.
330-
# In general, this means that templates will be UTF-8 inside of Rails,
331-
# regardless of the original source encoding.
332-
def compile(mod)
320+
# This method compiles the source of the template. The compilation of templates
321+
# involves setting strict_locals! if applicable, encoding the template, and setting
322+
# frozen string literal.
323+
def compiled_source
333324
strict_locals!
334325
source = encode!
335326
code = @handler.call(self, source)
@@ -343,7 +334,7 @@ def compile(mod)
343334

344335
# Make sure that the resulting String to be eval'd is in the
345336
# encoding of the code
346-
original_source = source
337+
@original_source = source
347338
source = +<<-end_src
348339
def #{method_name}(#{method_arguments})
349340
@virtual_path = #{@virtual_path.inspect};#{locals_code};#{code}
@@ -364,17 +355,33 @@ def #{method_name}(#{method_arguments})
364355
raise WrongEncodingError.new(source, Encoding.default_internal)
365356
end
366357

358+
if Template.frozen_string_literal
359+
"# frozen_string_literal: true\n#{source}"
360+
else
361+
source
362+
end
363+
end
364+
365+
# Among other things, this method is responsible for properly setting
366+
# the encoding of the compiled template.
367+
#
368+
# If the template engine handles encodings, we send the encoded
369+
# String to the engine without further processing. This allows
370+
# the template engine to support additional mechanisms for
371+
# specifying the encoding. For instance, ERB supports <%# encoding: %>
372+
#
373+
# Otherwise, after we figure out the correct encoding, we then
374+
# encode the source into <tt>Encoding.default_internal</tt>.
375+
# In general, this means that templates will be UTF-8 inside of Rails,
376+
# regardless of the original source encoding.
377+
def compile(mod)
367378
begin
368-
if Template.frozen_string_literal
369-
mod.module_eval("# frozen_string_literal: true\n#{source}", identifier, -1)
370-
else
371-
mod.module_eval(source, identifier, 0)
372-
end
379+
mod.module_eval(compiled_source, identifier, offset)
373380
rescue SyntaxError
374381
# Account for when code in the template is not syntactically valid; e.g. if we're using
375382
# ERB and the user writes <%= foo( %>, attempting to call a helper `foo` and interpolate
376383
# the result into the template, but missing an end parenthesis.
377-
raise SyntaxErrorInTemplate.new(self, original_source)
384+
raise SyntaxErrorInTemplate.new(self, @original_source)
378385
end
379386

380387
return unless @strict_locals
@@ -398,6 +405,14 @@ def #{method_name}(#{method_arguments})
398405
)
399406
end
400407

408+
def offset
409+
if Template.frozen_string_literal
410+
-1
411+
else
412+
0
413+
end
414+
end
415+
401416
def handle_render_error(view, e)
402417
if e.is_a?(Template::Error)
403418
e.sub_template_of(self)

0 commit comments

Comments
 (0)