-
-
Save wojtekmach/bc0cf1f1405ddd41962e7ddcfee12904 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule AutolinkTest do | |
use ExUnit.Case, async: true | |
@moduletag :tmp_dir | |
setup :setup_fixture | |
@tag files: %{ | |
"lib/mod.ex" => ~S''' | |
defmodule Mod do | |
@spec f(:uri_string.uri_map()) :: | |
[binary()] | |
def f(_), do: [] | |
end | |
''' | |
} | |
test "elixir" do | |
fun = fn | |
{:uri_string, :uri_map, 0} -> ":URI_STRING.URI_MAP" | |
{:binary, 0} -> "BINARY" | |
end | |
assert rewrite(Mod, fun, linewidth: 10) == | |
""" | |
-spec f(:URI_STRING.URI_MAP()) -> | |
[BINARY()].\ | |
""" | |
end | |
@tag files: %{ | |
"src/mod.erl" => ~S''' | |
-module(mod). | |
-export([f/1]). | |
-spec f(uri_string:uri_map()) -> | |
[binary()]. | |
f(_) -> []. | |
''' | |
} | |
test "erlang" do | |
fun = fn | |
{:uri_string, :uri_map, 0} -> "URI_STRING:URI_MAP" | |
{:binary, 0} -> "BINARY" | |
end | |
assert rewrite(:mod, fun, linewidth: 10) == | |
""" | |
-spec f(URI_STRING:URI_MAP()) -> | |
[BINARY()].\ | |
""" | |
end | |
def rewrite(mod, fun, erl_pp_opts \\ []) do | |
{:ok, {^mod, [{_, ch}]}} = :beam_lib.chunks(:code.which(mod), [~c"Dbgi"]) | |
dbgi = | |
case :erlang.binary_to_term(ch) do | |
{:debug_info_v1, :erl_abstract_code, {dbgi, _}} -> | |
dbgi | |
{:debug_info_v1, :elixir_erl, {:elixir_v1, _, dbgi}} -> | |
dbgi | |
end | |
[attr] = for {:attribute, _anno, :spec, _} = attr <- dbgi, do: attr | |
formatted = attr |> :erl_pp.attribute(erl_pp_opts) |> IO.iodata_to_binary() |> String.trim() | |
{:ok, tokens, _end_location} = :erl_scan.string(String.to_charlist(formatted), {1, 1}, []) | |
{:ok, {:attribute, _, :spec, {{name, _arity}, [spec]}}} = :erl_parse.parse_form(tokens) | |
quoted = Code.Typespec.spec_to_quoted(name, spec) | |
{:"::", _, [{^name, _, args}, right]} = quoted | |
{_, refs} = | |
Macro.prewalk([args, right], [], fn | |
{{:., meta, [mod, name]}, _meta, args}, acc when is_atom(mod) and is_atom(name) -> | |
text = "#{mod}:#{name}" | |
{args, [{text, {mod, name, length(args)}, meta} | acc]} | |
{name, meta, args} = ast, acc when is_atom(name) -> | |
text = Atom.to_string(name) | |
{ast, [{text, {name, length(args)}, meta} | acc]} | |
ast, acc -> | |
{ast, acc} | |
end) | |
formatted | |
|> String.split("\n") | |
|> rewrite_lines(refs, fun) | |
|> Enum.join("\n") | |
end | |
defp rewrite_lines(lines, [{text, ref, meta} | rest], fun) do | |
line = Keyword.fetch!(meta, :line) | |
column = Keyword.fetch!(meta, :column) | |
lines = | |
List.update_at(lines, line - 1, fn line_text -> | |
{before, rest} = String.split_at(line_text, column - 1) | |
<<^text::binary-size(byte_size(text))>> <> rest = rest | |
updated = fun.(ref) | |
before <> updated <> rest | |
end) | |
rewrite_lines(lines, rest, fun) | |
end | |
defp rewrite_lines(lines, [], _fun) do | |
lines | |
end | |
defp setup_fixture(c) do | |
File.cd!(c.tmp_dir, fn -> | |
app = :fixture | |
File.write!("mix.exs", """ | |
defmodule Fixture.MixProject do | |
use Mix.Project | |
def project do | |
[ | |
app: :#{app}, | |
version: "1.0.0" | |
] | |
end | |
end | |
""") | |
for {path, contents} <- c.files do | |
File.mkdir_p!(Path.dirname(path)) | |
File.write!(path, contents) | |
end | |
{_, 0} = System.cmd("mix", ["compile"], into: IO.stream()) | |
Code.append_paths(["_build/dev/lib/#{app}/ebin"]) | |
{:ok, _} = Application.ensure_all_started(app) | |
end) | |
c | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment