Skip to content

Commit

Permalink
fix(traverse): add missing ast traversals
Browse files Browse the repository at this point in the history
  • Loading branch information
kollhof committed Nov 18, 2020
1 parent 8bddd7b commit 39474d8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
29 changes: 15 additions & 14 deletions src/ast/analyze.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ get_expr_id = fn expr:



get_parent = fn expr, {parents}:
# {(get_expr_id expr): parent_expr=false} = parents
{(get_expr_id expr): parent_expr} = parents
parent_expr


is_parent = fn parent, expr, ctx:
match expr:
false: false
Expand All @@ -29,13 +35,6 @@ is_parent = fn parent, expr, ctx:



get_parent = fn expr, {parents}:
# {(get_expr_id expr): parent_expr=false} = parents
{(get_expr_id expr): parent_expr} = parents
parent_expr



add_parents = map [expr, parent], , {parents={}, ...ctx}:
expr_id = get_expr_id expr

Expand All @@ -53,8 +52,9 @@ get_scope = fn expr, ctx:
parent = get_parent expr, ctx

match parent:
# false:
# expr
--- istanbul ignore next ---
false:
expr

{type: 'module'}:
parent
Expand Down Expand Up @@ -83,7 +83,7 @@ may_bind = fn expr, ctx:
{type: 'rec', exprs: expr in ?}:
may_bind parent, ctx

{type: 'rec:kv', left: parent.right, right: expr}:
{type: 'rec:kv', left: expr, right: expr}:
may_bind parent, ctx

{type: 'rec:kv', right: expr}:
Expand All @@ -98,8 +98,8 @@ may_bind = fn expr, ctx:
{type: 'group', exprs: expr in ?}:
may_bind parent, ctx

# {right: expr}:
# may_bind parent, ctx
{type: 'spread'}:
may_bind parent, ctx

else:
false
Expand All @@ -122,8 +122,8 @@ is_referencing = fn expr, ctx:
{type: 'ident'}:
parent = get_parent expr, ctx
match parent:
{type: 'rec:kv', left: ? == expr}: false
{type: 'member', right: ? == expr}: false
{type: 'rec:kv', left: expr, right: ? != expr}: false
{type: 'member', right:expr}: false
else: true

{type: 'jsx:attr', value: {}}:
Expand All @@ -148,6 +148,7 @@ find_binding = fn expr, ctx:
{type: 'jsx:elem'}: expr.name
{type: 'jsx:attr'}: expr.name


[binding=false] = pipe ctx.bindings:
reverse
filter {value}: name == value
Expand Down
11 changes: 7 additions & 4 deletions src/ast/analyze.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ collect_duplicate_bindings = fn parsed:




analyze = fn source, collector:
pipe source:
parse ?, 'test.fnk'
Expand All @@ -56,11 +55,13 @@ analyze = fn source, collector:





source = '
foo = 1234

bar = fn spam, ham:
ni = spam.ni + ham"\${foo}"
bar = fn spam, {...ham}:
ni = spam.ni + ham"\${foo.spam}"
ni + undef

{ni, na: {nu}} = {foo}
Expand All @@ -69,7 +70,7 @@ source = '
match bar foo:
nu:
ni = foo
ni
[...ni]

<div />

Expand All @@ -83,6 +84,8 @@ source = '
foo

[ni] = [1234]
na = [...ni]
fn foo: {foo}
'


Expand Down
15 changes: 11 additions & 4 deletions src/ast/analyze.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Array [
"ident:foo@1:0",
"ident:bar@3:0",
"ident:spam@3:9",
"ident:ham@3:15",
"ident:ham@3:19",
"ident:ni@4:2",
"ident:ni@7:1",
"ident:nu@7:10",
Expand All @@ -14,6 +14,8 @@ Array [
"ident:Spam@17:0",
"ident:children@17:11",
"ident:ni@26:1",
"ident:na@27:0",
"ident:foo@28:3",
]
`;

Expand All @@ -26,13 +28,14 @@ Array [
exports[`analyze gets refs 1`] = `
Array [
"ident:foo@4:23 -> ident:foo@1:0",
"ident:foo@7:18 -> ident:foo@1:0",
"ident:foo@10:10 -> ident:foo@1:0",
"ident:foo@12:9 -> ident:foo@1:0",
"ident:foo@24:2 -> ident:foo@1:0",
"ident:bar@8:24 -> ident:bar@3:0",
"ident:bar@10:6 -> ident:bar@3:0",
"ident:spam@4:7 -> ident:spam@3:9",
"ident:ham@4:17 -> ident:ham@3:15",
"ident:ham@4:17 -> ident:ham@3:19",
"ident:ni@5:2 -> ident:ni@4:2",
"ident:undef@5:7 -> unbound",
"jsx:elem:div@15:0 -> unbound",
Expand All @@ -43,9 +46,11 @@ Array [
"ident:nu@8:12 -> ident:nu@7:10",
"ident:nu@8:35 -> ident:nu@7:10",
"ident:nu@11:2 -> ident:nu@7:10",
"ident:ni@13:4 -> ident:ni@12:4",
"ident:ni@13:8 -> ident:ni@12:4",
"jsx:elem:Spam@20:0 -> ident:Spam@17:0",
"ident:children@18:5 -> ident:children@17:11",
"ident:ni@27:9 -> ident:ni@26:1",
"ident:foo@28:9 -> ident:foo@28:3",
]
`;

Expand All @@ -54,7 +59,7 @@ Array [
"ident:foo@1:0 module@1:0",
"ident:bar@3:0 module@1:0",
"ident:spam@3:9 block:fn@3:6",
"ident:ham@3:15 block:fn@3:6",
"ident:ham@3:19 block:fn@3:6",
"ident:ni@4:2 block:fn@3:6",
"ident:ni@7:1 module@1:0",
"ident:nu@7:10 module@1:0",
Expand All @@ -63,5 +68,7 @@ Array [
"ident:Spam@17:0 module@1:0",
"ident:children@17:11 block:fn@17:7",
"ident:ni@26:1 module@1:0",
"ident:na@27:0 module@1:0",
"ident:foo@28:3 block:fn@28:0",
]
`;
6 changes: 5 additions & 1 deletion src/ast/traverse.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ get_child_exprs = fn expr:
{left: {}, right: {}}:
[expr.left, expr.right]

# spread
{right: {}}:
[expr.right]

# {left: {}}:
# [expr.left]

{exprs: []}:
expr.exprs

else:
# console.log '----------------', expr.type, expr.value
[]


Expand All @@ -52,3 +55,4 @@ walk = fn exprs, parent=false:
... [[expr, parent]] | chain walk children, expr



0 comments on commit 39474d8

Please sign in to comment.