diff --git a/lib/yard/handlers/ruby/alias_handler.rb b/lib/yard/handlers/ruby/alias_handler.rb index ca08af1fe..fe07de8f8 100644 --- a/lib/yard/handlers/ruby/alias_handler.rb +++ b/lib/yard/handlers/ruby/alias_handler.rb @@ -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 diff --git a/lib/yard/handlers/ruby/constant_handler.rb b/lib/yard/handlers/ruby/constant_handler.rb index ded5bfeb9..d90504c6b 100644 --- a/lib/yard/handlers/ruby/constant_handler.rb +++ b/lib/yard/handlers/ruby/constant_handler.rb @@ -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}" diff --git a/lib/yard/parser/ruby/ruby_parser.rb b/lib/yard/parser/ruby/ruby_parser.rb index 8a189e87b..e5b073495 100644 --- a/lib/yard/parser/ruby/ruby_parser.rb +++ b/lib/yard/parser/ruby/ruby_parser.rb @@ -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 @@ -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) @@ -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) diff --git a/spec/handlers/constant_handler_spec.rb b/spec/handlers/constant_handler_spec.rb index 85c9e7cc8..8d2643e24 100644 --- a/spec/handlers/constant_handler_spec.rb +++ b/spec/handlers/constant_handler_spec.rb @@ -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 diff --git a/spec/templates/helpers/method_helper_spec.rb b/spec/templates/helpers/method_helper_spec.rb index 978d325d1..eec71d7b2 100644 --- a/spec/templates/helpers/method_helper_spec.rb +++ b/spec/templates/helpers/method_helper_spec.rb @@ -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 ":""" + expect(format_constant(foo)).to eq ":''" expect(format_constant(bar)).to eq ':BAR' - expect(format_constant(baz)).to eq ":"B+z"" + expect(format_constant(baz)).to eq ":'B+z'" end end end