Skip to content

Commit fb20545

Browse files
committed
Changed aliases to import namespace specifiers. Added default option to alias, Made it so object keys are now computed
1 parent bf593e0 commit fb20545

36 files changed

Lines changed: 476 additions & 360 deletions

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
* Enhancement
33
* Added `JS` module with `new` and `mutate` functions
44
* All Standard libraries are rolled up into one elixir.js file and imported from that
5-
* Added DOM module for creating virtual DOM nodes, and diffing them and applying patches to real DOM
5+
* Added `DOM` module for creating virtual DOM nodes, and diffing them and applying patches to real DOM
6+
* Added `Fetch` module to wrap fetch API
7+
* Added `Keyword` module with functions, `has_key?` and `get`
8+
* Added `Agent` module with functions, `start`, `get`, `update`, and `get_and_update`
69

710
* Breaking
811
* import - `only` option is the only one allowed

lib/elixir_script.ex

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ defmodule ElixirScript do
4141
def transpile_quoted(quoted, opts \\ []) do
4242
include_path = Dict.get(opts, :include_path, false)
4343
root = Dict.get(opts, :root)
44-
env = Dict.get(opts, :env, __ENV__)
44+
env = Dict.get(opts, :env, custom_env)
4545

4646
case Translator.translate(quoted, env) do
4747
modules when is_list(modules) ->
4848
List.flatten(modules)
4949
|> Enum.map(fn(x) ->
50-
convert_to_code(x, root, include_path)
50+
convert_to_code(x, root, include_path, env)
5151
end)
5252
module ->
5353
List.wrap(
54-
convert_to_code(module, root, include_path)
54+
convert_to_code(module, root, include_path, env)
5555
)
5656
end
5757
end
@@ -63,7 +63,7 @@ defmodule ElixirScript do
6363
def transpile_path(path, opts \\ []) do
6464
include_path = Dict.get(opts, :include_path, false)
6565
root = Dict.get(opts, :root)
66-
env = Dict.get(opts, :env, __ENV__)
66+
env = Dict.get(opts, :env, custom_env)
6767

6868
path
6969
|> Path.wildcard
@@ -74,10 +74,16 @@ defmodule ElixirScript do
7474
end)
7575
|> List.flatten
7676
|> Enum.map(fn(x) ->
77-
convert_to_code(x, root, include_path)
77+
convert_to_code(x, root, include_path, env)
7878
end)
7979
end
8080

