rbs2ts 1.0.0.pre.alpha.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +18 -0
- data/.gitignore +1 -2
- data/.prettierignore +1 -0
- data/.vscode/launch.json +1 -1
- data/Gemfile +2 -0
- data/README.md +180 -1
- data/lib/rbs2ts/cli.rb +4 -2
- data/lib/rbs2ts/converter.rb +2 -0
- data/lib/rbs2ts/converter/declarations.rb +79 -3
- data/lib/rbs2ts/converter/helper.rb +16 -0
- data/lib/rbs2ts/converter/members.rb +140 -0
- data/lib/rbs2ts/converter/types.rb +39 -12
- data/lib/rbs2ts/version.rb +1 -1
- data/rbs2ts.gemspec +3 -18
- metadata +14 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63451fb8675f6e0e4420d64d7655385f8b32260c0e17b0ec9f796312372140b3
|
4
|
+
data.tar.gz: cfa90fb4ec6d06efc412ec1f1f28a000d23c3f16cba28ad0aa5f4cea4d2bda43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e8e8b4381ee67f4ad40a5a5cafa2590efe7f698b3f98bbc9625a0f74c975c4924cfd713a4450c67ba45bb346b3f620f2445707c27e09153eefe12ce60b21873a
|
7
|
+
data.tar.gz: 60832356723c81313fc06afa80a88d16555b815e3c94090db05392bee17f2de7bb30d8a5682cfdd04eaacbd4deeffdb4c1f6df5932b4bb21e5bee357aefdad9e
|
@@ -0,0 +1,18 @@
|
|
1
|
+
version: 2.1
|
2
|
+
orbs:
|
3
|
+
ruby: circleci/ruby@0.1.2
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
docker:
|
8
|
+
- image: circleci/ruby:3
|
9
|
+
executor: ruby/default
|
10
|
+
steps:
|
11
|
+
- checkout
|
12
|
+
- run:
|
13
|
+
name: Bundle Install
|
14
|
+
command: bundle -v
|
15
|
+
- ruby/bundle-install
|
16
|
+
- run:
|
17
|
+
name: RSpec
|
18
|
+
command: bundle exec rspec
|
data/.gitignore
CHANGED
data/.prettierignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
README.md
|
data/.vscode/launch.json
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Rbs2ts
|
2
2
|
|
3
|
-
|
3
|
+
A RubyGem that converts Ruby RBS to TypeScript definitions.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -13,3 +13,182 @@ gem install rbs2ts
|
|
13
13
|
```
|
14
14
|
rbs2ts convert type.rbs
|
15
15
|
```
|
16
|
+
|
17
|
+
## Example
|
18
|
+
|
19
|
+
from RBS
|
20
|
+
|
21
|
+
```
|
22
|
+
type TypeofInteger = Integer
|
23
|
+
type TypeofFloat = Float
|
24
|
+
type TypeofNumeric = Numeric
|
25
|
+
type TypeofString = String
|
26
|
+
type TypeofBool = Bool
|
27
|
+
type TypeofVoid = void
|
28
|
+
type TypeofUntyped = untyped
|
29
|
+
type TypeofNil = nil
|
30
|
+
|
31
|
+
type IntegerLiteral = 123
|
32
|
+
type StringLiteral = 'abc'
|
33
|
+
type TrueLiteral = true
|
34
|
+
type FalseLiteral = false
|
35
|
+
|
36
|
+
type IntersectionType = String & Integer & Bool
|
37
|
+
type UnionType = String | Integer | Bool
|
38
|
+
|
39
|
+
type ArrayType = Array[String]
|
40
|
+
|
41
|
+
type TupleType = [ ]
|
42
|
+
type TupleEmptyType = [String, Integer]
|
43
|
+
|
44
|
+
type OptionalType = String?
|
45
|
+
|
46
|
+
type RecordType = {
|
47
|
+
s: String,
|
48
|
+
next: {
|
49
|
+
i: Integer,
|
50
|
+
f: Float
|
51
|
+
}?
|
52
|
+
}
|
53
|
+
|
54
|
+
class Klass
|
55
|
+
attr_accessor a: String
|
56
|
+
attr_reader b: Integer
|
57
|
+
attr_writer c: Bool
|
58
|
+
|
59
|
+
attr_reader r: {
|
60
|
+
d: String,
|
61
|
+
e: {
|
62
|
+
f: String,
|
63
|
+
g: String?
|
64
|
+
}?
|
65
|
+
}
|
66
|
+
|
67
|
+
def to_tuple: () -> [{ s: String, f: Float }?]
|
68
|
+
def required_positional: (String) -> void
|
69
|
+
def required_positional_name: (String str) -> void
|
70
|
+
def optional_positional: (?String) -> void
|
71
|
+
def optional_positional_name: (?String? str) -> void
|
72
|
+
def rest_positional: (*String) -> void
|
73
|
+
def rest_positional_name: (*String str) -> void
|
74
|
+
def rest_positional_with_trailing: (*String, Integer) -> void
|
75
|
+
def rest_positional_name_with_trailing: (*String str, Integer trailing) -> void
|
76
|
+
def required_keyword: (str: String) -> void
|
77
|
+
def optional_keyword: (?str: String?) -> void
|
78
|
+
def rest_keywords: (**String) -> void
|
79
|
+
def rest_keywords_name: (**String rest) -> void
|
80
|
+
|
81
|
+
def all_arguments: (
|
82
|
+
String required_positional,
|
83
|
+
?Integer? optional_positional,
|
84
|
+
*String rest_positionals_s,
|
85
|
+
Integer trailing_positional_s,
|
86
|
+
required_keyword: String,
|
87
|
+
?optional_keyword: Integer?,
|
88
|
+
**String rest_keywords
|
89
|
+
) -> [{ s: String, f: Float }?]
|
90
|
+
end
|
91
|
+
|
92
|
+
module Module
|
93
|
+
def func: (String, Integer) -> { str: String, int: Integer }
|
94
|
+
end
|
95
|
+
|
96
|
+
interface _Interface
|
97
|
+
def func: (String, Integer) -> { str: String, int: Integer }
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
to TypeScript
|
102
|
+
|
103
|
+
```typescript
|
104
|
+
export type TypeofInteger = number;
|
105
|
+
|
106
|
+
export type TypeofFloat = number;
|
107
|
+
|
108
|
+
export type TypeofNumeric = number;
|
109
|
+
|
110
|
+
export type TypeofString = string;
|
111
|
+
|
112
|
+
export type TypeofBool = boolean;
|
113
|
+
|
114
|
+
export type TypeofVoid = void;
|
115
|
+
|
116
|
+
export type TypeofUntyped = any;
|
117
|
+
|
118
|
+
export type TypeofNil = null;
|
119
|
+
|
120
|
+
export type IntegerLiteral = 123;
|
121
|
+
|
122
|
+
export type StringLiteral = "abc";
|
123
|
+
|
124
|
+
export type TrueLiteral = true;
|
125
|
+
|
126
|
+
export type FalseLiteral = false;
|
127
|
+
|
128
|
+
export type IntersectionType = string & number & boolean;
|
129
|
+
|
130
|
+
export type UnionType = string | number | boolean;
|
131
|
+
|
132
|
+
export type ArrayType = string[];
|
133
|
+
|
134
|
+
export type TupleType = [];
|
135
|
+
|
136
|
+
export type TupleEmptyType = [string, number];
|
137
|
+
|
138
|
+
export type OptionalType = string | null | undefined;
|
139
|
+
|
140
|
+
export type RecordType = {
|
141
|
+
s: string;
|
142
|
+
next: {
|
143
|
+
i: number;
|
144
|
+
f: number;
|
145
|
+
} | null | undefined;
|
146
|
+
};
|
147
|
+
|
148
|
+
export declare class Klass {
|
149
|
+
a: string;
|
150
|
+
readonly b: number;
|
151
|
+
c: boolean;
|
152
|
+
readonly r: {
|
153
|
+
d: string;
|
154
|
+
e: {
|
155
|
+
f: string;
|
156
|
+
g: string | null | undefined;
|
157
|
+
} | null | undefined;
|
158
|
+
};
|
159
|
+
toTuple(): [({
|
160
|
+
s: string;
|
161
|
+
f: number;
|
162
|
+
} | null | undefined)];
|
163
|
+
requiredPositional(arg1: string): void;
|
164
|
+
requiredPositionalName(str: string): void;
|
165
|
+
optionalPositional(arg1?: string): void;
|
166
|
+
optionalPositionalName(str?: string | null | undefined): void;
|
167
|
+
restPositional(...arg1: string[]): void;
|
168
|
+
restPositionalName(...str: string[]): void;
|
169
|
+
restPositionalWithTrailing(arg1: string[], arg2: number): void;
|
170
|
+
restPositionalNameWithTrailing(str: string[], trailing: number): void;
|
171
|
+
requiredKeyword(arg1: { str: string }): void;
|
172
|
+
optionalKeyword(arg1: { str?: string | null | undefined }): void;
|
173
|
+
restKeywords(arg1: { [key: string]: unknown; }): void;
|
174
|
+
restKeywordsName(arg1: { [key: string]: unknown; }): void;
|
175
|
+
allArguments(requiredPositional: string, optionalPositional?: number | null | undefined, restPositionalsS?: string[], trailingPositionalS?: number, arg1?: { requiredKeyword: string, optionalKeyword?: number | null | undefined, [key: string]: unknown; }): [({
|
176
|
+
s: string;
|
177
|
+
f: number;
|
178
|
+
} | null | undefined)];
|
179
|
+
};
|
180
|
+
|
181
|
+
export namespace Module {
|
182
|
+
export declare function func(arg1: string, arg2: number): {
|
183
|
+
str: string;
|
184
|
+
int: number;
|
185
|
+
};
|
186
|
+
};
|
187
|
+
|
188
|
+
export interface Interface {
|
189
|
+
func(arg1: string, arg2: number): {
|
190
|
+
str: string;
|
191
|
+
int: number;
|
192
|
+
};
|
193
|
+
};
|
194
|
+
```
|
data/lib/rbs2ts/cli.rb
CHANGED
@@ -1,17 +1,19 @@
|
|
1
1
|
require "thor"
|
2
2
|
require "rbs2ts/converter"
|
3
|
-
|
3
|
+
|
4
4
|
module Rbs2ts
|
5
5
|
class Cli < Thor
|
6
6
|
desc "convert rbs to ts", "convert rbs to ts"
|
7
7
|
def convert(path)
|
8
|
+
::RBS.logger_level = :error
|
9
|
+
|
8
10
|
loader = ::RBS::EnvironmentLoader.new(core_root: nil)
|
9
11
|
|
10
12
|
loader.add(path: Pathname(path))
|
11
13
|
|
12
14
|
env = ::RBS::Environment.from_loader(loader).resolve_type_names
|
13
15
|
|
14
|
-
Converter::Declarations::Declarations.new(env.declarations).to_ts
|
16
|
+
puts Converter::Declarations::Declarations.new(env.declarations).to_ts
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
data/lib/rbs2ts/converter.rb
CHANGED
@@ -9,12 +9,18 @@ module Rbs2ts
|
|
9
9
|
def to_ts
|
10
10
|
decls_ts = @decls.map do |d|
|
11
11
|
case d
|
12
|
+
when ::RBS::AST::Declarations::Class then
|
13
|
+
Converter::Declarations::Class.new(d).to_ts
|
14
|
+
when ::RBS::AST::Declarations::Module then
|
15
|
+
Converter::Declarations::Module.new(d).to_ts
|
16
|
+
when ::RBS::AST::Declarations::Interface then
|
17
|
+
Converter::Declarations::Interface.new(d).to_ts
|
12
18
|
when ::RBS::AST::Declarations::Alias then
|
13
19
|
Converter::Declarations::Alias.new(d).to_ts
|
14
20
|
end
|
15
21
|
end
|
16
22
|
|
17
|
-
|
23
|
+
decls_ts.join("\n\n")
|
18
24
|
end
|
19
25
|
end
|
20
26
|
|
@@ -36,19 +42,89 @@ module Rbs2ts
|
|
36
42
|
|
37
43
|
attr_reader :declaration
|
38
44
|
end
|
39
|
-
|
45
|
+
|
40
46
|
class Class < Base
|
47
|
+
def to_ts
|
48
|
+
members_ts = declaration.members.map {|member|
|
49
|
+
member_to_ts(member)
|
50
|
+
}.reject(&:empty?).join("\n")
|
51
|
+
|
52
|
+
<<~TS
|
53
|
+
export declare class #{name} {
|
54
|
+
#{Helper.indent(members_ts)}
|
55
|
+
};
|
56
|
+
TS
|
57
|
+
.chomp
|
58
|
+
end
|
59
|
+
|
60
|
+
def member_to_ts(member)
|
61
|
+
case member
|
62
|
+
when ::RBS::AST::Members::AttrReader then
|
63
|
+
Converter::Members::AttrReader.new(member).to_ts
|
64
|
+
when ::RBS::AST::Members::AttrWriter then
|
65
|
+
Converter::Members::AttrWriter.new(member).to_ts
|
66
|
+
when ::RBS::AST::Members::AttrAccessor then
|
67
|
+
Converter::Members::AttrAccessor.new(member).to_ts
|
68
|
+
when ::RBS::AST::Members::MethodDefinition
|
69
|
+
Converter::Members::MethodDefinition.new(member).to_ts
|
70
|
+
else
|
71
|
+
''
|
72
|
+
end
|
73
|
+
end
|
41
74
|
end
|
42
75
|
|
43
76
|
class Module < Base
|
77
|
+
def to_ts
|
78
|
+
members_ts = declaration.members.map {|member|
|
79
|
+
member_to_ts(member)
|
80
|
+
}.reject(&:empty?).join("\n")
|
81
|
+
|
82
|
+
<<~TS
|
83
|
+
export namespace #{name} {
|
84
|
+
#{Helper.indent(members_ts)}
|
85
|
+
};
|
86
|
+
TS
|
87
|
+
.chomp
|
88
|
+
end
|
89
|
+
|
90
|
+
def member_to_ts(member)
|
91
|
+
case member
|
92
|
+
when ::RBS::AST::Members::MethodDefinition
|
93
|
+
ts = Converter::Members::MethodDefinition.new(member).to_ts
|
94
|
+
"export declare function #{ts}"
|
95
|
+
else
|
96
|
+
''
|
97
|
+
end
|
98
|
+
end
|
44
99
|
end
|
45
100
|
|
46
101
|
class Interface < Base
|
102
|
+
def to_ts
|
103
|
+
members_ts = declaration.members.map {|member|
|
104
|
+
member_to_ts(member)
|
105
|
+
}.reject(&:empty?).join("\n")
|
106
|
+
|
107
|
+
<<~TS
|
108
|
+
export interface #{name.gsub(/_/, '')} {
|
109
|
+
#{Helper.indent(members_ts)}
|
110
|
+
};
|
111
|
+
TS
|
112
|
+
.chomp
|
113
|
+
end
|
114
|
+
|
115
|
+
def member_to_ts(member)
|
116
|
+
case member
|
117
|
+
when ::RBS::AST::Members::MethodDefinition
|
118
|
+
Converter::Members::MethodDefinition.new(member).to_ts
|
119
|
+
else
|
120
|
+
''
|
121
|
+
end
|
122
|
+
end
|
47
123
|
end
|
48
124
|
|
49
125
|
class Alias < Base
|
50
126
|
def to_ts
|
51
|
-
"type #{name} = #{Converter::Types::Resolver.to_ts(declaration.type)};"
|
127
|
+
"export type #{name} = #{Converter::Types::Resolver.to_ts(declaration.type)};"
|
52
128
|
end
|
53
129
|
end
|
54
130
|
|
@@ -0,0 +1,140 @@
|
|
1
|
+
module Rbs2ts
|
2
|
+
module Converter
|
3
|
+
module Members
|
4
|
+
class Base
|
5
|
+
def initialize(member)
|
6
|
+
@member = member
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_ts
|
10
|
+
''
|
11
|
+
end
|
12
|
+
|
13
|
+
def name
|
14
|
+
CaseTransform.camel_lower(member.name.to_s.gsub(/:/, ''))
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :member
|
20
|
+
end
|
21
|
+
|
22
|
+
class AttrReader < Base
|
23
|
+
def to_ts
|
24
|
+
"readonly #{name}: #{Converter::Types::Resolver.to_ts(member.type)};"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class AttrWriter < Base
|
29
|
+
def to_ts
|
30
|
+
"#{name}: #{Converter::Types::Resolver.to_ts(member.type)};"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class AttrAccessor < Base
|
35
|
+
def to_ts
|
36
|
+
"#{name}: #{Converter::Types::Resolver.to_ts(member.type)};"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class MethodDefinition < Base
|
41
|
+
def initialize(member)
|
42
|
+
super
|
43
|
+
@args_count = 0
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_ts
|
47
|
+
"#{name}(#{args_to_ts}): #{return_type_to_ts};"
|
48
|
+
end
|
49
|
+
|
50
|
+
def args_to_ts
|
51
|
+
[
|
52
|
+
*required_positional_to_ts,
|
53
|
+
*optional_positional_to_ts,
|
54
|
+
*rest_positionals_to_ts,
|
55
|
+
*trailing_positionals_to_ts,
|
56
|
+
*keyword_args_to_ts
|
57
|
+
].join(", ")
|
58
|
+
end
|
59
|
+
|
60
|
+
def arg_name(arg)
|
61
|
+
name = arg.name.nil? ? next_default_arg_name : arg.name.to_s
|
62
|
+
CaseTransform.camel_lower(name)
|
63
|
+
end
|
64
|
+
|
65
|
+
def next_default_arg_name
|
66
|
+
@args_count = @args_count + 1
|
67
|
+
"arg#{@args_count.to_s}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def required_positional_to_ts
|
71
|
+
method_type.required_positionals.map {|arg|
|
72
|
+
"#{arg_name(arg)}: #{Converter::Types::Resolver.to_ts(arg.type)}"
|
73
|
+
}
|
74
|
+
end
|
75
|
+
|
76
|
+
def optional_positional_to_ts
|
77
|
+
method_type.optional_positionals.map {|arg|
|
78
|
+
"#{arg_name(arg)}?: #{Converter::Types::Resolver.to_ts(arg.type)}"
|
79
|
+
}
|
80
|
+
end
|
81
|
+
|
82
|
+
def rest_positionals_to_ts
|
83
|
+
arg = method_type.rest_positionals
|
84
|
+
|
85
|
+
return [] if arg.nil?
|
86
|
+
|
87
|
+
has_rest_after_arguments ?
|
88
|
+
["#{arg_name(arg)}#{optional_ts_code}: #{Converter::Types::Resolver.to_ts(arg.type)}[]"] :
|
89
|
+
["...#{arg_name(arg)}#{optional_ts_code}: #{Converter::Types::Resolver.to_ts(arg.type)}[]"]
|
90
|
+
end
|
91
|
+
|
92
|
+
def trailing_positionals_to_ts
|
93
|
+
method_type.trailing_positionals.map {|arg|
|
94
|
+
"#{arg_name(arg)}#{optional_ts_code}: #{Converter::Types::Resolver.to_ts(arg.type)}"
|
95
|
+
}
|
96
|
+
end
|
97
|
+
|
98
|
+
def keyword_args_to_ts
|
99
|
+
required_keywords_ts = method_type.required_keywords.map {|key, value|
|
100
|
+
"#{CaseTransform.camel_lower(key.to_s)}: #{Converter::Types::Resolver.to_ts(value.type)}"
|
101
|
+
}
|
102
|
+
optional_keywords_ts = method_type.optional_keywords.map {|key, value|
|
103
|
+
"#{CaseTransform.camel_lower(key.to_s)}?: #{Converter::Types::Resolver.to_ts(value.type)}"
|
104
|
+
}
|
105
|
+
rest_keywords_ts = method_type.rest_keywords.nil? ? [] : ["[key: string]: unknown;"]
|
106
|
+
|
107
|
+
ts_array = [
|
108
|
+
*required_keywords_ts,
|
109
|
+
*optional_keywords_ts,
|
110
|
+
*rest_keywords_ts
|
111
|
+
]
|
112
|
+
|
113
|
+
return [] if ts_array.empty?
|
114
|
+
|
115
|
+
ts = ts_array.join(', ')
|
116
|
+
|
117
|
+
["#{next_default_arg_name}#{optional_ts_code}: { #{ts} }"]
|
118
|
+
end
|
119
|
+
|
120
|
+
def method_type
|
121
|
+
member.types.first.type
|
122
|
+
end
|
123
|
+
|
124
|
+
def return_type_to_ts
|
125
|
+
Converter::Types::Resolver.to_ts(method_type.return_type)
|
126
|
+
end
|
127
|
+
|
128
|
+
def optional_ts_code
|
129
|
+
method_type.optional_positionals.present? ? '?' : ''
|
130
|
+
end
|
131
|
+
|
132
|
+
def has_rest_after_arguments
|
133
|
+
method_type.trailing_positionals.present? ||
|
134
|
+
method_type.required_keywords.present? ||
|
135
|
+
method_type.optional_keywords.present?
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -46,33 +46,56 @@ module Rbs2ts
|
|
46
46
|
end
|
47
47
|
|
48
48
|
class Record < ConverterBase
|
49
|
-
INDENT = ' '
|
50
|
-
@@nest = 0
|
51
|
-
|
52
49
|
def to_ts
|
53
|
-
@@nest = @@nest + 1
|
54
|
-
|
55
|
-
indent = INDENT * @@nest
|
56
50
|
field_lines = type.fields.map { |name, type|
|
57
|
-
"#{
|
51
|
+
"#{CaseTransform.camel_lower(name.to_s)}: #{Types::Resolver.to_ts(type)};"
|
58
52
|
}
|
59
53
|
|
60
|
-
@@nest = @@nest - 1
|
61
|
-
|
62
54
|
return '{}' if field_lines.empty?
|
63
55
|
|
64
56
|
field_ts = field_lines.join("\n")
|
65
57
|
|
66
58
|
ts = <<~CODE
|
67
59
|
{
|
68
|
-
#{field_ts}
|
69
|
-
|
60
|
+
#{Helper.indent(field_ts)}
|
61
|
+
}
|
70
62
|
CODE
|
71
63
|
|
72
64
|
ts.chomp
|
73
65
|
end
|
74
66
|
end
|
75
67
|
|
68
|
+
class Tuple < ConverterBase
|
69
|
+
def to_ts
|
70
|
+
tuple_types_ts = type.types.map {|t|
|
71
|
+
ts = Types::Resolver.to_ts(t)
|
72
|
+
|
73
|
+
if t.is_a?(::RBS::Types::Union) ||
|
74
|
+
t.is_a?(::RBS::Types::Intersection) ||
|
75
|
+
t.is_a?(::RBS::Types::Optional)
|
76
|
+
"(#{ts})"
|
77
|
+
else
|
78
|
+
ts
|
79
|
+
end
|
80
|
+
}.join(', ')
|
81
|
+
|
82
|
+
"[#{tuple_types_ts}]"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class Literal < ConverterBase
|
87
|
+
def to_ts
|
88
|
+
case type.literal
|
89
|
+
when ::String then
|
90
|
+
"\"#{type.literal}\""
|
91
|
+
when ::Integer, ::TrueClass, ::FalseClass then
|
92
|
+
"#{type.literal}"
|
93
|
+
else
|
94
|
+
'unknown'
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
76
99
|
class Optional < ConverterBase
|
77
100
|
def to_ts
|
78
101
|
"#{Types::Resolver.to_ts(type.type)} | null | undefined"
|
@@ -108,7 +131,7 @@ module Rbs2ts
|
|
108
131
|
Types::Array.new(type).to_ts
|
109
132
|
when 'String' then
|
110
133
|
Types::String.new(type).to_ts
|
111
|
-
when 'Integer' then
|
134
|
+
when 'Numeric', 'Integer', 'Float' then
|
112
135
|
Types::Integer.new(type).to_ts
|
113
136
|
when 'Bool' then
|
114
137
|
Types::Bool.new(type).to_ts
|
@@ -169,6 +192,8 @@ module Rbs2ts
|
|
169
192
|
Types::BasesNil
|
170
193
|
when ::RBS::Types::ClassInstance then
|
171
194
|
Types::ClassInstance
|
195
|
+
when ::RBS::Types::Literal then
|
196
|
+
Types::Literal
|
172
197
|
when ::RBS::Types::Optional then
|
173
198
|
Types::Optional
|
174
199
|
when ::RBS::Types::Union then
|
@@ -177,6 +202,8 @@ module Rbs2ts
|
|
177
202
|
Types::Intersection
|
178
203
|
when ::RBS::Types::Record then
|
179
204
|
Types::Record
|
205
|
+
when ::RBS::Types::Tuple then
|
206
|
+
Types::Tuple
|
180
207
|
else
|
181
208
|
Types::Fallback
|
182
209
|
end
|
data/lib/rbs2ts/version.rb
CHANGED
data/rbs2ts.gemspec
CHANGED
@@ -9,25 +9,10 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["mugi-uno"]
|
10
10
|
spec.email = ["mugi.uno@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary = "
|
13
|
-
spec.description = "Convert rbs to typescript"
|
12
|
+
spec.summary = "A RubyGem that converts Ruby RBS to TypeScript definitions."
|
14
13
|
spec.homepage = "https://github.com/mugi-uno/rbs2ts"
|
14
|
+
spec.license = "MIT"
|
15
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
16
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
32
17
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
18
|
end
|
@@ -39,7 +24,7 @@ Gem::Specification.new do |spec|
|
|
39
24
|
spec.add_dependency "thor"
|
40
25
|
spec.add_dependency "case_transform"
|
41
26
|
|
42
|
-
spec.add_development_dependency "bundler", "
|
27
|
+
spec.add_development_dependency "bundler", ">= 2.1.4"
|
43
28
|
spec.add_development_dependency "rake", "~> 10.0"
|
44
29
|
spec.add_development_dependency "rspec", "~> 3.0"
|
45
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rbs2ts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mugi-uno
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-01-
|
11
|
+
date: 2021-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rbs
|
@@ -56,14 +56,14 @@ dependencies:
|
|
56
56
|
name: bundler
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 2.1.4
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 2.1.4
|
69
69
|
- !ruby/object:Gem::Dependency
|
@@ -94,7 +94,7 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '3.0'
|
97
|
-
description:
|
97
|
+
description:
|
98
98
|
email:
|
99
99
|
- mugi.uno@gmail.com
|
100
100
|
executables:
|
@@ -102,7 +102,9 @@ executables:
|
|
102
102
|
extensions: []
|
103
103
|
extra_rdoc_files: []
|
104
104
|
files:
|
105
|
+
- ".circleci/config.yml"
|
105
106
|
- ".gitignore"
|
107
|
+
- ".prettierignore"
|
106
108
|
- ".rspec"
|
107
109
|
- ".travis.yml"
|
108
110
|
- ".vscode/launch.json"
|
@@ -116,11 +118,14 @@ files:
|
|
116
118
|
- lib/rbs2ts/cli.rb
|
117
119
|
- lib/rbs2ts/converter.rb
|
118
120
|
- lib/rbs2ts/converter/declarations.rb
|
121
|
+
- lib/rbs2ts/converter/helper.rb
|
122
|
+
- lib/rbs2ts/converter/members.rb
|
119
123
|
- lib/rbs2ts/converter/types.rb
|
120
124
|
- lib/rbs2ts/version.rb
|
121
125
|
- rbs2ts.gemspec
|
122
126
|
homepage: https://github.com/mugi-uno/rbs2ts
|
123
|
-
licenses:
|
127
|
+
licenses:
|
128
|
+
- MIT
|
124
129
|
metadata: {}
|
125
130
|
post_install_message:
|
126
131
|
rdoc_options: []
|
@@ -133,12 +138,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
133
138
|
version: '0'
|
134
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
140
|
requirements:
|
136
|
-
- - "
|
141
|
+
- - ">="
|
137
142
|
- !ruby/object:Gem::Version
|
138
|
-
version:
|
143
|
+
version: '0'
|
139
144
|
requirements: []
|
140
145
|
rubygems_version: 3.1.4
|
141
146
|
signing_key:
|
142
147
|
specification_version: 4
|
143
|
-
summary:
|
148
|
+
summary: A RubyGem that converts Ruby RBS to TypeScript definitions.
|
144
149
|
test_files: []
|