rbs_protobuf 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +6 -6
- data/.gitignore +1 -1
- data/.gitmodules +3 -0
- data/CHANGELOG.md +26 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +70 -0
- data/README.md +39 -16
- data/Rakefile +15 -2
- data/Steepfile +13 -0
- data/bin/setup +2 -0
- data/bin/steep +17 -0
- data/example/Steepfile +11 -5
- data/example/c.proto +12 -0
- data/exe/protoc-gen-rbs +15 -1
- data/lib/rbs_protobuf/name.rb +68 -0
- data/lib/rbs_protobuf/rbs_factory.rb +18 -14
- data/lib/rbs_protobuf/translator/base.rb +30 -5
- data/lib/rbs_protobuf/translator/protobuf_gem.rb +498 -217
- data/lib/rbs_protobuf/version.rb +1 -1
- data/lib/rbs_protobuf.rb +2 -1
- data/rbs_collection.lock.yaml +108 -0
- data/rbs_collection.yaml +19 -0
- data/rbs_protobuf.gemspec +2 -2
- data/sig/rbs_protobuf/name.rbs +45 -0
- data/sig/rbs_protobuf/rbs_factory.rbs +41 -0
- data/sig/rbs_protobuf/translator/base.rbs +35 -0
- data/sig/rbs_protobuf/translator/protobuf_gem.rbs +122 -0
- data/sig/rbs_protobuf/version.rbs +3 -0
- data/sig/rbs_protobuf.rbs +5 -0
- metadata +20 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 563169bcedcc8c4392d8ad6c428c8fd4f31b5ca3373705439fa94a0e3f05b138
|
4
|
+
data.tar.gz: a55dd8d55cc9b866b101e51bb90ba88aa5e138251c3083b010671b03346d9db8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0ea93fd55f7b9efb4a864156e428033da4116499c99ef8f222949ae301b16fe302cd00c022ce2ad91ac9216dca508a084a9c59fb5b296dbd5e41d4d8dd6b205
|
7
|
+
data.tar.gz: c143b93fce43ede0ff094a26f2e51d3d72e6b35853852cc11e1b571f74abbe68ec7f579af206bec8213c908bb34d1421986f57a6f14417d316d7b666491c04ce
|
data/.github/workflows/test.yml
CHANGED
@@ -12,12 +12,12 @@ jobs:
|
|
12
12
|
strategy:
|
13
13
|
matrix:
|
14
14
|
container_tag:
|
15
|
-
- 2.6-bionic
|
16
15
|
- 2.7-bionic
|
17
16
|
- 3.0-focal
|
17
|
+
- 3.1-focal
|
18
18
|
protoc_version:
|
19
|
-
- 3.
|
20
|
-
- 3.
|
19
|
+
- 3.18.2
|
20
|
+
- 3.19.4
|
21
21
|
container:
|
22
22
|
image: rubylang/ruby:${{ matrix.container_tag }}
|
23
23
|
steps:
|
@@ -30,7 +30,9 @@ jobs:
|
|
30
30
|
unzip protoc-${{ matrix.protoc_version }}-linux-x86_64.zip
|
31
31
|
mv bin/protoc /usr/local/bin/protoc
|
32
32
|
protoc --version
|
33
|
-
- uses: actions/checkout@
|
33
|
+
- uses: actions/checkout@v3
|
34
|
+
with:
|
35
|
+
submodules: true
|
34
36
|
- name: Run test
|
35
37
|
run: |
|
36
38
|
ruby -v
|
@@ -38,5 +40,3 @@ jobs:
|
|
38
40
|
bin/setup
|
39
41
|
bundle exec rake
|
40
42
|
bundle exec rake install
|
41
|
-
mkdir -p tmp/out
|
42
|
-
RBS_PROTOBUF_BACKEND=protobuf protoc --rbs_out=tmp/out -Iexample example/*.proto
|
data/.gitignore
CHANGED
data/.gitmodules
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# CHANGELOG
|
2
|
+
|
3
|
+
## 1.0.0 (2022-04-25)
|
4
|
+
|
5
|
+
This is version 1.0.0. 🎉
|
6
|
+
|
7
|
+
It includes changes on `nil` handling on required fields. Passing `nil` to required fields is prohibited by default, but there is an option to allow it, define `$RBS_PROTOBUF_ACCEPT_NIL_ATTR_WRITER` env var.
|
8
|
+
|
9
|
+
* Stop accepting `nil` to required fields ([\#16](https://github.com/square/rbs_protobuf/pull/16))
|
10
|
+
* Add an option to let required fields accept `nil` ([\#27](https://github.com/square/rbs_protobuf/pull/27))
|
11
|
+
* Fix extension generator ([\#21](https://github.com/square/rbs_protobuf/pull/21))
|
12
|
+
* Use overload for field write types ([\#22](https://github.com/square/rbs_protobuf/pull/22))
|
13
|
+
* Generate helper types and use them ([\#24](https://github.com/square/rbs_protobuf/pull/24))
|
14
|
+
|
15
|
+
## 0.3.0 (2022-04-05)
|
16
|
+
|
17
|
+
* Put protobuf _options_ in RBS comment ([\#15](https://github.com/square/rbs_protobuf/pull/15))
|
18
|
+
* Add `!` methods type definitions ([\#14](https://github.com/square/rbs_protobuf/pull/14))
|
19
|
+
|
20
|
+
## 0.2.0 (2022-03-18)
|
21
|
+
|
22
|
+
This is a maintenance release to support the latest version of RBS.
|
23
|
+
|
24
|
+
* Update to the latest RBS ([#11](https://github.com/square/rbs_protobuf/pull/11))
|
25
|
+
* Type check with Steep ([#11](https://github.com/square/rbs_protobuf/pull/11))
|
26
|
+
* Drop Ruby 2.6 support ([#11](https://github.com/square/rbs_protobuf/pull/11))
|
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rbs_protobuf (1.0.0)
|
5
|
+
activesupport (>= 4.0)
|
6
|
+
rbs (>= 2.2.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (7.0.2.3)
|
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
|
+
ffi (1.15.5)
|
19
|
+
i18n (1.10.0)
|
20
|
+
concurrent-ruby (~> 1.0)
|
21
|
+
language_server-protocol (3.16.0.3)
|
22
|
+
listen (3.7.1)
|
23
|
+
rb-fsevent (~> 0.10, >= 0.10.3)
|
24
|
+
rb-inotify (~> 0.9, >= 0.9.10)
|
25
|
+
middleware (0.1.0)
|
26
|
+
minitest (5.15.0)
|
27
|
+
parallel (1.22.1)
|
28
|
+
parser (3.1.2.0)
|
29
|
+
ast (~> 2.4.1)
|
30
|
+
protobuf (3.10.5)
|
31
|
+
activesupport (>= 3.2)
|
32
|
+
middleware
|
33
|
+
thor
|
34
|
+
thread_safe
|
35
|
+
rainbow (3.1.1)
|
36
|
+
rake (12.3.3)
|
37
|
+
rb-fsevent (0.11.1)
|
38
|
+
rb-inotify (0.10.1)
|
39
|
+
ffi (~> 1.0)
|
40
|
+
rbs (2.3.2)
|
41
|
+
steep (0.52.1)
|
42
|
+
activesupport (>= 5.1)
|
43
|
+
language_server-protocol (>= 3.15, < 4.0)
|
44
|
+
listen (~> 3.0)
|
45
|
+
parallel (>= 1.0.0)
|
46
|
+
parser (>= 3.0)
|
47
|
+
rainbow (>= 2.2.2, < 4.0)
|
48
|
+
rbs (>= 2.3.2)
|
49
|
+
terminal-table (>= 2, < 4)
|
50
|
+
terminal-table (3.0.2)
|
51
|
+
unicode-display_width (>= 1.1.1, < 3)
|
52
|
+
thor (1.2.1)
|
53
|
+
thread_safe (0.3.6)
|
54
|
+
tzinfo (2.0.4)
|
55
|
+
concurrent-ruby (~> 1.0)
|
56
|
+
unicode-display_width (2.1.0)
|
57
|
+
|
58
|
+
PLATFORMS
|
59
|
+
ruby
|
60
|
+
|
61
|
+
DEPENDENCIES
|
62
|
+
minitest (~> 5.0)
|
63
|
+
protobuf (~> 3.10)
|
64
|
+
rake (~> 12.0)
|
65
|
+
rbs
|
66
|
+
rbs_protobuf!
|
67
|
+
steep
|
68
|
+
|
69
|
+
BUNDLED WITH
|
70
|
+
2.3.11
|
data/README.md
CHANGED
@@ -20,7 +20,7 @@ message SearchRequest {
|
|
20
20
|
}
|
21
21
|
```
|
22
22
|
|
23
|
-
rbs_protobuf will generate the following RBS file including method definitions for each attribute with correct types.
|
23
|
+
rbs_protobuf will generate the following RBS file including method definitions for each attribute with correct types.
|
24
24
|
|
25
25
|
```rbs
|
26
26
|
module Protobuf
|
@@ -84,28 +84,26 @@ You may need `bundle exec protoc ...` to let bundler set up PATH.
|
|
84
84
|
|
85
85
|
## Type checking
|
86
86
|
|
87
|
-
To type check the output, you need to configure your tools to import [gem_rbs_collection](https://github.com/ruby/gem_rbs_collection).
|
87
|
+
To type check the output, you need to configure your tools to import [gem_rbs_collection](https://github.com/ruby/gem_rbs_collection) with `rbs collection` command.
|
88
88
|
|
89
|
-
|
90
|
-
|
91
|
-
|
89
|
+
```yaml
|
90
|
+
# Add the dependency in rbs_collection.yaml
|
91
|
+
gems:
|
92
|
+
- name: rbs_protobuf
|
93
|
+
```
|
92
94
|
|
93
|
-
|
95
|
+
We assume that you don't type check the generated `.pb.rb` code.
|
96
|
+
If you want to type check them, you need the definition of `Google::Protobuf`, which can be generated from [`descriptor.proto`](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto).
|
94
97
|
|
95
|
-
|
96
|
-
|
97
|
-
```ruby
|
98
|
-
target :app do
|
99
|
-
repo_path "vendor/rbs/gem_rbs_collection/gems"
|
100
|
-
library "protobuf"
|
101
|
-
end
|
102
|
-
```
|
98
|
+
Note: `rbs_protobuf` generates RBS files assuming some types added in [this PR](https://github.com/ruby/gem_rbs_collection/pull/145). Make sure you are using one of the newer versions of `rbs_gem_collection` definitions.
|
103
99
|
|
104
100
|
### Options
|
105
101
|
|
106
102
|
* `RBS_PROTOBUF_BACKEND` specifies the Ruby code generator gem. Supported value is `protobuf`. (We will add `google-protobuf` for `google-protobuf` gem.)
|
107
103
|
* `PB_UPCASE_ENUMS` is for `protobuf` gem support. Specify the environment variable to make enum value constants upper case.
|
108
104
|
* `RBS_PROTOBUF_NO_NESTED_NAMESPACE` is to make the RBS declarations flat.
|
105
|
+
* `RBS_PROTOBUF_EXTENSION` specifies what to do for extensions.
|
106
|
+
* `RBS_PROTOBUF_ACCEPT_NIL_ATTR_WRITER` is to allow passing `nil` to required fields.
|
109
107
|
|
110
108
|
## Supported features
|
111
109
|
|
@@ -116,10 +114,36 @@ end
|
|
116
114
|
| Packages | ✓ |
|
117
115
|
| Nested messages | ✓ |
|
118
116
|
| Maps | ✓ |
|
119
|
-
| Extensions |
|
117
|
+
| Extensions | Read next section |
|
120
118
|
| Services | Only generates classes |
|
121
119
|
| Oneof | No support in `protobuf` gem |
|
122
120
|
|
121
|
+
### Extensions
|
122
|
+
|
123
|
+
Adding extensions may cause problems if the name of new attribute conflicts.
|
124
|
+
|
125
|
+
```proto
|
126
|
+
extend SearchRequest {
|
127
|
+
// This extension defines an attribute.
|
128
|
+
optional string option = 100;
|
129
|
+
}
|
130
|
+
|
131
|
+
extend SearchRequest {
|
132
|
+
// Another extension defines another attribute with same name.
|
133
|
+
optional string option = 101;
|
134
|
+
}
|
135
|
+
```
|
136
|
+
|
137
|
+
In this case, defining two `option` attributes in RBS causes an error.
|
138
|
+
So, rbs_protobuf allows ignoring extensions for this case.
|
139
|
+
|
140
|
+
You can control the behavior with `RBS_PROTOBUF_EXTENSION` environment variable.
|
141
|
+
|
142
|
+
* `false`: Ignores extensions.
|
143
|
+
* `print`: Prints RBS for extensions instead of writing them to files. You can copy or modify the printed RBS, and put them in some RBS files.
|
144
|
+
* Any value else: Generates RBS for extensions.
|
145
|
+
* undefined: Ignores extensions but print messages to ask you to specify a value.
|
146
|
+
|
123
147
|
## Development
|
124
148
|
|
125
149
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test example:typecheck` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -132,7 +156,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
132
156
|
|
133
157
|
Bug reports and pull requests are welcome on GitHub at https://github.com/square/rbs_protobuf.
|
134
158
|
|
135
|
-
|
136
159
|
## License
|
137
160
|
|
138
161
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -13,6 +13,9 @@ task :default => [:test, "example:typecheck"]
|
|
13
13
|
namespace :example do
|
14
14
|
desc "Generate Ruby code with protobuf-gem"
|
15
15
|
task :protobufgem do
|
16
|
+
spec = Gem::Specification.find_by_name("protobuf")
|
17
|
+
proto_path = File.join(spec.gem_dir, "proto")
|
18
|
+
|
16
19
|
sh("rm -rf example/protobuf-gem")
|
17
20
|
sh("mkdir", "-p", "example/protobuf-gem")
|
18
21
|
sh(
|
@@ -21,17 +24,27 @@ namespace :example do
|
|
21
24
|
"--plugin=protoc-gen-ruby-protobuf=#{__dir__}/bin/protoc-gen-ruby",
|
22
25
|
"--ruby-protobuf_out=example/protobuf-gem",
|
23
26
|
"-Iexample",
|
27
|
+
"-I#{proto_path}",
|
24
28
|
"example/a.proto",
|
25
|
-
"example/b.proto"
|
29
|
+
"example/b.proto",
|
30
|
+
"example/c.proto"
|
26
31
|
)
|
27
32
|
sh(
|
28
|
-
{ "RBS_PROTOBUF_BACKEND" => "protobuf" },
|
33
|
+
{ "RBS_PROTOBUF_BACKEND" => "protobuf", "RBS_PROTOBUF_EXTENSION" => "true" },
|
29
34
|
"protoc",
|
30
35
|
"--rbs_out=example/protobuf-gem",
|
31
36
|
"-Iexample",
|
32
37
|
"example/a.proto",
|
33
38
|
"example/b.proto"
|
34
39
|
)
|
40
|
+
sh(
|
41
|
+
{ "RBS_PROTOBUF_BACKEND" => "protobuf", "RBS_PROTOBUF_EXTENSION" => "true", "RUBYOPT" => "-rbundler/setup -Iexample/protobuf-gem -rc.pb" },
|
42
|
+
"protoc",
|
43
|
+
"--rbs_out=example/protobuf-gem",
|
44
|
+
"-Iexample",
|
45
|
+
"-I#{proto_path}",
|
46
|
+
"example/c.proto"
|
47
|
+
)
|
35
48
|
end
|
36
49
|
|
37
50
|
desc "Type check generated code"
|
data/Steepfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
D = Steep::Diagnostic
|
2
|
+
|
3
|
+
target :lib do
|
4
|
+
signature "sig"
|
5
|
+
signature "vendor/gem_rbs_collection/gems/protobuf"
|
6
|
+
check "lib"
|
7
|
+
|
8
|
+
configure_code_diagnostics do |hash|
|
9
|
+
hash[D::Ruby::FallbackAny] = :hint
|
10
|
+
hash[D::Ruby::MethodDefinitionMissing] = :hint
|
11
|
+
hash[D::Ruby::UnknownConstant] = :information
|
12
|
+
end
|
13
|
+
end
|
data/bin/setup
CHANGED
data/bin/steep
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
BINSTUB_DIR=$(cd $(dirname $0); pwd)
|
4
|
+
GEMFILE=$(readlink -f ${BINSTUB_DIR}/../Gemfile)
|
5
|
+
ROOT_DIR=$(readlink -f ${BINSTUB_DIR}/..)
|
6
|
+
|
7
|
+
STEEP="bundle exec --gemfile=${GEMFILE} steep"
|
8
|
+
|
9
|
+
if type "rbenv" > /dev/null 2>&1; then
|
10
|
+
STEEP="rbenv exec ${STEEP}"
|
11
|
+
else
|
12
|
+
if type "rvm" > /dev/null 2>&1; then
|
13
|
+
STEEP="rvm ${ROOT_DIR} do ${STEEP}"
|
14
|
+
fi
|
15
|
+
fi
|
16
|
+
|
17
|
+
exec $STEEP $@
|
data/example/Steepfile
CHANGED
@@ -1,9 +1,15 @@
|
|
1
|
-
|
1
|
+
D = Steep::Diagnostic
|
2
|
+
|
3
|
+
target :lib do
|
4
|
+
collection_config "../rbs_collection.yaml"
|
2
5
|
|
3
|
-
target :protobuf_gem do
|
4
|
-
check "protobuf_gem_example.rb", "protobuf-gem"
|
5
6
|
signature "protobuf-gem"
|
7
|
+
signature "../vendor/gem_rbs_collection/gems/protobuf"
|
8
|
+
check "protobuf_gem_example.rb"
|
6
9
|
|
7
|
-
|
8
|
-
|
10
|
+
configure_code_diagnostics do |hash|
|
11
|
+
hash[D::Ruby::FallbackAny] = :hint
|
12
|
+
hash[D::Ruby::MethodDefinitionMissing] = :hint
|
13
|
+
hash[D::Ruby::UnknownConstant] = :information
|
14
|
+
end
|
9
15
|
end
|
data/example/c.proto
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
syntax = "proto2";
|
2
|
+
|
3
|
+
import "google/protobuf/descriptor.proto";
|
4
|
+
|
5
|
+
extend google.protobuf.FieldOptions {
|
6
|
+
optional bool super_custom_option = 22300;
|
7
|
+
}
|
8
|
+
|
9
|
+
message Message {
|
10
|
+
option deprecated = true;
|
11
|
+
optional string name = 1 [(super_custom_option) = true];
|
12
|
+
}
|
data/exe/protoc-gen-rbs
CHANGED
@@ -19,11 +19,25 @@ translator = case backend
|
|
19
19
|
when "protobuf"
|
20
20
|
upcase_enum = ENV.key?("PB_UPCASE_ENUMS")
|
21
21
|
no_nested_namespace = ENV.key?("RBS_PROTOBUF_NO_NESTED_NAMESPACE")
|
22
|
+
extension = case ENV["RBS_PROTOBUF_EXTENSION"]
|
23
|
+
when "false"
|
24
|
+
false
|
25
|
+
when nil
|
26
|
+
nil
|
27
|
+
when "print"
|
28
|
+
:print
|
29
|
+
else
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
accept_nil_writer = ENV.key?("RBS_PROTOBUF_ACCEPT_NIL_ATTR_WRITER")
|
22
34
|
|
23
35
|
RBSProtobuf::Translator::ProtobufGem.new(
|
24
36
|
input,
|
25
37
|
upcase_enum: upcase_enum,
|
26
|
-
nested_namespace: !no_nested_namespace
|
38
|
+
nested_namespace: !no_nested_namespace,
|
39
|
+
extension: extension,
|
40
|
+
accept_nil_writer: accept_nil_writer
|
27
41
|
)
|
28
42
|
when "google-protobuf"
|
29
43
|
raise NotImplementedError
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module RBSProtobuf
|
2
|
+
module Name
|
3
|
+
class Class
|
4
|
+
attr_reader :name
|
5
|
+
|
6
|
+
def initialize(name)
|
7
|
+
raise unless name.class?
|
8
|
+
@name = name
|
9
|
+
end
|
10
|
+
|
11
|
+
def instance_type(*args)
|
12
|
+
RBS::Types::ClassInstance.new(name: name, args: args, location: nil)
|
13
|
+
end
|
14
|
+
|
15
|
+
alias [] instance_type
|
16
|
+
|
17
|
+
def singleton_type
|
18
|
+
RBS::Types::ClassSingleton.new(name: name, location: nil)
|
19
|
+
end
|
20
|
+
|
21
|
+
def instance_type?(t)
|
22
|
+
t.is_a?(RBS::Types::ClassInstance) && t.name == name
|
23
|
+
end
|
24
|
+
|
25
|
+
def singleton_type?(t)
|
26
|
+
t.is_a?(RBS::Types::ClassSingleton) && t.name == name
|
27
|
+
end
|
28
|
+
|
29
|
+
def super_class(*args)
|
30
|
+
RBS::AST::Declarations::Class::Super.new(name: name, args: args, location: nil)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Alias
|
35
|
+
attr_reader :name
|
36
|
+
|
37
|
+
def initialize(name)
|
38
|
+
raise unless name.alias?
|
39
|
+
@name = name
|
40
|
+
end
|
41
|
+
|
42
|
+
def [](*args)
|
43
|
+
RBS::Types::Alias.new(name: name, args: args, location: nil)
|
44
|
+
end
|
45
|
+
|
46
|
+
def type?(t)
|
47
|
+
t.is_a?(RBS::Types::Alias) && t.name == name
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class Interface
|
52
|
+
attr_reader :name
|
53
|
+
|
54
|
+
def initialize(name)
|
55
|
+
raise unless name.interface?
|
56
|
+
@name = name
|
57
|
+
end
|
58
|
+
|
59
|
+
def [](*args)
|
60
|
+
RBS::Types::Interface.new(name: name, args: args, location: nil)
|
61
|
+
end
|
62
|
+
|
63
|
+
def type?(t)
|
64
|
+
t.is_a?(RBS::Types::Interface) && t.name == name
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -8,7 +8,7 @@ module RBSProtobuf
|
|
8
8
|
*path, name = string.delete_prefix("::").split("::").map(&:to_sym)
|
9
9
|
|
10
10
|
TypeName.new(
|
11
|
-
name: name,
|
11
|
+
name: name || raise,
|
12
12
|
namespace: Namespace.new(path: path, absolute: absolute)
|
13
13
|
)
|
14
14
|
end
|
@@ -72,7 +72,7 @@ module RBSProtobuf
|
|
72
72
|
type_name(name)
|
73
73
|
end
|
74
74
|
|
75
|
-
Types::Alias.new(name: type_name, location: nil)
|
75
|
+
Types::Alias.new(name: type_name, args: [], location: nil)
|
76
76
|
end
|
77
77
|
|
78
78
|
def function(return_type = Types::Bases::Void.new(location: nil))
|
@@ -87,7 +87,7 @@ module RBSProtobuf
|
|
87
87
|
end
|
88
88
|
|
89
89
|
def block(function, required: true)
|
90
|
-
|
90
|
+
Types::Block.new(
|
91
91
|
type: function,
|
92
92
|
required: required
|
93
93
|
)
|
@@ -98,8 +98,12 @@ module RBSProtobuf
|
|
98
98
|
end
|
99
99
|
|
100
100
|
def method_type(params: [], type:, block: nil, location: nil)
|
101
|
+
type_params = params.map do |name|
|
102
|
+
AST::TypeParam.new(name: name, variance: :invariant, upper_bound: nil, location: nil)
|
103
|
+
end
|
104
|
+
|
101
105
|
MethodType.new(
|
102
|
-
type_params:
|
106
|
+
type_params: type_params,
|
103
107
|
type: type,
|
104
108
|
block: block,
|
105
109
|
location: location
|
@@ -114,10 +118,14 @@ module RBSProtobuf
|
|
114
118
|
end
|
115
119
|
|
116
120
|
def optional_type(type, location: nil)
|
117
|
-
Types::Optional
|
118
|
-
type
|
119
|
-
|
120
|
-
|
121
|
+
if type.is_a?(Types::Optional)
|
122
|
+
type
|
123
|
+
else
|
124
|
+
Types::Optional.new(
|
125
|
+
type: type,
|
126
|
+
location: location
|
127
|
+
)
|
128
|
+
end
|
121
129
|
end
|
122
130
|
|
123
131
|
def type_var(name, location: nil)
|
@@ -128,12 +136,8 @@ module RBSProtobuf
|
|
128
136
|
end
|
129
137
|
|
130
138
|
def module_type_params(*params)
|
131
|
-
params.
|
132
|
-
|
133
|
-
name: param,
|
134
|
-
variance: :invariant,
|
135
|
-
skip_validation: false
|
136
|
-
))
|
139
|
+
params.map do |name|
|
140
|
+
AST::TypeParam.new(name: name, variance: :invariant, upper_bound: nil, location: nil)
|
137
141
|
end
|
138
142
|
end
|
139
143
|
|
@@ -42,10 +42,12 @@ module RBSProtobuf
|
|
42
42
|
raise NotImplementedError
|
43
43
|
end
|
44
44
|
|
45
|
-
def comment_for_path(source_code_info, path)
|
45
|
+
def comment_for_path(source_code_info, path, options:)
|
46
46
|
loc = source_code_info.location.find {|loc| loc.path == path }
|
47
|
+
|
48
|
+
comments = []
|
49
|
+
|
47
50
|
if loc
|
48
|
-
comments = []
|
49
51
|
if loc.leading_comments.length > 0
|
50
52
|
comments << loc.leading_comments.strip
|
51
53
|
end
|
@@ -55,9 +57,31 @@ module RBSProtobuf
|
|
55
57
|
if comments.empty? && !loc.leading_detached_comments.empty?
|
56
58
|
comments << loc.leading_detached_comments.join("\n\n").strip
|
57
59
|
end
|
60
|
+
end
|
61
|
+
|
62
|
+
if options
|
63
|
+
# @type var opts: Array[[Symbol, untyped]]
|
64
|
+
opts = []
|
65
|
+
options.each_field do |key, value|
|
66
|
+
if options.field?(key.fully_qualified_name)
|
67
|
+
opts << [key.fully_qualified_name, value]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
unless opts.empty?
|
72
|
+
unless comments.empty?
|
73
|
+
comments << "----"
|
74
|
+
end
|
75
|
+
comments << "Protobuf options:"
|
76
|
+
list = opts.map {|key, value| "- `#{key} = #{value.inspect}`" }
|
77
|
+
comments << list.join("\n")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
unless comments.empty?
|
58
82
|
RBS::AST::Comment.new(
|
59
83
|
location: nil,
|
60
|
-
string: comments.join("\n\n")
|
84
|
+
string: comments.join("\n\n") + "\n\n"
|
61
85
|
)
|
62
86
|
end
|
63
87
|
end
|
@@ -76,8 +100,7 @@ module RBSProtobuf
|
|
76
100
|
when FieldDescriptorProto::Type::TYPE_DOUBLE, FieldDescriptorProto::Type::TYPE_FLOAT
|
77
101
|
RBS::BuiltinNames::Float.instance_type
|
78
102
|
when FieldDescriptorProto::Type::TYPE_BOOL
|
79
|
-
factory.
|
80
|
-
factory.literal_type(false))
|
103
|
+
factory.bool_type()
|
81
104
|
else
|
82
105
|
raise "Unknown base type: #{type}"
|
83
106
|
end
|
@@ -88,6 +111,8 @@ module RBSProtobuf
|
|
88
111
|
|
89
112
|
*path, name = string.delete_prefix(".").split(".").map {|s| ActiveSupport::Inflector.upcase_first(s).to_sym }
|
90
113
|
|
114
|
+
name or raise
|
115
|
+
|
91
116
|
factory.instance_type(
|
92
117
|
RBS::TypeName.new(
|
93
118
|
name: name,
|