Skip to content

Commit

Permalink
Merge pull request #37 from fink-lang/cleanup
Browse files Browse the repository at this point in the history
cleanup
  • Loading branch information
kollhof authored Apr 13, 2020
2 parents ff0bbbe + b238a19 commit d981810
Show file tree
Hide file tree
Showing 62 changed files with 833 additions and 670 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@fink/cli": "^2.3.0",
"@fink/jest": "^1.1.0",
"@fink/larix": "^4.10.0",
"@fink/loxia": "^4.9.0",
"@fink/loxia": "^4.10.0",
"commitizen": "^4.0.4",
"cz-conventional-changelog": "^3.1.0",
"jest-cli": "^25.3.0",
Expand Down
60 changes: 30 additions & 30 deletions src/index.test.fnk
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
{generate} = import '.'
{fink2js} = import './testing'
{describe, it, to_throw} = import './testing/jest'
{describe, it, expect, to_throw} = import './testing/jest'


describe:: 'errors', fn:
it:: 'throws with code snippet', fn:
(
fn: fink2js(`
123 = foo`
)
) to_throw `
test.fnk:1:0
1| 123 = foo
^

Unable to transform 'assign ='.`

(
fn: generate({
type: 'test',
op: null,
loc: {
start: {pos: 0, line: 1, column: 0},
end: {pos: 9, line: 1, column: 9}
}
}, 'test.fnk', 'foobar')

) to_throw `
test.fnk:1:0
1| foobar
^

Unable to transform 'test'.

Unknown expression`
expect::
fn: fink2js`
123 = foo`

to_throw:: `
test.fnk:1:0
1| 123 = foo
^

Unable to transform 'assign ='.`

expect::
fn: generate({
type: 'test',
op: null,
loc: {
start: {pos: 0, line: 1, column: 0},
end: {pos: 9, line: 1, column: 9}
}
}, 'test.fnk', 'foobar')

to_throw:: `
test.fnk:1:0
1| foobar
^

Unable to transform 'test'.

Unknown expression`
7 changes: 2 additions & 5 deletions src/js/async.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
{isArrowFunctionExpression, isFunctionExpression} = import '@babel/types'


assign = Object.assign


transform_async = fn path:
# TODO: should avoid mutating nodes
match path:
isArrowFunctionExpression:: ?:
path.node assign {async: true}
Object.assign:: path.node, {async: true}

isFunctionExpression:: ?:
path.node assign {async: true}
Object.assign:: path.node, {async: true}

{parentPath: {}}:
transform_async:: path.parentPath
Expand Down
2 changes: 1 addition & 1 deletion src/js/do-expression.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ last_expressions = fn path:

isBlockStatement:: ?:
body = get_body:: path
last = body.(body.length - 1)
[..., last] = body
match last:
isExpressionStatement:: ?:
[last]
Expand Down
12 changes: 5 additions & 7 deletions src/js/types.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ expr_block = fn ...exprs:
...pipe exprs:
map node:
match node:
{type: ?.endsWith:: 'Expression'}: expressionStatement:: node
isIdentifier:: ?: expressionStatement:: node
{type: ?.endsWith:: 'Expression'}:
expressionStatement:: node
isIdentifier:: ?:
expressionStatement:: node
else: node
]

typof = fn value: unaryExpression:: 'typeof', value


split_last = fn [...items]:
[items.slice(0, -1), items.(items.length - 1)]


and = fn left, ...rest:
match rest:
{length: ? > 0}:
Expand Down Expand Up @@ -59,7 +57,7 @@ not_nullish = fn value:

ident = fn name:
match name:
# TODO: typeof name == 'string': identifier(escape_ident(name)) or
# TODO: typeof name == 'string': identifier:: escape_ident:: name or
{constructor: String}: identifier:: escape_ident:: name
else:
# {type: 'Identifier'}
Expand Down
19 changes: 12 additions & 7 deletions src/lang/assignment/index.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ transform_spread_left = fn {left}, {transform}:


slice = fn items, start, end=null:
start_num = numericLiteral:: start

start_end = match end:
null: [numericLiteral(start)]
else: [numericLiteral(start), numericLiteral(end)]
null:
[start_num]
else:
end_num = numericLiteral:: end
[start_num, end_num]

callExpression::
memberExpression::
Expand All @@ -54,18 +59,18 @@ slice = fn items, start, end=null:
start_end


transform_spread_right = fn node, ctx:
transform_spread_right = fn {left, right}, ctx:
items = ctx.unique_ident:: 'items'
items_init = variableDeclaration::
'const'
[
variableDeclarator::
arrayPattern:: [restElement:: items]
transform_value:: node.right, ctx
transform_value:: right, ctx
]

