Skip to content

Commit

Permalink
feat(import): add support for module imports
Browse files Browse the repository at this point in the history
  • Loading branch information
kollhof committed Feb 26, 2020
1 parent 61bb84d commit 39363bd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {transform_module} from './transform/module';
import {transform_unary} from './transform/unary';
import {transform_member} from './transform/member';
import {transform_inifx} from './transform/infix';
import {transform_import} from './transform/import';
import {transform_other, escape_ident, var_prefix} from './transform/other';

import {
Expand Down Expand Up @@ -69,7 +70,8 @@ const unary_ops = {
arithm_prefix: transform_unary,
await: transform_await,
'...': transform_spread,
'!': transform_unary
'!': transform_unary,
import: transform_import
};

const binary_ops = {
Expand Down Expand Up @@ -151,7 +153,7 @@ const transform_expr = (node, ctx)=> {
const transform = get_transformer(node.op, node.type);

if (transform === undefined) {
throw code_frame_err(new Error('Unknown expression'), node, ctx.code);
throw code_frame_err(new Error('Unknown expression'), node, ctx);
}

try {
Expand All @@ -160,14 +162,15 @@ const transform_expr = (node, ctx)=> {
const wrapped = wrap(node, foo);
return wrapped;
} catch (err) {
throw code_frame_err(err, node, ctx.code);
throw code_frame_err(err, node, ctx);
}
};


const transform = (node, code)=> {
const transform = (node, code, filename)=> {
let id_ctr = 0;
const ast = transform_expr(node, {
filename,
code,
unique_ident: (name)=> {
id_ctr += 1;
Expand All @@ -185,7 +188,7 @@ const transform = (node, code)=> {


export const generate = (ast, filename, code)=> {
const new_ast = transform(ast, code);
const new_ast = transform(ast, code, filename);

const options = {
// retainLines: true,
Expand Down
6 changes: 3 additions & 3 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test('errors', ()=> {
123 = foo
`)
).toThrow(unindent_text(`
Transform Error 'assign =':
test.fnk:1:0: Unable to transform 'assign =':
1| 123 = foo
^
Expand All @@ -18,15 +18,15 @@ test('errors', ()=> {
expect(
()=> generate({
type: 'test',
op: '####',
op: null,
loc: {
start: {pos: 0, line: 1, column: 0},
end: {pos: 9, line: 1, column: 9}
}
}, 'test.fnk', 'foobar')

).toThrow(unindent_text(`
Transform Error 'test ####':
test.fnk:1:0: Unable to transform 'test':
1| foobar
^
Expand Down
9 changes: 9 additions & 0 deletions src/transform/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {identifier} from '@babel/types';
import {call} from '../types';


export const transform_import = (node, ctx)=> {
const right = ctx.transform(node.right);
return call(identifier('require'))(right);
};

12 changes: 12 additions & 0 deletions src/transform/import.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {fink2js} from '../testing';


test('compiles attempt', ()=> {
expect(
fink2js(`
{foo} = import \`./spam\`
`)
).toMatchSnapshot();
});


0 comments on commit 39363bd

Please sign in to comment.