Skip to content

Commit a7a1ef1

Browse files
committed
Add internal ExDoc.Utils.warn/2, closes #1762
1 parent 816b171 commit a7a1ef1

File tree

11 files changed

+43
-41
lines changed

11 files changed

+43
-41
lines changed

lib/ex_doc/autolink.ex

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -514,29 +514,23 @@ defmodule ExDoc.Autolink do
514514

515515
@doc false
516516
def warn(config, message) do
517-
# TODO: Remove on Elixir v1.14
518-
stacktrace_info =
519-
if unquote(Version.match?(System.version(), ">= 1.14.0")) do
520-
f =
521-
case config.current_kfa do
522-
{:function, f, a} ->
523-
[function: {f, a}]
517+
f =
518+
case config.current_kfa do
519+
{:function, f, a} ->
520+
[function: {f, a}]
524521

525-
_ ->
526-
[]
527-
end
528-
529-
[file: config.file, line: config.line, module: config.current_module] ++ f
530-
else
531-
[]
522+
_ ->
523+
[]
532524
end
533525

526+
stacktrace_info = [file: config.file, line: config.line, module: config.current_module] ++ f
527+
534528
case config.warnings do
535529
:emit ->
536-
IO.warn(message, stacktrace_info)
530+
ExDoc.Utils.warn(message, stacktrace_info)
537531

538532
:raise ->
539-
IO.warn(message, stacktrace_info)
533+
ExDoc.Utils.warn(message, stacktrace_info)
540534
raise "fail due to warnings"
541535

542536
:send ->

lib/ex_doc/doc_ast.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,14 @@ defmodule ExDoc.DocAST do
177177
try do
178178
render_code(pre_attr, lang, lexer, opts, code, outer_opts)
179179
rescue
180-
e ->
181-
IO.puts(:stderr, [
182-
IO.ANSI.format([:yellow, "warning: ", :reset]),
183-
"crashed while highlighting #{lang} snippet:\n\n",
184-
full_block,
185-
"\nFull error message shown next:\n\n",
186-
Exception.format(:error, e, __STACKTRACE__)
187-
])
180+
_ ->
181+
ExDoc.Utils.warn(
182+
[
183+
"crashed while highlighting #{lang} snippet:\n\n",
184+
full_block
185+
],
186+
__STACKTRACE__
187+
)
188188

189189
full_block
190190
end

lib/ex_doc/formatter/epub.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ defmodule ExDoc.Formatter.EPUB do
6161
html = Templates.extra_template(config, title, title_content, content)
6262

6363
if File.regular?(output) do
64-
IO.puts(:stderr, "warning: file #{Path.relative_to_cwd(output)} already exists")
64+
ExDoc.Utils.warn("file #{Path.relative_to_cwd(output)} already exists", [])
6565
end
6666

6767
File.write!(output, html)

lib/ex_doc/formatter/html.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ defmodule ExDoc.Formatter.HTML do
246246
html = Templates.extra_template(config, node, extra_type(extension), nodes_map, refs)
247247

248248
if File.regular?(output) do
249-
IO.puts(:stderr, "warning: file #{Path.relative_to_cwd(output)} already exists")
249+
ExDoc.Utils.warn("warning: file #{Path.relative_to_cwd(output)} already exists", [])
250250
end
251251

252252
File.write!(output, html)
@@ -541,7 +541,7 @@ defmodule ExDoc.Formatter.HTML do
541541

542542
defp generate_redirect(filename, config, redirect_to) do
543543
unless case_sensitive_file_regular?("#{config.output}/#{redirect_to}") do
544-
IO.puts(:stderr, "warning: #{filename} redirects to #{redirect_to}, which does not exist")
544+
ExDoc.Utils.warn("#{filename} redirects to #{redirect_to}, which does not exist", [])
545545
end
546546

547547
content = Templates.redirect_template(config, redirect_to)

lib/ex_doc/language.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ defmodule ExDoc.Language do
180180
def get(:erlang, _module), do: {:ok, ExDoc.Language.Erlang}
181181

182182
def get(language, module) when is_atom(language) and is_atom(module) do
183-
IO.warn(
183+
ExDoc.Utils.warn(
184184
"skipping module #{module}, reason: unsupported language (#{language})",
185185
[]
186186
)

lib/ex_doc/language/elixir.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ defmodule ExDoc.Language.Elixir do
3939
}
4040

4141
true ->
42-
IO.warn("skipping docs for module #{inspect(module)}, reason: :no_debug_info", [])
42+
ExDoc.Utils.warn(
43+
"skipping docs for module #{inspect(module)}, reason: :no_debug_info",
44+
[]
45+
)
4346
end
4447
end
4548

lib/ex_doc/language/erlang.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ defmodule ExDoc.Language.Erlang do
5858
}
5959
}
6060
else
61-
IO.warn("skipping docs for module #{inspect(module)}, reason: :no_debug_info", [])
61+
ExDoc.Utils.warn("skipping docs for module #{inspect(module)}, reason: :no_debug_info", [])
6262
end
6363
end
6464

lib/ex_doc/markdown/earmark.ex

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,7 @@ defmodule ExDoc.Markdown.Earmark do
4747

4848
defp print_messages(messages, options) do
4949
for {_severity, line, message} <- messages do
50-
# TODO: Remove on Elixir v1.14
51-
stacktrace_info =
52-
if unquote(Version.match?(System.version(), ">= 1.14.0")) do
53-
[file: options[:file], line: line]
54-
else
55-
[]
56-
end
57-
58-
IO.warn(message, stacktrace_info)
50+
ExDoc.Utils.warn(message, file: options[:file], line: line)
5951
end
6052
end
6153

lib/ex_doc/retriever.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ defmodule ExDoc.Retriever do
104104
docs
105105

106106
{:error, reason} ->
107-
IO.warn("skipping docs for module #{inspect(module)}, reason: #{reason}", [])
107+
ExDoc.Utils.warn("skipping docs for module #{inspect(module)}, reason: #{reason}", [])
108108
false
109109
end
110110

lib/ex_doc/utils.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
defmodule ExDoc.Utils do
22
@moduledoc false
33

4+
@doc """
5+
Emits a warning.
6+
"""
7+
def warn(message, stacktrace_info) do
8+
if unquote(Version.match?(System.version(), ">= 1.14.0")) do
9+
stacktrace_info
10+
else
11+
[]
12+
end
13+
14+
IO.warn(message, stacktrace_info)
15+
end
16+
417
@doc """
518
Runs the `before_closing_head_tag` callback.
619
"""

test/ex_doc/formatter/html_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ defmodule ExDoc.Formatter.HTMLTest do
120120
generate_docs(doc_config(context, main: "Randomerror"))
121121
end)
122122

123-
assert output =~ "warning: index.html redirects to Randomerror.html, which does not exist\n"
123+
assert output =~ "index.html redirects to Randomerror.html, which does not exist\n"
124124
assert File.regular?(tmp_dir <> "/html/index.html")
125125
assert File.regular?(tmp_dir <> "/html/RandomError.html")
126126
end

0 commit comments

Comments
 (0)