rbs 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ruby.yml +28 -0
  3. data/.gitignore +12 -0
  4. data/.rubocop.yml +15 -0
  5. data/BSDL +22 -0
  6. data/CHANGELOG.md +9 -0
  7. data/COPYING +56 -0
  8. data/Gemfile +6 -0
  9. data/README.md +93 -0
  10. data/Rakefile +142 -0
  11. data/bin/annotate-with-rdoc +157 -0
  12. data/bin/console +14 -0
  13. data/bin/query-rdoc +103 -0
  14. data/bin/setup +10 -0
  15. data/bin/sort +89 -0
  16. data/bin/test_runner.rb +16 -0
  17. data/docs/CONTRIBUTING.md +97 -0
  18. data/docs/sigs.md +148 -0
  19. data/docs/stdlib.md +152 -0
  20. data/docs/syntax.md +528 -0
  21. data/exe/rbs +7 -0
  22. data/lib/rbs.rb +64 -0
  23. data/lib/rbs/ast/annotation.rb +27 -0
  24. data/lib/rbs/ast/comment.rb +27 -0
  25. data/lib/rbs/ast/declarations.rb +395 -0
  26. data/lib/rbs/ast/members.rb +362 -0
  27. data/lib/rbs/buffer.rb +50 -0
  28. data/lib/rbs/builtin_names.rb +55 -0
  29. data/lib/rbs/cli.rb +558 -0
  30. data/lib/rbs/constant.rb +26 -0
  31. data/lib/rbs/constant_table.rb +150 -0
  32. data/lib/rbs/definition.rb +170 -0
  33. data/lib/rbs/definition_builder.rb +919 -0
  34. data/lib/rbs/environment.rb +281 -0
  35. data/lib/rbs/environment_loader.rb +136 -0
  36. data/lib/rbs/environment_walker.rb +124 -0
  37. data/lib/rbs/errors.rb +187 -0
  38. data/lib/rbs/location.rb +102 -0
  39. data/lib/rbs/method_type.rb +123 -0
  40. data/lib/rbs/namespace.rb +91 -0
  41. data/lib/rbs/parser.y +1344 -0
  42. data/lib/rbs/prototype/rb.rb +553 -0
  43. data/lib/rbs/prototype/rbi.rb +587 -0
  44. data/lib/rbs/prototype/runtime.rb +381 -0
  45. data/lib/rbs/substitution.rb +46 -0
  46. data/lib/rbs/test.rb +26 -0
  47. data/lib/rbs/test/errors.rb +61 -0
  48. data/lib/rbs/test/hook.rb +294 -0
  49. data/lib/rbs/test/setup.rb +58 -0
  50. data/lib/rbs/test/spy.rb +325 -0
  51. data/lib/rbs/test/test_helper.rb +183 -0
  52. data/lib/rbs/test/type_check.rb +254 -0
  53. data/lib/rbs/type_name.rb +70 -0
  54. data/lib/rbs/types.rb +936 -0
  55. data/lib/rbs/variance_calculator.rb +138 -0
  56. data/lib/rbs/vendorer.rb +47 -0
  57. data/lib/rbs/version.rb +3 -0
  58. data/lib/rbs/writer.rb +269 -0
  59. data/lib/ruby/signature.rb +7 -0
  60. data/rbs.gemspec +46 -0
  61. data/stdlib/abbrev/abbrev.rbs +60 -0
  62. data/stdlib/base64/base64.rbs +71 -0
  63. data/stdlib/benchmark/benchmark.rbs +372 -0
  64. data/stdlib/builtin/array.rbs +1997 -0
  65. data/stdlib/builtin/basic_object.rbs +280 -0
  66. data/stdlib/builtin/binding.rbs +177 -0
  67. data/stdlib/builtin/builtin.rbs +45 -0
  68. data/stdlib/builtin/class.rbs +145 -0
  69. data/stdlib/builtin/comparable.rbs +116 -0
  70. data/stdlib/builtin/complex.rbs +400 -0
  71. data/stdlib/builtin/constants.rbs +37 -0
  72. data/stdlib/builtin/data.rbs +5 -0
  73. data/stdlib/builtin/deprecated.rbs +2 -0
  74. data/stdlib/builtin/dir.rbs +413 -0
  75. data/stdlib/builtin/encoding.rbs +607 -0
  76. data/stdlib/builtin/enumerable.rbs +404 -0
  77. data/stdlib/builtin/enumerator.rbs +260 -0
  78. data/stdlib/builtin/errno.rbs +781 -0
  79. data/stdlib/builtin/errors.rbs +582 -0
  80. data/stdlib/builtin/exception.rbs +194 -0
  81. data/stdlib/builtin/false_class.rbs +40 -0
  82. data/stdlib/builtin/fiber.rbs +68 -0
  83. data/stdlib/builtin/fiber_error.rbs +12 -0
  84. data/stdlib/builtin/file.rbs +1076 -0
  85. data/stdlib/builtin/file_test.rbs +59 -0
  86. data/stdlib/builtin/float.rbs +696 -0
  87. data/stdlib/builtin/gc.rbs +243 -0
  88. data/stdlib/builtin/hash.rbs +1029 -0
  89. data/stdlib/builtin/integer.rbs +707 -0
  90. data/stdlib/builtin/io.rbs +683 -0
  91. data/stdlib/builtin/kernel.rbs +576 -0
  92. data/stdlib/builtin/marshal.rbs +161 -0
  93. data/stdlib/builtin/match_data.rbs +271 -0
  94. data/stdlib/builtin/math.rbs +369 -0
  95. data/stdlib/builtin/method.rbs +185 -0
  96. data/stdlib/builtin/module.rbs +1104 -0
  97. data/stdlib/builtin/nil_class.rbs +82 -0
  98. data/stdlib/builtin/numeric.rbs +409 -0
  99. data/stdlib/builtin/object.rbs +824 -0
  100. data/stdlib/builtin/proc.rbs +429 -0
  101. data/stdlib/builtin/process.rbs +1227 -0
  102. data/stdlib/builtin/random.rbs +267 -0
  103. data/stdlib/builtin/range.rbs +226 -0
  104. data/stdlib/builtin/rational.rbs +424 -0
  105. data/stdlib/builtin/rb_config.rbs +57 -0
  106. data/stdlib/builtin/regexp.rbs +1083 -0
  107. data/stdlib/builtin/ruby_vm.rbs +14 -0
  108. data/stdlib/builtin/signal.rbs +55 -0
  109. data/stdlib/builtin/string.rbs +1901 -0
  110. data/stdlib/builtin/string_io.rbs +284 -0
  111. data/stdlib/builtin/struct.rbs +40 -0
  112. data/stdlib/builtin/symbol.rbs +228 -0
  113. data/stdlib/builtin/thread.rbs +1108 -0
  114. data/stdlib/builtin/thread_group.rbs +23 -0
  115. data/stdlib/builtin/time.rbs +1047 -0
  116. data/stdlib/builtin/trace_point.rbs +290 -0
  117. data/stdlib/builtin/true_class.rbs +46 -0
  118. data/stdlib/builtin/unbound_method.rbs +153 -0
  119. data/stdlib/builtin/warning.rbs +17 -0
  120. data/stdlib/coverage/coverage.rbs +62 -0
  121. data/stdlib/csv/csv.rbs +773 -0
  122. data/stdlib/erb/erb.rbs +392 -0
  123. data/stdlib/find/find.rbs +40 -0
  124. data/stdlib/ipaddr/ipaddr.rbs +247 -0
  125. data/stdlib/json/json.rbs +335 -0
  126. data/stdlib/pathname/pathname.rbs +1093 -0
  127. data/stdlib/prime/integer-extension.rbs +23 -0
  128. data/stdlib/prime/prime.rbs +188 -0
  129. data/stdlib/securerandom/securerandom.rbs +9 -0
  130. data/stdlib/set/set.rbs +301 -0
  131. data/stdlib/tmpdir/tmpdir.rbs +53 -0
  132. metadata +292 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2559e695400fc8d1c360aa80b1a9065f5ea0ae24b59faa91907102f7fc46d572
