inch 0.5.0.rc3 → 0.5.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +15 -0
  3. data/.rubocop_todo.yml +94 -0
  4. data/CHANGELOG.md +26 -2
  5. data/Gemfile +5 -2
  6. data/README.md +3 -3
  7. data/Rakefile +6 -2
  8. data/bin/inch +3 -3
  9. data/config/{example.yml → .inch.yml.sample} +1 -0
  10. data/config/base.rb +53 -0
  11. data/config/elixir.rb +43 -0
  12. data/config/nodejs.rb +10 -0
  13. data/config/{defaults.rb → ruby.rb} +5 -54
  14. data/inch.gemspec +7 -7
  15. data/lib/inch.rb +14 -9
  16. data/lib/inch/api.rb +10 -10
  17. data/lib/inch/api/compare.rb +2 -2
  18. data/lib/inch/api/compare/code_objects.rb +2 -3
  19. data/lib/inch/api/compare/codebases.rb +1 -1
  20. data/lib/inch/api/diff.rb +13 -16
  21. data/lib/inch/api/filter.rb +1 -1
  22. data/lib/inch/api/get.rb +2 -2
  23. data/lib/inch/api/options/base.rb +4 -4
  24. data/lib/inch/api/options/filter.rb +0 -4
  25. data/lib/inch/api/options/suggest.rb +2 -6
  26. data/lib/inch/api/stats.rb +0 -1
  27. data/lib/inch/api/suggest.rb +9 -10
  28. data/lib/inch/cli.rb +6 -6
  29. data/lib/inch/cli/arguments.rb +2 -2
  30. data/lib/inch/cli/command.rb +15 -15
  31. data/lib/inch/cli/command/base.rb +30 -9
  32. data/lib/inch/cli/command/base_list.rb +2 -2
  33. data/lib/inch/cli/command/console.rb +5 -5
  34. data/lib/inch/cli/command/diff.rb +9 -9
  35. data/lib/inch/cli/command/inspect.rb +5 -4
  36. data/lib/inch/cli/command/list.rb +4 -4
  37. data/lib/inch/cli/command/options/base.rb +17 -16
  38. data/lib/inch/cli/command/options/base_list.rb +9 -5
  39. data/lib/inch/cli/command/options/console.rb +4 -3
  40. data/lib/inch/cli/command/options/diff.rb +8 -6
  41. data/lib/inch/cli/command/options/inspect.rb +2 -2
  42. data/lib/inch/cli/command/options/list.rb +3 -2
  43. data/lib/inch/cli/command/options/show.rb +2 -2
  44. data/lib/inch/cli/command/options/stats.rb +2 -1
  45. data/lib/inch/cli/command/options/suggest.rb +6 -3
  46. data/lib/inch/cli/command/output/base.rb +4 -6
  47. data/lib/inch/cli/command/output/console.rb +6 -7
  48. data/lib/inch/cli/command/output/diff.rb +6 -6
  49. data/lib/inch/cli/command/output/inspect.rb +6 -8
  50. data/lib/inch/cli/command/output/list.rb +0 -1
  51. data/lib/inch/cli/command/output/show.rb +4 -5
  52. data/lib/inch/cli/command/output/stats.rb +21 -21
  53. data/lib/inch/cli/command/output/suggest.rb +31 -28
  54. data/lib/inch/cli/command/show.rb +4 -4
  55. data/lib/inch/cli/command/stats.rb +4 -4
  56. data/lib/inch/cli/command/suggest.rb +6 -6
  57. data/lib/inch/cli/command_parser.rb +6 -5
  58. data/lib/inch/cli/sparkline_helper.rb +6 -6
  59. data/lib/inch/cli/trace_helper.rb +1 -1
  60. data/lib/inch/cli/yardopts_helper.rb +4 -3
  61. data/lib/inch/code_object.rb +3 -3
  62. data/lib/inch/code_object/converter.rb +6 -8
  63. data/lib/inch/code_object/provider.rb +11 -11
  64. data/lib/inch/code_object/proxy.rb +61 -30
  65. data/lib/inch/codebase.rb +7 -8
  66. data/lib/inch/codebase/object.rb +60 -0
  67. data/lib/inch/codebase/objects.rb +8 -10
  68. data/lib/inch/codebase/objects_filter.rb +7 -5
  69. data/lib/inch/codebase/proxy.rb +4 -4
  70. data/lib/inch/codebase/serializer.rb +3 -3
  71. data/lib/inch/config.rb +42 -11
  72. data/lib/inch/config/base.rb +8 -29
  73. data/lib/inch/config/codebase.rb +32 -7
  74. data/lib/inch/config/evaluation.rb +61 -0
  75. data/lib/inch/core_ext.rb +1 -1
  76. data/lib/inch/core_ext/string.rb +1 -1
  77. data/lib/inch/evaluation.rb +13 -21
  78. data/lib/inch/evaluation/file.rb +4 -2
  79. data/lib/inch/evaluation/grade_list.rb +4 -4
  80. data/lib/inch/evaluation/priority_range.rb +3 -0
  81. data/lib/inch/evaluation/proxy.rb +139 -14
  82. data/lib/inch/evaluation/role.rb +99 -0
  83. data/lib/inch/language.rb +3 -0
  84. data/lib/inch/language/elixir/code_object/base.rb +197 -0
  85. data/lib/inch/language/elixir/code_object/function_object.rb +74 -0
  86. data/lib/inch/language/elixir/code_object/module_object.rb +23 -0
  87. data/lib/inch/language/elixir/code_object/type_object.rb +11 -0
  88. data/lib/inch/language/elixir/evaluation/base.rb +28 -0
  89. data/lib/inch/language/elixir/evaluation/function_object.rb +31 -0
  90. data/lib/inch/language/elixir/evaluation/module_object.rb +27 -0
  91. data/lib/inch/language/elixir/evaluation/type_object.rb +11 -0
  92. data/lib/inch/language/elixir/import.rb +24 -0
  93. data/lib/inch/language/elixir/provider/reader.rb +19 -0
  94. data/lib/inch/language/elixir/provider/reader/object.rb +63 -0
  95. data/lib/inch/language/elixir/provider/reader/object/base.rb +191 -0
  96. data/lib/inch/language/elixir/provider/reader/object/function_object.rb +26 -0
  97. data/lib/inch/language/elixir/provider/reader/object/module_object.rb +22 -0
  98. data/lib/inch/language/elixir/provider/reader/object/type_object.rb +15 -0
  99. data/lib/inch/language/elixir/provider/reader/parser.rb +55 -0
  100. data/lib/inch/language/elixir/roles/base.rb +32 -0
  101. data/lib/inch/language/elixir/roles/function.rb +112 -0
  102. data/lib/inch/language/elixir/roles/module.rb +64 -0
  103. data/lib/inch/language/elixir/roles/object.rb +76 -0
  104. data/lib/inch/language/elixir/roles/type.rb +13 -0
  105. data/lib/inch/language/nodejs/import.rb +8 -0
  106. data/lib/inch/language/nodejs/provider/jsdoc.rb +19 -0
  107. data/lib/inch/language/nodejs/provider/jsdoc/object.rb +55 -0
  108. data/lib/inch/language/nodejs/provider/jsdoc/object/base.rb +191 -0
  109. data/lib/inch/language/nodejs/provider/jsdoc/parser.rb +59 -0
  110. data/lib/inch/language/ruby/code_object/base.rb +197 -0
  111. data/lib/inch/language/ruby/code_object/class_object.rb +10 -0
  112. data/lib/inch/language/ruby/code_object/class_variable_object.rb +10 -0
  113. data/lib/inch/language/ruby/code_object/constant_object.rb +10 -0
  114. data/lib/inch/language/ruby/code_object/method_object.rb +85 -0
  115. data/lib/inch/language/ruby/code_object/method_parameter_object.rb +64 -0
  116. data/lib/inch/language/ruby/code_object/module_object.rb +10 -0
  117. data/lib/inch/language/ruby/code_object/namespace_object.rb +35 -0
  118. data/lib/inch/language/ruby/evaluation/base.rb +45 -0
  119. data/lib/inch/language/ruby/evaluation/class_object.rb +10 -0
  120. data/lib/inch/language/ruby/evaluation/class_variable_object.rb +21 -0
  121. data/lib/inch/language/ruby/evaluation/constant_object.rb +21 -0
  122. data/lib/inch/language/ruby/evaluation/method_object.rb +74 -0
  123. data/lib/inch/language/ruby/evaluation/module_object.rb +10 -0
  124. data/lib/inch/language/ruby/evaluation/namespace_object.rb +30 -0
  125. data/lib/inch/language/ruby/import.rb +34 -0
  126. data/lib/inch/language/ruby/provider/yard.rb +58 -0
  127. data/lib/inch/language/ruby/provider/yard/docstring.rb +162 -0
  128. data/lib/inch/language/ruby/provider/yard/nodoc_helper.rb +97 -0
  129. data/lib/inch/language/ruby/provider/yard/object.rb +63 -0
  130. data/lib/inch/language/ruby/provider/yard/object/base.rb +325 -0
  131. data/lib/inch/language/ruby/provider/yard/object/class_object.rb +14 -0
  132. data/lib/inch/language/ruby/provider/yard/object/class_variable_object.rb +14 -0
  133. data/lib/inch/language/ruby/provider/yard/object/constant_object.rb +14 -0
  134. data/lib/inch/language/ruby/provider/yard/object/method_object.rb +170 -0
  135. data/lib/inch/language/ruby/provider/yard/object/method_parameter_object.rb +94 -0
  136. data/lib/inch/language/ruby/provider/yard/object/method_signature.rb +120 -0
  137. data/lib/inch/language/ruby/provider/yard/object/module_object.rb +14 -0
  138. data/lib/inch/language/ruby/provider/yard/object/namespace_object.rb +46 -0
  139. data/lib/inch/language/ruby/provider/yard/object/root_object.rb +14 -0
  140. data/lib/inch/language/ruby/provider/yard/parser.rb +71 -0
  141. data/lib/inch/language/ruby/roles/base.rb +17 -0
  142. data/lib/inch/language/ruby/roles/class_variable.rb +53 -0
  143. data/lib/inch/language/ruby/roles/constant.rb +53 -0
  144. data/lib/inch/language/ruby/roles/method.rb +112 -0
  145. data/lib/inch/language/ruby/roles/method_parameter.rb +84 -0
  146. data/lib/inch/language/ruby/roles/missing.rb +24 -0
  147. data/lib/inch/language/ruby/roles/namespace.rb +89 -0
  148. data/lib/inch/language/ruby/roles/object.rb +120 -0
  149. data/lib/inch/rake.rb +2 -2
  150. data/lib/inch/rake/suggest.rb +7 -2
  151. data/lib/inch/utils/buffered_ui.rb +16 -0
  152. data/lib/inch/utils/shell_helper.rb +1 -1
  153. data/lib/inch/utils/ui.rb +5 -5
  154. data/lib/inch/utils/weighted_list.rb +2 -3
  155. data/lib/inch/version.rb +1 -1
  156. data/test/fixtures/{code_examples → ruby/code_examples}/lib/foo.rb +0 -0
  157. data/test/fixtures/{diff1 → ruby/diff1}/lib/diff1.rb +0 -0
  158. data/test/fixtures/{diff2 → ruby/diff2}/lib/diff2.rb +0 -0
  159. data/test/fixtures/{inch-yml → ruby/inch-yml}/.inch.yml +0 -0
  160. data/test/fixtures/{inch-yml → ruby/inch-yml}/foo/bar.rb +0 -0
  161. data/test/fixtures/{inch-yml → ruby/inch-yml}/foo/vendor/base.rb +0 -0
  162. data/test/fixtures/{parameters → ruby/parameters}/lib/foo.rb +0 -0
  163. data/test/fixtures/{readme → ruby/readme}/lib/foo.rb +0 -0
  164. data/test/fixtures/{really_good → ruby/really_good}/lib/foo.rb +0 -0
  165. data/test/fixtures/{really_good_pedantic → ruby/really_good_pedantic}/lib/foo.rb +0 -0
  166. data/test/fixtures/{simple → ruby/simple}/README +0 -0
  167. data/test/fixtures/{simple → ruby/simple}/lib/broken.rb +11 -19
  168. data/test/fixtures/ruby/simple/lib/broken_ruby_2_0_features.rb +7 -0
  169. data/test/fixtures/ruby/simple/lib/directives.rb +8 -0
  170. data/test/fixtures/{simple → ruby/simple}/lib/foo.rb +22 -1
  171. data/test/fixtures/{simple → ruby/simple}/lib/nodoc.rb +0 -0
  172. data/test/fixtures/{simple → ruby/simple}/lib/role_methods.rb +0 -0
  173. data/test/fixtures/{simple → ruby/simple}/lib/role_namespaces.rb +0 -0
  174. data/test/fixtures/{visibility → ruby/visibility}/lib/foo.rb +0 -0
  175. data/test/fixtures/{yardopts → ruby/yardopts}/.yardopts +0 -0
  176. data/test/fixtures/{yardopts → ruby/yardopts}/foo/bar.rb +0 -0
  177. data/test/integration/api/compare/codebases.rb +3 -3
  178. data/test/integration/cli/command/console_test.rb +6 -6
  179. data/test/integration/cli/command/diff_test.rb +62 -0
  180. data/test/integration/cli/command/inspect_test.rb +5 -6
  181. data/test/integration/cli/command/list_test.rb +4 -5
  182. data/test/integration/cli/command/show_test.rb +5 -5
  183. data/test/integration/cli/command/stats_test.rb +3 -3
  184. data/test/integration/cli/command/suggest_test.rb +13 -13
  185. data/test/integration/stats_options_test.rb +4 -4
  186. data/test/integration/visibility_options_test.rb +14 -14
  187. data/test/shared/base_list.rb +4 -3
  188. data/test/test_helper.rb +13 -13
  189. data/test/unit/api/filter_test.rb +7 -7
  190. data/test/unit/api/get_test.rb +1 -1
  191. data/test/unit/api/list_test.rb +1 -1
  192. data/test/unit/api/options/base_test.rb +3 -3
  193. data/test/unit/api/stats_test.rb +1 -1
  194. data/test/unit/api/suggest_test.rb +3 -3
  195. data/test/unit/cli/arguments_test.rb +2 -2
  196. data/test/unit/cli/command/base_test.rb +1 -1
  197. data/test/unit/cli/command/options/base_list_test.rb +2 -2
  198. data/test/unit/cli/command/options/base_object_test.rb +1 -1
  199. data/test/unit/cli/command/options/base_test.rb +1 -1
  200. data/test/unit/cli/command_parser_test.rb +3 -3
  201. data/test/unit/cli/trace_helper_test.rb +1 -1
  202. data/test/unit/cli/yardopts_helper_test.rb +6 -5
  203. data/test/unit/code_object/converter_test.rb +1 -1
  204. data/test/unit/code_object/provider_test.rb +2 -2
  205. data/test/unit/code_object/proxy_test.rb +2 -2
  206. data/test/unit/codebase/objects_test.rb +2 -2
  207. data/test/unit/codebase/proxy_test.rb +7 -5
  208. data/test/unit/config/codebase_test.rb +4 -4
  209. data/test/unit/config_test.rb +28 -0
  210. data/test/unit/evaluation/{role/base_test.rb → role_test.rb} +7 -7
  211. data/test/unit/{code_object/proxy → language/ruby/code_object}/method_object_test.rb +66 -21
  212. data/test/unit/{code_object → language/ruby}/provider/yard/docstring_test.rb +74 -38
  213. data/test/unit/{code_object → language/ruby}/provider/yard/nodoc_helper_test.rb +9 -7
  214. data/test/unit/{code_object → language/ruby}/provider/yard/object/method_object_test.rb +21 -11
  215. data/test/unit/language/ruby/provider/yard_test.rb +25 -0
  216. data/test/unit/utils/buffered_ui_test.rb +48 -0
  217. data/test/unit/utils/ui_test.rb +7 -7
  218. data/test/unit/utils/weighted_list_test.rb +7 -4
  219. metadata +139 -93
  220. data/lib/inch/code_object/provider/yard.rb +0 -57
  221. data/lib/inch/code_object/provider/yard/docstring.rb +0 -133
  222. data/lib/inch/code_object/provider/yard/nodoc_helper.rb +0 -93
  223. data/lib/inch/code_object/provider/yard/object.rb +0 -60
  224. data/lib/inch/code_object/provider/yard/object/base.rb +0 -302
  225. data/lib/inch/code_object/provider/yard/object/class_object.rb +0 -12
  226. data/lib/inch/code_object/provider/yard/object/class_variable_object.rb +0 -12
  227. data/lib/inch/code_object/provider/yard/object/constant_object.rb +0 -12
  228. data/lib/inch/code_object/provider/yard/object/method_object.rb +0 -153
  229. data/lib/inch/code_object/provider/yard/object/method_parameter_object.rb +0 -88
  230. data/lib/inch/code_object/provider/yard/object/method_signature.rb +0 -114
  231. data/lib/inch/code_object/provider/yard/object/module_object.rb +0 -12
  232. data/lib/inch/code_object/provider/yard/object/namespace_object.rb +0 -47
  233. data/lib/inch/code_object/provider/yard/object/root_object.rb +0 -12
  234. data/lib/inch/code_object/provider/yard/parser.rb +0 -62
  235. data/lib/inch/code_object/proxy/base.rb +0 -241
  236. data/lib/inch/code_object/proxy/class_object.rb +0 -8
  237. data/lib/inch/code_object/proxy/class_variable_object.rb +0 -8
  238. data/lib/inch/code_object/proxy/constant_object.rb +0 -8
  239. data/lib/inch/code_object/proxy/method_object.rb +0 -82
  240. data/lib/inch/code_object/proxy/method_parameter_object.rb +0 -60
  241. data/lib/inch/code_object/proxy/module_object.rb +0 -8
  242. data/lib/inch/code_object/proxy/namespace_object.rb +0 -33
  243. data/lib/inch/evaluation/object_schema.rb +0 -30
  244. data/lib/inch/evaluation/proxy/base.rb +0 -164
  245. data/lib/inch/evaluation/proxy/class_object.rb +0 -8
  246. data/lib/inch/evaluation/proxy/class_variable_object.rb +0 -19
  247. data/lib/inch/evaluation/proxy/constant_object.rb +0 -19
  248. data/lib/inch/evaluation/proxy/method_object.rb +0 -65
  249. data/lib/inch/evaluation/proxy/module_object.rb +0 -8
  250. data/lib/inch/evaluation/proxy/namespace_object.rb +0 -27
  251. data/lib/inch/evaluation/role/base.rb +0 -92
  252. data/lib/inch/evaluation/role/class_variable.rb +0 -55
  253. data/lib/inch/evaluation/role/constant.rb +0 -55
  254. data/lib/inch/evaluation/role/method.rb +0 -126
  255. data/lib/inch/evaluation/role/method_parameter.rb +0 -91
  256. data/lib/inch/evaluation/role/missing.rb +0 -20
  257. data/lib/inch/evaluation/role/namespace.rb +0 -85
  258. data/lib/inch/evaluation/role/object.rb +0 -143
  259. data/test/unit/code_object/provider/yard_test.rb +0 -25