81+
defp custom_env() do
82+
require Logger
83+
require ElixirScript.Lib.JS
84+
__ENV__
85+
end
86+
8187
@doc """
8288
Copies the javascript that makes up the ElixirScript standard libs
8389
to the specified location
@@ -86,27 +92,27 @@ defmodule ElixirScript do
8692
File.cp_r!(operating_path <> "/dist", destination)
8793
end
8894

89-
defp convert_to_code(js_ast, root, include_path) do
95+
defp convert_to_code(js_ast, root, include_path, env) do
9096
js_ast
91-
|> process_module(root)
97+
|> process_module(root, env)
9298
|> javascript_ast_to_code
9399
|> process_include_path(include_path)
94100
end
95101

96-
defp process_module(%JSModule{} = module, root) do
102+
defp process_module(%JSModule{} = module, root, env) do
97103
file_path = create_file_name(module)
98104

99-
program = create_standard_lib_imports(root) ++ module.body
105+
program = create_standard_lib_imports(root, env) ++ module.body
100106
|> ESTree.Tools.Builder.program
101107

102108
{ file_path, program }
103109
end
104110

105-
defp process_module(module, _root) do
111+
defp process_module(module, _root, _) do
106112
{ "", module }
107113
end
108114

109-
defp create_standard_lib_imports(root) do
115+
defp create_standard_lib_imports(root, env) do
110116
module_name_list = [:Elixir]
111117
options = [
112118
from: root(root) <> "elixir",
@@ -118,7 +124,7 @@ defmodule ElixirScript do
118124
]
119125
]
120126

121-
[ElixirScript.Translator.Import.make_import(module_name_list, options)]
127+
[ElixirScript.Translator.Import.make_import(module_name_list, options, env)]
122128
end
123129

124130
defp root(nil) do

lib/elixir_script/lib/dom.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule ElixirScript.Lib.DOM do
22
alias ESTree.Tools.Builder, as: JS
33
alias ElixirScript.Translator
44

5+
56
def translate_dom_function(name, params, env) do
67
do_translate({name, [], params}, env)
78
end
@@ -41,7 +42,15 @@ defmodule ElixirScript.Lib.DOM do
4142
end
4243

4344
defp config_to_map(config) do
45+
config = Enum.map(config, fn({key, value}) ->
46+
if is_atom(key) do
47+
{Atom.to_string(key), value}
48+
else
49+
{key, value}
50+
end
51+
end)
52+
4453
{:%{}, [], config}
4554
end
4655

47-
end
56+
end

lib/elixir_script/lib/js.ex

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ defmodule ElixirScript.Lib.JS do
33
alias ElixirScript.Translator
44
alias ElixirScript.Translator.Utils
55

6+
defmacro new(module, params)
7+
8+
defmacro update(object, property, value)
9+
10+
@doc false
611
def translate_js_function(name, params, env) do
712
do_translate({name, [], params}, env)
813
end
@@ -14,7 +19,14 @@ defmodule ElixirScript.Lib.JS do
1419
)
1520
end
1621

17-
defp do_translate({:mutate, _, [object, property, value]}, env) do
22+
defp do_translate({:new, _, [module_name, params]}, env) do
23+
Builder.new_expression(
24+
Translator.translate(module_name, env),
25+
Enum.map(params, &Translator.translate(&1, env))
26+
)
27+
end
28+
29+
defp do_translate({:update, _, [object, property, value]}, env) do
1830
Builder.assignment_expression(
1931
:=,
2032
Builder.member_expression(

lib/elixir_script/lib/kernel.ex

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ defmodule ElixirScript.Lib.Kernel do
4040
)
4141
end
4242

43-
defp do_translate({:.., _, [first, last]}, _) do
44-
Translator.translate(quote do: Range.(unquote(first), unquote(last)))
43+
defp do_translate({:.., _, [first, last]}, env) do
44+
quoted_range = quote do: Range.(unquote(first), unquote(last))
45+
46+
Translator.translate(quoted_range, env)
4547
end
4648

4749
defp do_translate({:abs, _, [number]}, env) do
@@ -164,12 +166,12 @@ defmodule ElixirScript.Lib.Kernel do
164166
|> Translator.translate(env)
165167
end
166168

167-
defp do_translate({:|>, _, [left, right]}, _) do
169+
defp do_translate({:|>, _, [left, right]}, env) do
168170
case right do
169171
{{:., meta, [module, fun]}, meta2, params} ->
170-
Translator.translate({{:., meta, [module, fun]}, meta2, [left] ++ params})
172+
Translator.translate({{:., meta, [module, fun]}, meta2, [left] ++ params}, env)
171173
{fun, meta, params} ->
172-
Translator.translate({fun, meta, [left] ++ params})
174+
Translator.translate({fun, meta, [left] ++ params}, env)
173175
end
174176
end
175177

@@ -198,18 +200,18 @@ defmodule ElixirScript.Lib.Kernel do
198200
)
199201
end
200202

201-
defp do_translate({:raise, _, [alias_info, attributes]}, _) do
203+
defp do_translate({:raise, _, [alias_info, attributes]}, env) do
202204
{_, _, name} = alias_info
203205

204-
Raise.throw_error(name, attributes)
206+
Raise.throw_error(name, attributes, env)
205207
end
206208

207-
defp do_translate({:raise, _, [message]}, _) do
208-
Raise.throw_error(message)
209+
defp do_translate({:raise, _, [message]}, env) do
210+
Raise.throw_error(message, env)
209211
end
210212

211-
defp do_translate({:to_string, _, [param]}, _) when is_binary(param) do
212-
Translator.translate(param)
213+
defp do_translate({:to_string, _, [param]}, env) when is_binary(param) do
214+
Translator.translate(param, env)
213215
end
214216

215217
defp do_translate({name, _, params}, env) do

lib/elixir_script/pattern_matching/match.ex

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule ElixirScript.PatternMatching.Match do
2+
@moduledoc false
3+
24
alias ESTree.Tools.Builder, as: JS
35
alias ElixirScript.Translator
46
alias ElixirScript.Translator.Utils
@@ -88,30 +90,30 @@ defmodule ElixirScript.PatternMatching.Match do
8890
)
8991
end
9092

91-
def build_match(params) do
92-
Enum.map(params, &do_build_match(&1))
93+
def build_match(params, env) do
94+
Enum.map(params, &do_build_match(&1, env))
9395
|> reduce_patterns
9496
end
9597

96-
defp do_build_match({:^, _, [value]}) do
97-
{ [bound(Translator.translate(value))], [nil] }
98+
defp do_build_match({:^, _, [value]}, env) do
99+
{ [bound(Translator.translate(value, env))], [nil] }
98100
end
99101

100-
defp do_build_match({:_, _, _}) do
102+
defp do_build_match({:_, _, _}, env) do
101103
{ [@wildcard], [JS.identifier(:undefined)] }
102104
end
103105

104-
defp do_build_match([{:|, _, [head, tail]}]) do
105-
{ [@head_tail], [Translator.translate(head), Translator.translate(tail)] }
106+
defp do_build_match([{:|, _, [head, tail]}], env) do
107+
{ [@head_tail], [Translator.translate(head, env), Translator.translate(tail, env)] }
106108
end
107109

108-
defp do_build_match({:<>, _, [prefix, value]}) do
109-
{ [startsWith(prefix)], [Translator.translate(value)] }
110+
defp do_build_match({:<>, _, [prefix, value]}, env) do
111+
{ [startsWith(prefix)], [Translator.translate(value, env)] }
110112
end
111113

112-
defp do_build_match({:%{}, _, props}) do
114+
defp do_build_match({:%{}, _, props}, env) do
113115
properties = Enum.map(props, fn({key, value}) ->
114-
{pattern, params} = do_build_match(value)
116+
{pattern, params} = do_build_match(value, env)
115117
{ JS.property(JS.literal(key), hd(List.wrap(pattern))), params }
116118
end)
117119

@@ -122,44 +124,44 @@ defmodule ElixirScript.PatternMatching.Match do
122124
{ JS.object_expression(List.wrap(props)), params }
123125
end
124126

125-
defp do_build_match({:%, _, [{:__aliases__, _, name}, {:%{}, meta, props}]}) do
127+
defp do_build_match({:%, _, [{:__aliases__, _, name}, {:%{}, meta, props}]}, env) do
126128
props = [{"__struct__" ,List.last(name)}] ++ props
127-
do_build_match({:%{}, meta, props})
129+
do_build_match({:%{}, meta, props}, env)
128130
end
129131

130-
defp do_build_match({:=, _, [{name, _, _}, right]}) when not name in [:%, :{}, :__aliases__, :^] do
131-
unify(name, right)
132+
defp do_build_match({:=, _, [{name, _, _}, right]}, env) when not name in [:%, :{}, :__aliases__, :^] do
133+
unify(name, right, env)
132134
end
133135

134-
defp do_build_match({:=, _, [left, {name, _, _}]}) when not name in [:%, :{}, :__aliases__, :^] do
135-
unify(name, left)
136+
defp do_build_match({:=, _, [left, {name, _, _}]}, env) when not name in [:%, :{}, :__aliases__, :^] do
137+
unify(name, left, env)
136138
end
137139

138-
defp do_build_match(list) when is_list(list) do
140+
defp do_build_match(list, env) when is_list(list) do
139141
{ patterns, params } = list
140-
|> Enum.map(&build_match([&1]))
142+
|> Enum.map(&build_match([&1], env))
141143
|> reduce_patterns
142144

143145
{[make_list(patterns)], params}
144146
end
145147

146-
defp do_build_match(term) when is_number(term) or is_binary(term) or is_boolean(term) or is_atom(term) or is_nil(term) do
147-
{ [Translator.translate(term)], [] }
148+
defp do_build_match(term, env) when is_number(term) or is_binary(term) or is_boolean(term) or is_atom(term) or is_nil(term) do
149+
{ [Translator.translate(term, env)], [] }
148150
end
149151

150-
defp do_build_match({ one, two }) do
151-
do_build_match({:{}, [], [one, two]})
152+
defp do_build_match({ one, two }, env) do
153+
do_build_match({:{}, [], [one, two]}, env)
152154
end
153155

154-
defp do_build_match({:{}, _, list}) do
156+
defp do_build_match({:{}, _, list}, env) do
155157
{ patterns, params } = list
156-
|> Enum.map(&build_match([&1]))
158+
|> Enum.map(&build_match([&1], env))
157159
|> reduce_patterns
158160

159161
{[make_tuple(patterns)], params}
160162
end
161163

162-
defp do_build_match({name, _, _}) do
164+
defp do_build_match({name, _, _}, env) do
163165
name = Utils.filter_name(name)
164166
{ [@parameter], [JS.identifier(name)] }
165167
end
@@ -171,8 +173,8 @@ defmodule ElixirScript.PatternMatching.Match do
171173
end)
172174
end
173175

174-
defp unify(target, source) do
175-
{patterns, params} = build_match([source])
176+
defp unify(target, source, env) do
177+
{patterns, params} = build_match([source], env)
176178
{ [capture(hd(patterns))], params ++ [JS.identifier(Utils.filter_name(target))] }
177179
end
178180

lib/elixir_script/preprocess/using.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ElixirScript.Preprocess.Using do
2-
2+
@moduledoc false
33

44
def process(ast, env) do
55
Macro.prewalk(ast, fn(x) ->

0 commit comments

Comments
 (0)