rbs2ts 1.0.0.pre.alpha.3 → 1.0.0.pre.alpha.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb1b221a5b895071e46b77dcf36fd1df3e789b0af6f91cb78adbfef7aa633c1c
4
- data.tar.gz: 1a452ee8992929e84d0cfde11006e1b9918a4dbfb5cd597506523d6491199c7f
3
+ metadata.gz: 19a2022b86feae5d562c59457ea77de37ef3a94f6c32b2b148f278610cf3e64d
4
+ data.tar.gz: 94639b023729996b58f6a7ed7f59cbfe58b7738e820ea70fb100ad9e7f079690
5
5
  SHA512:
6
- metadata.gz: c6242c6655d8ab4d43267c4fb4d7a99471d27047b5b1ea04025aada0066f272b61ffa353a06bcd238a11890d3a65ff5bf32cb6471543a00d5036b734e5b84cba
7
- data.tar.gz: 9a9d8d78d4eadd2cd9999063d841d878259f5bc52ccfbb0ea02b4691ef0a3133d8c8ab4cb954767925d0564e0c930697b1cd0017a4d476704a8f08515933bfd1
6
+ metadata.gz: 918d61866526af2f4d258fe349c5617dfc65d799086fff57fb32bfa2e6104713fed20558dd2e451ba09c860316aedb07f6637a5cdd650babfa477a61ca490f78
7
+ data.tar.gz: 5fd2d4d0c042f9a226f89dd8867d48464ccae74333afb5f77e437eb793f5db85ae2882c49ac600f2abd8caf7f8e948d9cefa23b28ce30b4b8cb54dc8d663321d
@@ -0,0 +1 @@
1
+ README.md
@@ -10,7 +10,7 @@
10
10
  "request": "launch",
11
11
  "program": "${workspaceRoot}/exe/rbs2ts",
12
12
  "useBundler": true,
13
- "args": ["convert", "spec/sample_rbs/test.rbs"]
13
+ "args": ["convert", "spec/fixtures/test.rbs"]
14
14
  }
15
15
  ]
16
16
  }
data/README.md CHANGED
@@ -1,7 +1,5 @@
1
1
  # Rbs2ts
2
2
 
