Skip to content

Commit

Permalink
$mol_view_tree2_to_js slots in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Nov 4, 2023
1 parent a710da9 commit fb84d48
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 67 deletions.
9 changes: 9 additions & 0 deletions view/tree2/class/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ namespace $ {
return [ operator.clone([ prop.clone([]) ]) ]
},

'^': ( operator, belt) => {
if (operator.kids.length === 0) return [ operator ]
const prop = this.$mol_view_tree2_child( operator )
const defs = prop.hack( belt )
if( defs.length ) props_inner.push( prop.clone( defs ) )

return [ operator.clone([ prop.clone([]) ]) ]
}

})

return [ ... props_root , ... props_inner ]
Expand Down
15 changes: 9 additions & 6 deletions view/tree2/prop/parts.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
namespace $ {
function get_parts(str: string) {
return $$.$mol_view_tree2_prop_parts($mol_tree2.struct(str))
}

$mol_test({
'wrong order'($) {
$mol_assert_fail(() => {
$mol_view_tree2_prop_parts('some_bla?*')
get_parts('some_bla?*')
})
},

'empty'($) {
$mol_assert_fail(() => {
$mol_view_tree2_prop_parts('')
get_parts('')
})
},

'prop with index'($) {
const parts = $mol_view_tree2_prop_parts('some_bla*')
const parts = get_parts('some_bla*')
$mol_assert_equal(parts.name, 'some_bla')
$mol_assert_equal(parts.key, '*')
$mol_assert_equal(parts.next, '')
},

'prop with index and value'($) {
const parts = $mol_view_tree2_prop_parts('some_bla*?')
const parts = get_parts('some_bla*?')
$mol_assert_equal(parts.name, 'some_bla')
$mol_assert_equal(parts.key, '*')
$mol_assert_equal(parts.next, '?')
},

'legacy indexed'($) {
const parts = $mol_view_tree2_prop_parts('Some*default')
const parts = get_parts('Some*default')
$mol_assert_equal(parts.name, 'Some')
$mol_assert_equal(parts.key, '*')
$mol_assert_equal(parts.next, '')
},

'legacy indexed value'($) {
const parts = $mol_view_tree2_prop_parts('Some*k?v')
const parts = get_parts('Some*k?v')
$mol_assert_equal(parts.name, 'Some')
$mol_assert_equal(parts.key, '*')
$mol_assert_equal(parts.next, '?')
Expand Down
10 changes: 7 additions & 3 deletions view/tree2/prop/parts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace $ {
export function $mol_view_tree2_prop_parts( prop: string ) {
const groups = [ ...prop.matchAll( $mol_view_tree2_prop_signature ) ][0]?.groups
if (! groups) throw new Error(`${$mol_view_tree2_prop_signature} not matched: ${prop}`)
export function $mol_view_tree2_prop_parts(this: $, prop: $mol_tree2 ) {
const groups = [ ...prop.type.matchAll( $mol_view_tree2_prop_signature ) ][0]?.groups
if (! groups) {
this.$mol_fail(
$mol_view_tree2_error_str`Required prop like some*? at ${prop.span}`
)
}

return {
name: groups.name,
Expand Down
21 changes: 10 additions & 11 deletions view/tree2/to/dts/dts.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace $ {

function name_of( prop: $mol_tree2 ) {
return $mol_view_tree2_prop_parts(prop.type).name
function name_of(this: $, prop: $mol_tree2) {
return this.$mol_view_tree2_prop_parts(prop).name
}

function params_of( prop: $mol_tree2, ... val: $mol_tree2[] ) {
function params_of( this: $, prop: $mol_tree2, ... val: $mol_tree2[] ) {

const { key, next } = $mol_view_tree2_prop_parts(prop.type)
const { key, next } = this.$mol_view_tree2_prop_parts(prop)

return prop.struct( 'line', [
prop.data('( '),
Expand Down Expand Up @@ -48,15 +47,15 @@ namespace $ {
] ),
... props.map( prop => {

const name = name_of(prop)
const name = name_of.call(this, prop)
if (methods.has(name)) return undefined

methods.add(name)
const bind_res = ( bind: $mol_tree2 )=> [
bind.data( 'ReturnType< ' ),
klass.data( klass.type ),
bind.data( '["' ),
bind.kids[0].data( name_of( bind.kids[0] ) ),
bind.kids[0].data( name_of.call(this, bind.kids[0] ) ),
bind.data( '"] >' ),
]

Expand Down Expand Up @@ -143,7 +142,7 @@ namespace $ {

for( const over of input.kids ) {

const name = name_of( over )
const name = name_of.call(this, over )
const bind = over.kids[0]

if( bind.type === '=>' ) {
Expand All @@ -161,8 +160,8 @@ namespace $ {
aliases.push(
pr.struct( 'indent', [
pr.struct( 'line', [
pr.data( name_of( pr ) ),
params_of( pr, ... res ),
pr.data( name_of.call(this, pr ) ),
params_of.call(this, pr, ... res ),
bind.data( ': ' ),
... res,
] ),
Expand Down Expand Up @@ -209,7 +208,7 @@ namespace $ {
return prop.struct( 'indent', [
prop.struct( 'line', [
prop.data( name ),
params_of( prop, ... val ),
params_of.call(this, prop, ... val ),
prop.data(': '),
... val,
] )
Expand Down
77 changes: 35 additions & 42 deletions view/tree2/to/js/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ namespace $ {

const err = $mol_view_tree2_error_str

function name_of( prop: $mol_tree2 ) {
return $mol_view_tree2_prop_parts(prop.type).name
function name_of(this: $, prop: $mol_tree2 ) {
return this.$mol_view_tree2_prop_parts(prop).name
}

function params_of( prop: $mol_tree2, bidi = true ) {
function params_of( this: $, prop: $mol_tree2, bidi = true ) {

const { key, next } = $mol_view_tree2_prop_parts(prop.type)
const { key, next } = this.$mol_view_tree2_prop_parts(prop)

return prop.struct( '(,)', [
... key ? [ prop.struct( 'id' ) ] : [],
Expand All @@ -17,9 +17,9 @@ namespace $ {

}

function args_of( prop: $mol_tree2, bidi = true ) {
function args_of( this: $, prop: $mol_tree2, bidi = true ) {

const { key, next } = $mol_view_tree2_prop_parts(prop.type)
const { key, next } = this.$mol_view_tree2_prop_parts(prop)

return prop.struct( '(,)', [
... key ? [ prop.struct( key.length > 1 ? key.slice(1) : 'id' ) ] : [],
Expand All @@ -28,6 +28,18 @@ namespace $ {

}

function call_of(this: $, bind: $mol_tree2, bidi = true) {
const child = this.$mol_view_tree2_child( bind )

return bind.struct( '()', [
child.struct( 'this' ),
child.struct( '[]', [
child.data( name_of.call( this, child ) ),
] ),
args_of.call(this, child, bidi ),
] )
}

type Context = { chain?: string[] }

const localized_string = $$.$mol_tree2_from_string(`
Expand All @@ -45,16 +57,16 @@ namespace $ {
klass: $mol_tree2
addons: $mol_tree2[]
members: $mol_tree2[]
methods: Set<string>
methods: Map<string, $mol_tree2>
},
prop: $mol_tree2
) {
const { klass, members, addons, methods } = acc
const { name, key, next } = $mol_view_tree2_prop_parts(prop.type)
const { name, key, next } = this.$mol_view_tree2_prop_parts(prop)

if (methods.has(name)) return acc

methods.add(name)
methods.set(name, prop)

const decorate = ()=> {
return prop.struct( '()', [
Expand Down Expand Up @@ -88,28 +100,9 @@ namespace $ {
})
},

'<=': bind => [
bind.struct( '()', [
bind.kids[0].struct( 'this' ),
bind.kids[0].struct( '[]', [
bind.kids[0].data( name_of( bind.kids[0] ) ),
] ),
args_of( bind.kids[0], false ),
] ),
],

'<=>': bind => {
const method_name = name_of( bind.kids[0] )
return [
bind.struct( '()', [
bind.kids[0].struct( 'this' ),
bind.kids[0].struct( '[]', [
bind.kids[0].data( method_name ),
] ),
args_of( bind.kids[0], true ),
] ),
]
},
'<=': bind => [ call_of.call(this, bind, false) ],

'<=>': bind => [ call_of.call(this, bind, true) ],

'=>': bind => [],

Expand All @@ -118,7 +111,7 @@ namespace $ {
ref.struct( '()', [
ref.struct( ref.kids[0]?.type ? 'this' : 'super' ),
ref.struct( '[]', [
ref.data( ref.kids[0]?.type ?? name ),
ref.data( ref.kids[0]?.type ? name_of.call(this, ref.kids[0]) : name ),
] ),
ref.struct( '(,)' )
]),
Expand All @@ -138,7 +131,7 @@ namespace $ {
field.data( field_name ),
field.kids[0].type === '<=>'
? field.struct( '=>', [
params_of( field ),
params_of.call(this, field ),
... field.hack( belt ),
] )
: field.hack<Context>( belt, {... context, chain: [...context.chain ?? [], field_name] })[0],
Expand All @@ -165,7 +158,7 @@ namespace $ {

if( over.type === '/' ) continue

const oname = name_of( over )
const oname = name_of.call(this, over )
const bind = over.kids[0]
if( bind.type === '@' ) {
overrides.push(
Expand All @@ -177,7 +170,7 @@ namespace $ {
] ),
] ),
over.struct( '=>', [
params_of( over ),
params_of.call(this, over ),
... localized_string.hack({
'#key': key => [ bind.data( `${ klass.type }_${ name }_${ oname }` ) ],
}),
Expand All @@ -191,20 +184,20 @@ namespace $ {

members.push(
pr.struct( '.', [
pr.data( name_of( pr ) ),
params_of( pr ),
pr.data( name_of.call(this, pr ) ),
params_of.call(this, pr ),
bind.struct( '{;}', [
over.struct( 'return', [
over.struct( '()', [
over.struct( 'this' ),
over.struct( '[]', [
over.data( name ),
] ),
args_of( prop ),
args_of.call(this, prop ),
over.struct( '[]', [
over.data( oname ),
] ),
args_of( over ),
args_of.call(this, over ),
] ),
] )
] ),
Expand All @@ -222,7 +215,7 @@ namespace $ {
] ),
] ),
over.struct( '=>', [
args_of( over ),
args_of.call(this, over ),
over.struct( '()', over.hack( belt )),
] ),
] ),
Expand Down Expand Up @@ -261,7 +254,7 @@ namespace $ {
members.push(
prop.struct( '.', [
prop.data( name ),
params_of( prop ),
params_of.call(this, prop ),
prop.struct( '{;}', [
... next && ! is_delegate ? [
prop.struct( 'if', [
Expand Down Expand Up @@ -295,7 +288,7 @@ namespace $ {
const props = this.$mol_view_tree2_class_props( klass )
const addons = [] as $mol_tree2[]
const members = [] as $mol_tree2[]
const acc = { klass, addons, members, methods: new Set<string>() }
const acc = { klass, addons, members, methods: new Map<string, $mol_tree2>() }

for( const prop of props ) {
try {
Expand Down
2 changes: 1 addition & 1 deletion view/tree2/to/js/test/ex/array_channel_boolean.view.tree
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$mol_view_tree2_to_js_test_ex_array_channel_boolean_foo $mol_view
bar /
bar /boolean
false
true
5 changes: 5 additions & 0 deletions view/tree2/to/js/test/ex/array_indexed.view.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$mol_view_tree2_to_js_test_ex_array_indexed_foo $mol_view
tags* /string
<= tag1* \t1
^ slot* /
<= tag2* \t2
9 changes: 9 additions & 0 deletions view/tree2/to/js/test/ex/array_slot.view.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$mol_view_tree2_to_js_test_ex_array_slot_foo $mol_view
foot /string|number
<= foot1 \foot1
^ insert /string|number
<= ins1 \ins1
^ sub_ins /number
<= sub_ins1 1
<= ins2 \ins2
<= foot2 \foot2
23 changes: 21 additions & 2 deletions view/tree2/to/js/test/js.array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ namespace $ {

'Array channel boolean'( $ ) {
const _foo = $mol_view_tree2_to_js_test_ex_array_channel_boolean_foo

const foo = _foo.make({ $ })
type assert_sub = $mol_type_assert<ReturnType<typeof foo['bar']>, readonly boolean[]>
$mol_assert_like(
_foo.make({ $ }).bar(),
foo.bar(),
[ false, true ],
)

Expand Down Expand Up @@ -60,6 +61,24 @@ namespace $ {
$mol_assert_like(bar.arr()[1], bar.sup()[0])

},

'Array slot' ($) {
const _foo = $mol_view_tree2_to_js_test_ex_array_slot_foo
const foo = _foo.make({ $ })
type assert_foot = $mol_type_assert<ReturnType<typeof foo['foot']>, readonly(string | number)[]>
$mol_assert_like(foo.foot(), [ 'foot1', 'ins1', 1, 'ins2', 'foot2' ])
},

'Array indexed' ($) {
const _foo = $mol_view_tree2_to_js_test_ex_array_indexed_foo
const foo = _foo.make({ $ })
type assert_tag = $mol_type_assert<Parameters<typeof foo['tags']>[0], any>
type assert_slot = $mol_type_assert<Parameters<typeof foo['slot']>[0], any>
type assert_tag1 = $mol_type_assert<Parameters<typeof foo['tag1']>[0], any>
type assert_tag2 = $mol_type_assert<Parameters<typeof foo['tag2']>[0], any>
$mol_assert_like(foo.tags(1), [ 't1', 't2' ])
$mol_assert_like(foo.slot(1), [ 't2' ])
}
})

}
Loading

0 comments on commit fb84d48

Please sign in to comment.