yard-heuristics 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,25 +4,98 @@
4
4
  module YARDHeuristics
5
5
  load File.expand_path('../yard-heuristics/version.rb', __FILE__)
6
6
  Version.load
7
+
8
+ ParamTypes = {
9
+ 'index' => %w'Integer',
10
+ 'object' => %w'Object',
11
+ 'range' => %w'Range',
12
+ 'string' => %w'String'
13
+ }
14
+
15
+ ReturnTypes = {
16
+ :<< => :self_or_type,
17
+ :>> => :self_or_type,
18
+ :== => %w'Boolean',
19
+ :=== => %w'Boolean',
20
+ :=~ => %w'Boolean',
21
+ :<=> => %w'Integer nil',
22
+ :+ => :type,
23
+ :- => :type,
24
+ :* => :type,
25
+ :/ => :type,
26
+ :each => %w'self',
27
+ :each_with_index => %w'self',
28
+ :inspect => %w'String',
29
+ :length => %w'Integer',
30
+ :size => %w'Integer',
31
+ :to_s => %w'String',
32
+ :to_str => %w'String'
33
+ }
34
+
35
+ class << self
36
+ def set_or_add_param_tag(overload, name, types)
37
+ base = name.sub(/\A[&*]/, '')
38
+ if tag = overload.tags.find{ |e| e.tag_name == 'param' and e.name == base }
39
+ tag.types = types unless tag.types
40
+ else
41
+ tag = YARD::Tags::Tag.new(:param, '', types, base)
42
+ if overload.respond_to? :docstring
43
+ overload.docstring.add_tag tag
44
+ else
45
+ overload.tags << tag
46
+ end
47
+ end
48
+ self
49
+ end
50
+ end
7
51
  end
8
52
 
9
53
  YARD::DocstringParser.after_parse do |parser|
10
54
  next unless YARD::CodeObjects::MethodObject === parser.object
11
- if parser.object.parameters.assoc('other') and not parser.tags.select{ |e| e.tag_name == 'param' }.find{ |e| e.name == 'other' }
55
+ next if parser.text.empty? and parser.tags.empty?
56
+ name = parser.object.namespace ?
57
+ (parser.object.namespace.namespace ?
58
+ parser.object.namespace.relative_path(parser.object.namespace) :
59
+ parser.object.namespace.name) :
60
+ parser.object.name
61
+ if parser.object.parameters.assoc('other') and
62
+ not parser.tags.select{ |e| e.tag_name == 'param' }.
63
+ find{ |e| YARD::Tags::RefTagList === e or e.name == 'other' }
12
64
  parser.tags <<
13
- YARD::Tags::Tag.new(:param,
14
- '',
15
- parser.object.namespace.namespace ?
16
- parser.object.namespace.relative_path(parser.object.namespace) :
17
- parser.object.namespace,
18
- 'other')
65
+ YARD::Tags::Tag.new(:param, '', name, 'other')
19
66
  end
20
- returns = parser.tags.select{ |e| e.tag_name == 'return' }
21
- if [:===, :==, :=~].include? parser.object.name and returns.size < 2
22
- if returns.empty?
23
- parser.tags << YARD::Tags::Tag.new(:return, '', %w'Boolean')
24
- elsif not returns.first.types
25
- returns.first.types = %w'Boolean'
67
+ ([parser] + parser.tags.select{ |e| YARD::Tags::OverloadTag === e }).each do |overload|
68
+ parameters = (YARD::Tags::OverloadTag === overload and overload.parameters) ||
69
+ parser.object.parameters
70
+ parameters.each do |name, _|
71
+ types = YARDHeuristics::ParamTypes[name] and
72
+ YARDHeuristics.set_or_add_param_tag(overload, name, types)
73
+ end
74
+ YARDHeuristics.set_or_add_param_tag(overload, parameters.last.first, %w'Proc') if
75
+ parameters.last and parameters.last.first =~ /\A&/
76
+
77
+ returns = overload.tags.select{ |e| e.tag_name == 'return' }
78
+ types = YARDHeuristics::ReturnTypes[(YARD::Tags::OverloadTag === overload and
79
+ overload.name) || parser.object.name]
80
+ case types
81
+ when :self_or_type
82
+ types = parser.tags.select{ |e| e.tag_name == 'param' }.find{ |e| e.name == 'other' } ?
83
+ [name] :
84
+ %w'self'
85
+ when :type
86
+ types = [name]
87
+ end
88
+ if types and returns.size < 2
89
+ if returns.size == 0
90
+ tag = YARD::Tags::Tag.new(:return, '', types)
91
+ if overload.respond_to? :docstring
92
+ overload.docstring.add_tag tag
93
+ else
94
+ overload.tags << tag
95
+ end
96
+ elsif not returns.first.types
97
+ returns.first.types = types
98
+ end
26
99
  end
27
100
  end
28
101
  end
@@ -3,7 +3,7 @@
3
3
  require 'inventory-1.0'
4
4
 
5
5
  module YARDHeuristics
