Skip to content

Commit

Permalink
fix %w() source in []/[]= parsed context. fixes #461
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jan 21, 2012
1 parent 67a85d3 commit 6ea701c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/yard/parser/ruby/ruby_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ def add_token(token, data)
undef on_bare_assoc_hash
undef on_assoclist_from_args
undef on_aref
undef on_aref_field
undef on_lbracket
undef on_rbracket
undef on_qwords_new
Expand Down Expand Up @@ -309,12 +310,19 @@ def on_unary(op, val)
end

def on_aref(*args)
@map[:lbracket].pop
ll, lc = *@map[:aref].pop
sr = args.first.source_range.first..lc
lr = args.first.line_range.first..ll
AstNode.new(:aref, args, :char => sr, :line => lr)
end


def on_aref_field(*args)
@map[:lbracket].pop
AstNode.new(:aref_field, args,
:listline => lineno..lineno, :listchar => charno...charno)
end

def on_array(other)
node = AstNode.node_class_for(:array).new(:array, [other])
map = @map[MAPPINGS[node.type]]
Expand All @@ -323,6 +331,10 @@ def on_array(other)
node.source_range = Range.new(sstart, @ns_charno - 1)
node.line_range = Range.new(lstart, lineno)
else
sstart = other.source_range.begin
lstart = other.line_range.begin
node.source_range = Range.new(sstart, @ns_charno - 1)
node.line_range = Range.new(lstart, lineno)
node.source_range = other.source_range
node.line_range = other.line_range
end
Expand Down
18 changes: 17 additions & 1 deletion spec/parser/ruby/ruby_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,23 @@ class Foo
s.jump(:array).source.should == '%w( foo bar )'
end
end


it "should parse %w() array source in object[] parsed context" do
s = stmts(<<-eof)
{}[:key]
FOO = %w( foo bar )
eof
s[1].jump(:array).source.should == '%w( foo bar )'
end

it "should parse %w() array source in object[]= parsed context" do
s = stmts(<<-eof)
{}[:key] = :value
FOO = %w( foo bar )
eof
s[1].jump(:array).source.should == '%w( foo bar )'
end

it "should parse [] as array" do
s = stmt(<<-eof)
class Foo
Expand Down

0 comments on commit 6ea701c

Please sign in to comment.