rbs2ts 1.0.0.pre.alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 475224f8b22d01a2d3e11e6f4988ce8b96aaa36c1d6b7692d1a48f7e6ff9d7cc
4
+ data.tar.gz: 1274cd9d2b5d516b8d1e88e283a68e6e3c76686fecfa83f9dcd030090e60f21c
5
+ SHA512:
6
+ metadata.gz: 6e193c1144b33b6d12b792554ec288bfaf0a070c8d40cae480b64b7e23a875b32dbb1801d32e779443c78614681f75b45752ed82f2c7909e7694a0b41a972da1
7
+ data.tar.gz: 6bbc894fba29723892e8dd7f4c98dd25c014abd58e18d9423215ac9bf1e13dc4237e0c875e4e6984543de80b52465aef19330adbfb34799ae2ba732d3166346e
data/.gitignore ADDED
@@ -0,0 +1,60 @@
1
+ ### https://raw.github.com/github/gitignore/218a941be92679ce67d0484547e3e142b2f5f6f0/Ruby.gitignore
2
+
3
+ *.gem
4
+ *.rbc
5
+ /.config
6
+ /coverage/
7
+ /InstalledFiles
8
+ /pkg/
9
+ /spec/reports/
10
+ /spec/examples.txt
11
+ /test/tmp/
12
+ /test/version_tmp/
13
+ /tmp/
14
+
15
+ # Used by dotenv library to load environment variables.
16
+ # .env
17
+
18
+ # Ignore Byebug command history file.
19
+ .byebug_history
20
+
21
+ ## Specific to RubyMotion:
22
+ .dat*
23
+ .repl_history
24
+ build/
25
+ *.bridgesupport
26
+ build-iPhoneOS/
27
+ build-iPhoneSimulator/
28
+
29
+ ## Specific to RubyMotion (use of CocoaPods):
30
+ #
31
+ # We recommend against adding the Pods directory to your .gitignore. However
32
+ # you should judge for yourself, the pros and cons are mentioned at:
33
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
34
+ #
35
+ # vendor/Pods/
36
+
37
+ ## Documentation cache and generated files:
38
+ /.yardoc/
39
+ /_yardoc/
40
+ /doc/
41
+ /rdoc/
42
+
43
+ ## Environment normalization:
44
+ /.bundle/
45
+ /vendor/bundle
46
+ /lib/bundler/man/
47
+
48
+ # for a library or gem, you might want to ignore these files since the code is
49
+ # intended to run in multiple environments; otherwise, check them in:
50
+ Gemfile.lock
51
+ .ruby-version
52
+ .ruby-gemset
53
+
54
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
55
+ .rvmrc
56
+
57
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
58
+ # .rubocop-https?--*
59
+
60
+
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,16 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "name": "Debug rbs2ts",
9
+ "type": "Ruby",
10
+ "request": "launch",
11
+ "program": "${workspaceRoot}/exe/rbs2ts",
12
+ "useBundler": true,
13
+ "args": ["convert", "spec/sample_rbs/test.rbs"]
14
+ }
15
+ ]
16
+ }
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rbs2ts.gemspec
6
+ gemspec
7
+
8
+ gem "debase"
9
+ gem "ruby-debug-ide"
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # Rbs2ts
2
+
3
+ Convert RBS to TypeScript type definition.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem install rbs2ts
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```
14
+ rbs2ts convert type.rbs
15
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rbs2ts"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/rbs2ts ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rbs2ts"
4
+
5
+ ::Rbs2ts::Cli.start
data/lib/rbs2ts/cli.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "thor"
2
+ require "rbs2ts/converter"
3
+
4
+ module Rbs2ts
5
+ class Cli < Thor
6
+ desc "convert rbs to ts", "convert rbs to ts"
7
+ def convert(path)
8
+ loader = ::RBS::EnvironmentLoader.new(core_root: nil)
9
+
10
+ loader.add(path: Pathname(path))
11
+
12
+ env = ::RBS::Environment.from_loader(loader).resolve_type_names
13
+
14
+ Converter::Declarations::Declarations.new(env.declarations).to_ts
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,62 @@
1
+ module Rbs2ts
2
+ module Converter
3
+ module Declarations
4
+ class Declarations
5
+ def initialize(decls)
6
+ @decls = decls
7
+ end
8
+
9
+ def to_ts
10
+ decls_ts = @decls.map do |d|
11
+ case d
12
+ when ::RBS::AST::Declarations::Alias then
13
+ Converter::Declarations::Alias.new(d).to_ts
14
+ end
15
+ end
16
+
17
+ puts decls_ts.join("\n\n")
18
+ end
19
+ end
20
+
21
+ # reference: RBS::AST::Declarations
22
+ class Base
23
+ def initialize(declaration)
24
+ @declaration = declaration
25
+ end
26
+
27
+ def to_ts
28
+ ''
29
+ end
30
+
31
+ def name
32
+ declaration.name.to_s.gsub(/:/, '')
33
+ end
34
+
35
+ private
36
+
37
+ attr_reader :declaration
38
+ end
39
+
40
+ class Class < Base
41
+ end
42
+
43
+ class Module < Base
44
+ end
45
+
46
+ class Interface < Base
47
+ end
48
+
49
+ class Alias < Base
50
+ def to_ts
51
+ "type #{name} = #{Converter::Types::Resolver.to_ts(declaration.type)};"
52
+ end
53
+ end
54
+
55
+ class Constant < Base
56
+ end
57
+
58
+ class Global < Base
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,187 @@
1
+ module Rbs2ts
2
+ module Converter
3
+ module Types
4
+ class ConverterBase
5
+ def initialize(type)
6
+ @type = type
7
+ end
8
+
9
+ def to_ts
10
+ raise 'not implemented'
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :type
16
+ end
17
+
18
+ class Fallback < ConverterBase
19
+ def to_ts
20
+ 'unknown'
21
+ end
22
+ end
23
+
24
+ class BasesBool < ConverterBase
25
+ def to_ts
26
+ 'boolean'
27
+ end
28
+ end
29
+
30
+ class BasesVoid < ConverterBase
31
+ def to_ts
32
+ 'void'
33
+ end
34
+ end
35
+
36
+ class BasesAny < ConverterBase
37
+ def to_ts
38
+ 'any'
39
+ end
40
+ end
41
+
42
+ class BasesNil < ConverterBase
43
+ def to_ts
44
+ 'null'
45
+ end
46
+ end
47
+
48
+ class Record < ConverterBase
49
+ INDENT = ' '
50
+ @@nest = 0
51
+
52
+ def to_ts
53
+ @@nest = @@nest + 1
54
+
55
+ indent = INDENT * @@nest
56
+ field_lines = type.fields.map { |name, type|
57
+ "#{indent}#{CaseTransform.camel_lower(name.to_s)}: #{Types::Resolver.to_ts(type)};"
58
+ }
59
+
60
+ @@nest = @@nest - 1
61
+
62
+ return '{}' if field_lines.empty?
63
+
64
+ field_ts = field_lines.join("\n")
65
+
66
+ ts = <<~CODE
67
+ {
68
+ #{field_ts}
69
+ #{INDENT * @@nest}}
70
+ CODE
71
+
72
+ ts.chomp
73
+ end
74
+ end
75
+
76
+ class Optional < ConverterBase
77
+ def to_ts
78
+ "#{Types::Resolver.to_ts(type.type)} | null | undefined"
79
+ end
80
+ end
81
+
82
+ class Union < ConverterBase
83
+ def to_ts
84
+ types_ts = type.types.map { |type|
85
+ ts = Types::Resolver.to_ts(type)
86
+ type.is_a?(::RBS::Types::Optional) ? "(#{ts})" : ts
87
+ }
88
+
89
+ types_ts.join(' | ')
90
+ end
91
+ end
92
+
93
+ class Intersection < ConverterBase
94
+ def to_ts
95
+ types_ts = type.types.map { |type|
96
+ ts = Types::Resolver.to_ts(type)
97
+ type.is_a?(::RBS::Types::Union) || type.is_a?(::RBS::Types::Optional) ? "(#{ts})" : ts
98
+ }
99
+
100
+ types_ts.join(' & ')
101
+ end
102
+ end
103
+
104
+ class ClassInstance < ConverterBase
105
+ def to_ts
106
+ case type.name.to_s
107
+ when 'Array' then
108
+ Types::Array.new(type).to_ts
109
+ when 'String' then
110
+ Types::String.new(type).to_ts
111
+ when 'Integer' then
112
+ Types::Integer.new(type).to_ts
113
+ when 'Bool' then
114
+ Types::Bool.new(type).to_ts
115
+ else
116
+ type.name.to_s.gsub(/:/, '')
117
+ end
118
+ end
119
+ end
120
+
121
+ class Array < ConverterBase
122
+ def to_ts
123
+ array_type = type.args.first
124
+ array_type_ts = Types::Resolver.to_ts(array_type)
125
+
126
+ if array_type.is_a?(::RBS::Types::Union) ||
127
+ array_type.is_a?(::RBS::Types::Intersection) ||
128
+ array_type.is_a?(::RBS::Types::Optional)
129
+ "(#{array_type_ts})[]"
130
+ else
131
+ "#{array_type_ts}[]"
132
+ end
133
+
134
+ end
135
+ end
136
+
137
+ class String < ConverterBase
138
+ def to_ts
139
+ 'string'
140
+ end
141
+ end
142
+
143
+ class Integer < ConverterBase
144
+ def to_ts
145
+ 'number'
146
+ end
147
+ end
148
+
149
+ class Bool < ConverterBase
150
+ def to_ts
151
+ 'boolean'
152
+ end
153
+ end
154
+
155
+ class Resolver
156
+ def self.to_ts(type)
157
+ Resolver.resolve(type).new(type).to_ts
158
+ end
159
+
160
+ def self.resolve(type)
161
+ case type
162
+ when ::RBS::Types::Bases::Bool then
163
+ Types::BasesBool
164
+ when ::RBS::Types::Bases::Void then
165
+ Types::BasesVoid
166
+ when ::RBS::Types::Bases::Any then
167
+ Types::BasesAny
168
+ when ::RBS::Types::Bases::Nil then
169
+ Types::BasesNil
170
+ when ::RBS::Types::ClassInstance then
171
+ Types::ClassInstance
172
+ when ::RBS::Types::Optional then
173
+ Types::Optional
174
+ when ::RBS::Types::Union then
175
+ Types::Union
176
+ when ::RBS::Types::Intersection then
177
+ Types::Intersection
178
+ when ::RBS::Types::Record then
179
+ Types::Record
180
+ else
181
+ Types::Fallback
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,7 @@
1
+ module Rbs2ts::Converter
2
+ end
3
+
4
+ require 'case_transform'
5
+
6
+ require 'rbs2ts/converter/declarations'
7
+ require 'rbs2ts/converter/types'
@@ -0,0 +1,3 @@
1
+ module Rbs2ts
2
+ VERSION = "1.0.0-alpha.2"
3
+ end
data/lib/rbs2ts.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Rbs2ts
2
+ class Error < StandardError; end
3
+ end
4
+
5
+ require "rbs"
6
+ require "rbs2ts/version"
7
+ require "rbs2ts/cli"
data/rbs2ts.gemspec ADDED
@@ -0,0 +1,45 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rbs2ts/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rbs2ts"
8
+ spec.version = Rbs2ts::VERSION
9
+ spec.authors = ["mugi-uno"]
10
+ spec.email = ["mugi.uno@gmail.com"]
11
+
12
+ spec.summary = "Convert rbs to typescript"
13
+ spec.description = "Convert rbs to typescript"
14
+ spec.homepage = "https://github.com/mugi-uno/rbs2ts"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+
21
+ # spec.metadata["homepage_uri"] = spec.homepage
22
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
23
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_dependency "rbs", "~> 1.0.0"
39
+ spec.add_dependency "thor"
40
+ spec.add_dependency "case_transform"
41
+
42
+ spec.add_development_dependency "bundler", "~> 2.1.4"
43
+ spec.add_development_dependency "rake", "~> 10.0"
44
+ spec.add_development_dependency "rspec", "~> 3.0"
45
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbs2ts
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre.alpha.2
5
+ platform: ruby
6
+ authors:
7
+ - mugi-uno
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rbs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
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
+ - !ruby/object:Gem::Dependency
42
+ name: case_transform
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.1.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.1.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Convert rbs to typescript
98
+ email:
99
+ - mugi.uno@gmail.com
100
+ executables:
101
+ - rbs2ts
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - ".vscode/launch.json"
109
+ - Gemfile
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - exe/rbs2ts
115
+ - lib/rbs2ts.rb
116
+ - lib/rbs2ts/cli.rb
117
+ - lib/rbs2ts/converter.rb
118
+ - lib/rbs2ts/converter/declarations.rb
119
+ - lib/rbs2ts/converter/types.rb
120
+ - lib/rbs2ts/version.rb
121
+ - rbs2ts.gemspec
122
+ homepage: https://github.com/mugi-uno/rbs2ts
123
+ licenses: []
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.3.1
139
+ requirements: []
140
+ rubygems_version: 3.1.4
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Convert rbs to typescript
144
+ test_files: []