Skip to content

Commit

Permalink
refactor: make rest of transforms non-stateful
Browse files Browse the repository at this point in the history
BREAKING CHANGE: transform errors have different structure
  • Loading branch information
kollhof committed Oct 26, 2020
1 parent 15e5a56 commit 0fd1576
Show file tree
Hide file tree
Showing 28 changed files with 235 additions and 308 deletions.
30 changes: 14 additions & 16 deletions src/generate.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,35 @@ babel_traverse = import '@babel/traverse'
{default: traverse} = babel_traverse

{transformFromAstSync} = import '@babel/core'
{is_empty} = import '@fink/std-lib/iter.fnk'

{transform_ast} = import './lang/init.fnk'
{transform} = import './lang/transform.fnk'
{init_ctx} = import './lang/init.fnk'

{transform_do_expr} = import './js/do-expression.fnk'
{transform_async} = import './js/async.fnk'
{module_transforms} = import './js/module.fnk'



transform = fn node, code, filename, options:
ast = transform_ast node, code, filename, options
transform_file = fn fink_ast, code, filename, options:
ctx = init_ctx code, filename

[error, [js_ast]=[]] = try:
transform fink_ast, {...ctx, options}

extras = match options:
{module_type: 'cjs'}: module_transforms
else: {}

match ast:
{errors: is_empty ?}:
traverse ast, dict:
match error:
false:
traverse js_ast, dict:
DoExpression: transform_do_expr
AwaitExpression: {enter: transform_async}
...extras
ast
{...js_ast, errors: []}
else:
{errors: [error]}



Expand All @@ -51,17 +56,10 @@ babel_generate = fn ast, filename, source, options:


generate = fn ast, filename, source, options={}:
js_ast = transform ast, source, filename, options
js_ast = transform_file ast, source, filename, options

match js_ast:
{errors: [{}]}:
{code: '', source_map: '', errors: js_ast.errors}
else:
babel_generate js_ast, filename, source, options







12 changes: 8 additions & 4 deletions src/generate.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ describe 'errors', fn:
'

it 'errors with code snippet', fn:
{errors: [{error}]} = fink2js '
{errors: [{message}]} = fink2js '
foo = bar
123 = foo
shrub = ni'

expect
error
message
to_equal '
test.fnk:2:0
1| foo = bar
Expand All @@ -66,6 +66,8 @@ describe 'errors', fn:
3| shrub = ni

Unable to transform `assign =`.

TypeError: Property left of AssignmentExpression expected node to be of a type ["LVal"] but instead got "NumericLiteral"
'

it 'errors with bad babel options', fn:
Expand All @@ -79,7 +81,7 @@ describe 'errors', fn:


it 'errors when provided with unknown tokens', fn:
{errors: [{error}]} = generate
{errors: [{message}]} = generate
dict:
type: 'test'
op: 'foobar'
Expand All @@ -90,11 +92,13 @@ describe 'errors', fn:
'test.fnk', 'foobar'

expect
error
message
to_equal '
test.fnk:1:0
1| foobar
^

Unable to transform `test foobar`.

Unknown expression.
'
3 changes: 0 additions & 3 deletions src/js/identifier.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ check_ident = rx'
[_$\p{L}][_$\p{L}\p{N}]*$'


# TODO: move into ctx
var_prefix = 'ˆ'

str_36 = base_n ?, 36


Expand Down
11 changes: 6 additions & 5 deletions src/lang/assignment/init.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ babel_types = import '@babel/types'
} = babel_types
{length, is_empty} = import '@fink/std-lib/iter.fnk'

{unique_ident} = import '../../js/types.fnk'
{transform_left: xform_left} = import '../../js/left.fnk'
{wrap_with_comment_loc} = import '../comments/init.fnk'
{add, any} = import '../context.fnk'
Expand Down Expand Up @@ -85,9 +86,9 @@ slice = fn items, start, end=false:

transform_spread_right = fn expr, ctx:
{left, right} = expr
items = ctx.unique_ident 'items'
[items, init_ctx] = unique_ident 'items', ctx
# TODO: wrap declarator?
[init, next_ctx] = transform_value right, ctx
[init, next_ctx] = transform_value right, init_ctx