4
+ data.tar.gz: e32722dcf716317553827e15b3232e8971f3d613c3b2bd7f71a5b902e2fef8b3
5
+ SHA512:
6
+ metadata.gz: c26e77f6a2506cf1414d12aa74e1783bf8f7ca40d5e803831c9deb2e8b290954db119bb89fa8d26a24b76e8713b26fdd6535e557e59788808addbc3e96a07ec8
7
+ data.tar.gz: 10a1e4a16e16f627bda62c565258b84bb830c7a85b0d74acbd9187094626b3e76eca4f8bacc5d77758755bfa82d5dacdd7dc6a374b53f3778d0421b449013d40
@@ -0,0 +1,28 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request: {}
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: "ubuntu-latest"
12
+ strategy:
13
+ matrix:
14
+ container_tag:
15
+ - master-nightly-bionic
16
+ - 2.6.5-bionic
17
+ - 2.7.0-bionic
18
+ container:
19
+ image: rubylang/ruby:${{ matrix.container_tag }}
20
+ steps:
21
+ - uses: actions/checkout@v1
22
+ - name: Run test
23
+ run: |
24
+ ruby -v
25
+ gem install bundler
26
+ bin/setup
27
+ bundle exec rake
28
+ bundle exec rake build
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /pkg/
6
+ /spec/reports/
7
+ /tmp/
8
+ /Gemfile.lock
9
+ /.ruby-version
10
+ /lib/rbs/parser.rb
11
+ /lib/rbs/parser.output
12
+ /vendor/sigs
@@ -0,0 +1,15 @@
1
+ require: rubocop-rubycw
2
+ AllCops:
3
+ TargetRubyVersion: 2.7
4
+ DisabledByDefault: true
5
+ Exclude:
6
+ - 'vendor/bundle/**/*'
7
+ Rubycw/Rubycw:
8
+ Enabled: true
9
+ Exclude:
10
+ - 'test/**/*_test.rb'
11
+
12
+ Lint/DuplicateMethods:
13
+ Enabled: true
14
+ Include:
15
+ - 'test/**/*_test.rb'
data/BSDL ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (C) 2019 Soutaro Matsumoto. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions
5
+ are met:
6
+ 1. Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ 2. Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22
+ SUCH DAMAGE.
@@ -0,0 +1,9 @@
1
+ # CHANGELOG
2
+
3
+ ## master
4
+
5
+ * The first release of RBS gem.
6
+
7
+ ## 0.1.0
8
+
9
+ * Version 0.1.0 is the original `rbs` gem and it is different software from RBS.
data/COPYING ADDED
@@ -0,0 +1,56 @@
1
+ ruby-signature is copyrighted free software by Soutaro Matsumoto <matsumoto@soutaro.com>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a. place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b. use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c. give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d. make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a. distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b. accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c. give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d. make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ruby-signature.gemspec
4
+ gemspec
5
+
6
+ gem "ruby-signature-amber", path: "test/assets/test-gem"
@@ -0,0 +1,93 @@
1
+ # RBS
2
+
3
+ RBS provides syntax and semantics definition for the `Ruby Signature` language, `.rbs` files.
4
+ It consists of a parser, the syntax, and class definition interpreter, the semantics.
5
+
6
+ ## Build
7
+
8
+ We haven't published a gem yet.
9
+ You need to install the dependencies, and build its parser with `bin/setup`.
10
+
11
+ ```
12
+ $ bin/setup
13
+ $ bundle exec exe/rbs
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```
19
+ $ rbs list
20
+ $ rbs ancestors ::Object
21
+ $ rbs methods ::Object
22
+ $ rbs method ::Object tap
23
+ ```
24
+
25
+ ### rbs [--class|--module|interface] list
26
+
27
+ ```
28
+ $ rbs list
29
+ ```
30
+
31
+ This command lists all of the classes/modules/interfaces defined in `.rbs` files.
32
+
33
+ ### rbs ancestors [--singleton|--instance] CLASS
34
+
35
+ ```
36
+ $ rbs ancestors Array # ([].class.ancestors)
37
+ $ rbs ancestors --singleton Array # (Array.class.ancestors)
38
+ ```
39
+
40
+ This command prints the _ancestors_ of the class.
41
+ The name of the command is borrowed from `Class#ancestors`, but the semantics is a bit different.
42
+ The `ancestors` command is more precise (I believe).
43
+
44
+ ### rbs methods [--singleton|--instance] CLASS
45
+
46
+ ```
47
+ $ rbs methods ::Integer # 1.methods
48
+ $ rbs methods --singleton ::Object # Object.methods
49
+ ```
50
+
51
+ This command prints all methods provided for the class.
52
+
53
+ ### rbs method [--singleton|--instance] CLASS METHOD
54
+
55
+ ```
56
+ $ rbs method ::Integer '+' # 1+2
57
+ $ rbs method --singleton ::Object tap # Object.tap { ... }
58
+ ```
59
+
60
+ This command prints type and properties of the method.
61
+
62
+ ### Options
63
+
64
+ It accepts two global options, `-r` and `-I`.
65
+
66
+ `-r` is for libraries. You can specify the names of libraries.
67
+
68
+ ```
69
+ $ rbs -r set list
70
+ ```
71
+
72
+ `-I` is for application signatures. You can specify the name of directory.
73
+
74
+ ```
75
+ $ rbs -I sig list
76
+ ```
77
+
78
+ ## Guides
79
+
80
+ - [Standard library signature contribution guide](docs/CONTRIBUTING.md)
81
+ - [Writing signatures guide](docs/sigs.md)
82
+ - [Stdlib signatures guide](docs/stdlib.md)
83
+ - [Syntax](docs/syntax.md)
84
+
85
+ ## Development
86
+
87
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
88
+
89
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/ruby-signature.
@@ -0,0 +1,142 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"].reject do |path|
8
+ path =~ %r{test/stdlib/}
9
+ end
10
+ end
11
+
12
+ multitask :default => [:test, :stdlib_test, :rubocop, :validate]
13
+
14
+ task :validate => :parser do
15
+ sh "rbs validate"
16
+ end
17
+
18
+ FileList["test/stdlib/**/*_test.rb"].each do |test|
19
+ multitask test => :parser do
20
+ sh "ruby bin/test_runner.rb #{test}"
21
+ end
22
+ multitask stdlib_test: test
23
+ end
24
+
25
+ task :rubocop do
26
+ sh "rubocop --parallel"
27
+ end
28
+
29
+ rule ".rb" => ".y" do |t|
30
+ sh "racc -v -o #{t.name} #{t.source}"
31
+ end
32
+
33
+ task :parser => "lib/rbs/parser.rb"
34
+ task :test => :parser
35
+ task :stdlib_test => :parser
36
+ task :build => :parser
37
+
38
+ namespace :generate do
39
+ task :stdlib_test, [:class] do |_task, args|
40
+ klass = args.fetch(:class) do
41
+ raise "Class name is necessary. e.g. rake 'generate:stdlib_test[String]'"
42
+ end
43
+
44
+ path = Pathname("test/stdlib/#{klass}_test.rb")
45
+ raise "#{path} already exists!" if path.exist?
46
+
47
+ require "erb"
48
+ require "ruby/signature"
49
+
50
+ class TestTemplateBuilder
51
+ attr_reader :klass, :env
52
+
53
+ def initialize(klass)
54
+ @klass = klass
55
+
56
+ @env = Ruby::Signature::Environment.new
57
+ Ruby::Signature::EnvironmentLoader.new.load(env: @env)
58
+ end
59
+
60
+ def call
61
+ ERB.new(<<~ERB, trim_mode: "-").result(binding)
62
+ require_relative "test_helper"
63
+
64
+ class <%= klass %>Test < StdlibTest
65
+ target <%= klass %>
66
+ # library "pathname", "set", "securerandom" # Declare library signatures to load
67
+ using hook.refinement
68
+ <%- class_methods.each do |method_name, definition| %>
69
+ def test_class_method_<%= test_name_for(method_name) %>
70
+ <%- definition.method_types.each do |method_type| -%>
71
+ # <%= method_type %>
72
+ <%= klass %>.<%= method_name %>
73
+ <%- end -%>
74
+ end
75
+ <%- end -%>
76
+ <%- instance_methods.each do |method_name, definition| %>
77
+ def test_<%= test_name_for(method_name) %>
78
+ <%- definition.method_types.each do |method_type| -%>
79
+ # <%= method_type %>
80
+ <%= klass %>.new.<%= method_name %>
81
+ <%- end -%>
82
+ end
83
+ <%- end -%>
84
+ end
85
+ ERB
86
+ end
87
+
88
+ private
89
+
90
+ def test_name_for(method_name)
91
+ {
92
+ :== => 'double_equal',
93
+ :!= => 'not_equal',
94
+ :=== => 'triple_equal',
95
+ :[] => 'square_bracket',
96
+ :[]= => 'square_bracket_assign',
97
+ :> => 'greater_than',
98
+ :< => 'less_than',
99
+ :>= => 'greater_than_equal_to',
100
+ :<= => 'less_than_equal_to',
101
+ :<=> => 'spaceship',
102
+ :+ => 'plus',
103
+ :- => 'minus',
104
+ :* => 'multiply',
105
+ :/ => 'divide',
106
+ :** => 'power',
107
+ :% => 'modulus',
108
+ :& => 'and',
109
+ :| => 'or',
110
+ :^ => 'xor',
111
+ :>> => 'right_shift',
112
+ :<< => 'left_shift',
113
+ :=~ => 'pattern_match',
114
+ :!~ => 'does_not_match',
115
+ :~ => 'tilde'
116
+ }.fetch(method_name, method_name)
117
+ end
118
+
119
+ def type_name
120
+ @type_name ||= Ruby::Signature::TypeName.new(name: klass.to_sym, namespace: Ruby::Signature::Namespace.new(path: [], absolute: true))
121
+ end
122
+
123
+ def class_methods
124
+ @class_methods ||= Ruby::Signature::DefinitionBuilder.new(env: env).build_singleton(type_name).methods.select {|_, definition|
125
+ definition.implemented_in.name.absolute! == type_name
126
+ }
127
+ end
128
+
129
+ def instance_methods
130
+ @instance_methods ||= Ruby::Signature::DefinitionBuilder.new(env: env).build_instance(type_name).methods.select {|_, definition|
131
+ definition.implemented_in.name.absolute! == type_name
132
+ }
133
+ end
134
+ end
135
+
136
+ path.write TestTemplateBuilder.new(klass).call
137
+
138
+ puts "Created: #{path}"
139
+ end
140
+ end
141
+
142
+ CLEAN.include("lib/rbs/parser.rb")
@@ -0,0 +1,157 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rbs"
5
+ require "rdoc"
6
+
7
+ def store_for_class(name, stores:)
8
+ stores.find do |store|
9
+ store.find_class_named(name) || store.find_module_named(name)
10
+ end
11
+ end
12
+
13
+ def format_comment(comment)
14
+ out = RDoc::Markup::Document.new
15
+ out << comment
16
+ formatter = RDoc::Markup::ToMarkdown.new
17
+ out.accept(formatter)
18
+ end
19
+
20
+ def comment_for_constant(decl, stores:)
21
+ class_name = decl.name.namespace.to_type_name.to_s
22
+ klass = store_for_class(class_name, stores: stores)&.yield_self {|store|
23
+ store.find_class_named(class_name) || store.find_module_named(class_name)
24
+ }
25
+
26
+ if klass
27
+ constant = klass.constants.find do |const|
28
+ const.name == decl.name.name.to_s
29
+ end
30
+
31
+ if constant&.documented?
32
+ string = format_comment(constant.comment)
33
+ RBS::AST::Comment.new(location: nil, string: string)
34
+ end
35
+ end
36
+ end
37
+
38
+ def comment_for_class(decl, stores:)
39
+ name = decl.name.to_s
40
+ klass = store_for_class(name, stores: stores)&.yield_self {|store|
41
+ store.find_class_named(name) || store.find_module_named(name)
42
+ }
43
+
44
+ if klass&.documented?
45
+ string = format_comment(klass.comment)
46
+ RBS::AST::Comment.new(location: nil, string: string)
47
+ end
48
+ end
49
+
50
+ def comment_for_method(klass, method, stores:)
51
+ method = store_for_class(klass, stores: stores)&.load_method(klass, method)
52
+
53
+ if method&.documented?
54
+ out = RDoc::Markup::Document.new
55
+
56
+ out << method.comment
57
+
58
+ if method.arglists
59
+ out << RDoc::Markup::Heading.new(1, "arglists 💪👽🚨 << Delete this section")
60
+ arglists = method.arglists.chomp.split("\n").map {|line| line + "\n" }
61
+ out << RDoc::Markup::Verbatim.new(*arglists)
62
+ end
63
+
64
+ string = out.accept(RDoc::Markup::ToMarkdown.new)
65
+ RBS::AST::Comment.new(location: nil, string: string)
66
+ end
67
+
68
+ rescue RDoc::Store::MissingFileError
69
+ puts " 👺 No document found for #{klass}#{method}"
70
+ nil
71
+ end
72
+
73
+ if ARGV.empty?
74
+ puts 'annotate-with-rdoc [RBS files...]'
75
+ exit
76
+ end
77
+
78
+ def print_members(stores, klass_name, members)
79
+ members.each do |member|
80
+ case member
81
+ when RBS::AST::Members::MethodDefinition
82
+ puts " Processing #{member.name}..."
83
+
84
+ method_name = case
85
+ when member.instance?
86
+ "##{member.name}"
87
+ when member.singleton?
88
+ "::#{member.name}"
89
+ end
90
+
91
+ comment = comment_for_method(klass_name, method_name, stores: stores)
92
+
93
+ unless comment
94
+ if member.instance? && member.name == :initialize
95
+ comment = comment_for_method(klass_name, '::new', stores: stores)
96
+ end
97
+ end
98
+
99
+ member.instance_variable_set(:@comment, comment)
100
+ when RBS::AST::Members::AttrReader, RBS::AST::Members::AttrAccessor, RBS::AST::Members::AttrWriter
101
+ puts " 👻 Attributes not supported (#{klass_name})"
102
+ when RBS::AST::Members::Alias
103
+ puts " Processing #{member.new_name}(alias)..."
104
+ prefix = case
105
+ when member.instance?
106
+ "#"
107
+ when member.singleton?
108
+ "."
109
+ end
110
+ name = "#{prefix}#{member.new_name}"
111
+
112
+ comment = comment_for_method(klass_name, name, stores: stores)
113
+ member.instance_variable_set(:@comment, comment)
114
+ end
115
+ end
116
+ end
117
+
118
+ stores = []
119
+ RDoc::RI::Paths.each true, true, false, false do |path, type|
120
+ puts "Loading store from #{path}..."
121
+ store = RDoc::RI::Store.new(path, type)
122
+ store.load_all
123
+ stores << store
124
+ end
125
+
126
+ ARGV.map {|f| Pathname(f) }.each do |path|
127
+ puts "Opening #{path}..."
128
+
129
+ buffer = RBS::Buffer.new(name: path, content: path.read)
130
+ sigs = RBS::Parser.parse_signature(buffer)
131
+
132
+ sigs.each do |decl|
133
+ case decl
134
+ when RBS::AST::Declarations::Constant
135
+ puts " Importing documentation for #{decl.name}..."
136
+ comment = comment_for_constant(decl, stores: stores)
137
+ decl.instance_variable_set(:@comment, comment)
138
+ when RBS::AST::Declarations::Class, RBS::AST::Declarations::Module
139
+ puts " Importing documentation for #{decl.name}..."
140
+ comment = comment_for_class(decl, stores: stores)
141
+ decl.instance_variable_set(:@comment, comment)
142
+
143
+ print_members stores, decl.name.to_s, decl.members
144
+ when RBS::AST::Declarations::Extension
145
+ puts " Importing documentation for #{decl.name} (#{decl.extension_name})"
146
+
147
+ print_members stores, decl.name.to_s, decl.members
148
+ end
149
+ end
150
+
151
+ puts "Writing #{path}..."
152
+ path.open('w') do |out|
153
+ writer = RBS::Writer.new(out: out)
154
+ writer.write sigs
155
+ end
156
+ end
157
+