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

Config compiler #304

Merged
merged 5 commits into from
Aug 2, 2023
Merged
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
6 changes: 5 additions & 1 deletion apps/common/lib/elixir/features.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
defmodule Elixir.Features do
def with_diagnostics? do
Version.match?(System.version(), ">= 1.15.3")
function_exported?(Code, :with_diagnostics, 1)
end

def compile_wont_change_directory? do
Version.match?(System.version(), ">= 1.15.0")
end

def config_reader? do
Version.match?(System.version(), ">= 1.11.0")
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Lexical.RemoteControl.Build.Document do
alias Lexical.Document
alias Lexical.RemoteControl.Build.Document.Compilers

@compilers [Compilers.Elixir, Compilers.EEx, Compilers.NoOp]
@compilers [Compilers.Config, Compilers.Elixir, Compilers.EEx, Compilers.NoOp]

def compile(%Document{} = document) do
compiler = Enum.find(@compilers, & &1.recognizes?(document))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
defmodule Lexical.RemoteControl.Build.Document.Compilers.Config do
@moduledoc """
A compiler for elixir configuration
"""
alias Elixir.Features
alias Lexical.Document
alias Lexical.Plugin.V1.Diagnostic
alias Lexical.RemoteControl.Build

@behaviour Build.Document.Compiler
require Logger

def recognizes?(%Document{} = document) do
in_config_dir? =
document.path
|> Path.dirname()
|> String.starts_with?(config_dir())

in_config_dir? and Path.extname(document.path) == ".exs"
end

def enabled? do
Features.config_reader?()
end

def compile(%Document{} = document) do
do_compile(document)
end

defp config_dir do
# This function is called inside a call to `in_project` so we can
# call Mix.Project.config() directly
Mix.Project.config()
|> Keyword.get(:config_path)
|> Path.expand()
|> Path.dirname()
end

defp do_compile(%Document{} = document) do
if Features.with_diagnostics?() do
compile_with_diagnostics(document)
else
raw_compile(document)
end
end

defp raw_compile(%Document{} = document) do
contents = Document.to_string(document)

try do
Config.Reader.eval!(document.path, contents)
{:ok, []}
rescue
e ->
{:error, [to_result(document, e)]}
end
end

defp compile_with_diagnostics(%Document{} = document) do
{result, diagnostics} =
apply(Code, :with_diagnostics, [
# credo:disable-for-previous-line
fn ->
raw_compile(document)
end
])

diagnostic_results = Enum.map(diagnostics, &to_result(document, &1))

case result do
{:error, errors} ->
{:error, reject_logged_messages(errors ++ diagnostic_results)}

_ ->
{:ok, reject_logged_messages(diagnostic_results)}
end
end

defp to_result(%Document{} = document, %CompileError{} = error) do
Diagnostic.Result.new(document.uri, error.line, Exception.message(error), :error, "Elixir")
end

defp to_result(%Document{} = document, %error_type{} = error)
when error_type in [SyntaxError, TokenMissingError] do
Diagnostic.Result.new(
document.uri,
{error.line, error.column},
Exception.message(error),
:error,
"Elixir"
)
end

defp to_result(%Document{} = document, %{
position: position,
message: message,
severity: severity
}) do
Diagnostic.Result.new(
document.path,
position,
message,
severity,
"Elixir"
)
end

defp reject_logged_messages(results) do
Enum.reject(results, &(&1.message =~ "have been logged"))
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
defmodule Lexical.RemoteControl.Build.Document.Compilers.ConfigTest do
alias Lexical.Document
alias Lexical.RemoteControl.Build.Document.Compilers

use ExUnit.Case
import Lexical.Test.CodeSigil
import Compilers.Config

def document_with_path(left, right) do
left
|> Path.join(right)
|> document_with_path()
end

def document_with_path(path) when is_list(path) do
path
|> Path.join()
|> document_with_path()
end

def document_with_path(path) do
Document.new(path, "", 1)
end

def document(contents) do
config_dir()
|> Path.join("config.exs")
|> Document.new(contents, 0)
end

def config_dir do
Mix.Project.config()
|> Keyword.get(:config_path)
|> Path.expand()
|> Path.dirname()
end

describe "recognizes/1" do
test "files in the config directory are recognized" do
assert recognizes?(document_with_path(config_dir(), "test.exs"))
assert recognizes?(document_with_path(config_dir(), "foo.exs"))
assert recognizes?(document_with_path([config_dir(), "other", "foo.exs"]))
end

test "files in the config directory with relative paths are recognized" do
assert recognizes?(document_with_path("../../config/test.exs"))
end

test "files outside the config directory are not recognized" do
refute recognizes?(document_with_path(__ENV__.file))
end

test "only .exs files are recognized" do
refute recognizes?(document_with_path(config_dir(), "foo.ex"))
refute recognizes?(document_with_path(config_dir(), "foo.yaml"))
refute recognizes?(document_with_path(config_dir(), "foo.eex"))
refute recognizes?(document_with_path(config_dir(), "foo.heex"))
end
end

describe "compile/1" do
test "it produces diagnostics for syntax errors" do
assert {:error, [result]} =
",="
|> document()
|> compile()

assert result.message =~ "syntax error before"
assert result.position == {1, 1}
assert result.severity == :error
assert result.source == "Elixir"
end

test "it produces diagnostics for compile errors" do
assert {:error, [result]} =
~q[
import Config
configure :my_app, key: 3
]
|> document()
|> compile()

assert result.message =~ "undefined function"
assert result.position == 2
assert result.severity == :error
assert result.source == "Elixir"
end

test "it produces diagnostics for Token missing errors" do
assert {:error, [result]} =
"fn foo -> e"
|> document()
|> compile()

assert result.message =~ "missing terminator"
assert result.position == {1, 12}
assert result.severity == :error
assert result.source == "Elixir"
end

test "it produces no diagnostics on success" do
assert {:ok, []} =
~q[
import Config
config :my_app, key: 3, other_key: 6
]
|> document()
|> compile()
end
end
end
Loading