items_init = wrap_with_comment_loc
variableDeclaration
Expand Down Expand Up @@ -141,10 +142,10 @@ transform_await_left = fn node, ctx:

transform_await_right = fn expr, ctx:
{left, right} = expr
items = ctx.unique_ident 'items'
iter = ctx.unique_ident 'iter'
[items, iter_ctx] = unique_ident 'items', ctx
[iter, init_ctx] = unique_ident 'iter', iter_ctx

[init, next_ctx] = transform_value right, ctx
[init, next_ctx] = transform_value right, init_ctx

items_init = wrap_with_comment_loc
variableDeclaration 'const', [variableDeclarator items, init]
Expand Down
9 changes: 2 additions & 7 deletions src/lang/block/init.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ babel_types = import '@babel/types'

{add, any} = import '../context.fnk'
{wrap_with_comment_loc} = import '../comments/init.fnk'
{transform, collect_with_ctx} = import '../transform.fnk'
{transform, map_with_ctx} = import '../transform.fnk'



Expand All @@ -33,12 +33,7 @@ block_statement = fn expr, ctx:

exprs_block = fn exprs, ctx:
pipe exprs:
# TODO: duplicate in other modules
map expr, expr_ctx=ctx:
[js_expr, next_ctx] = block_statement expr, expr_ctx
([js_expr, next_ctx], next_ctx)

collect_with_ctx ctx
map_with_ctx ctx, block_statement



Expand Down
17 changes: 5 additions & 12 deletions src/lang/call/call.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,19 @@ babel_types = import '@babel/types'
{callExpression, identifier} = babel_types
{is_empty, length} = import '@fink/std-lib/iter.fnk'

{transform, collect_with_ctx} = import '../transform.fnk'
{transform, map_with_ctx} = import '../transform.fnk'



transform_args = fn args, ctx:
[js_args, next_ctx] = pipe args:
map expr, arg_ctx=ctx:
[arg, next_ctx] = match expr:
pipe args:
map_with_ctx ctx, fn expr, arg_ctx:
match expr:
{type: 'empty'}:
undef = identifier 'undefined'
[undef, ctx]
[(identifier 'undefined'), ctx]
else:
transform expr, arg_ctx

([arg, next_ctx], next_ctx)

collect_with_ctx ctx

[js_args, next_ctx]



transform_single_arg = fn [expr], ctx:
Expand Down
14 changes: 6 additions & 8 deletions src/lang/call/pipe.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,35 @@ babel_types = import '@babel/types'
{callExpression} = babel_types
{is_empty} = import '@fink/std-lib/iter.fnk'

{assign, lets, expr_block, undef} = import '../../js/types.fnk'
{assign, lets, expr_block, undef, unique_ident} = import '../../js/types.fnk'
{transform_value} = import '../partial/init.fnk'
{wrap_with_comment_loc} = import '../comments/init.fnk'
{transform, collect_with_ctx} = import '../transform.fnk'
{transform, map_with_ctx} = import '../transform.fnk'



transform_pipe = fn node, ctx:
{unique_ident} = ctx
{exprs} = node

[start_value, pipe_ctx] = match node.args:
[start_value, id_ctx] = match node.args:
is_empty ?:
[(undef _), ctx]
else:
[arg] = node.args
transform arg, ctx

result = unique_ident 'pipe_result'
[result, pipe_ctx] = unique_ident 'pipe_result', id_ctx

[pipe_calls, next_ctx] = pipe exprs:
map expr, callee_ctx=pipe_ctx:
map_with_ctx pipe_ctx, fn expr, callee_ctx:
[callee, next_ctx] = transform_value expr, callee_ctx

js = wrap_with_comment_loc
assign result, callExpression callee, [result]
expr

([js, next_ctx], next_ctx)
[js, next_ctx]

collect_with_ctx ctx

js = expr_block
wrap_with_comment_loc
Expand Down
Loading

0 comments on commit 0fd1576

Please sign in to comment.