Skip to content

Commit bda405f

Browse files
authored
Merge pull request #29 from mizukami234/fix/parsing-failed-if-dyna-symbol-as-a-hash-key-exists
Bug: parsing failed if dyna_symbols as a hash key exists
2 parents 767abac + f51e37d commit bda405f

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/power_assert/parser.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ def extract_idents(sexp)
7777
when :opassign
7878
_, _, (_, op_name, (_, op_column)), s0 = sexp
7979
extract_idents(s0) + [Ident[:method, op_name.sub(/=\z/, ''), op_column]]
80-
when :assoclist_from_args, :bare_assoc_hash, :dyna_symbol, :paren, :string_embexpr,
80+
when :dyna_symbol
81+
if sexp[1][0].is_a? Symbol
82+
# sexp[1] can be [:string_content, [..]] while parsing { "a": 1 }
83+
extract_idents(sexp[1])
84+
else
85+
sexp[1].flat_map {|s| extract_idents(s) }
86+
end
87+
when :assoclist_from_args, :bare_assoc_hash, :paren, :string_embexpr,
8188
:regexp_literal, :xstring_literal
8289
sexp[1].flat_map {|s| extract_idents(s) }
8390
when :command

test/dyna_symbol_key_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
if defined?(RubyVM) and ! RubyVM::InstructionSequence.compile_option[:specialized_instruction]
2+
warn "#{__FILE__}: specialized_instruction is set to false"
3+
end
4+
5+
require_relative 'test_helper'
6+
7+
class TestDynaSymbolKey < Test::Unit::TestCase
8+
include PowerAssertTestHelper
9+
10+
data do
11+
[
12+
['{"a": b}',
13+
[[:method, "b", 6]]],
14+
].each_with_object({}) {|(source, expected_idents, expected_paths), h| h[source] = [expected_idents, expected_paths, source] }
15+
end
16+
def test_parser(*args)
17+
_test_parser(*args)
18+
end
19+
20+
sub_test_case 'branch' do
21+
t do
22+
assert_equal <<END.chomp, assertion_message {
23+
{"a": 1.to_s}.nil?
24+
| |
25+
| false
26+
"1"
27+
END
28+
{"a": 1.to_s}.nil?
29+
}
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)