Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional validate_heex func #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/safe_code/validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ defmodule SafeCode.Validator do
|> validate_quoted(opts)
end

@spec validate_heex(binary, keyword) :: {:ok, ast :: Macro.t()} | {:error, Exception.t()}
def validate_heex(heex, opts \\ []) when is_binary(heex) do
quoted =
heex
|> HeexParser.parse_template()
|> validate_quoted(include_phoenix(opts))
Comment on lines +30 to +33
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets call directly into the other one, that way these two won't drift if a change is made to the parsing logic in one and doesn't get copied to the other version.

Suggested change
quoted =
heex
|> HeexParser.parse_template()
|> validate_quoted(include_phoenix(opts))
quoted = validate_heex!(heex, opts)


{:ok, quoted}
rescue
error -> {:error, error}
end

def validate_heex!(heex, opts \\ []) when is_binary(heex) do
heex
|> HeexParser.parse_template()
Expand Down
39 changes: 39 additions & 0 deletions test/safe_code/validator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,45 @@ defmodule SafeCode.ValidatorTest do
end

describe "validate_heex/1" do
test "basic" do
heex = """
hello <%= 1 + 1 %> how <%= 2+2 %> are you
"""

assert {:ok, _quoted} = Validator.validate_heex(heex)
end

test "for loop" do
heex = """
<%= for foo <- bar do %>
<%= foo %>
<% end %>
"""

assert {:ok, _quoted} = Validator.validate_heex(heex)
end

test "return the error tuple when invalid template" do
heex = """
<div>
hello <%= 1 + 1 %> how <%= 2+2 %> are you
"""

assert {:error, %Phoenix.LiveView.HTMLTokenizer.ParseError{} = error} = Validator.validate_heex(heex)
assert error.description == "end of template reached without closing tag for <div>"
end

test "return an error tuple on problem function" do
str = """
<%= System.cmd("touch", ["foo"]) %>
"""

assert {:error, %InvalidNode{} = error} = Validator.validate_heex(str)
assert error.message == "System . :cmd\n\nast:\n{:., [line: 1], [{:__aliases__, [line: 1], [:System]}, :cmd]}"
end
end

describe "validate_heex!/1" do
test "basic" do
heex = """
hello <%= 1 + 1 %> how <%= 2+2 %> are you
Expand Down