|
1
|
+ |
defmodule OpenAgentsWeb.ComponentAttrsTest do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
Every enumerated component attribute states the values it accepts.
|
|
4
|
+ |
|
|
5
|
+ |
Phoenix checks a literal attribute at a call site only when the declaration
|
|
6
|
+ |
carries a `values:` list. Without one it accepts anything, and the failure is
|
|
7
|
+ |
silent all the way to the browser: `UI.github_login/1` declared
|
|
8
|
+ |
`attr :size, :atom, default: :md`, `:md` is not one of `button/1`'s sizes, so
|
|
9
|
+ |
the button rendered `data-size="md"`, matched no size rule, and arrived with
|
|
10
|
+ |
no height, no padding and no type scale. Nothing warned, no test failed, and
|
|
11
|
+ |
the control simply looked wrong.
|
|
12
|
+ |
|
|
13
|
+ |
The rule is narrow and mechanical: an `:atom` attribute is an enumeration, so
|
|
14
|
+ |
it must say what it enumerates. That is enough to hand the problem to the
|
|
15
|
+ |
compiler, which then rejects a bad call site by name — and it did
|
|
16
|
+ |
immediately, catching `graph_node/1` being passed work-item statuses its
|
|
17
|
+ |
declaration did not admit.
|
|
18
|
+ |
|
|
19
|
+ |
`:string`, `:boolean`, `:map`, `:list`, `:any` and `:global` are unbounded by
|
|
20
|
+ |
nature and are not checked.
|
|
21
|
+ |
"""
|
|
22
|
+ |
|
|
23
|
+ |
use ExUnit.Case, async: true
|
|
24
|
+ |
|
|
25
|
+ |
# `attr` declarations wrap, so the options are read up to the next `attr`,
|
|
26
|
+ |
# `slot`, `def`, or blank-line boundary rather than to the end of the line.
|
|
27
|
+ |
@declaration ~r/^[ \t]*attr :(\w+), :atom\b(?<options>(?:.|\n)*?)(?=\n[ \t]*(?:attr |slot |def |defp |@doc|\n))/m
|
|
28
|
+ |
|
|
29
|
+ |
test "every atom attribute declares the values it accepts" do
|
|
30
|
+ |
offenders =
|
|
31
|
+ |
for path <- component_sources(),
|
|
32
|
+ |
source = File.read!(path),
|
|
33
|
+ |
match <- Regex.scan(@declaration, source, capture: :all_but_first, return: :index),
|
|
34
|
+ |
{offender, line} <- offense(source, match),
|
|
35
|
+ |
do: {relative(path), line, offender}
|
|
36
|
+ |
|
|
37
|
+ |
assert offenders == [], """
|
|
38
|
+ |
These `:atom` attributes do not declare a `values:` list, so Phoenix cannot
|
|
39
|
+ |
check what call sites pass them and a wrong value fails silently in the
|
|
40
|
+ |
browser rather than loudly at compile time:
|
|
41
|
+ |
|
|
42
|
+ |
#{Enum.map_join(offenders, "\n", fn {file, line, name} -> " #{file}:#{line} attr :#{name}" end)}
|
|
43
|
+ |
|
|
44
|
+ |
Add `values: [...]`. If the attribute genuinely accepts any atom, it is not
|
|
45
|
+ |
an enumeration -- declare it as `:any` and say why in a comment.
|
|
46
|
+ |
"""
|
|
47
|
+ |
end
|
|
48
|
+ |
|
|
49
|
+ |
defp offense(source, [{name_start, name_length}, {options_start, options_length}]) do
|
|
50
|
+ |
name = binary_part(source, name_start, name_length)
|
|
51
|
+ |
options = binary_part(source, options_start, options_length)
|
|
52
|
+ |
|
|
53
|
+ |
if String.contains?(options, "values:") do
|
|
54
|
+ |
[]
|
|
55
|
+ |
else
|
|
56
|
+ |
[{name, line_of(source, name_start)}]
|
|
57
|
+ |
end
|
|
58
|
+ |
end
|
|
59
|
+ |
|
|
60
|
+ |
defp offense(_source, _match), do: []
|
|
61
|
+ |
|
|
62
|
+ |
defp component_sources do
|
|
63
|
+ |
Path.wildcard("lib/openagents_web/**/*.ex")
|
|
64
|
+ |
end
|
|
65
|
+ |
|
|
66
|
+ |
defp line_of(source, offset),
|
|
67
|
+ |
do: source |> binary_part(0, offset) |> String.split("\n") |> length()
|
|
68
|
+ |
|
|
69
|
+ |
defp relative(path), do: String.replace_leading(path, "lib/openagents_web/", "")
|
|
70
|
+ |
end
|