diff --git a/docs/Tags.md b/docs/Tags.md index 63e95889c..1852fe9a1 100644 --- a/docs/Tags.md +++ b/docs/Tags.md @@ -213,9 +213,10 @@ having exactly those 3 elements) would be listed as: `Array(String, Fixnum, Hash Some literals are accepted by virtue of being Ruby literals, but also by YARD conventions. Here is a non-exhaustive list of certain accepted literal values: -* `true`, `false`, `nil` — used when a method returns these explicit literal - values. Note that if your method returns both `true` or `false`, you should use - the `Boolean` conventional type instead. +* `true`, `false`, `nil`, `:foo` — used when a method returns + these explicit literal values. Note that if your method returns both + `true` or `false`, you should use the `Boolean` conventional type + instead. * `self` — has the same meaning as Ruby's "self" keyword in the context of parameters or return types. Recommended mostly for {tag:return} tags that are chainable. @@ -280,4 +281,3 @@ Note that you might not need a tag title if you are hiding it. The title part can be omitted. {yard:include_tags} - diff --git a/lib/yard/code_objects/base.rb b/lib/yard/code_objects/base.rb index 561481317..83aa0c012 100644 --- a/lib/yard/code_objects/base.rb +++ b/lib/yard/code_objects/base.rb @@ -57,8 +57,10 @@ def push(value) # Regular expression to match namespaces (const A or complex path A::B) NAMESPACEMATCH = /(?:(?:#{NSEPQ}\s*)?#{CONSTANTMATCH})+/ - # Regular expression to match a method name - METHODNAMEMATCH = %r{[a-zA-Z_]\w*[!?=]?|[-+~]\@|<<|>>|=~|===?|![=~]?|<=>|[<>]=?|\*\*|[-/+%^&*~`|]|\[\]=?} + # Regular expression to match an identifier like a variable or method name + IDENTIFIERMATCH = %r{[a-zA-Z_]\w*[!?=]?|[-+~]\@|<<|>>|=~|===?|![=~]?|<=>|[<>]=?|\*\*|[-/+%^&*~`|]|\[\]=?} + + METHODNAMEMATCH = IDENTIFIERMATCH # Regular expression to match a fully qualified method def (self.foo, Class.foo). METHODMATCH = /(?:(?:#{NAMESPACEMATCH}|[a-z]\w*)\s*(?:#{CSEPQ}|#{NSEPQ})\s*)?#{METHODNAMEMATCH}/ diff --git a/lib/yard/tags/types_explainer.rb b/lib/yard/tags/types_explainer.rb index d87657665..3fc1dba82 100644 --- a/lib/yard/tags/types_explainer.rb +++ b/lib/yard/tags/types_explainer.rb @@ -102,6 +102,7 @@ class Parser :fixed_collection_start => /\(/, :fixed_collection_end => /\)/, :type_name => /#{ISEP}#{METHODNAMEMATCH}|#{NAMESPACEMATCH}|\w+/, + :symbol => /:#{IDENTIFIERMATCH}/, :type_next => /[,;]/, :whitespace => /\s+/, :hash_collection_start => /\{/, @@ -130,7 +131,7 @@ def parse next unless (match.nil? && @scanner.eos?) || (match && token = @scanner.scan(match)) found = true case token_type - when :type_name + when :type_name, :symbol raise SyntaxError, "expecting END, got name '#{token}'" if name name = token when :type_next diff --git a/spec/docstring_parser_spec.rb b/spec/docstring_parser_spec.rb index 1d6a9d33c..ac50adc88 100644 --- a/spec/docstring_parser_spec.rb +++ b/spec/docstring_parser_spec.rb @@ -251,6 +251,15 @@ def foo(a) end eof end + it "does not warn on constant values" do + expect(log).to_not receive(:warn) + YARD.parse_string <<-eof + # @param [false, true, nil, 4, :foo] a + # @return [void] + def self.bar(a); end + eof + end + it "warns on mismatching param with inline method modifier" do expect(log).to receive(:warn).with(/@param tag has unknown parameter name: notaparam/) YARD.parse_string <<-eof diff --git a/spec/tags/types_explainer_spec.rb b/spec/tags/types_explainer_spec.rb index 8075790de..16d046c97 100644 --- a/spec/tags/types_explainer_spec.rb +++ b/spec/tags/types_explainer_spec.rb @@ -39,7 +39,7 @@ def parse_fail(types) end it "works for a constant value" do - ['false', 'true', 'nil', '4'].each do |name| + ['false', 'true', 'nil', '4', ':foo'].each do |name| @t.name = name expect(@t.to_s).to eq name expect(@t.to_s(false)).to eq name @@ -154,6 +154,11 @@ def parse_fail(types) expect(type.first.name).to eq "Hash" end + it "parses constant values" do + type = parse("false, true, nil, 4, :foo") + expect(type.map(&:name)).to eq ['false', 'true', 'nil', '4', ':foo'] + end + it "does not accept two commas in a row" do parse_fail "A,,B" end