Skip to content

Commit 1b28f77

Browse files
authored
- builder.rb: fix hash value omission considering some local vars as constants (#1064)
A constant always start in uppercase but a local will not always start with lowercase. `_foo` and `💎` are both valid local variables.
1 parent fad33d5 commit 1b28f77

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/parser/builders/default.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,10 @@ def pair_label(key_t)
524524

525525
label = value(key_t)
526526
value =
527-
if label =~ /\A[[:lower:]]/
528-
n(:ident, [ label.to_sym ], Source::Map::Variable.new(value_l))
529-
else
527+
if label =~ /\A[[:upper:]]/
530528
n(:const, [ nil, label.to_sym ], Source::Map::Constant.new(nil, value_l, value_l))
529+
else
530+
n(:ident, [ label.to_sym ], Source::Map::Variable.new(value_l))
531531
end
532532
pair_keyword(key_t, accessible(value))
533533
end

test/test_parser.rb

+24
Original file line numberDiff line numberDiff line change
@@ -10241,6 +10241,30 @@ def test_hash_pair_value_omission
1024110241
| ~~~~~ expression (pair)},
1024210242
SINCE_3_1)
1024310243

10244+
assert_parses(
10245+
s(:begin,
10246+
s(:lvasgn, :foo,
10247+
s(:int, 1)),
10248+
s(:hash,
10249+
s(:pair,
10250+
s(:sym, :foo),
10251+
s(:lvar, :foo)))),
10252+
%q{foo = 1; {foo:}},
10253+
%q{},
10254+
SINCE_3_1)
10255+
10256+
assert_parses(
10257+
s(:begin,
10258+
s(:lvasgn, :_foo,
10259+
s(:int, 1)),
10260+
s(:hash,
10261+
s(:pair,
10262+
s(:sym, :_foo),
10263+
s(:lvar, :_foo)))),
10264+
%q{_foo = 1; {_foo:}},
10265+
%q{},
10266+
SINCE_3_1)
10267+
1024410268
assert_parses(
1024510269
s(:hash,
1024610270
s(:pair, s(:sym, :BAR), s(:const, nil, :BAR))),

0 commit comments

Comments
 (0)