Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/yard/code_objects/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def push(value)
# Regular expression to match a fully qualified method def (self.foo, Class.foo).
METHODMATCH = /(?:(?:#{NAMESPACEMATCH}|[a-z]\w*)\s*(?:#{CSEPQ}|#{NSEPQ})\s*)?#{METHODNAMEMATCH}/

# Regular expression to match symbol and string literals
LITERALMATCH = /:\w+|'[^']*'|"[^"]*"/
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this constant make sense here?


# All builtin Ruby exception classes for inheritance tree.
BUILTIN_EXCEPTIONS = ["ArgumentError", "ClosedQueueError", "EncodingError",
"EOFError", "Exception", "FiberError", "FloatDomainError", "IndexError",
Expand Down
4 changes: 3 additions & 1 deletion lib/yard/tags/types_explainer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def initialize(name)
def to_s(singular = true)
if name[0, 1] == "#"
singular ? "an object that responds to #{name}" : "objects that respond to #{name}"
elsif name[0, 1] == ":" || (name[0, 1] =~ /['"]/ && name[-1, 1] =~ /['"]/)
"a literal value #{name}"
elsif name[0, 1] =~ /[A-Z]/
singular ? "a#{name[0, 1] =~ /[aeiou]/i ? 'n' : ''} " + name : "#{name}#{name[-1, 1] =~ /[A-Z]/ ? "'" : ''}s"
else
Expand Down Expand Up @@ -101,7 +103,7 @@ class Parser
:collection_end => />/,
:fixed_collection_start => /\(/,
:fixed_collection_end => /\)/,
:type_name => /#{ISEP}#{METHODNAMEMATCH}|#{NAMESPACEMATCH}|\w+/,
:type_name => /#{ISEP}#{METHODNAMEMATCH}|#{NAMESPACEMATCH}|#{LITERALMATCH}|\w+/,
:type_next => /[,;]/,
:whitespace => /\s+/,
:hash_collection_start => /\{/,
Expand Down
20 changes: 20 additions & 0 deletions spec/code_objects/constants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ def silence_warnings
end
end

describe :LITERALMATCH do
it "matches symbol literals" do
expect(":symbol"[CodeObjects::LITERALMATCH]).to eq ":symbol"
expect(":some_symbol"[CodeObjects::LITERALMATCH]).to eq ":some_symbol"
expect("not_a_symbol"[CodeObjects::LITERALMATCH]).to be nil
end

it "matches single-quoted string literals" do
expect("'string'"[CodeObjects::LITERALMATCH]).to eq "'string'"
expect("'some string with spaces'"[CodeObjects::LITERALMATCH]).to eq "'some string with spaces'"
expect("not_quoted"[CodeObjects::LITERALMATCH]).to be nil
end

it "matches double-quoted string literals" do
expect('"string"'[CodeObjects::LITERALMATCH]).to eq '"string"'
expect('"some string with spaces"'[CodeObjects::LITERALMATCH]).to eq '"some string with spaces"'
expect("not_quoted"[CodeObjects::LITERALMATCH]).to be nil
end
end

describe :BUILTIN_EXCEPTIONS do
it "includes all base exceptions" do
bad_names = []
Expand Down
22 changes: 21 additions & 1 deletion spec/tags/types_explainer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def parse_fail(types)
expect(@t.to_s(false)).to eq name
end
end

it "works for literal values" do
[':symbol', "'5'"].each do |name|
@t.name = name
expect(@t.to_s).to eq "a literal value #{name}"
expect(@t.to_s(false)).to eq "a literal value #{name}"
end
end
end

describe CollectionType, '#to_s' do
Expand Down Expand Up @@ -131,6 +139,17 @@ def parse_fail(types)
expect(type[3].name).to eq "E"
end

it 'parses a list of literal values' do
type = parse("true, false, nil, 4, :symbol, '5'")
expect(type.size).to eq 6
expect(type[0].name).to eq "true"
expect(type[1].name).to eq "false"
expect(type[2].name).to eq "nil"
expect(type[3].name).to eq "4"
expect(type[4].name).to eq ":symbol"
expect(type[5].name).to eq "'5'"
end

it "parses a collection type" do
type = parse("MyList<String>")
expect(type.first).to be_a(CollectionType)
Expand Down Expand Up @@ -192,7 +211,8 @@ def parse_fail(types)
a Hash with keys made of (Foos or Bars) and values of (Symbols or Numbers)",
"#weird_method?, #<=>, #!=" => "an object that responds to #weird_method?;
an object that responds to #<=>;
an object that responds to #!="
an object that responds to #!=",
":symbol, 'string'" => "a literal value :symbol; a literal value 'string'"
}
expect.each do |input, expected|
explain = YARD::Tags::TypesExplainer.explain(input)
Expand Down
Loading