Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for quoted symbols #1168

Merged
merged 3 commits into from May 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/yard/handlers/ruby/constant_handler.rb
Expand Up @@ -20,7 +20,11 @@ class YARD::Handlers::Ruby::ConstantHandler < YARD::Handlers::Ruby::Base

def process_constant(statement)
name = statement[0].source
value = statement[1].source
value = if statement[1].type == :dyna_symbol
statement[1].first.source.intern.inspect
else
statement[1].source
end
obj = P(namespace, name)
if obj.is_a?(NamespaceObject) && obj.namespace == namespace
raise YARD::Parser::UndocumentableError, "constant for existing #{obj.type} #{obj}"
Expand Down
13 changes: 11 additions & 2 deletions lib/yard/parser/ruby/ruby_parser.rb
Expand Up @@ -282,8 +282,17 @@ def add_token(token, data)
end
end

if @tokens.last && @tokens.last[0] == :symbeg
@tokens[-1] = [:symbol, ":" + data, @tokens.last[2]]
if token == :symbeg
@symbol = [:symbol, data, [lineno, charno]]
elsif @symbol
case token
when :tstring_content
@symbol[1] += data
when :tstring_end, :const, :ident
@symbol[1] += data
@tokens << @symbol
@symbol = nil
end
elsif @heredoc_state == :started
@heredoc_tokens << [token, data, [lineno, charno]]

Expand Down
3 changes: 3 additions & 0 deletions spec/handlers/constant_handler_spec.rb
Expand Up @@ -33,6 +33,9 @@
expect(obj.constants[0].docstring).to eq 'A constant.'
expect(obj.constants[0].name).to eq :CONSTANT
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 ':""'
end

it "turns Const = Struct.new('Name', :sym) into class Const with attr :sym" do
Expand Down
2 changes: 2 additions & 0 deletions spec/handlers/examples/constant_handler_001.rb.txt
Expand Up @@ -21,6 +21,8 @@ MyEmptyStruct = Struct.new
MyStructWithConstant = Struct.new do
# A constant.
CONSTANT = 42
# Special constant (empty symbol)
EMPTY = :''
end

# A crazy struct.
Expand Down
13 changes: 13 additions & 0 deletions spec/parser/ruby/ruby_parser_spec.rb
Expand Up @@ -303,6 +303,19 @@ def hello; end
end
end

it "properly tokenizes symbols" do
tokens = tokenize(<<-eof)
class X
Foo = :''
Fuu = :bar
Bar = :BAR
Baz = :"B+z"
end
eof
symbols = tokens.select {|t| t[0] == :symbol }.map {|t| t[1] }
expect(symbols).to eq %w(:'' :bar :BAR :"B+z")
end

it "parses %w() array in constant declaration" do
s = stmt(<<-eof)
class Foo
Expand Down
25 changes: 25 additions & 0 deletions spec/templates/helpers/method_helper_spec.rb
Expand Up @@ -79,4 +79,29 @@ def foo; end
expect(format_block(Registry.at('#foo'))).to eq "{|a, b, c| ... }"
end
end

describe "#format_constant" do
include YARD::Templates::Helpers::HtmlHelper

it "displays correctly constant values which are quoted symbols" do
YARD.parse_string %(
class TestFmtConst
Foo = :''
Bar = :BAR
Baz = :'B+z'
end
)
# html_syntax_highlight will be called by format_constant for
# Foo, Bar and Baz and in turn will enquire for options.highlight
expect(self).to receive(:options).exactly(3).times.and_return(
Options.new.update(:highlight => false)
)
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(bar)).to eq ':BAR'
expect(format_constant(baz)).to eq ":&quot;B+z&quot;"
end
end
end