3
- [![mugi_uno](https://circleci.com/<VCS>/<ORG_NAME>/<PROJECT_NAME>.svg?style=svg)](LINK)
4
-
5
3
  Convert RBS to TypeScript type definition.
6
4
 
7
5
  ## Installation
@@ -16,16 +14,136 @@ gem install rbs2ts
16
14
  rbs2ts convert type.rbs
17
15
  ```
18
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 UnionType = String & Integer & Bool
37
+ type IntersectionType = 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_s: () -> String
68
+ def tuple: () -> [{ s: String, f: Float }?]
69
+ end
70
+ ```
71
+
72
+ to TypeScript
73
+
74
+ ```typescript
75
+ export type TypeofInteger = number;
76
+
77
+ export type TypeofFloat = number;
78
+
79
+ export type TypeofNumeric = number;
80
+
81
+ export type TypeofString = string;
82
+
83
+ export type TypeofBool = boolean;
84
+
85
+ export type TypeofVoid = void;
86
+
87
+ export type TypeofUntyped = any;
88
+
89
+ export type TypeofNil = null;
90
+
91
+ export type IntegerLiteral = 123;
92
+
93
+ export type StringLiteral = "abc";
94
+
95
+ export type TrueLiteral = true;
96
+
97
+ export type FalseLiteral = false;
98
+
99
+ export type UnionType = string & number & boolean;
100
+
101
+ export type IntersectionType = string | number | boolean;
102
+
103
+ export type ArrayType = string[];
104
+
105
+ export type TupleType = [];
106
+
107
+ export type TupleEmptyType = [string, number];
108
+
109
+ export type OptionalType = string | null | undefined;
110
+
111
+ export type RecordType = {
112
+ s: string;
113
+ next: {
114
+ i: number;
115
+ f: number;
116
+ } | null | undefined;
117
+ };
118
+
119
+ export namespace Klass {
120
+ export type a = string;
121
+ export type b = number;
122
+ export type r = {
123
+ d: string;
124
+ e: {
125
+ f: string;
126
+ g: string | null | undefined;
127
+ } | null | undefined;
128
+ };
129
+ export type toSReturnType = string;
130
+ export type tupleReturnType = [({
131
+ s: string;
132
+ f: number;
133
+ } | null | undefined)];
134
+ };
135
+ ```
136
+
19
137
  ---
20
138
 
21
139
  ## ToDo
22
140
 
23
141
  - [x] Literal type
24
142
  - [ ] Interface type
25
- - [ ] Literal type
26
- - [ ] Tuple Type
27
- - [ ] Base Types
28
- - [ ] Method Type (Argument Types and Return Types)
29
- - [ ] Class declaration
143
+ - [x] Literal type
144
+ - [x] Tuple Type
145
+ - [x] Base Types
146
+ - [x] Method Type (Argument Types and Return Types)
147
+ - [x] Class declaration
30
148
  - [ ] Module declaration
31
149
  - [ ] Interface declaration
@@ -9,6 +9,8 @@ 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
12
14
  when ::RBS::AST::Declarations::Alias then
13
15
  Converter::Declarations::Alias.new(d).to_ts
14
16
  end
@@ -38,6 +40,40 @@ module Rbs2ts
38
40
  end
39
41
 
40
42
  class Class < Base
43
+ INDENT = ' '
44
+ @@nest = 0
45
+
46
+ def to_ts
47
+ @@nest = @@nest + 1
48
+
49
+ members_ts = declaration.members.map {|member|
50
+ ts = member_to_ts(member)
51
+ ts
52
+ .split("\n")
53
+ .map {|t| "#{INDENT * @@nest}#{t}" }
54
+ .join("\n")
55
+ }.reject(&:empty?).join("\n")
56
+
57
+ @@nest = @@nest - 1
58
+
59
+ <<~TS
60
+ export namespace #{name} {
61
+ #{members_ts}
62
+ };
63
+ TS
64
+ .chomp
65
+ end
66
+
67
+ def member_to_ts(member)
68
+ case member
69
+ when ::RBS::AST::Members::AttrReader, ::RBS::AST::Members::AttrAccessor then
70
+ "export type #{CaseTransform.camel_lower(member.name)} = #{Converter::Types::Resolver.to_ts(member.type)};"
71
+ when ::RBS::AST::Members::MethodDefinition
72
+ "export type #{CaseTransform.camel_lower(member.name)}ReturnType = #{Converter::Types::Resolver.to_ts(member.types.first.type.return_type)};"
73
+ else
74
+ ''
75
+ end
76
+ end
41
77
  end
42
78
 
43
79
  class Module < Base
@@ -48,7 +84,7 @@ module Rbs2ts
48
84
 
49
85
  class Alias < Base
50
86
  def to_ts
51
- "type #{name} = #{Converter::Types::Resolver.to_ts(declaration.type)};"
87
+ "export type #{name} = #{Converter::Types::Resolver.to_ts(declaration.type)};"
52
88
  end
53
89
  end
54
90
 
@@ -73,6 +73,24 @@ module Rbs2ts
73
73
  end
74
74
  end
75
75
 
76
+ class Tuple < ConverterBase
77
+ def to_ts
78
+ tuple_types_ts = type.types.map {|t|
79
+ ts = Types::Resolver.to_ts(t)
80
+
81
+ if t.is_a?(::RBS::Types::Union) ||
82
+ t.is_a?(::RBS::Types::Intersection) ||
83
+ t.is_a?(::RBS::Types::Optional)
84
+ "(#{ts})"
85
+ else
86
+ ts
87
+ end
88
+ }.join(', ')
89
+
90
+ "[#{tuple_types_ts}]"
91
+ end
92
+ end
93
+
76
94
  class Literal < ConverterBase
77
95
  def to_ts
78
96
  case type.literal
@@ -121,7 +139,7 @@ module Rbs2ts
121
139
  Types::Array.new(type).to_ts
122
140
  when 'String' then
123
141
  Types::String.new(type).to_ts
124
- when 'Integer' then
142
+ when 'Numeric', 'Integer', 'Float' then
125
143
  Types::Integer.new(type).to_ts
126
144
  when 'Bool' then
127
145
  Types::Bool.new(type).to_ts
@@ -192,6 +210,8 @@ module Rbs2ts
192
210
  Types::Intersection
193
211
  when ::RBS::Types::Record then
194
212
  Types::Record
213
+ when ::RBS::Types::Tuple then
214
+ Types::Tuple
195
215
  else
196
216
  Types::Fallback
197
217
  end
@@ -1,3 +1,3 @@
1
1
  module Rbs2ts
2
- VERSION = "1.0.0-alpha.3"
2
+ VERSION = "1.0.0-alpha.4"
3
3
  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.0.0.pre.alpha.3
4
+ version: 1.0.0.pre.alpha.4
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-14 00:00:00.000000000 Z
11
+ date: 2021-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rbs
@@ -104,6 +104,7 @@ extra_rdoc_files: []
104
104
  files:
105
105
  - ".circleci/config.yml"
106
106
  - ".gitignore"
107
+ - ".prettierignore"
107
108
  - ".rspec"
108
109
  - ".travis.yml"
109
110
  - ".vscode/launch.json"