len = node.left.exprs.length - 1
idx = node.left.exprs.findIndex:: fn expr:
len = left.exprs.length - 1
idx = left.exprs.findIndex:: fn expr:
match expr:
{type: 'spread'}: true
else: false
Expand Down Expand Up @@ -98,7 +103,7 @@ transform_assign = fn node, ctx:
else:
left = transform_left:: ctx.transform:: node.left
right = transform_value:: node.right, ctx
# wrap(node, assignmentExpression('=', left, right))
# wrap with loc and comment?
assignmentExpression:: '=', left, right


Expand Down
56 changes: 31 additions & 25 deletions src/lang/assignment/index.test.fnk
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
{fink2js} = import '../../testing'
{describe, it, eq, snapshot} = import '../../testing/jest'
{describe, it, expect, to_match_snapshot} = import '../../testing/jest'


describe:: 'assignment', fn:
it:: 'compiles', fn:
fink2js(`
assign = 1
# simple
assign2 = assign
expect::
fink2js`
assign = 1
# simple
assign2 = assign

no_clash_with_await = 13
no_clash_with_await = 13

assign_lang_const1 = true
assign_lang_const2 = false
assign_lang_const1 = true
assign_lang_const2 = false

multi_line_assign = 123
+ 234 +
-567
- 1111
`) eq snapshot
multi_line_assign = 123
+ 234 +
-567
- 1111
`
to_match_snapshot


it:: 'compiles destructuring', fn:
fink2js(`
# standard JS destructuring
[...head, last] = '1234'
`) eq snapshot


fink2js(`
# enhanced destructuring
[foo, bar, ...spam, ni, shrub] = '123'

[a, , ..., b, c] = '123'
`) eq snapshot
expect::
fink2js`
# standard JS destructuring
[...head, last] = '1234'
`
to_match_snapshot


expect::
fink2js`
# enhanced destructuring
[foo, bar, ...spam, ni, shrub] = '123'

[a, , ..., b, c] = '123'
`
to_match_snapshot
28 changes: 15 additions & 13 deletions src/lang/async/index.test.fnk
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
{fink2js} = import '../../testing'
{describe, it, eq, snapshot} = import '../../testing/jest'
{describe, it, expect, to_match_snapshot} = import '../../testing/jest'


describe:: 'await', fn:
it:: 'compiles', fn:

fink2js(`
task1 = fn foo: - await foo
expect::
fink2js`
task1 = fn foo: - await foo

task2 = fn foo: await (foo + 4)
task2 = fn foo: await (foo + 4)

task3 = fn foo:
bar = await foo()
bar + 123
task3 = fn foo:
bar = await foo()
bar + 123

a_gen = unfold curr=0:
match shrub:
spam: await ni(curr)
else : curr + 1
a_gen = unfold curr=0:
match shrub:
spam: await ni(curr)
else : curr + 1

await ni
`) eq snapshot
await ni
`
to_match_snapshot



11 changes: 6 additions & 5 deletions src/lang/block/index.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
{consts} = import '../../js/types'


block_statement = fn {transform}: fn expr:
block_statement = fn expr, {transform}:
st = transform:: expr

match st:
isAssignmentExpression:: ?:
decl = consts:: st.left, st.right
{...decl, leadingComments: st.leadingComments}
else:
# TODO: check if (isExpression(st)) ...
# TODO: check if isExpression:: ?: ...
expressionStatement:: st


Expand All @@ -26,6 +26,7 @@ transform_block = fn node, ctx:
ctx.transform:: expr
else:
doExpression::
blockStatement::
exprs.map:: block_statement:: ctx

blockStatement:: [
...pipe exprs:
map expr: block_statement:: expr, ctx
]
2 changes: 1 addition & 1 deletion src/lang/call/call.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ transform_call = fn node, ctx:
map expr:
ctx.transform:: expr

{...callExpression(callee, args)}
{...callExpression:: callee, args}
16 changes: 9 additions & 7 deletions src/lang/call/call.test.fnk
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{fink2js} = import '../../testing'
{describe, it, eq, snapshot} = import '../../testing/jest'
{describe, it, expect, to_match_snapshot} = import '../../testing/jest'


describe:: 'call', fn:
it:: 'compiles', fn:

fink2js(`
call1 = a(ni, x=123, ...x)
call2 = a(ni)
call3 = a ()
call4 = a(fn x: x*2)
`) eq snapshot
expect::
fink2js`
call1 = a(ni, x=123, ...x)
call2 = a(ni)
call3 = a ()
call4 = a(fn x: x*2)
`
to_match_snapshot



20 changes: 11 additions & 9 deletions src/lang/call/pipe.test.fnk
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{fink2js} = import '../../testing'
{describe, it, eq, snapshot} = import '../../testing/jest'
{describe, it, expect, to_match_snapshot} = import '../../testing/jest'


describe:: 'pipe', fn:
it:: 'compiles', fn:

fink2js(`
pipe:
foo
bar
spam
expect::
fink2js`
pipe:
foo
bar
spam

pipe [1, 2, 3]:
map item: item * 2
`) eq snapshot
pipe [1, 2, 3]:
map item: item * 2
`
to_match_snapshot

Loading

0 comments on commit d981810

Please sign in to comment.