Skip to content

Commit

Permalink
Fix parsing of dyna_symbol nodes
Browse files Browse the repository at this point in the history
Fixes #1256
  • Loading branch information
lsegal committed Jun 27, 2019
1 parent 6d8b9b9 commit 225ded9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
6 changes: 3 additions & 3 deletions lib/yard/handlers/ruby/alias_handler.rb
Expand Up @@ -7,13 +7,13 @@ class YARD::Handlers::Ruby::AliasHandler < YARD::Handlers::Ruby::Base
process do
names = []
if statement.type == :alias
names = statement.map {|o| o.jump(:ident, :op, :kw, :const).source }
names = statement.map {|o| o.jump(:ident, :op, :kw, :const, :string_content).source }
elsif statement.call?
statement.parameters(false).each do |obj|
case obj.type
when :symbol_literal, :dyna_symbol
when :symbol_literal
names << obj.jump(:ident, :op, :kw, :const).source
when :string_literal
when :string_literal, :dyna_symbol
names << obj.jump(:string_content).source
end
end
Expand Down
6 changes: 1 addition & 5 deletions lib/yard/handlers/ruby/constant_handler.rb
Expand Up @@ -20,11 +20,7 @@ class YARD::Handlers::Ruby::ConstantHandler < YARD::Handlers::Ruby::Base

def process_constant(statement)
name = statement[0].source
value = if statement[1].type == :dyna_symbol
statement[1].first.source.intern.inspect
else
statement[1].source
end
value = statement[1].source
obj = P(namespace, name)
if obj.is_a?(NamespaceObject) && obj.namespace == namespace
raise YARD::Parser::UndocumentableError, "constant for existing #{obj.type} #{obj}"
Expand Down
14 changes: 13 additions & 1 deletion lib/yard/parser/ruby/ruby_parser.rb
Expand Up @@ -333,6 +333,7 @@ def add_token(token, data)
undef on_bodystmt
undef on_top_const_ref
undef on_const_path_ref
undef on_dyna_symbol

def on_program(*args)
args.first
Expand Down Expand Up @@ -410,6 +411,16 @@ def on_rbracket(tok)
visit_ns_token(:rbracket, tok, false)
end

def on_dyna_symbol(sym)
rng = if sym.source_range.size == 0 # rubocop:disable Style/ZeroLengthPredicate
(sym.source_range.begin - 3)...sym.source_range.end
else
(sym.source_range.begin - 2)..(sym.source_range.end + 1)
end
AstNode.new(:dyna_symbol, [sym], :line => lineno..lineno,
:listline => lineno..lineno, :listchar => rng, :char => rng)
end

def on_top_const_ref(*args)
type = :top_const_ref
node = AstNode.node_class_for(type).new(type, args)
Expand Down Expand Up @@ -481,7 +492,8 @@ def on_lambda(*args)
end

def on_string_content(*args)
AstNode.new(:string_content, args, :listline => lineno..lineno, :listchar => charno..charno)
chr_rng = args.empty? ? charno...charno : charno..charno
AstNode.new(:string_content, args, :listline => lineno..lineno, :listchar => chr_rng)
end

def on_rescue(exc, *args)
Expand Down
2 changes: 1 addition & 1 deletion spec/handlers/constant_handler_spec.rb
Expand Up @@ -35,7 +35,7 @@
expect(obj.constants[0].value).to eq "42"
expect(obj.constants[1].docstring).to eq 'Special constant (empty symbol)'
expect(obj.constants[1].name).to eq :EMPTY
expect(obj.constants[1].value).to eq ':""'
expect(obj.constants[1].value).to eq ":''"
end

it "turns Const = Struct.new('Name', :sym) into class Const with attr :sym" do
Expand Down
4 changes: 2 additions & 2 deletions spec/templates/helpers/method_helper_spec.rb
Expand Up @@ -99,9 +99,9 @@ class TestFmtConst
foo, bar, baz = %w(Foo Bar Baz).map do |c|
Registry.at("TestFmtConst::#{c}").value
end
expect(format_constant(foo)).to eq ":&quot;&quot;"
expect(format_constant(foo)).to eq ":&#39;&#39;"
expect(format_constant(bar)).to eq ':BAR'
expect(format_constant(baz)).to eq ":&quot;B+z&quot;"
expect(format_constant(baz)).to eq ":&#39;B+z&#39;"
end
end
end

0 comments on commit 225ded9

Please sign in to comment.