Skip to content

Commit bbeb5b0

Browse files
committed
Fix parser translator scope issues for implicit hash values
`builder.pair_label` is no good since it makes use of variables that the parser gem encountered. Since the prism translator doesn't keep proper track of that information, the following code interprets the implicit value as a local variable, even though it is not in scope: ```rb def foo bar = 123 end { bar: } ```
1 parent 817a95d commit bbeb5b0

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

lib/prism/translation/parser/compiler.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,17 @@ def visit_assoc_node(node)
150150
builder.pair_quoted(token(key.opening_loc), [builder.string_internal([key.unescaped, srange(key.value_loc)])], token(key.closing_loc), visit(node.value))
151151
end
152152
elsif node.value.is_a?(ImplicitNode)
153-
if (value = node.value.value).is_a?(LocalVariableReadNode)
154-
builder.pair_keyword(
155-
[key.unescaped, srange(key)],
156-
builder.ident([value.name, srange(key.value_loc)]).updated(:lvar)
157-
)
153+
value = node.value.value
154+
155+
implicit_value = if value.is_a?(CallNode)
156+
builder.call_method(nil, nil, [value.name, srange(value.message_loc)])
157+
elsif value.is_a?(ConstantReadNode)
158+
builder.const([value.name, srange(key.value_loc)])
158159
else
159-
builder.pair_label([key.unescaped, srange(key.location)])
160+
builder.ident([value.name, srange(key.value_loc)]).updated(:lvar)
160161
end
162+
163+
builder.pair_keyword([key.unescaped, srange(key)], implicit_value)
161164
elsif node.operator_loc
162165
builder.pair(visit(key), token(node.operator_loc), visit(node.value))
163166
elsif key.is_a?(SymbolNode) && key.opening_loc.nil?

0 commit comments

Comments
 (0)