Skip to content
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
34 changes: 23 additions & 11 deletions lib/lua/parser/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ defmodule Lua.Parser.Error do
lines = String.split(source_code, "\n")

header = [
IO.ANSI.red() <> IO.ANSI.bright() <> "Parse Error" <> IO.ANSI.reset(),
color("Parse Error", IO.ANSI.red() <> IO.ANSI.bright()),
""
]

Expand Down Expand Up @@ -268,7 +268,7 @@ defmodule Lua.Parser.Error do
if error.suggestion do
[
"",
IO.ANSI.cyan() <> "Suggestion:" <> IO.ANSI.reset(),
color("Suggestion:", IO.ANSI.cyan()),
indent(error.suggestion, 2)
]
else
Expand All @@ -279,7 +279,7 @@ defmodule Lua.Parser.Error do
if length(error.related) > 0 do
[
"",
IO.ANSI.yellow() <> "Related errors:" <> IO.ANSI.reset()
color("Related errors:", IO.ANSI.yellow())
] ++ Enum.flat_map(error.related, fn rel -> ["", indent(format(rel, source_code), 2)] end)
else
[]
Expand All @@ -294,10 +294,10 @@ defmodule Lua.Parser.Error do
@spec format_multiple([t()], String.t()) :: String.t()
def format_multiple(errors, source_code) do
header = [
IO.ANSI.red() <>
IO.ANSI.bright() <>
"Found #{length(errors)} parse error#{if length(errors) == 1, do: "", else: "s"}" <>
IO.ANSI.reset(),
color(
"Found #{length(errors)} parse error#{if length(errors) == 1, do: "", else: "s"}",
IO.ANSI.red() <> IO.ANSI.bright()
),
""
]

Expand All @@ -306,7 +306,7 @@ defmodule Lua.Parser.Error do
|> Enum.with_index(1)
|> Enum.flat_map(fn {error, idx} ->
[
IO.ANSI.yellow() <> "Error #{idx}:" <> IO.ANSI.reset(),
color("Error #{idx}:", IO.ANSI.yellow()),
format(error, source_code),
""
]
Expand All @@ -317,6 +317,18 @@ defmodule Lua.Parser.Error do

# Private helpers

# ANSI helpers — no-ops unless the terminal actually supports color, so
# piping to a file or a non-TTY never embeds raw escape codes. Mirrors
# `Lua.VM.ErrorFormatter.color/2` so parse and runtime errors gate the same
# way (see issue #382).
defp color(text, ansi) do
if IO.ANSI.enabled?() do
ansi <> text <> IO.ANSI.reset()
else
text
end
end

defp format_context(lines, position) do
line_num = position.line
column = position.column
Expand All @@ -335,15 +347,15 @@ defmodule Lua.Parser.Error do
if num == line_num do
# Error line
pointer = String.duplicate(" ", String.length(format_line_number(num)) + 3 + column - 1)
pointer = pointer <> IO.ANSI.red() <> "^" <> IO.ANSI.reset()
pointer = pointer <> color("^", IO.ANSI.red())

[
IO.ANSI.red() <> line_str <> IO.ANSI.reset(),
color(line_str, IO.ANSI.red()),
pointer
]
else
# Context line
[IO.ANSI.faint() <> line_str <> IO.ANSI.reset()]
[color(line_str, IO.ANSI.faint())]
end
end)

Expand Down
39 changes: 39 additions & 0 deletions test/lua/parser/error_ansi_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule Lua.Parser.ErrorAnsiTest do
@moduledoc """
Locks the ANSI gating on parser error output.

Parse errors color their output only when `IO.ANSI.enabled?/0` is true,
matching the runtime `Lua.VM.ErrorFormatter` path. The suite otherwise runs
with ANSI disabled (see test/test_helper.exs), so these tests flip it on
locally and restore it. That toggle mutates global application env, so this
module is `async: false` to keep it from racing concurrent tests that assert
on plain-text output.
"""
use ExUnit.Case, async: false

alias Lua.Parser

setup do
Application.put_env(:elixir, :ansi_enabled, true)
on_exit(fn -> Application.put_env(:elixir, :ansi_enabled, false) end)
end

describe "with ANSI enabled" do
test "uses ANSI colors for better readability" do
assert {:error, msg} = Parser.parse("if x then")
# Red for errors
assert msg =~ "\e[31m"
# Bold
assert msg =~ "\e[1m"
# Reset
assert msg =~ "\e[0m"
# Cyan for suggestions
assert msg =~ "\e[36m"
end

test "parse_chunk/1 carries colored errors when ANSI is on" do
assert {:error, %Lua.CompilerException{errors: errors}} = Lua.parse_chunk("asdf")
assert Enum.any?(errors, &(&1 =~ "\e["))
end
end
end
26 changes: 10 additions & 16 deletions test/lua/parser/error_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,16 @@ defmodule Lua.Parser.ErrorTest do
assert msg =~ "│"
# Error pointer
assert msg =~ "^"
# ANSI color codes
assert msg =~ "\e["
end

test "format output is gated on IO.ANSI.enabled?" do
refute IO.ANSI.enabled?(), "test env should have ANSI disabled"

# Regression for #382: parse errors must not embed raw ANSI escape
# codes when color is disabled. The strings flow out of parse_chunk/1
# inside %Lua.CompilerException{}, so a leak here surfaces there too.
assert {:error, msg} = Parser.parse("asdf")
refute msg =~ "\e["
end

test "includes line and column information" do
Expand Down Expand Up @@ -149,20 +157,6 @@ defmodule Lua.Parser.ErrorTest do
assert {:error, msg} = Parser.parse(code)
assert msg =~ "Suggestion"
end

test "uses ANSI colors for better readability" do
code = "if x then"

assert {:error, msg} = Parser.parse(code)
# Red for errors
assert msg =~ "\e[31m"
# Bold
assert msg =~ "\e[1m"
# Reset
assert msg =~ "\e[0m"
# Cyan for suggestions
assert msg =~ "\e[36m"
end
end

describe "parse_raw API" do
Expand Down
11 changes: 11 additions & 0 deletions test/lua_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,17 @@ defmodule LuaTest do
refute message =~ "Lua.CompilerException"
end

test "parse_chunk/1 errors carry no ANSI escapes when color is disabled" do
refute IO.ANSI.enabled?(), "test env should have ANSI disabled"

# Regression for #382: the formatted parse-error strings stored in
# %Lua.CompilerException{} must respect IO.ANSI.enabled?/0 rather than
# embedding raw escape codes unconditionally.
assert {:error, %Lua.CompilerException{errors: errors}} = Lua.parse_chunk("asdf")
refute Enum.any?(errors, &(&1 =~ "\e["))
refute Exception.message(Lua.CompilerException.exception(formatted: errors)) =~ "\e["
end

test "chunks can be loaded multiple times" do
lua = Lua.new()
chunk = ~LUA[print("hello")]c
Expand Down
Loading