Skip to content

Commit

Permalink
Handle 3.1 shorthand hash syntax
Browse files Browse the repository at this point in the history
Fixes #1700
  • Loading branch information
presidentbeef committed Apr 8, 2022
1 parent 96c8e82 commit 48e14b4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/brakeman/processors/alias_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,30 @@ def process_hash exp
end
end

exp
# Return early unless there might be short-hand syntax,
# since handling it is kind of expensive.
return exp unless exp.any? { |e| e.nil? }

# Need to handle short-hand hash syntax
new_hash = [:hash]
hash_iterate(exp) do |key, value|
# e.g. { a: }
if value.nil? and symbol? key
# Only handling local variables for now, not calls
lvar = s(:lvar, key.value)
if var_value = env[lvar]
new_hash << key << var_value.deep_clone(key.line || 0)
else
# If the value is unknown, assume it was a call
# and set the value to a call
new_hash.concat << key << s(:call, nil, key.value).line(key.line || 0)
end
else
new_hash.concat << key << value
end
end

Sexp.from_array(new_hash).line(exp.line || 0)
end

#Merge values into hash when processing
Expand Down
16 changes: 16 additions & 0 deletions test/tests/alias_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@ def test_hash_double_splat
RUBY
end

def test_hash_shorthand_syntax
assert_alias '2', <<-RUBY
a = 1
b = 2
h = { a:, b: }
h[:b]
RUBY
end

def test_hash_shorthand_syntax_unknown_value
assert_alias 'b', <<-RUBY
h = { a:, b: }
h[:b]
RUBY
end

def test_splat_array_args
assert_alias 'x(1, b, :c)', <<-RUBY
a = b
Expand Down

0 comments on commit 48e14b4

Please sign in to comment.