add typescript generation support to code module
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
module code
|
||||
|
||||
import freeflowuniverse.herolib.core.pathlib
|
||||
import os
|
||||
|
||||
pub interface IFile {
|
||||
write(string, WriteOptions) !
|
||||
@@ -16,4 +17,14 @@ pub mut:
|
||||
pub fn (f File) write(path string, params WriteOptions) ! {
|
||||
mut fd_file := pathlib.get_file(path: '${path}/${f.name}.${f.extension}')!
|
||||
fd_file.write(f.content)!
|
||||
if f.extension == 'ts' {
|
||||
return f.typescript(path, params)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (f File) typescript(path string, params WriteOptions) ! {
|
||||
format := true
|
||||
if format {
|
||||
os.execute('npx prettier --write ${path}')
|
||||
}
|
||||
}
|
||||
@@ -15,45 +15,6 @@ pub mut:
|
||||
has_return bool @[omitempty]
|
||||
}
|
||||
|
||||
pub struct Param {
|
||||
pub mut:
|
||||
required bool @[omitempty]
|
||||
mutable bool @[omitempty]
|
||||
is_shared bool @[omitempty]
|
||||
is_optional bool @[omitempty]
|
||||
is_result bool @[omitempty]
|
||||
description string @[omitempty]
|
||||
name string @[omitempty]
|
||||
typ Type @[omitempty]
|
||||
struct_ Struct @[omitempty]
|
||||
}
|
||||
|
||||
|
||||
// // todo: maybe make 'is_' fields methods?
|
||||
// pub struct Type {
|
||||
// pub mut:
|
||||
// is_reference bool @[str: skip]
|
||||
// is_map bool @[str: skip]
|
||||
// is_array bool
|
||||
// is_mutable bool @[str: skip]
|
||||
// is_shared bool @[str: skip]
|
||||
// is_optional bool @[str: skip]
|
||||
// is_result bool @[str: skip]
|
||||
// symbol string
|
||||
// mod string @[str: skip]
|
||||
// }
|
||||
|
||||
@[params]
|
||||
pub struct Params{
|
||||
pub:
|
||||
v string
|
||||
}
|
||||
|
||||
pub fn new_param(params Params) !Param {
|
||||
// TODO: implement function from file line
|
||||
return parse_param(params.v)!
|
||||
}
|
||||
|
||||
pub fn new_function(code string) !Function {
|
||||
// TODO: implement function from file line
|
||||
return parse_function(code)!
|
||||
|
||||
@@ -17,18 +17,6 @@ pub:
|
||||
is_multi bool
|
||||
}
|
||||
|
||||
pub struct Struct {
|
||||
pub mut:
|
||||
name string
|
||||
description string
|
||||
mod string
|
||||
is_pub bool
|
||||
embeds []Struct @[str: skip]
|
||||
generics map[string]string @[str: skip]
|
||||
attrs []Attribute
|
||||
fields []StructField
|
||||
}
|
||||
|
||||
pub struct Sumtype {
|
||||
pub:
|
||||
name string
|
||||
@@ -36,21 +24,6 @@ pub:
|
||||
types []Type
|
||||
}
|
||||
|
||||
pub struct StructField {
|
||||
pub mut:
|
||||
comments []Comment
|
||||
attrs []Attribute
|
||||
name string
|
||||
description string
|
||||
default string
|
||||
is_pub bool
|
||||
is_mut bool
|
||||
is_ref bool
|
||||
anon_struct Struct @[str: skip] // sometimes fields may hold anonymous structs
|
||||
typ Type
|
||||
structure Struct @[str: skip]
|
||||
}
|
||||
|
||||
pub struct Attribute {
|
||||
pub:
|
||||
name string // [name]
|
||||
@@ -58,38 +31,6 @@ pub:
|
||||
arg string // [name: arg]
|
||||
}
|
||||
|
||||
pub fn parse_param(code_ string) !Param {
|
||||
mut code := code_.trim_space()
|
||||
|
||||
if code == '!' {
|
||||
return Param{is_result: true}
|
||||
} else if code == '?' {
|
||||
return Param{is_optional: true}
|
||||
}
|
||||
|
||||
is_mut := code.starts_with('mut ')
|
||||
if is_mut {
|
||||
code = code.trim_string_left('mut ').trim_space()
|
||||
}
|
||||
split := code.split(' ').filter(it != '')
|
||||
|
||||
if split.len == 1 {
|
||||
// means anonymous param
|
||||
return Param{
|
||||
typ: type_from_symbol(split[0])
|
||||
mutable: is_mut
|
||||
}
|
||||
}
|
||||
if split.len != 2 {
|
||||
return error('invalid param format: ${code_}')
|
||||
}
|
||||
return Param{
|
||||
name: split[0]
|
||||
typ: type_from_symbol(split[1])
|
||||
mutable: is_mut
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Alias {
|
||||
pub:
|
||||
name string
|
||||
|
||||
91
lib/core/code/param.v
Normal file
91
lib/core/code/param.v
Normal file
@@ -0,0 +1,91 @@
|
||||
module code
|
||||
|
||||
import freeflowuniverse.herolib.core.texttools
|
||||
|
||||
pub struct Param {
|
||||
pub mut:
|
||||
required bool @[omitempty]
|
||||
mutable bool @[omitempty]
|
||||
is_shared bool @[omitempty]
|
||||
is_optional bool @[omitempty]
|
||||
is_result bool @[omitempty]
|
||||
description string @[omitempty]
|
||||
name string @[omitempty]
|
||||
typ Type @[omitempty]
|
||||
struct_ Struct @[omitempty]
|
||||
}
|
||||
|
||||
|
||||
// // todo: maybe make 'is_' fields methods?
|
||||
// pub struct Type {
|
||||
// pub mut:
|
||||
// is_reference bool @[str: skip]
|
||||
// is_map bool @[str: skip]
|
||||
// is_array bool
|
||||
// is_mutable bool @[str: skip]
|
||||
// is_shared bool @[str: skip]
|
||||
// is_optional bool @[str: skip]
|
||||
// is_result bool @[str: skip]
|
||||
// symbol string
|
||||
// mod string @[str: skip]
|
||||
// }
|
||||
|
||||
@[params]
|
||||
pub struct Params{
|
||||
pub:
|
||||
v string
|
||||
}
|
||||
|
||||
pub fn new_param(params Params) !Param {
|
||||
// TODO: implement function from file line
|
||||
return parse_param(params.v)!
|
||||
}
|
||||
|
||||
pub fn (param Param) vgen() string {
|
||||
sym := param.typ.symbol()
|
||||
param_name := texttools.name_fix_snake(param.name)
|
||||
mut vstr := '${param_name} ${sym}'
|
||||
if param.mutable {
|
||||
vstr = 'mut ${vstr}'
|
||||
}
|
||||
return '${vstr}'
|
||||
}
|
||||
|
||||
pub fn (p Param) typescript() string {
|
||||
name := texttools.name_fix_snake(p.name)
|
||||
suffix := if p.is_optional {'?'} else {''}
|
||||
return '${name}${suffix}: ${p.typ.typescript()};'
|
||||
}
|
||||
|
||||
|
||||
pub fn parse_param(code_ string) !Param {
|
||||
mut code := code_.trim_space()
|
||||
|
||||
if code == '!' {
|
||||
return Param{is_result: true}
|
||||
} else if code == '?' {
|
||||
return Param{is_optional: true}
|
||||
}
|
||||
|
||||
is_mut := code.starts_with('mut ')
|
||||
if is_mut {
|
||||
code = code.trim_string_left('mut ').trim_space()
|
||||
}
|
||||
split := code.split(' ').filter(it != '')
|
||||
|
||||
if split.len == 1 {
|
||||
// means anonymous param
|
||||
return Param{
|
||||
typ: type_from_symbol(split[0])
|
||||
mutable: is_mut
|
||||
}
|
||||
}
|
||||
if split.len != 2 {
|
||||
return error('invalid param format: ${code_}')
|
||||
}
|
||||
return Param{
|
||||
name: split[0]
|
||||
typ: type_from_symbol(split[1])
|
||||
mutable: is_mut
|
||||
}
|
||||
}
|
||||
35
lib/core/code/struct.v
Normal file
35
lib/core/code/struct.v
Normal file
@@ -0,0 +1,35 @@
|
||||
module code
|
||||
|
||||
import freeflowuniverse.herolib.core.texttools
|
||||
|
||||
pub struct Struct {
|
||||
pub mut:
|
||||
name string
|
||||
description string
|
||||
mod string
|
||||
is_pub bool
|
||||
embeds []Struct @[str: skip]
|
||||
generics map[string]string @[str: skip]
|
||||
attrs []Attribute
|
||||
fields []StructField
|
||||
}
|
||||
|
||||
pub struct StructField {
|
||||
Param
|
||||
pub mut:
|
||||
comments []Comment
|
||||
attrs []Attribute
|
||||
description string
|
||||
default string
|
||||
is_pub bool
|
||||
is_mut bool
|
||||
is_ref bool
|
||||
anon_struct Struct @[str: skip] // sometimes fields may hold anonymous structs
|
||||
structure Struct @[str: skip]
|
||||
}
|
||||
|
||||
pub fn (s Struct) typescript() string {
|
||||
name := texttools.name_fix_pascal(s.name)
|
||||
fields := s.fields.map(it.typescript()).join_lines()
|
||||
return 'export interface ${name} {\n${fields}\n}'
|
||||
}
|
||||
@@ -51,7 +51,9 @@ const type_f64 = Float{
|
||||
}
|
||||
|
||||
|
||||
pub type Type = Array | Object | Result | Integer | Alias | String
|
||||
pub type Type = Array | Object | Result | Integer | Alias | String | Boolean
|
||||
|
||||
pub struct Boolean{}
|
||||
|
||||
pub struct Integer {
|
||||
bytes u8
|
||||
@@ -88,6 +90,7 @@ pub fn (t Type) symbol() string {
|
||||
}
|
||||
Alias {t.name}
|
||||
String {'string'}
|
||||
Boolean {'bool'}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,3 +110,15 @@ pub struct Result {
|
||||
pub:
|
||||
typ Type
|
||||
}
|
||||
|
||||
pub fn (t Type) typescript() string {
|
||||
return match t {
|
||||
Array { '[]${t.typ.symbol()}' }
|
||||
Object { t.name }
|
||||
Result { '!${t.typ.symbol()}'}
|
||||
Boolean { 'boolean'}
|
||||
Integer { 'number' }
|
||||
Alias {t.name}
|
||||
String {'string'}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,15 +152,6 @@ pub fn (function Function) vgen(options WriteOptions) string {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (param Param) vgen() string {
|
||||
sym := param.typ.symbol()
|
||||
param_name := texttools.name_fix_snake(param.name)
|
||||
mut vstr := '${param_name} ${sym}'
|
||||
if param.mutable {
|
||||
vstr = 'mut ${vstr}'
|
||||
}
|
||||
return '${vstr}'
|
||||
}
|
||||
|
||||
// vgen_function generates a function statement for a function
|
||||
pub fn (struct_ Struct) vgen() string {
|
||||
|
||||
Reference in New Issue
Block a user