Skip to content

Commit da359cd

Browse files
committed
Fixed broken javascript tests. Added specs for translate functions.
1 parent ee4f652 commit da359cd

File tree

7 files changed

+49
-25
lines changed

7 files changed

+49
-25
lines changed

lib/elixir_script/prelude/macro/env.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
defmodule ElixirScript.Macro.Env do
2+
@type t :: %ElixirScript.Macro.Env{
3+
module: atom,
4+
file: binary,
5+
line: non_neg_integer,
6+
function: { atom, non_neg_integer } | nil,
7+
context: :match | :guard | nil,
8+
aliases: [{atom, atom}],
9+
requires: [atom],
10+
functions: [{atom, [{ atom, non_neg_integer }]}],
11+
macros: [{atom, [{ atom, non_neg_integer }]}],
12+
macro_aliases: [{atom, {integer, atom}}],
13+
context_modules: [atom],
14+
vars: [{atom, atom | non_neg_integer}],
15+
export_vars: [{atom, atom | non_neg_integer}] | nil,
16+
lexical_tracker: nil,
17+
caller: t | nil
18+
}
19+
220
defstruct [
321
module: nil,
422
file: nil,

lib/elixir_script/translator.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule ElixirScript.Translator do
22
@moduledoc """
3-
Translates the given Elixir AST into JavaScript AST
3+
Translates Elixir AST into JavaScript AST
44
"""
55
alias ElixirScript.Translator.Primitive
66
alias ElixirScript.Translator.Expression
@@ -25,12 +25,20 @@ defmodule ElixirScript.Translator do
2525

2626

2727
@doc """
28-
Translates Elixir AST to JavaScript AST
28+
Translates the given Elixir AST to JavaScript AST. The given `env` is a `ElixirScript.Macro.Env`
29+
used to track the variables, imports, aliases, and scopes like `Macro.Env`. The JavaScript AST and
30+
the an updated `ElixirScript.Macro.Env` is returned
2931
"""
32+
@spec translate(term, ElixirScript.Macro.Env.t) :: { ESTree.Node.t, ElixirScript.Macro.Env.t }
3033
def translate(ast, env) do
3134
do_translate(ast, env)
3235
end
3336

37+
38+
@doc """
39+
Same as `translate/2`, but returns only the JavaScript AST
40+
"""
41+
@spec translate!(term, ElixirScript.Macro.Env.t) :: ESTree.Node.t
3442
def translate!(ast, env) do
3543
{ js_ast, _ } = translate(ast, env)
3644
js_ast

priv/Elixir.js

Lines changed: 12 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/javascript/lib/core/functions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PID, Tuple, Integer, Float } from './primitives';
1+
import { Tuple } from './primitives';
22
import BitString from './bit_string';
33
import Patterns from './patterns';
44

src/javascript/tests/bit_string.spec.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var BitString = require('../lib/core').BitString;
2-
var SpecialForms = require('../lib/core').SpecialForms;
32
var Patterns = require('../lib/core').Patterns;
43
var expect = require('chai').expect;
54

@@ -11,19 +10,19 @@ describe('BitString', function(){
1110

1211
describe('creation', function(){
1312
it('create properly', function(){
14-
let bs = SpecialForms.bitstring(BitString.integer(1));
13+
let bs = new BitString(BitString.integer(1));
1514
expect(is_match(bs.value, [1])).to.equal(true);
1615

17-
bs = SpecialForms.bitstring(BitString.binary("foo"));
16+
bs = new BitString(BitString.binary("foo"));
1817
expect(is_match(bs.value, [102, 111, 111])).to.equal(true);
1918

20-
bs = SpecialForms.bitstring(BitString.integer(0), BitString.binary("foo"));
19+
bs = new BitString(BitString.integer(0), BitString.binary("foo"));
2120
expect(is_match(bs.value, [0, 102, 111, 111])).to.equal(true);
2221

23-
bs = SpecialForms.bitstring(BitString.float(3.14));
22+
bs = new BitString(BitString.float(3.14));
2423
expect(is_match(bs.value, [64, 9, 30, 184, 81, 235, 133, 31])).to.equal(true);
2524

26-
bs = SpecialForms.bitstring(BitString.signed(BitString.integer(-100)));
25+
bs = new BitString(BitString.signed(BitString.integer(-100)));
2726
expect(is_match(bs.value, [156])).to.equal(true);
2827
});
2928
});

src/javascript/tests/case.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Patterns = require("../lib/core/patterns");
22
var Enum = require('../lib/enum');
3+
var Tuple = require('../lib/core').Tuple;
34
var SpecialForms = require('../lib/core').SpecialForms;
45
var expect = require('chai').expect;
56

@@ -9,7 +10,7 @@ describe('case', () => {
910
it('case', () => {
1011
let clauses = [
1112
Patterns.make_case(
12-
[SpecialForms.tuple(Symbol.for("selector"), Patterns.variable(), Patterns.variable())],
13+
[new Tuple(Symbol.for("selector"), Patterns.variable(), Patterns.variable())],
1314
function(i, value){ return value; },
1415
function(i){ return Kernel.is_integer(i); }
1516
),

src/javascript/tests/special_forms.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('SpecialForms', function(){
55

66
describe('map_update', function(){
77
it('creates new object', function(){
8-
const foo = SpecialForms.map({foo: "bar", fizz: "buzz"});
8+
const foo = Object.freeze({foo: "bar", fizz: "buzz"});
99
const bar = SpecialForms.map_update(foo, {baz: "bar", fizz: "fizzbuzz"});
1010

1111
expect(foo instanceof Object).to.equal(bar instanceof Object);

0 commit comments

Comments
 (0)