diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..b43bf86 --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +README.md diff --git a/README.md b/README.md index c26a81a..ac888dd 100644 --- a/README.md +++ b/README.md @@ -14,16 +14,136 @@ gem install rbs2ts rbs2ts convert type.rbs ``` +## Example + +from RBS + +``` +type TypeofInteger = Integer +type TypeofFloat = Float +type TypeofNumeric = Numeric +type TypeofString = String +type TypeofBool = Bool +type TypeofVoid = void +type TypeofUntyped = untyped +type TypeofNil = nil + +type IntegerLiteral = 123 +type StringLiteral = 'abc' +type TrueLiteral = true +type FalseLiteral = false + +type UnionType = String & Integer & Bool +type IntersectionType = String | Integer | Bool + +type ArrayType = Array[String] + +type TupleType = [ ] +type TupleEmptyType = [String, Integer] + +type OptionalType = String? + +type RecordType = { + s: String, + next: { + i: Integer, + f: Float + }? +} + +class Klass + attr_accessor a: String + attr_reader b: Integer + attr_writer c: Bool + + attr_reader r: { + d: String, + e: { + f: String, + g: String? + }? + } + + def to_s: () -> String + def tuple: () -> [{ s: String, f: Float }?] +end +``` + +to TypeScript + +```typescript +export type TypeofInteger = number; + +export type TypeofFloat = number; + +export type TypeofNumeric = number; + +export type TypeofString = string; + +export type TypeofBool = boolean; + +export type TypeofVoid = void; + +export type TypeofUntyped = any; + +export type TypeofNil = null; + +export type IntegerLiteral = 123; + +export type StringLiteral = "abc"; + +export type TrueLiteral = true; + +export type FalseLiteral = false; + +export type UnionType = string & number & boolean; + +export type IntersectionType = string | number | boolean; + +export type ArrayType = string[]; + +export type TupleType = []; + +export type TupleEmptyType = [string, number]; + +export type OptionalType = string | null | undefined; + +export type RecordType = { + s: string; + next: { + i: number; + f: number; + } | null | undefined; +}; + +export namespace Klass { + export type a = string; + export type b = number; + export type r = { + d: string; + e: { + f: string; + g: string | null | undefined; + } | null | undefined; + }; + export type toSReturnType = string; + export type tupleReturnType = [({ + s: string; + f: number; + } | null | undefined)]; +}; +``` + --- ## ToDo - [x] Literal type - [ ] Interface type -- [ ] Literal type -- [ ] Tuple Type -- [ ] Base Types -- [ ] Method Type (Argument Types and Return Types) -- [ ] Class declaration +- [x] Literal type +- [x] Tuple Type +- [x] Base Types +- [x] Method Type (Argument Types and Return Types) +- [x] Class declaration - [ ] Module declaration - [ ] Interface declaration diff --git a/spec/fixtures/test.rbs b/spec/fixtures/test.rbs index 78d879e..8413cfa 100644 --- a/spec/fixtures/test.rbs +++ b/spec/fixtures/test.rbs @@ -1,48 +1,39 @@ -type StrOrIntOrBool = String | Integer | Bool -type StrAndIntAndBool = String & Integer & Bool - -type X = String & Integer | Bool -type Y = String & (Integer | Bool) - -type Arr = Array[(Integer | String?)?] - -type Base = { - bool: bool, - void: void, - any: any, - nil: nil -} - -type Foo = { - str: String, - str_o: String?, - int: Integer, - int_o: Integer?, - bool: Bool, - bool_o: Bool? -} - -type Bar = { - type: String, - address: { - zipcode: String, - tel: String? +type TypeofInteger = Integer +type TypeofFloat = Float +type TypeofNumeric = Numeric +type TypeofString = String +type TypeofBool = Bool +type TypeofVoid = void +type TypeofUntyped = untyped +type TypeofNil = nil + +type IntegerLiteral = 123 +type StringLiteral = 'abc' +type TrueLiteral = true +type FalseLiteral = false + +type UnionType = String & Integer & Bool +type IntersectionType = String | Integer | Bool + +type ArrayType = Array[String] + +type TupleType = [ ] +type TupleEmptyType = [String, Integer] + +type OptionalType = String? + +type RecordType = { + s: String, + next: { + i: Integer, + f: Float }? } -type Baz = { - foo: Foo, - bar: Bar -} - -type FooAndBar = Foo & Bar - -type FooOrBar = Foo | Bar - class Klass - attr_reader a: String - attr_writer b: Integer - attr_accessor c: Boolean + attr_accessor a: String + attr_reader b: Integer + attr_writer c: Bool attr_reader r: { d: String, @@ -53,12 +44,5 @@ class Klass } def to_s: () -> String - def self.new: () -> AnObject - def self?.sqrt: (Numeric) -> Numeric + def tuple: () -> [{ s: String, f: Float }?] end - -type TupleEmpty = [ ] -type Tuple1 = [String] -type Tuple2 = [Integer, Integer] -type Tuple3 = [String, Integer?, Bool] -type Tuple4 = [{ s: String, i: Integer }]