Skip to content

Commit

Permalink
Merge pull request #1701 from presidentbeef/shorthand_hash_syntax
Browse files Browse the repository at this point in the history
Handle 3.1 shorthand hash syntax
  • Loading branch information
presidentbeef authored Apr 9, 2022
2 parents 96c8e82 + 5bf297b commit abf910d
Show file tree
Hide file tree
Showing 2 changed files with 48 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
24 changes: 24 additions & 0 deletions test/tests/alias_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,30 @@ 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:, c: 1 }
h[:b]
RUBY
end

def test_hash_shorthand_syntax_mix
assert_alias '3', <<-RUBY
a = 1
h = { a:, b:, c: 3 }
h[:c]
RUBY
end

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

0 comments on commit abf910d

Please sign in to comment.