@@ -0,0 +1,97 @@
1
+ require 'English'
2
+
3
+ module Inch
4
+ module Language
5
+ module Ruby
6
+ module Provider
7
+ module YARD
8
+ module NodocHelper
9
+ # Returns true if the code object is somehow marked not to be
10
+ # documented.
11
+ #
12
+ # @note Doesnot recognize ":startdoc:" and ":stopdoc:"
13
+ #
14
+ def nodoc?
15
+ tagged_as_private? || nodoc_comment?
16
+ end
17
+
18
+ NO_DOC_REGEX = /#\s*\:nodoc\:/
19
+ NO_DOC_ALL_REGEX = /#\s*\:nodoc\:\s*all/
20
+ DOC_REGEX = /#\s*\:doc\:/
21
+
22
+ def nodoc_comment?
23
+ explicit_nodoc_comment? || implicit_nodoc_comment?
24
+ end
25
+
26
+ def explicit_nodoc_comment?
27
+ declarations.any? { |str| str =~ NO_DOC_REGEX }
28
+ end
29
+
30
+ def explicit_nodoc_all_comment?
31
+ declarations.any? { |str| str =~ NO_DOC_ALL_REGEX }
32
+ end
33
+
34
+ def explicit_doc_comment?
35
+ declarations.any? { |str| str =~ DOC_REGEX }
36
+ end
37
+
38
+ def implicit_nodoc_all_comment?
39
+ if parent
40
+ parent.explicit_nodoc_all_comment? ||
41
+ parent.implicit_nodoc_all_comment?
42
+ end
43
+ end
44
+
45
+ def implicit_nodoc_comment?
46
+ return false if explicit_doc_comment?
47
+
48
+ if parent
49
+ return false if parent.explicit_doc_comment?
50
+
51
+ if namespace?
52
+ if parent.explicit_nodoc_all_comment?
53
+ return true
54
+ else
55
+ return parent.implicit_nodoc_all_comment?
56
+ end
57
+ else
58
+ if parent.explicit_nodoc_comment?
59
+ return true
60
+ else
61
+ return parent.implicit_nodoc_all_comment?
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ # Returns all lines in all files declaring the object
68
+ #
69
+ # @example
70
+ # declarations # => ["class Base # :nodoc:", "class Foo < Base"]
71
+ #
72
+ # @return [Array<String>]
73
+ def declarations
74
+ @declarations ||= files.map do |f|
75
+ get_line_no(f.filename, f.line_no)
76
+ end
77
+ end
78
+
79
+ # Returns a +line_number+ from a file
80
+ #
81
+ # @param filename [String]
82
+ # @param line_number [Fixnum]
83
+ # @return [String]
84
+ def get_line_no(filename, line_number)
85
+ f = File.open(filename)
86
+ line_number.times { f.gets }
87
+ result = $LAST_READ_LINE
88
+ f.close
89
+ result.encode("UTF-8", "binary",
90
+ invalid: :replace, undef: :replace, replace: "")
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,63 @@
1
+ module Inch
2
+ module Language
3
+ module Ruby
4
+ module Provider
5
+ module YARD
6
+ # CodeObject::Provider::YARD::Object object represent code objects.
7
+ #
8
+ module Object
9
+ class << self
10
+ def clear_cache
11
+ @cache = {}
12
+ end
13
+
14
+ # Returns a Proxy object for the given +yard_object+
15
+ #
16
+ # @param yard_object [YARD::CodeObject]
17
+ # @return [Provider::YARD::Object]
18
+ def for(yard_object)
19
+ @cache ||= {}
20
+ if (proxy_object = @cache[cache_key(yard_object)])
21
+ proxy_object
22
+ else
23
+ @cache[cache_key(yard_object)] =
24
+ class_for(yard_object).new(yard_object)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ # Returns a Proxy class for the given +yard_object+
31
+ #
32
+ # @param yard_object [YARD::CodeObject]
33
+ # @return [Class]
34
+ def class_for(yard_object)
35
+ class_name = yard_object.class.to_s.split("::").last
36
+ const_get(class_name)
37
+ rescue NameError
38
+ Base
39
+ end
40
+
41
+ # Returns a cache key for the given +yard_object+
42
+ #
43
+ # @param yard_object [YARD::CodeObject]
44
+ # @return [String]
45
+ def cache_key(yard_object)
46
+ yard_object.path
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ require "inch/language/ruby/provider/yard/object/base"
57
+ require "inch/language/ruby/provider/yard/object/namespace_object"
58
+ require "inch/language/ruby/provider/yard/object/class_object"
59
+ require "inch/language/ruby/provider/yard/object/class_variable_object"
60
+ require "inch/language/ruby/provider/yard/object/constant_object"
61
+ require "inch/language/ruby/provider/yard/object/method_object"
62
+ require "inch/language/ruby/provider/yard/object/method_parameter_object"
63
+ require "inch/language/ruby/provider/yard/object/module_object"
@@ -0,0 +1,325 @@
1
+ require "forwardable"
2
+
3
+ module Inch
4
+ module Language
5
+ module Ruby
6
+ module Provider
7
+ module YARD
8
+ module Object
9
+ # @abstract
10
+ class Base
11
+ extend Forwardable
12
+ include YARD::NodocHelper
13
+
14
+ # @return [YARD::CodeObjects::Base] the actual (YARD) code object
15
+ attr_reader :object
16
+
17
+ # @return [String] the codebase's directory
18
+ attr_accessor :base_dir
19
+
20
+ # Tags considered by wrapper methods like {#has_code_example?}
21
+ CONSIDERED_YARD_TAGS = %w(api example param private return since)
22
+ AUTO_GENERATED_TAG_NAMES = %w(raise yield)
23
+
24
+ # convenient shortcuts to (YARD) code object
25
+ def_delegators :object,
26
+ :type, :namespace, :source, :source_type, :group,
27
+ :dynamic, :visibility
28
+
29
+ # @param object [YARD::CodeObjects::Base] the actual (YARD) code
30
+ # object
31
+ def initialize(object)
32
+ @object = object
33
+ @api_tag = __api_tag
34
+ @parent = __parent
35
+ @private_tag = __private_tag
36
+ end
37
+
38
+ # Returns the fullname of the object that the current object
39
+ # is an alias for
40
+ attr_accessor :aliased_object_fullname
41
+
42
+ # Returns the fullnames of the objects that are aliases
43
+ # for the current object
44
+ def aliases_fullnames
45
+ []
46
+ end
47
+
48
+ def api_tag?
49
+ !api_tag.nil?
50
+ end
51
+
52
+ attr_reader :api_tag
53
+
54
+ # To be overridden
55
+ # @see Proxy::NamespaceObject
56
+ # @return [CodeObject::Proxy,nil] the child inside the current
57
+ # object or +nil+
58
+ def child(_name)
59
+ nil
60
+ end
61
+
62
+ # @return [Array,nil] the full names of the children of the current
63
+ # object
64
+ def children_fullnames
65
+ []
66
+ end
67
+
68
+ # To be overridden
69
+ # @see Proxy::NamespaceObject
70
+ def children
71
+ []
72
+ end
73
+
74
+ RUBY_CORE = %w(
75
+ Array Bignum BasicObject Object Module Class Complex NilClass
76
+ Numeric String Float Fiber FiberError Continuation Dir File
77
+ Encoding Enumerator StopIteration Enumerator::Generator
78
+ Enumerator::Yielder Exception SystemExit SignalException Interrupt
79
+ StandardError TypeError ArgumentError IndexError KeyError
80
+ RangeError ScriptError SyntaxError LoadError NotImplementedError
81
+ NameError NoMethodError RuntimeError SecurityError NoMemoryError
82
+ EncodingError SystemCallError Encoding::CompatibilityError
83
+ File::Stat IO Hash ENV IOError EOFError ARGF RubyVM
84
+ RubyVM::InstructionSequence Math::DomainError ZeroDivisionError
85
+ FloatDomainError Integer Fixnum Data TrueClass FalseClass Mutex
86
+ Thread Proc LocalJumpError SystemStackError Method UnboundMethod
87
+ Binding Process::Status Random Range Rational RegexpError Regexp
88
+ MatchData Symbol Struct ThreadGroup ThreadError Time
89
+ Encoding::UndefinedConversionError
90
+ Encoding::InvalidByteSequenceError
91
+ Encoding::ConverterNotFoundError Encoding::Converter RubyVM::Env
92
+
93
+ Comparable Kernel File::Constants Enumerable Errno FileTest GC
94
+ ObjectSpace GC::Profiler IO::WaitReadable IO::WaitWritable Marshal
95
+ Math Process Process::UID Process::GID Process::Sys Signal
96
+ )
97
+ def core?
98
+ RUBY_CORE.include?(name.to_s)
99
+ end
100
+
101
+ # @return [Docstring]
102
+ def docstring
103
+ @docstring ||= Docstring.new(object.docstring)
104
+ end
105
+
106
+ # Returns all files declaring the object in the form of an Array of
107
+ # Arrays containing the location of their declaration.
108
+ #
109
+ # @return [Array<CodeLocation>]
110
+ def files
111
+ object.files.map do |(filename, line_no)|
112
+ CodeLocation.new(base_dir, filename, line_no)
113
+ end
114
+ rescue ::YARD::CodeObjects::ProxyMethodError
115
+ # this error is raised by YARD
116
+ # see broken.rb in test fixtures
117
+ []
118
+ end
119
+
120
+ # CodeLocation is a utility class to find declarations of objects in
121
+ # files
122
+ class CodeLocation < Struct.new(:base_dir, :relative_path, :line_no)
123
+ def filename
124
+ File.join(base_dir, relative_path)
125
+ end
126
+ end
127
+
128
+ # Returns the name of the file where the object is declared first
129
+ # @return [String] a filename
130
+ def filename
131
+ # just checking the first file (which is the file where an object
132
+ # is first declared)
133
+ files.first && files.first.filename
134
+ end
135
+
136
+ def fullname
137
+ @fullname ||= object.path
138
+ end
139
+
140
+ def name
141
+ @name ||= object.name
142
+ end
143
+
144
+ def has_children?
145
+ !children.empty?
146
+ end
147
+
148
+ def has_code_example?
149
+ !tags(:example).empty? ||
150
+ docstring.contains_code_example?
151
+ end
152
+
153
+ def has_doc?
154
+ !docstring.empty?
155
+ end
156
+
157
+ def has_multiple_code_examples?
158
+ if tags(:example).size > 1 || docstring.code_examples.size > 1
159
+ true
160
+ else
161
+ if (tag = tag(:example))
162
+ multi_code_examples?(tag.text)
163
+ elsif (text = docstring.code_examples.first)
164
+ multi_code_examples?(text)
165
+ else
166
+ false
167
+ end
168
+ end
169
+ end
170
+
171
+ def has_unconsidered_tags?
172
+ !unconsidered_tags.empty?
173
+ end
174
+
175
+ def in_root?
176
+ depth == 1
177
+ end
178
+
179
+ # The depth of the following is 4:
180
+ #
181
+ # Foo::Bar::Baz#initialize
182
+ # ^ ^ ^ ^
183
+ # 1 << 2 << 3 << 4
184
+ #
185
+ # +depth+ answers the question "how many layers of code objects are
186
+ # above this one?"
187
+ #
188
+ # @note top-level counts, that's why Foo has depth 1!
189
+ #
190
+ # @return [Fixnum] the depth of the object in terms of namespace
191
+ def depth
192
+ @__depth ||= __depth
193
+ end
194
+
195
+ # @return [Boolean] +true+ if the object represents a method
196
+ def method?
197
+ false
198
+ end
199
+
200
+ # @return [Boolean] +true+ if the object represents a namespace
201
+ def namespace?
202
+ false
203
+ end
204
+
205
+ # @return [String] the documentation comments
206
+ def original_docstring
207
+ object.docstring.all.to_s
208
+ end
209
+
210
+ def parameters
211
+ []
212
+ end
213
+
214
+ # @return [Array,nil] the parent of the current object or +nil+
215
+ attr_reader :parent
216
+
217
+ def __parent
218
+ YARD::Object.for(object.parent) if object.parent
219
+ end
220
+
221
+ def private?
222
+ visibility == :private
223
+ end
224
+
225
+ def tagged_as_internal_api?
226
+ private_api_tag? || docstring.describes_internal_api?
227
+ end
228
+
229
+ def tagged_as_private?
230
+ private_tag? || docstring.describes_private_object?
231
+ end
232
+
233
+ def protected?
234
+ visibility == :protected
235
+ end
236
+
237
+ def public?
238
+ visibility == :public
239
+ end
240
+
241
+ def in_root?
242
+ depth == 1
243
+ end
244
+
245
+ # @return [Boolean] +true+ if the object has no documentation at all
246
+ def undocumented?
247
+ original_docstring.empty?
248
+ end
249
+
250
+ def unconsidered_tag_count
251
+ unconsidered_tags.size
252
+ end
253
+
254
+ def inspect
255
+ "#<#{self.class}: #{fullname}>"
256
+ end
257
+
258
+ protected
259
+
260
+ def multi_code_examples?(text)
261
+ text.scan(/\b(#{Regexp.escape(name)})[^_0-9\!\?]/m).size > 1
262
+ end
263
+
264
+ # @return [Boolean]
265
+ # +true+ if the object or its parent is tagged as @private
266
+ def private_tag?
267
+ !private_tag.nil?
268
+ end
269
+
270
+ attr_reader :private_tag
271
+
272
+ def private_api_tag?
273
+ api_tag && api_tag.text == "private"
274
+ end
275
+
276
+ def tag(name)
277
+ tags(name).first
278
+ end
279
+
280
+ def tags(name = nil)
281
+ object.tags(name)
282
+ rescue ::YARD::CodeObjects::ProxyMethodError
283
+ # this error is raised by YARD
284
+ # see broken.rb in test fixtures
285
+ []
286
+ end
287
+
288
+ # @return [Array]
289
+ # YARD tags that are not already covered by other wrapper methods
290
+ def unconsidered_tags
291
+ @unconsidered_tags ||= tags.reject do |tag|
292
+ auto_generated_tag?(tag) ||
293
+ CONSIDERED_YARD_TAGS.include?(tag.tag_name)
294
+ end
295
+ end
296
+
297
+ def __depth(i = 0)
298
+ if parent
299
+ parent.__depth(i + 1)
300
+ else
301
+ i
302
+ end
303
+ end
304
+
305
+ private
306
+
307
+ def __api_tag
308
+ tag(:api) || (parent && parent.api_tag)
309
+ end
310
+
311
+ def __private_tag
312
+ tag(:private) || (parent && parent.private_tag)
313
+ end
314
+
315
+ def auto_generated_tag?(tag)
316
+ tag.text.to_s.empty? &&
317
+ AUTO_GENERATED_TAG_NAMES.include?(tag.tag_name)
318
+ end
319
+ end
320
+ end
321
+ end
322
+ end
323
+ end
324
+ end
325
+ end