Skip to content

Commit

Permalink
Merge pull request #81 from fink-lang/import-explicit-ext
Browse files Browse the repository at this point in the history
feat(import): support explicit file ext in import URLs and translate .fnk to .js
  • Loading branch information
kollhof authored Oct 3, 2020
2 parents e2de1d4 + e06c4dd commit 5f7ac4d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/js/do-expression.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
isReturnStatement, arrowFunctionExpression, callExpression
} = import '@babel/types'
{set_props} = import '@fink/js-interop/reflect'
{undefined} = import '@fink/js-interop/nullish'
{is_empty} = import '@fink/std-lib/iter'
{wrap_with_comment_loc} = import '../lang/comments'

Expand All @@ -13,10 +14,9 @@

get_body = fn path: path.get 'body'

[no_result] = []

# TODO: suport _ or nothing instead of having to use item
consume_all = fold item: no_result
consume_all = fold item: undefined


simple = fn body, sl=false:
Expand Down Expand Up @@ -77,7 +77,7 @@ last_expressions = fn path:
else:
# TODO: don't use mutation
items.push last
no_result
undefined
items

else:
Expand Down Expand Up @@ -174,4 +174,4 @@ transform_do_expr = fn path:
path.replaceWith
callExpression arrow, []

no_result
undefined
22 changes: 21 additions & 1 deletion src/lang/module/import.fnk
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
{callExpression, Import: async_import} = import '@babel/types'
{ends_with, slice} = import '@fink/std-lib/str'



resolve_ext = fn import_url:
match import_url:
ends_with ?, '.fnk':
'${slice import_url, 0, -4}.js'
else:
import_url



transform_import = fn node, {transform}:
right = transform node.right
right = match node.right:
{type: 'string', exprs: [..., {type: 'string:text'}]}:
[...exprs, url] = node.right.exprs

transform dict:
...node.right
exprs: [...exprs, {...url, value: resolve_ext url.value}]
else:
transform node.right

callExpression
async_import _
[right]
8 changes: 5 additions & 3 deletions src/lang/module/index.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

{wrap_with_comment_loc} = import '../comments'
{block_statement} = import '../block'
{transform_import: transform_async_import} = import './import'
{transform_import: transform_async_import, resolve_ext} = import './import'



# TODO should larix handle shebang as special expr?
Expand All @@ -24,6 +25,7 @@ get_shebang = fn expr:
[[], expr]



transform_import = fn node, {transform}:
[...imports] = match node.left:
{type: 'ident'}:
Expand All @@ -47,7 +49,7 @@ transform_import = fn node, {transform}:
importDeclaration
imports
wrap_with_comment_loc
stringLiteral url.value
stringLiteral resolve_ext url.value
node.right.right


Expand All @@ -62,7 +64,7 @@ transform_module = fn node, ctx:
{op: 'import'}:
{right: {exprs: [url]}} = expr
wrap_with_comment_loc
importDeclaration [], stringLiteral url.value
importDeclaration [], stringLiteral resolve_ext url.value
expr

{right: {op: 'import'}}:
Expand Down
23 changes: 14 additions & 9 deletions src/lang/module/index.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,25 @@ describe 'module', fn:

# leading comment

# imp comment
# static imports
import 'shrub'
import '@fink/foo/bar.fnk'

# imp comment 2
foo = import './foo'
# default import
foo = import './foo.fnk'

# imp comment 3
{bar} = import './spam'
{foo: spam} = import './shrub'
# import exported values
{bar} = import './spam.fnk'
{foo: spam} = import './shrub.fnk'

# expr comment
shrub = foo + bar
ni = await import spam
# dynamic imports with static URL
ni = await import './shrub.fnk'

# dynamic imports with dynamic URLs
shrub = foo + bar
ni2 = await import shrub
ni3 = await import '\${shrub}'
ni4 = await import './\${shrub}.fnk'
# trailing comment
"
to_match_snapshot
Expand Down
21 changes: 13 additions & 8 deletions src/lang/module/index.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
exports[`module handles comments 1`] = `
"#!/usr/bin/env node
// leading comment
// imp comment
// static imports
import \\"shrub\\";
// imp comment 2
import foo from \\"./foo\\";
// imp comment 3
import { bar } from \\"./spam\\";
import { foo as spam } from \\"./shrub\\";
// expr comment
import \\"@fink/foo/bar.js\\";
// default import
import foo from \\"./foo.js\\";
// import exported values
import { bar } from \\"./spam.js\\";
import { foo as spam } from \\"./shrub.js\\";
// dynamic imports with static URL
export const ni = await import(\`./shrub.js\`);
// dynamic imports with dynamic URLs
export const shrub = foo + bar;
export const ni = await import(spam);
export const ni2 = await import(shrub);
export const ni3 = await import(\`\${shrub}\`);
export const ni4 = await import(\`./\${shrub}.js\`);
// trailing comment"
`;

0 comments on commit 5f7ac4d

Please sign in to comment.