Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ckochx committed Sep 7, 2021
0 parents commit be2a382
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
27 changes: 27 additions & 0 deletions .gitignore
@@ -0,0 +1,27 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
ex_factor-*.tar


# Temporary files for e.g. tests
/tmp
21 changes: 21 additions & 0 deletions README.md
@@ -0,0 +1,21 @@
# ExFactor

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `ex_factor` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:ex_factor, "~> 0.1.0"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/ex_factor](https://hexdocs.pm/ex_factor).

48 changes: 48 additions & 0 deletions lib/ex_factor.ex
@@ -0,0 +1,48 @@
defmodule ExFactor do
@moduledoc """
Documentation for `ExFactor`.
"""

@doc """
Identify public functions from a module AST.
"""
def public_functions({:ok, ast}) do
# Macro.prewalk(ast, [], fn node, acc ->
# # walk_ast(node, acc, :def)
# {node, walk_ast(node, acc, :def)}
# # |> IO.inspect(label: "walk_ast")
# end)

Macro.postwalk(ast, [], fn node, acc ->
# walk_ast(node, acc, :def)
{node, walk_ast(node, acc, :def)}
# |> IO.inspect(label: "walk_ast")
end)
end

def private_functions({:ok, ast}) do
# Macro.prewalk(ast, [], fn node, acc ->
# # walk_ast(node, acc, :def)
# {node, walk_ast(node, acc, :def)}
# # |> IO.inspect(label: "walk_ast")
# end)

Macro.postwalk(ast, [], fn node, acc ->
# walk_ast(node, acc, :def)
{node, walk_ast(node, acc, :defp)}
# |> IO.inspect(label: "walk_ast")
end)
end

defp walk_ast({tkn, _, [{name, _meta, _args} | _]} = func, acc, token) when tkn == token do
# func |> IO.inspect(label: "walk func TOKEN: #{token}")
map = %{name: name, ast: func}
[map | acc]
end

defp walk_ast(_func, acc, _token) do
# func |> IO.inspect(label: "walk fallback token: #{token}")
acc
# |> IO.inspect(label: "fallback accum")
end
end
29 changes: 29 additions & 0 deletions mix.exs
@@ -0,0 +1,29 @@
defmodule ExFactor.MixProject do
use Mix.Project

def project do
[
app: :ex_factor,
version: "0.1.0",
elixir: "~> 1.11",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:credo, ">= 1.5.0", only: [:dev, :test]}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
6 changes: 6 additions & 0 deletions mix.lock
@@ -0,0 +1,6 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"credo": {:hex, :credo, "1.5.6", "e04cc0fdc236fefbb578e0c04bd01a471081616e741d386909e527ac146016c6", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "4b52a3e558bd64e30de62a648518a5ea2b6e3e5d2b164ef5296244753fc7eb17"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
}
87 changes: 87 additions & 0 deletions test/ex_factor_test.exs
@@ -0,0 +1,87 @@
defmodule ExFactorTest do
use ExUnit.Case

test "it should report public fns" do
{_ast, [f1]} = """
defmodule CredoSampleModule do
@somedoc "This is somedoc"
# no aliases
def pub1(arg1) do
:ok
end
end
"""
|> Code.string_to_quoted()
|> ExFactor.public_functions()

assert f1.name == :pub1
end

test "it should report TWO public fns" do
{_ast, [f1, f2]} = """
defmodule CredoSampleModule do
@somedoc "This is somedoc"
# no aliases
def pub1(arg1) do
:ok
end
def pub2(arg2) do
:yes
end
defp pub3(arg3) do
:private
end
end
"""
|> Code.string_to_quoted()
|> ExFactor.public_functions()
assert f2.name == :pub1
assert f1.name == :pub2
end

test "it should report private fns" do
{_ast, [f1]} = """
defmodule CredoSampleModule do
@somedoc "This is somedoc"
# no aliases
defp priv1(arg1) do
:ok
end
end
"""
|> Code.string_to_quoted()
|> ExFactor.private_functions()

assert f1.name == :priv1
end

test "it should report TWO private fns" do
{_ast, [f1, f2]} = """
defmodule CredoSampleModule do
@somedoc "This is somedoc"
# no aliases
def pub1(arg1) do
:ok
end
def pub2(arg2) do
:yes
end
defp priv3(arg3) do
:private
end
defp priv4(arg4) do
:private
end
end
"""
|> Code.string_to_quoted()
|> ExFactor.private_functions()
assert f2.name == :priv3
assert f1.name == :priv4
end
end
1 change: 1 addition & 0 deletions test/test_helper.exs
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit be2a382

Please sign in to comment.