6
- Version = Inventory.new(1, 0, 0){
6
+ Version = Inventory.new(1, 0, 1){
7
7
  def dependencies
8
8
  super + Inventory::Dependencies.new{
9
9
  development 'inventory-rake', 1, 3, 0
@@ -10,6 +10,101 @@ class A
10
10
  end
11
11
  end
12
12
  EOS
13
- YARD::Registry.at('A#a').docstring.tags(:param).map{ |e| [e.name, e.types] }
13
+ YARD::Registry.at('A#a').docstring.tags(:param).map{ |e| [e.name, e.types] }
14
+ end
15
+
16
+ expect %w'self' do
17
+ YARD::Registry.clear
18
+ YARD::Parser::SourceParser.parse_string(<<EOS)
19
+ class A
20
+ # Placeholder
21
+ def each
22
+ end
23
+ end
24
+ EOS
25
+ YARD::Registry.at('A#each').docstring.tags(:return).first.types
26
+ end
27
+
28
+ expect %w'self' do
29
+ YARD::Registry.clear
30
+ YARD::Parser::SourceParser.parse_string(<<EOS)
31
+ class A
32
+ # @overload
33
+ # Enumerates objects
34
+ #
35
+ # @yieldparam [Object] object
36
+ # @overload
37
+ # @return [Enumerator<Object>] An Enumerator
38
+ def each
39
+ end
40
+ end
41
+ EOS
42
+ YARD::Registry.at('A#each').docstring.tags(:overload).first.tag(:return).types
43
+ end
44
+
45
+ expect %w'A' do
46
+ YARD::Registry.clear
47
+ YARD::Parser::SourceParser.parse_string(<<EOS)
48
+ class A
49
+ # @return The addition of the receiver and _other_
50
+ def +(other)
51
+ end
52
+ end
53
+ EOS
54
+ YARD::Registry.at('A#+').docstring.tags(:return).first.types
55
+ end
56
+
57
+ expect %w'A' do
58
+ YARD::Registry.clear
59
+ YARD::Parser::SourceParser.parse_string(<<EOS)
60
+ class A
61
+ # @return The shifted value
62
+ def <<(other)
63
+ end
64
+ end
65
+ EOS
66
+ YARD::Registry.at('A#<<').docstring.tags(:return).first.types
67
+ end
68
+
69
+ expect %w'self' do
70
+ YARD::Registry.clear
71
+ YARD::Parser::SourceParser.parse_string(<<EOS)
72
+ class A
73
+ # @return The shifted value
74
+ def <<(element)
75
+ end
76
+ end
77
+ EOS
78
+ YARD::Registry.at('A#<<').docstring.tags(:return).first.types
79
+ end
80
+
81
+ expect %w'Range' do
82
+ YARD::Registry.clear
83
+ YARD::Parser::SourceParser.parse_string(<<EOS)
84
+ # A
85
+ def a(range)
86
+ end
87
+ EOS
88
+ YARD::Registry.at('#a').docstring.tags(:param).last.types
89
+ end
90
+
91
+ expect %w'Integer' do
92
+ YARD::Registry.clear
93
+ YARD::Parser::SourceParser.parse_string(<<EOS)
94
+ # A
95
+ def a(index)
96
+ end
97
+ EOS
98
+ YARD::Registry.at('#a').docstring.tags(:param).last.types
99
+ end
100
+
101
+ expect %w'Proc' do
102
+ YARD::Registry.clear
103
+ YARD::Parser::SourceParser.parse_string(<<EOS)
104
+ # @return The shifted value
105
+ def a(&block)
106
+ end
107
+ EOS
108
+ YARD::Registry.at('#a').docstring.tags(:param).last.types
14
109
  end
15
110
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yard-heuristics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-01 00:00:00.000000000 Z
12
+ date: 2012-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: inventory
16
- requirement: &2152773000 !ruby/object:Gem::Requirement
16
+ requirement: &15064488 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.3'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152773000
24
+ version_requirements: *15064488
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: inventory-rake
27
- requirement: &2152772300 !ruby/object:Gem::Requirement
27
+ requirement: &15063012 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '1.3'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2152772300
35
+ version_requirements: *15063012
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: lookout
38
- requirement: &2152771700 !ruby/object:Gem::Requirement
38
+ requirement: &15061752 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '3.0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152771700
46
+ version_requirements: *15061752
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: lookout-rake
49
- requirement: &2152771080 !ruby/object:Gem::Requirement
49
+ requirement: &15061260 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '3.0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2152771080
57
+ version_requirements: *15061260
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: yard
60
- requirement: &2152770340 !ruby/object:Gem::Requirement
60
+ requirement: &15091644 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 0.8.0
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *2152770340
68
+ version_requirements: *15091644
69
69
  description: ! " YARD-Heuristics\n\n YARD-Heuristics
70
70
  heuristically determines types of parameters and return\n values to YARD.\n"
71
71
  email: now@bitwi.se
@@ -81,6 +81,7 @@ files:
81
81
  - Rakefile
82
82
  homepage: https://github.com/now/yard-heuristics
83
83
  licenses: []
84
+ metadata: {}
84
85
  post_install_message:
85
86
  rdoc_options: []
86
87
  require_paths:
@@ -99,8 +100,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
100
  version: '0'
100
101
  requirements: []
101
102
  rubyforge_project:
102
- rubygems_version: 1.8.11
103
+ rubygems_version: 1.8.10
103
104
  signing_key:
104
- specification_version: 3
105
+ specification_version: 4
105
106
  summary: values to YARD.
106
107
  test_files: []