diff --git a/lib/prism/parse_result.rb b/lib/prism/parse_result.rb index 5b22184ef12572..8ba4e195e6236e 100644 --- a/lib/prism/parse_result.rb +++ b/lib/prism/parse_result.rb @@ -477,8 +477,9 @@ def deconstruct_keys(keys) # A Location object representing the location of this token in the source. def location - return @location if @location.is_a?(Location) - @location = Location.new(source, @location >> 32, @location & 0xFFFFFFFF) + location = @location + return location if location.is_a?(Location) + @location = Location.new(source, location >> 32, location & 0xFFFFFFFF) end # Implement the pretty print interface for Token. diff --git a/prism/templates/lib/prism/node.rb.erb b/prism/templates/lib/prism/node.rb.erb index 81a007fcd3d809..a8a02cf72ea1d0 100644 --- a/prism/templates/lib/prism/node.rb.erb +++ b/prism/templates/lib/prism/node.rb.erb @@ -9,8 +9,9 @@ module Prism # A Location instance that represents the location of this node in the # source. def location - return @location if @location.is_a?(Location) - @location = Location.new(source, @location >> 32, @location & 0xFFFFFFFF) + location = @location + return location if location.is_a?(Location) + @location = Location.new(source, location >> 32, location & 0xFFFFFFFF) end def newline? # :nodoc: @@ -196,18 +197,20 @@ module Prism <%- case field -%> <%- when Prism::LocationField -%> def <%= field.name %> - return @<%= field.name %> if @<%= field.name %>.is_a?(Location) - @<%= field.name %> = Location.new(source, @<%= field.name %> >> 32, @<%= field.name %> & 0xFFFFFFFF) + location = @<%= field.name %> + return location if location.is_a?(Location) + @<%= field.name %> = Location.new(source, location >> 32, location & 0xFFFFFFFF) end <%- when Prism::OptionalLocationField -%> def <%= field.name %> - case @<%= field.name %> + location = @<%= field.name %> + case location when nil nil when Location - @<%= field.name %> + location else - @<%= field.name %> = Location.new(source, @<%= field.name %> >> 32, @<%= field.name %> & 0xFFFFFFFF) + @<%= field.name %> = Location.new(source, location >> 32, location & 0xFFFFFFFF) end end <%- else -%>