orthoses-yard 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d57da650c9fa9921fe11457ff26920168316e9eb43ff101cc9aedd6472c998e5
4
+ data.tar.gz: 96fab97f4f9ea79e773a5f0bd4eaf8fa5be3aac236be6819b74c89c9acf8a80f
5
+ SHA512:
6
+ metadata.gz: ed202e833e51fe62faf2b2c2d2554d5f14af1399dfca052f4fa38a06cda920232bcd66a46b1e2cd3b7683322527fddd6b3dbcc0504b4a99a0d996675253a33f9
7
+ data.tar.gz: 4af5a24ec753a2d92371deed81de517bc558b7c30a215986f9b1b138def44d65ffaa105c6853dc757f2d2a3fd9442434d19a95c50fa227688fd186840b84442a
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at co000ri@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in orthoses-yard.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rgot", "~> 1.1"
10
+ gem "rbs"
11
+ gem "steep"
12
+ gem "debug"
data/Gemfile.lock ADDED
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ orthoses-yard (0.1.0)
5
+ orthoses
6
+ yard
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (7.0.4)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 1.6, < 2)
14
+ minitest (>= 5.1)
15
+ tzinfo (~> 2.0)
16
+ ast (2.4.2)
17
+ concurrent-ruby (1.1.10)
18
+ debug (1.6.2)
19
+ irb (>= 1.3.6)
20
+ reline (>= 0.3.1)
21
+ ffi (1.15.5)
22
+ i18n (1.12.0)
23
+ concurrent-ruby (~> 1.0)
24
+ io-console (0.5.11)
25
+ irb (1.4.1)
26
+ reline (>= 0.3.0)
27
+ language_server-protocol (3.17.0.1)
28
+ listen (3.7.1)
29
+ rb-fsevent (~> 0.10, >= 0.10.3)
30
+ rb-inotify (~> 0.9, >= 0.9.10)
31
+ minitest (5.16.3)
32
+ orthoses (1.1.0)
33
+ rbs (~> 2.0)
34
+ parallel (1.22.1)
35
+ parser (3.1.2.1)
36
+ ast (~> 2.4.1)
37
+ rainbow (3.1.1)
38
+ rake (13.0.6)
39
+ rb-fsevent (0.11.2)
40
+ rb-inotify (0.10.1)
41
+ ffi (~> 1.0)
42
+ rbs (2.6.0)
43
+ reline (0.3.1)
44
+ io-console (~> 0.5)
45
+ rgot (1.1.0)
46
+ steep (1.1.1)
47
+ activesupport (>= 5.1)
48
+ language_server-protocol (>= 3.15, < 4.0)
49
+ listen (~> 3.0)
50
+ parallel (>= 1.0.0)
51
+ parser (>= 3.1)
52
+ rainbow (>= 2.2.2, < 4.0)
53
+ rbs (>= 2.3.2)
54
+ terminal-table (>= 2, < 4)
55
+ terminal-table (3.0.2)
56
+ unicode-display_width (>= 1.1.1, < 3)
57
+ tzinfo (2.0.5)
58
+ concurrent-ruby (~> 1.0)
59
+ unicode-display_width (2.3.0)
60
+ webrick (1.7.0)
61
+ yard (0.9.28)
62
+ webrick (~> 1.7.0)
63
+
64
+ PLATFORMS
65
+ arm64-darwin-21
66
+ x86_64-linux
67
+
68
+ DEPENDENCIES
69
+ debug
70
+ orthoses-yard!
71
+ rake (~> 13.0)
72
+ rbs
73
+ rgot (~> 1.1)
74
+ steep
75
+
76
+ BUNDLED WITH
77
+ 2.3.16
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 ksss
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # Orthoses::YARD
2
+
3
+ Orthoses extention for [YARD](https://github.com/lsegal/yard).
4
+
5
+ `Orthoses::YARD` automatically generate RBS from YARD comment for methods.
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add orthoses-yard
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install orthoses-yard
16
+
17
+ ## Example
18
+
19
+ from
20
+
21
+ ```rb
22
+ class Foo
23
+ # @param [Boolean] a
24
+ # @param [String, nil] b
25
+ # @param [Array<Float>] c
26
+ # @param [String] d
27
+ # @param [Symbol, nil] e
28
+ # @param [Hash{Symbol => String, nil}] f
29
+ # @return [void]
30
+ def foo(a, b = nil, *c, d:, e: nil, **f)
31
+ 1
32
+ end
33
+ end
34
+ ```
35
+
36
+ to
37
+
38
+ ```rbs
39
+ class Foo
40
+ # @param [Boolean] a
41
+ # @param [String, nil] b
42
+ # @param [Array<Float>] c
43
+ # @param [String] d
44
+ # @param [Symbol, nil] e
45
+ # @param [Hash{Symbol => String, nil}] f
46
+ # @return [void]
47
+ def foo: (bool a, ?String? b, *Array[Float] c, d: String, ?e: Symbol?, **Hash[Symbol, String?] f) -> void
48
+ end
49
+ ```
50
+
51
+ ## Usage
52
+
53
+ Please see https://github.com/ksss/orthoses-yard/blob/main/examples/yard/Rakefile
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ksss/orthoses-yard. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ksss/orthoses-yard/blob/main/CODE_OF_CONDUCT.md).
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
68
+
69
+ ## Code of Conduct
70
+
71
+ Everyone interacting in the Orthoses::Yard project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ksss/orthoses-yard/blob/main/CODE_OF_CONDUCT.md).
72
+
73
+ # TODO
74
+
75
+ - [ ] Add yard docstring
76
+ - [ ] Support @yieldparam, @yieldreturn tag
77
+ - [ ] Support @option tag
78
+ - [ ] Support interface (e.g. #read)
79
+ - [ ] Support generics
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "yard", path: "src"
6
+ gem "orthoses-yard", path: "../../"
7
+ gem "rake"
8
+ gem "rack"
9
+ gem "sord"
10
+ gem "debug"
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: ../..
3
+ specs:
4
+ orthoses-yard (0.1.0)
5
+ orthoses
6
+ yard
7
+
8
+ PATH
9
+ remote: src
10
+ specs:
11
+ yard (0.9.28)
12
+ webrick (~> 1.7.0)
13
+
14
+ GEM
15
+ remote: https://rubygems.org/
16
+ specs:
17
+ ast (2.4.2)
18
+ commander (4.6.0)
19
+ highline (~> 2.0.0)
20
+ debug (1.6.2)
21
+ irb (>= 1.3.6)
22
+ reline (>= 0.3.1)
23
+ highline (2.0.3)
24
+ io-console (0.5.11)
25
+ irb (1.4.1)
26
+ reline (>= 0.3.0)
27
+ orthoses (1.1.0)
28
+ rbs (~> 2.0)
29
+ parlour (5.0.0)
30
+ commander (~> 4.5)
31
+ parser
32
+ rainbow (~> 3.0)
33
+ sorbet-runtime (>= 0.5)
34
+ parser (3.1.2.1)
35
+ ast (~> 2.4.1)
36
+ rack (3.0.0)
37
+ rainbow (3.1.1)
38
+ rake (13.0.6)
39
+ rbs (2.6.0)
40
+ reline (0.3.1)
41
+ io-console (~> 0.5)
42
+ sorbet-runtime (0.5.10445)
43
+ sord (4.0.0)
44
+ commander (~> 4.5)
45
+ parlour (~> 5.0)
46
+ sorbet-runtime
47
+ yard
48
+ webrick (1.7.0)
49
+
50
+ PLATFORMS
51
+ arm64-darwin-21
52
+
53
+ DEPENDENCIES
54
+ debug
55
+ orthoses-yard!
56
+ rack
57
+ rake
58
+ sord
59
+ yard!
60
+
61
+ BUNDLED WITH
62
+ 2.4.0.dev
@@ -0,0 +1,7 @@
1
+ file :src do
2
+ sh "git clone --depth 1 https://github.com/lsegal/yard.git src"
3
+ end
4
+
5
+ task :generate => [:src] do
6
+ load 'generate.rb'
7
+ end
@@ -0,0 +1,71 @@
1
+ require 'orthoses-yard'
2
+ require 'fileutils'
3
+ require 'pathname'
4
+
5
+ FileUtils.rm_rf('out')
6
+ Orthoses.logger.level = :warn
7
+ Orthoses::Builder.new do
8
+ use Orthoses::CreateFileByName,
9
+ base_dir: 'out'
10
+ use Orthoses::Filter do |name, content|
11
+ name.start_with?('YARD') ||
12
+ name.start_with?('Ripper') ||
13
+ name.start_with?('OpenStruct') ||
14
+ name.start_with?('SymbolHash') ||
15
+ name.start_with?('Rake') ||
16
+ name.start_with?('WEBrick') ||
17
+ name.start_with?('RDoc')
18
+ end
19
+ use Orthoses::Tap do |store|
20
+ store['YARD'].header = 'module YARD'
21
+ store['YARD::CodeObjects'].header = 'module YARD::CodeObjects'
22
+ store['YARD::Handlers'].header = 'module YARD::Handlers'
23
+ store['YARD::Handlers::C'].header = 'module YARD::Handlers::C'
24
+ store['YARD::Handlers::Common'].header = 'module YARD::Handlers::Common'
25
+ store['YARD::Handlers::Ruby'].header = 'module YARD::Handlers::Ruby'
26
+ # TODO: support generics
27
+ store['YARD::Tags::Library'] << 'def self.labels: () -> SymbolHash'
28
+ end
29
+ use Orthoses::YARD,
30
+ globs: [
31
+ 'src/lib/yard.rb',
32
+ 'src/lib/yard/**/*.rb'
33
+ ]
34
+ use Orthoses::Autoload
35
+ run -> {
36
+ require 'yard'
37
+ YARD::Tags::Library.define_tag("YARD Tag Signature", 'yard.signature'.to_sym, nil)
38
+ YARD::Tags::Library.define_tag("YARD Tag", 'yard.tag'.to_sym, :with_types_and_name)
39
+ YARD::Tags::Library.define_tag("YARD Directive", 'yard.directive'.to_sym, :with_types_and_name)
40
+ # YARD::Tags::Library.visible_tags -= ['yard.tag'].map(&:to_sym)
41
+ }
42
+ end.call
43
+
44
+ Pathname("out").join("EXTERNAL_TODO.rbs").write(<<~RBS)
45
+ # !!! GENERATED CODE !!!
46
+ class OpenStruct
47
+ end
48
+
49
+ class Ripper
50
+ end
51
+
52
+ module Rake
53
+ class TaskLib
54
+ end
55
+ end
56
+
57
+
58
+ module WEBrick
59
+ module HTTPServlet
60
+ class AbstractServlet
61
+ end
62
+ end
63
+ end
64
+
65
+ module RDoc
66
+ module Markup
67
+ class ToHtml
68
+ end
69
+ end
70
+ end
71
+ RBS
@@ -0,0 +1,64 @@
1
+ ---
2
+ sources:
3
+ - name: ruby/gem_rbs_collection
4
+ remote: https://github.com/ruby/gem_rbs_collection.git
5
+ revision: main
6
+ repo_dir: gems
7
+ path: ".gem_rbs_collection"
8
+ gems:
9
+ - name: rbs
10
+ version: 2.6.0
11
+ source:
12
+ type: rubygems
13
+ - name: io-console
14
+ version: '0'
15
+ source:
16
+ type: stdlib
17
+ - name: orthoses
18
+ version: 1.1.0
19
+ source:
20
+ type: rubygems
21
+ - name: orthoses-yard
22
+ version: 0.1.0
23
+ source:
24
+ type: rubygems
25
+ - name: rack
26
+ version: 2.2.2
27
+ source:
28
+ type: git
29
+ name: ruby/gem_rbs_collection
30
+ revision: d5d79cf5cc4df61ccb4a28f3e744ea7970cfabf9
31
+ remote: https://github.com/ruby/gem_rbs_collection.git
32
+ repo_dir: gems
33
+ - name: logger
34
+ version: '0'
35
+ source:
36
+ type: stdlib
37
+ - name: set
38
+ version: '0'
39
+ source:
40
+ type: stdlib
41
+ - name: pathname
42
+ version: '0'
43
+ source:
44
+ type: stdlib
45
+ - name: json
46
+ version: '0'
47
+ source:
48
+ type: stdlib
49
+ - name: optparse
50
+ version: '0'
51
+ source:
52
+ type: stdlib
53
+ - name: rubygems
54
+ version: '0'
55
+ source:
56
+ type: stdlib
57
+ - name: tsort
58
+ version: '0'
59
+ source:
60
+ type: stdlib
61
+ - name: monitor
62
+ version: '0'
63
+ source:
64
+ type: stdlib
@@ -0,0 +1,12 @@
1
+ # Download sources
2
+ sources:
3
+ - name: ruby/gem_rbs_collection
4
+ remote: https://github.com/ruby/gem_rbs_collection.git
5
+ revision: main
6
+ repo_dir: gems
7
+
8
+ # A directory to install the downloaded RBSs
9
+ path: .gem_rbs_collection
10
+
11
+ gems:
12
+ - name: rbs
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ class YARD
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,390 @@
1
+ module Orthoses
2
+ class YARD
3
+ class YARD2RBS
4
+ class << self
5
+ def run(yardoc:, &block)
6
+ new(yardoc: yardoc, block: block).run
7
+ end
8
+ end
9
+
10
+ # @return [YARD::CodeObjects::t]
11
+ attr_reader :yardoc
12
+
13
+ # @return [Proc]
14
+ attr_reader :block
15
+
16
+ # @return [RBS::Types::Bases::Any]
17
+ attr_reader :untyped
18
+
19
+ # @return [RBS::Types::Bases::Void]
20
+ attr_reader :void
21
+
22
+ # @return [RBS::Types::Bases::Bool]
23
+ attr_reader :bool
24
+
25
+ def initialize(yardoc:, block:)
26
+ @yardoc = yardoc
27
+ @block = block
28
+
29
+ # statics
30
+ @untyped = ::RBS::Types::Bases::Any.new(location: nil)
31
+ @void = ::RBS::Types::Bases::Void.new(location: nil)
32
+ @bool = ::RBS::Types::Bases::Bool.new(location: nil)
33
+ end
34
+
35
+ # @return [void]
36
+ def run
37
+ Orthoses.logger.info("YARD will generate about #{yardoc.inspect}")
38
+ block.call(yardoc.path, yardoc.docstring, nil)
39
+ yardoc.children.each do |child|
40
+ case child.type
41
+ when :module, :class
42
+ self.class.new(yardoc: child, block: block).run
43
+ end
44
+ end
45
+ generate_for_attributes
46
+ generate_for_methods
47
+ generate_for_constants
48
+ end
49
+
50
+ # @return [void]
51
+ def generate_for_attributes
52
+ yardoc.attributes.each do |kind, attributes|
53
+ prefix = kind == :class ? 'self.' : ''
54
+ attributes.each do |name, class_attributes|
55
+ if class_attributes[:read] && class_attributes[:write]
56
+ visibility = class_attributes[:read].visibility == :private ? 'private ' : ''
57
+ type = tag_types_to_rbs_type(class_attributes[:read].tags('return').flat_map(&:types))
58
+ block.call(yardoc.path, class_attributes[:read].docstring, "#{visibility}attr_accessor #{prefix}#{name}: #{type}")
59
+ elsif class_attributes[:read]
60
+ visibility = class_attributes[:read].visibility == :private ? 'private ' : ''
61
+ type = tag_types_to_rbs_type(class_attributes[:read].tags('return').flat_map(&:types))
62
+ block.call(yardoc.path, class_attributes[:read].docstring, "#{visibility}attr_reader #{prefix}#{name}: #{type}")
63
+ elsif class_attributes[:write]
64
+ visibility = class_attributes[:write].visibility == :private ? 'private ' : ''
65
+ type = tag_types_to_rbs_type(class_attributes[:write].tags('return').flat_map(&:types))
66
+ block.call(yardoc.path, class_attributes[:write].docstring, "#{visibility}attr_writer #{prefix}#{name}: #{type}")
67
+ else
68
+ raise "bug"
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ # @return [void]
75
+ def generate_for_methods
76
+ yardoc.meths(inherited: false).each do |meth|
77
+ # skip attribute methods because of generate_for_attributes
78
+ next if meth.attr_info
79
+
80
+ # skip no tags methods
81
+ next if meth.tags.empty?
82
+
83
+ namespace = meth.namespace
84
+ method_name = meth.name
85
+ next if namespace.to_s.empty?
86
+
87
+ begin
88
+ mod = Object.const_get(namespace.to_s)
89
+ case meth.scope
90
+ when :class
91
+ prefix = 'self.'
92
+ method_object = mod.method(method_name)
93
+ when :instance
94
+ prefix = ''
95
+ method_object = mod.instance_method(method_name)
96
+ else
97
+ raise "bug"
98
+ end
99
+ rescue NameError
100
+ Orthoses.logger.warn("[YARD] skip #{meth.inspect} because cannot get method object")
101
+ next
102
+ end
103
+
104
+ required_positionals = []
105
+ optional_positionals = []
106
+ rest_positionals = nil
107
+ trailing_positionals = []
108
+ required_keywords = {}
109
+ optional_keywords = {}
110
+ rest_keywords = nil
111
+ yield_params = []
112
+ yield_return = nil
113
+
114
+ requireds = required_positionals
115
+
116
+ # @type var method_object: (Method | UnboundMethod)
117
+ method_object.parameters.each do |kind, name|
118
+ type = meth.tags("param")
119
+ .find { |tag| tag.name == name.to_s }
120
+ &.then { |tag| tag_types_to_rbs_type(tag.types) } || untyped
121
+
122
+ case kind
123
+ when :req
124
+ requireds << ::RBS::Types::Function::Param.new(name: name, type: type)
125
+ when :opt
126
+ requireds = trailing_positionals
127
+ optional_positionals << ::RBS::Types::Function::Param.new(name: name, type: type)
128
+ when :rest
129
+ requireds = trailing_positionals
130
+ name = nil if name == :*
131
+ rest_positionals = ::RBS::Types::Function::Param.new(name: name, type: type)
132
+ when :keyreq
133
+ required_keywords[name] = ::RBS::Types::Function::Param.new(name: nil, type: type)
134
+ when :key
135
+ optional_keywords[name] = ::RBS::Types::Function::Param.new(name: nil, type: type)
136
+ when :keyrest
137
+ rest_keywords = ::RBS::Types::Function::Param.new(name: name, type: type)
138
+ when :block
139
+ # block parameters cannot get by method object
140
+ else
141
+ raise "bug"
142
+ end
143
+ end
144
+
145
+ return_type =
146
+ if method_object.name == :initialize
147
+ void
148
+ else
149
+ tag_types_to_rbs_type(meth.tags("return").flat_map(&:types))
150
+ end
151
+
152
+ function = ::RBS::Types::Function.new(
153
+ required_positionals: required_positionals,
154
+ optional_positionals: optional_positionals,
155
+ rest_positionals: rest_positionals,
156
+ trailing_positionals: trailing_positionals,
157
+ required_keywords: required_keywords,
158
+ optional_keywords: optional_keywords,
159
+ rest_keywords: rest_keywords,
160
+ return_type: return_type,
161
+ )
162
+
163
+ yield_type =
164
+ if !meth.tags("yieldparam").empty? || !meth.tags("yieldreturn").empty?
165
+ block_required_positionals = meth.tags("yieldparam").map do |tag|
166
+ ::RBS::Types::Function::Param.new(
167
+ name: tag.name,
168
+ type: tag_types_to_rbs_type(tag.types) || untyped
169
+ )
170
+ end
171
+ block_return_type = tag_types_to_rbs_type(meth.tags("yieldreturn").flat_map(&:types)) || untyped
172
+ ::RBS::Types::Block.new(
173
+ required: true,
174
+ type: ::RBS::Types::Function.new(
175
+ required_positionals: block_required_positionals,
176
+ optional_positionals: [],
177
+ rest_positionals: nil,
178
+ trailing_positionals: [],
179
+ required_keywords: {},
180
+ optional_keywords: {},
181
+ rest_keywords: nil,
182
+ return_type: block_return_type,
183
+ ),
184
+ )
185
+ else
186
+ nil
187
+ end
188
+
189
+ method_type = ::RBS::MethodType.new(
190
+ location: nil,
191
+ type_params: [],
192
+ type: function,
193
+ block: yield_type
194
+ )
195
+
196
+ visibility = meth.visibility == :private ? 'private ' : ''
197
+ block.call(yardoc.to_s, meth.docstring, "#{visibility}def #{prefix}#{method_name}: #{method_type}")
198
+ end
199
+ end
200
+
201
+ # @return [void]
202
+ def generate_for_constants
203
+ yardoc.constants(inherited: false).each do |const|
204
+ return_tags = const.tags('return')
205
+ return_type = return_tags.empty? ? untyped : tag_types_to_rbs_type(return_tags.flat_map(&:types))
206
+ block.call(const.namespace.to_s, const.docstring, "#{const.name}: #{return_type}")
207
+ end
208
+ end
209
+
210
+ # @return [RBS::Types::t]
211
+ def tag_types_to_rbs_type(tag_types)
212
+ return untyped if tag_types.nil?
213
+ return untyped if tag_types.empty?
214
+
215
+ begin
216
+ types_explainers = ::YARD::Tags::TypesExplainer::Parser.parse(tag_types.uniq.join(", "))
217
+ rescue SyntaxError
218
+ return untyped
219
+ end
220
+
221
+ wrap(recursive_resolve(types_explainers)).tap do |rbs|
222
+ Orthoses.logger.debug("#{yardoc.inspect} tag #{tag_types} => #{rbs}")
223
+ end
224
+ end
225
+
226
+ # @return [RBS::Types::t]
227
+ def wrap(types)
228
+ if types.nil? || types.empty? || types == [untyped]
229
+ return untyped
230
+ end
231
+
232
+ if 1 < types.length
233
+ if index = types.find_index { |t| t.to_s == "nil" }
234
+ types.delete_at(index)
235
+ is_optional = true
236
+ if types == [untyped]
237
+ return untyped
238
+ end
239
+ end
240
+ end
241
+ is_union = 1 < types.length
242
+
243
+ if is_union
244
+ if is_optional
245
+ ::RBS::Types::Optional.new(
246
+ type: ::RBS::Types::Union.new(
247
+ types: types,
248
+ location: nil,
249
+ ),
250
+ location: nil,
251
+ )
252
+ else
253
+ ::RBS::Types::Union.new(
254
+ types: types,
255
+ location: nil,
256
+ )
257
+ end
258
+ elsif is_optional
259
+ ::RBS::Types::Optional.new(
260
+ type: types.first,
261
+ location: nil,
262
+ )
263
+ else
264
+ types.first
265
+ end
266
+ end
267
+
268
+ # @return [Array<RBS::Types::t>]
269
+ def recursive_resolve(types_explainer_types)
270
+ types_explainer_types.map do |types_explainer_type|
271
+ case types_explainer_type
272
+ when ::YARD::Tags::TypesExplainer::FixedCollectionType
273
+ ::RBS::Types::Tuple.new(
274
+ types: recursive_resolve(types_explainer_type.types),
275
+ location: nil
276
+ )
277
+ when ::YARD::Tags::TypesExplainer::CollectionType
278
+ type = wrap(recursive_resolve(types_explainer_type.types))
279
+ if types_explainer_type.name == "Class"
280
+ if type.to_s == "untyped"
281
+ untyped
282
+ else
283
+ ::RBS::Types::ClassSingleton.new(
284
+ name: type,
285
+ location: nil
286
+ )
287
+ end
288
+ else
289
+ ::RBS::Types::ClassInstance.new(
290
+ name: TypeName(types_explainer_type.name),
291
+ args: [type],
292
+ location: nil
293
+ )
294
+ end
295
+ when ::YARD::Tags::TypesExplainer::HashCollectionType
296
+ ::RBS::Types::ClassInstance.new(
297
+ name: TypeName(types_explainer_type.name),
298
+ args: [
299
+ wrap(recursive_resolve(types_explainer_type.key_types)),
300
+ wrap(recursive_resolve(types_explainer_type.value_types)),
301
+ ],
302
+ location: nil
303
+ )
304
+ when ::YARD::Tags::TypesExplainer::Type
305
+ if types_explainer_type.name.start_with?('#')
306
+ Orthoses.logger.debug("interface for #{types_explainer_type.name} in #{yardoc.namespace} set `untyped` because it not implemented yet")
307
+ untyped
308
+ # interface
309
+ # case types_explainer_type.name
310
+ # when '#to_s'
311
+ # ::RBS::Types::Interface.new(
312
+ # name: '_ToS',
313
+ # args: [],
314
+ # location: nil
315
+ # )
316
+ # else
317
+ # interface_method = types_explainer_type.name[1..-1]
318
+ # if interface_method.end_with?('=')
319
+ # interface_name = "_#{interface_method[0..-2].split('_').map(&:capitalize).join}Eq"
320
+ # @store[interface_name] << "def #{interface_method}: (untyped) -> untyped"
321
+ # else
322
+ # interface_name = "_#{interface_method.split('_').map(&:capitalize).join}"
323
+ # @store[interface_name] << "def #{interface_method}: () -> untyped"
324
+ # end
325
+
326
+ # ::RBS::Types::Interface.new(
327
+ # name: interface_name,
328
+ # args: [],
329
+ # location: nil
330
+ # )
331
+ # end
332
+ else
333
+ case types_explainer_type.name
334
+ when "Object" then next untyped
335
+ when "Boolean" then next bool
336
+ end
337
+
338
+ begin
339
+ rbs_type = ::RBS::Parser.parse_type(types_explainer_type.name)
340
+ case rbs_type
341
+ when ::RBS::Types::Bases::Base, ::RBS::Types::Literal
342
+ next rbs_type
343
+ when ::RBS::Types::Alias
344
+ end
345
+ rescue ::RBS::ParsingError
346
+ end
347
+
348
+ if Utils.rbs_defined_class?(types_explainer_type.name, collection: true)
349
+ ::RBS::Types::ClassInstance.new(
350
+ name: TypeName(types_explainer_type.name),
351
+ args: temporary_type_params(types_explainer_type.name),
352
+ location: nil
353
+ )
354
+ else
355
+ name =
356
+ case types_explainer_type.name
357
+ when "Fixnum"
358
+ "Integer"
359
+ else
360
+ resolved = ::YARD::Registry.resolve(yardoc.namespace, types_explainer_type.name, true, false)
361
+ if resolved
362
+ resolved.to_s
363
+ else
364
+ Orthoses.logger.warn("#{types_explainer_type.name} in #{yardoc.namespace} set `untyped` because it cannot resolved type")
365
+ next untyped
366
+ end
367
+ end
368
+
369
+ ::RBS::Types::ClassInstance.new(
370
+ name: TypeName(name),
371
+ args: [],
372
+ location: nil
373
+ )
374
+ end
375
+ end
376
+ else
377
+ raise "bug"
378
+ end
379
+ end
380
+ end
381
+
382
+ # @return [Array<RBS::Types::Bases::Any>]
383
+ def temporary_type_params(name)
384
+ params = Utils.known_type_params(name)
385
+ return [] unless params
386
+ params.map { untyped }
387
+ end
388
+ end
389
+ end
390
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+ require 'orthoses'
3
+ require_relative "yard/version"
4
+ require_relative "yard/yard2rbs"
5
+
6
+ module Orthoses
7
+ # use Orthoses::YARD, globs: "lib/**/*.rb"
8
+ class YARD
9
+ def initialize(loader, globs:)
10
+ @loader = loader
11
+ @globs = Array(globs)
12
+ end
13
+
14
+ def call
15
+ @loader.call.tap do |store|
16
+ require 'yard'
17
+
18
+ ::YARD.parse(@globs)
19
+ ::YARD::Registry.root.children.each do |yardoc|
20
+ case yardoc.type
21
+ when :class, :module
22
+ YARD2RBS.run(yardoc: yardoc) do |namespace, docstring, rbs|
23
+ if rbs.nil?
24
+ store[namespace]
25
+ else
26
+ Orthoses.logger.debug("#{namespace} << #{rbs}")
27
+ all = docstring.all.then { |it| it.empty? ? "" : "#{it.gsub(/^/, '# ')}\n" }
28
+ store[namespace] << "#{all}#{rbs}"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1 @@
1
+ require_relative 'orthoses/yard'
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/orthoses/yard/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "orthoses-yard"
7
+ spec.version = Orthoses::YARD::VERSION
8
+ spec.authors = ["ksss"]
9
+ spec.email = ["co000ri@gmail.com"]
10
+
11
+ spec.summary = "Orthoses extention for YARD."
12
+ spec.description = "Orthoses extention for YARD."
13
+ spec.homepage = "https://github.com/ksss/orthoses-yard"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = spec.homepage
20
+
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ next true if (f == __FILE__)
24
+ next true if f.match?(%r{\A(?:bin|known_sig)/}) # dir
25
+ next true if f.match?(%r{\A\.(?:git)}) # git
26
+ next true if f.match?(%r{\A(?:rbs_collection|Steepfile|Rakefile)}) # top file
27
+ next true if f.match?(%r{_test\.rb\z}) # test
28
+ false
29
+ end
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_dependency "orthoses"
36
+ spec.add_dependency "yard"
37
+ end
@@ -0,0 +1,31 @@
1
+ # GENERATED FILE
2
+
3
+ class Orthoses::YARD::YARD2RBS
4
+ # @return [YARD::CodeObjects::t]
5
+ attr_reader yardoc: untyped
6
+ # @return [Proc]
7
+ attr_reader block: Proc
8
+ # @return [RBS::Types::Bases::Any]
9
+ attr_reader untyped: RBS::Types::Bases::Any
10
+ # @return [RBS::Types::Bases::Void]
11
+ attr_reader void: RBS::Types::Bases::Void
12
+ # @return [RBS::Types::Bases::Bool]
13
+ attr_reader bool: RBS::Types::Bases::Bool
14
+ def initialize: (yardoc: untyped, block: untyped) -> void
15
+ # @return [void]
16
+ def run: () -> void
17
+ # @return [void]
18
+ def generate_for_attributes: () -> void
19
+ # @return [void]
20
+ def generate_for_methods: () -> void
21
+ # @return [void]
22
+ def generate_for_constants: () -> void
23
+ # @return [RBS::Types::t]
24
+ def tag_types_to_rbs_type: (untyped tag_types) -> untyped
25
+ # @return [RBS::Types::t]
26
+ def wrap: (untyped types) -> untyped
27
+ # @return [Array<RBS::Types::t>]
28
+ def recursive_resolve: (untyped types_explainer_types) -> untyped
29
+ # @return [Array<RBS::Types::Bases::Any>]
30
+ def temporary_type_params: (untyped name) -> Array[RBS::Types::Bases::Any]
31
+ end
@@ -0,0 +1,6 @@
1
+ # GENERATED FILE
2
+
3
+ class Orthoses::YARD
4
+ def initialize: (untyped loader, globs: untyped) -> void
5
+ VERSION: untyped
6
+ end
@@ -0,0 +1,38 @@
1
+ module YARD
2
+ module Registry
3
+ end
4
+
5
+ module Tags
6
+ module TypesExplainer
7
+ class Parser
8
+ end
9
+
10
+ class Type
11
+ end
12
+
13
+ class CollectionType < Type
14
+ end
15
+
16
+ class FixedCollectionType < CollectionType
17
+ end
18
+
19
+ class HashCollectionType < Type
20
+ end
21
+ end
22
+ end
23
+
24
+ module CodeObjects
25
+ type t = ModuleObject | ClassObject | MethodObject
26
+ class Base
27
+ def attributes: () -> { class: Hash[Symbol, untyped], instance: Hash[Symbol, untyped] }
28
+ end
29
+ class NamespaceObject < Base
30
+ end
31
+ class ModuleObject < NamespaceObject
32
+ end
33
+ class ClassObject < NamespaceObject
34
+ end
35
+ class MethodObject < Base
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: orthoses-yard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ksss
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: orthoses
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Orthoses extention for YARD.
42
+ email:
43
+ - co000ri@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CODE_OF_CONDUCT.md
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - examples/yard/Gemfile
54
+ - examples/yard/Gemfile.lock
55
+ - examples/yard/Rakefile
56
+ - examples/yard/generate.rb
57
+ - examples/yard/rbs_collection.lock.yaml
58
+ - examples/yard/rbs_collection.yaml
59
+ - lib/orthoses-yard.rb
60
+ - lib/orthoses/yard.rb
61
+ - lib/orthoses/yard/version.rb
62
+ - lib/orthoses/yard/yard2rbs.rb
63
+ - orthoses-yard.gemspec
64
+ - sig/orthoses/yard.rbs
65
+ - sig/orthoses/yard/yard2_rbs.rbs
66
+ - sig_patch/yard.rbs
67
+ homepage: https://github.com/ksss/orthoses-yard
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ homepage_uri: https://github.com/ksss/orthoses-yard
72
+ source_code_uri: https://github.com/ksss/orthoses-yard
73
+ changelog_uri: https://github.com/ksss/orthoses-yard
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.6.0
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.3.16
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Orthoses extention for YARD.
93
+ test_files: []