Rename ast properties to align with given ast
The properties were either not needed at all, or were named back when there was only one node type. now that each node is it's own struct, they really don't need to share any common properties.
This commit is contained in:
parent
d40ff3e0b1
commit
07a1d5d451
|
@ -17,8 +17,6 @@ defmodule Temple.Parser do
|
|||
@type ast ::
|
||||
%Empty{}
|
||||
| %Text{}
|
||||
| %TempleNamespaceNonvoid{}
|
||||
| %TempleNamespaceVoid{}
|
||||
| %Components{}
|
||||
| %NonvoidElementsAliases{}
|
||||
| %VoidElementsAliases{}
|
||||
|
|
|
@ -2,7 +2,7 @@ defmodule Temple.Parser.AnonymousFunctions do
|
|||
@moduledoc false
|
||||
@behaviour Temple.Parser
|
||||
|
||||
defstruct content: nil, attrs: [], children: []
|
||||
defstruct elixir_ast: nil, children: []
|
||||
|
||||
alias Temple.Parser
|
||||
|
||||
|
@ -28,15 +28,11 @@ defmodule Temple.Parser.AnonymousFunctions do
|
|||
|
||||
children = Temple.Parser.parse(block)
|
||||
|
||||
Temple.Ast.new(
|
||||
__MODULE__,
|
||||
content: expression,
|
||||
children: children
|
||||
)
|
||||
Temple.Ast.new(__MODULE__, elixir_ast: expression, children: children)
|
||||
end
|
||||
|
||||
defimpl Temple.EEx do
|
||||
def to_eex(%{content: {name, _, args}, children: children}) do
|
||||
def to_eex(%{elixir_ast: {name, _, args}, children: children}) do
|
||||
{_do_and_else, args} = Temple.Parser.Utils.split_args(args)
|
||||
|
||||
{args, {func, _, [{arrow, _, [[{arg, _, _}], _block]}]}, args2} =
|
||||
|
|
|
@ -2,7 +2,7 @@ defmodule Temple.Parser.Components do
|
|||
@moduledoc false
|
||||
@behaviour Temple.Parser
|
||||
|
||||
defstruct content: nil, attrs: [], children: []
|
||||
defstruct module: nil, assigns: [], children: []
|
||||
|
||||
@impl Temple.Parser
|
||||
def applicable?({:c, _, _}) do
|
||||
|
@ -26,21 +26,19 @@ defmodule Temple.Parser.Components do
|
|||
Temple.Parser.parse(do_and_else[:do])
|
||||
end
|
||||
|
||||
Temple.Ast.new(
|
||||
__MODULE__,
|
||||
meta: %{type: :component},
|
||||
content: Macro.expand_once(component_module, __ENV__),
|
||||
attrs: assigns,
|
||||
Temple.Ast.new(__MODULE__,
|
||||
module: Macro.expand_once(component_module, __ENV__),
|
||||
assigns: assigns,
|
||||
children: children
|
||||
)
|
||||
end
|
||||
|
||||
defimpl Temple.EEx do
|
||||
def to_eex(%{content: component_module, attrs: assigns, children: []}) do
|
||||
def to_eex(%{module: module, assigns: assigns, children: []}) do
|
||||
[
|
||||
"<%= Phoenix.View.render",
|
||||
" ",
|
||||
Macro.to_string(component_module),
|
||||
Macro.to_string(module),
|
||||
", ",
|
||||
":self,",
|
||||
" ",
|
||||
|
@ -50,10 +48,10 @@ defmodule Temple.Parser.Components do
|
|||
]
|
||||
end
|
||||
|
||||
def to_eex(%{content: component_module, attrs: assigns, children: children}) do
|
||||
def to_eex(%{module: module, assigns: assigns, children: children}) do
|
||||
[
|
||||
"<%= Phoenix.View.render_layout ",
|
||||
Macro.to_string(component_module),
|
||||
Macro.to_string(module),
|
||||
", ",
|
||||
":self",
|
||||
", ",
|
||||
|
|
|
@ -2,7 +2,7 @@ defmodule Temple.Parser.Default do
|
|||
@moduledoc false
|
||||
@behaviour Temple.Parser
|
||||
|
||||
defstruct content: nil, attrs: [], children: []
|
||||
defstruct elixir_ast: nil
|
||||
|
||||
alias Temple.Parser
|
||||
|
||||
|
@ -11,14 +11,11 @@ defmodule Temple.Parser.Default do
|
|||
|
||||
@impl Parser
|
||||
def run(ast) do
|
||||
Temple.Ast.new(
|
||||
__MODULE__,
|
||||
content: ast
|
||||
)
|
||||
Temple.Ast.new(__MODULE__, elixir_ast: ast)
|
||||
end
|
||||
|
||||
defimpl Temple.EEx do
|
||||
def to_eex(%{content: expression}) do
|
||||
def to_eex(%{elixir_ast: expression}) do
|
||||
["<%= ", Macro.to_string(expression), " %>\n"]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ defmodule Temple.Parser.DoExpressions do
|
|||
|
||||
@behaviour Parser
|
||||
|
||||
defstruct content: nil, attrs: [], children: []
|
||||
defstruct elixir_ast: nil, children: []
|
||||
|
||||
@impl Parser
|
||||
def applicable?({_, _, args}) when is_list(args) do
|
||||
|
@ -26,15 +26,11 @@ defmodule Temple.Parser.DoExpressions do
|
|||
Temple.Parser.parse(do_and_else[:else])
|
||||
end
|
||||
|
||||
Temple.Ast.new(
|
||||
__MODULE__,
|
||||
children: [do_body, else_body],
|
||||
content: {name, meta, args}
|
||||
)
|
||||
Temple.Ast.new(__MODULE__, elixir_ast: {name, meta, args}, children: [do_body, else_body])
|
||||
end
|
||||
|
||||
defimpl Temple.EEx do
|
||||
def to_eex(%{content: expression, children: [do_body, else_body]}) do
|
||||
def to_eex(%{elixir_ast: expression, children: [do_body, else_body]}) do
|
||||
[
|
||||
"<%= ",
|
||||
Macro.to_string(expression),
|
||||
|
|
|
@ -2,7 +2,7 @@ defmodule Temple.Parser.Empty do
|
|||
@moduledoc false
|
||||
@behaviour Temple.Parser
|
||||
|
||||
defstruct content: nil, attrs: [], children: []
|
||||
defstruct []
|
||||
|
||||
alias Temple.Parser
|
||||
|
||||
|
@ -12,10 +12,7 @@ defmodule Temple.Parser.Empty do
|
|||
|
||||
@impl Parser
|
||||
def run(_ast) do
|
||||
Temple.Ast.new(
|
||||
__MODULE__,
|
||||
meta: %{type: :empty}
|
||||
)
|
||||
Temple.Ast.new(__MODULE__)
|
||||
end
|
||||
|
||||
defimpl Temple.EEx do
|
||||
|
|
|
@ -2,7 +2,7 @@ defmodule Temple.Parser.Match do
|
|||
@moduledoc false
|
||||
@behaviour Temple.Parser
|
||||
|
||||
defstruct content: nil, attrs: [], children: []
|
||||
defstruct elixir_ast: nil
|
||||
|
||||
alias Temple.Parser
|
||||
|
||||
|
@ -15,16 +15,12 @@ defmodule Temple.Parser.Match do
|
|||
|
||||
@impl Parser
|
||||
def run(macro) do
|
||||
Temple.Ast.new(
|
||||
__MODULE__,
|
||||
meta: %{type: :match},
|
||||
content: macro
|
||||
)
|
||||
Temple.Ast.new(__MODULE__, elixir_ast: macro)
|
||||
end
|
||||
|
||||
defimpl Temple.EEx do
|
||||
def to_eex(%{content: content}) do
|
||||
["<% ", Macro.to_string(content), " %>"]
|
||||
def to_eex(%{elixir_ast: elixir_ast}) do
|
||||
["<% ", Macro.to_string(elixir_ast), " %>"]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@ defmodule Temple.Parser.NonvoidElementsAliases do
|
|||
@moduledoc false
|
||||
@behaviour Temple.Parser
|
||||
|
||||
defstruct content: nil, attrs: [], children: []
|
||||
defstruct name: nil, attrs: [], children: []
|
||||
|
||||
alias Temple.Parser
|
||||
|
||||
|
@ -25,25 +25,19 @@ defmodule Temple.Parser.NonvoidElementsAliases do
|
|||
|
||||
children = Temple.Parser.parse(do_and_else[:do])
|
||||
|
||||
Temple.Ast.new(
|
||||
__MODULE__,
|
||||
content: to_string(name),
|
||||
meta: %{type: :nonvoid_alias},
|
||||
attrs: args,
|
||||
children: children
|
||||
)
|
||||
Temple.Ast.new(__MODULE__, name: to_string(name), attrs: args, children: children)
|
||||
end
|
||||
|
||||
defimpl Temple.EEx do
|
||||
def to_eex(%{content: content, attrs: attrs, children: children}) do
|
||||
def to_eex(%{name: name, attrs: attrs, children: children}) do
|
||||
[
|
||||
"<",
|
||||
content,
|
||||
name,
|
||||
Temple.Parser.Utils.compile_attrs(attrs),
|
||||
">\n",
|
||||
for(child <- children, do: Temple.EEx.to_eex(child)),
|
||||
"\n</",
|
||||
content,
|
||||
name,
|
||||
">"
|
||||
]
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ defmodule Temple.Parser.RightArrow do
|
|||
|
||||
@behaviour Parser
|
||||
|
||||
defstruct content: nil, attrs: [], children: []
|
||||
defstruct elixir_ast: nil, children: []
|
||||
|
||||
@impl Parser
|
||||
def applicable?({:->, _, _}), do: true
|
||||
|
@ -14,19 +14,14 @@ defmodule Temple.Parser.RightArrow do
|
|||
def run({_, _, [[pattern], args]}) do
|
||||
children = Parser.parse(args)
|
||||
|
||||
Temple.Ast.new(
|
||||
__MODULE__,
|
||||
meta: %{type: :right_arrow},
|
||||
content: pattern,
|
||||
children: children
|
||||
)
|
||||
Temple.Ast.new(__MODULE__, elixir_ast: pattern, children: children)
|
||||
end
|
||||
|
||||
defimpl Temple.EEx do
|
||||
def to_eex(%{content: content, children: children}) do
|
||||
def to_eex(%{elixir_ast: elixir_ast, children: children}) do
|
||||
[
|
||||
"<% ",
|
||||
Macro.to_string(content),
|
||||
Macro.to_string(elixir_ast),
|
||||
" -> %>\n",
|
||||
for(child <- children, do: Temple.EEx.to_eex(child))
|
||||
]
|
||||
|
|
|
@ -2,8 +2,6 @@ defmodule Temple.Parser.TempleNamespaceNonvoid do
|
|||
@moduledoc false
|
||||
@behaviour Temple.Parser
|
||||
|
||||
defstruct content: nil, attrs: [], children: []
|
||||
|
||||
alias Temple.Parser
|
||||
|
||||
@impl Parser
|
||||
|
|
|
@ -2,18 +2,14 @@ defmodule Temple.Parser.TempleNamespaceVoid do
|
|||
@moduledoc false
|
||||
@behaviour Temple.Parser
|
||||
|
||||
defstruct content: nil, attrs: [], children: []
|
||||
|
||||
alias Temple.Parser
|
||||
|
||||
@impl Parser
|
||||
@impl Temple.Parser
|
||||
def applicable?({{:., _, [{:__aliases__, _, [:Temple]}, name]}, _meta, _args}) do
|
||||
name in Parser.void_elements_aliases()
|
||||
name in Temple.Parser.void_elements_aliases()
|
||||
end
|
||||
|
||||
def applicable?(_), do: false
|
||||
|
||||
@impl Parser
|
||||
@impl Temple.Parser
|
||||
def run({name, meta, args}) do
|
||||
{:., _, [{:__aliases__, _, [:Temple]}, name]} = name
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ defmodule Temple.Parser.Text do
|
|||
@moduledoc false
|
||||
@behaviour Temple.Parser
|
||||
|
||||
defstruct content: nil, attrs: [], children: []
|
||||
defstruct text: nil
|
||||
|
||||
alias Temple.Parser
|
||||
|
||||
|
@ -12,14 +12,11 @@ defmodule Temple.Parser.Text do
|
|||
|
||||
@impl Parser
|
||||
def run(text) do
|
||||
Temple.Ast.new(
|
||||
__MODULE__,
|
||||
content: text
|
||||
)
|
||||
Temple.Ast.new(__MODULE__, text: text)
|
||||
end
|
||||
|
||||
defimpl Temple.EEx do
|
||||
def to_eex(%{content: text}) do
|
||||
def to_eex(%{text: text}) do
|
||||
[text, "\n"]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,36 +2,29 @@ defmodule Temple.Parser.VoidElementsAliases do
|
|||
@moduledoc false
|
||||
@behaviour Temple.Parser
|
||||
|
||||
defstruct content: nil, attrs: [], children: []
|
||||
defstruct name: nil, attrs: []
|
||||
|
||||
alias Temple.Parser
|
||||
|
||||
@impl Parser
|
||||
@impl Temple.Parser
|
||||
def applicable?({name, _, _}) do
|
||||
name in Parser.void_elements_aliases()
|
||||
name in Temple.Parser.void_elements_aliases()
|
||||
end
|
||||
|
||||
def applicable?(_), do: false
|
||||
|
||||
@impl Parser
|
||||
@impl Temple.Parser
|
||||
def run({name, _, args}) do
|
||||
{_do_and_else, [args]} = Temple.Parser.Utils.split_args(args)
|
||||
|
||||
name = Parser.void_elements_lookup()[name]
|
||||
name = Temple.Parser.void_elements_lookup()[name]
|
||||
|
||||
Temple.Ast.new(
|
||||
__MODULE__,
|
||||
content: name,
|
||||
meta: %{type: :void_alias},
|
||||
attrs: args
|
||||
)
|
||||
Temple.Ast.new(__MODULE__, name: name, attrs: args)
|
||||
end
|
||||
|
||||
defimpl Temple.EEx do
|
||||
def to_eex(%{content: content, attrs: attrs}) do
|
||||
def to_eex(%{name: name, attrs: attrs}) do
|
||||
[
|
||||
"<",
|
||||
to_string(content),
|
||||
to_string(name),
|
||||
Temple.Parser.Utils.compile_attrs(attrs),
|
||||
">\n"
|
||||
]
|
||||
|
|
|
@ -55,11 +55,10 @@ defmodule Temple.Parser.AnonymousFunctionsTest do
|
|||
ast = AnonymousFunctions.run(raw_ast)
|
||||
|
||||
assert %AnonymousFunctions{
|
||||
content: _,
|
||||
elixir_ast: _,
|
||||
children: [
|
||||
%Temple.Parser.Default{
|
||||
content: ^expected_child,
|
||||
children: []
|
||||
elixir_ast: ^expected_child
|
||||
}
|
||||
]
|
||||
} = ast
|
||||
|
|
|
@ -50,8 +50,8 @@ defmodule Temple.Parser.ComponentsTest do
|
|||
ast = Components.run(raw_ast)
|
||||
|
||||
assert %Components{
|
||||
content: SomeModule,
|
||||
attrs: [],
|
||||
module: SomeModule,
|
||||
assigns: [],
|
||||
children: _
|
||||
} = ast
|
||||
end
|
||||
|
@ -65,8 +65,8 @@ defmodule Temple.Parser.ComponentsTest do
|
|||
ast = Components.run(ast)
|
||||
|
||||
assert %Components{
|
||||
content: SomeModule,
|
||||
attrs: [foo: :bar],
|
||||
module: SomeModule,
|
||||
assigns: [foo: :bar],
|
||||
children: _
|
||||
} = ast
|
||||
end
|
||||
|
@ -84,8 +84,8 @@ defmodule Temple.Parser.ComponentsTest do
|
|||
ast = Components.run(raw_ast)
|
||||
|
||||
assert %Components{
|
||||
content: SomeModule,
|
||||
attrs: [foo: :bar],
|
||||
module: SomeModule,
|
||||
assigns: [foo: :bar],
|
||||
children: _
|
||||
} = ast
|
||||
end
|
||||
|
@ -99,8 +99,8 @@ defmodule Temple.Parser.ComponentsTest do
|
|||
ast = Components.run(raw_ast)
|
||||
|
||||
assert %Components{
|
||||
content: SomeModule,
|
||||
attrs: [foo: :bar],
|
||||
module: SomeModule,
|
||||
assigns: [foo: :bar],
|
||||
children: []
|
||||
} = ast
|
||||
end
|
||||
|
|
|
@ -23,10 +23,7 @@ defmodule Temple.Parser.DefaultTest do
|
|||
|
||||
ast = Default.run(expression)
|
||||
|
||||
assert %Default{
|
||||
content: expression,
|
||||
children: []
|
||||
} == ast
|
||||
assert %Default{elixir_ast: expression} == ast
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -28,9 +28,9 @@ defmodule Temple.Parser.DoExpressionsTest do
|
|||
ast = DoExpressions.run(raw_ast)
|
||||
|
||||
assert %DoExpressions{
|
||||
content: _,
|
||||
elixir_ast: _,
|
||||
children: [
|
||||
[%Temple.Parser.Text{content: "bob", children: []}],
|
||||
[%Temple.Parser.Text{text: "bob"}],
|
||||
nil
|
||||
]
|
||||
} = ast
|
||||
|
|
|
@ -26,10 +26,7 @@ defmodule Temple.Parser.EmptyTest do
|
|||
for _ <- [nil, []] do
|
||||
ast = Empty.run(nil)
|
||||
|
||||
assert %Empty{
|
||||
content: nil,
|
||||
children: []
|
||||
} == ast
|
||||
assert %Empty{} == ast
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -33,10 +33,7 @@ defmodule Temple.Parser.MatchTest do
|
|||
|
||||
ast = Match.run(expression)
|
||||
|
||||
assert %Match{
|
||||
content: expression,
|
||||
children: []
|
||||
} == ast
|
||||
assert %Match{elixir_ast: expression} == ast
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -61,16 +61,16 @@ defmodule Temple.Parser.NonvoidElementsAliasesTest do
|
|||
ast = NonvoidElementsAliases.run(raw_ast)
|
||||
|
||||
assert %NonvoidElementsAliases{
|
||||
content: "div",
|
||||
name: "div",
|
||||
attrs: [class: "foo", id: {:var, [], _}],
|
||||
children: [
|
||||
%NonvoidElementsAliases{
|
||||
content: "select",
|
||||
name: "select",
|
||||
children: [
|
||||
%NonvoidElementsAliases{
|
||||
content: "option",
|
||||
name: "option",
|
||||
children: [
|
||||
%Temple.Parser.Text{content: "foo"}
|
||||
%Temple.Parser.Text{text: "foo"}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
|
|
@ -50,11 +50,10 @@ defmodule Temple.Parser.RightArrowTest do
|
|||
ast = RightArrow.run(raw_ast)
|
||||
|
||||
assert %RightArrow{
|
||||
content: :bing,
|
||||
elixir_ast: :bing,
|
||||
children: [
|
||||
%Temple.Parser.Default{
|
||||
content: ^bong,
|
||||
children: []
|
||||
elixir_ast: ^bong
|
||||
}
|
||||
]
|
||||
} = ast
|
||||
|
|
|
@ -48,9 +48,9 @@ defmodule Temple.Parser.TempleNamespaceNonvoidTest do
|
|||
ast = TempleNamespaceNonvoid.run(raw_ast)
|
||||
|
||||
assert %NonvoidElementsAliases{
|
||||
content: "div",
|
||||
name: "div",
|
||||
attrs: [class: "foo", id: {:var, [], _}],
|
||||
children: [%Temple.Parser.Text{content: "foo"}]
|
||||
children: [%Temple.Parser.Text{text: "foo"}]
|
||||
} = ast
|
||||
end
|
||||
end
|
||||
|
|
|
@ -44,9 +44,8 @@ defmodule Temple.Parser.TempleNamespaceVoidTest do
|
|||
ast = TempleNamespaceVoid.run(raw_ast)
|
||||
|
||||
assert %VoidElementsAliases{
|
||||
content: :meta,
|
||||
attrs: [class: "foo", id: {:var, [], _}],
|
||||
children: []
|
||||
name: :meta,
|
||||
attrs: [class: "foo", id: {:var, [], _}]
|
||||
} = ast
|
||||
end
|
||||
end
|
||||
|
|
|
@ -25,10 +25,7 @@ defmodule Temple.Parser.TextTest do
|
|||
text = "string literal"
|
||||
ast = Text.run(text)
|
||||
|
||||
assert %Text{
|
||||
content: text,
|
||||
children: []
|
||||
} == ast
|
||||
assert %Text{text: text} == ast
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -49,9 +49,8 @@ defmodule Temple.Parser.VoidElementsAliasesTest do
|
|||
ast = VoidElementsAliases.run(raw_ast)
|
||||
|
||||
assert %VoidElementsAliases{
|
||||
content: :meta,
|
||||
attrs: [content: "foo"],
|
||||
children: []
|
||||
name: :meta,
|
||||
attrs: [content: "foo"]
|
||||
} = ast
|
||||
end
|
||||
end
|
||||
|
|
Reference in a new issue