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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ jobs:
include:
- elixir: 1.11.1
otp: 21.3.8.17
warnings-as-errors: true
- elixir: 1.11.4
otp: 23.2
check_formatted: true
warnings-as-errors: true
- elixir: 1.12.0-rc.0
otp: 24.0-rc3
env:
Expand Down
11 changes: 11 additions & 0 deletions lib/surface/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ defmodule Surface.Compiler do
end

defp process_attributes(mod, [{name, value, attr_meta} | attrs], meta, acc) do
unquoted_string? = attr_meta[:unquoted_string?]
name = String.to_atom(name)
attr_meta = Helpers.to_meta(attr_meta, meta)
{type, type_opts} = Surface.TypeHandler.attribute_type_and_opts(mod, name, attr_meta)
Expand All @@ -629,6 +630,16 @@ defmodule Surface.Compiler do
)
end

if unquoted_string? do
message = """
passing unquoted attribute values has been deprecated and will be removed in future versions.

Hint: replace `#{name}=#{value}` with `#{name}={#{value}}`
"""

IOHelper.warn(message, meta.caller, fn _ -> meta.line end)
end

node = %AST.Attribute{
type: type,
type_opts: type_opts,
Expand Down
3 changes: 3 additions & 0 deletions lib/surface/compiler/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,19 @@ defmodule Surface.Compiler.Parser do
end

defp translate_attr({name, {:string, "true", %{delimiter: nil}}, meta}) do
meta = Map.put(meta, :unquoted_string?, true)
{name, true, to_meta(meta)}
end

defp translate_attr({name, {:string, "false", %{delimiter: nil}}, meta}) do
meta = Map.put(meta, :unquoted_string?, true)
{name, false, to_meta(meta)}
end

defp translate_attr({name, {:string, value, %{delimiter: nil}}, meta}) do
case Integer.parse(value) do
{int_value, ""} ->
meta = Map.put(meta, :unquoted_string?, true)
{name, int_value, to_meta(meta)}

_ ->
Expand Down
42 changes: 42 additions & 0 deletions test/compiler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,48 @@ defmodule Surface.CompilerSyncTest do

alias Surface.CompilerTest.{Button, Column, GridLive}, warn: false

test "warning when passing integer attribute values that are not enclosed by {}" do
code = """
<div>
<div tabindex=1 />
</div>
"""

{:warn, line, message} = run_compile(code, __ENV__)

assert message =~ """
passing unquoted attribute values has been deprecated and will be removed in future versions.

Hint: replace `tabindex=1` with `tabindex={1}`
"""

assert line == 2
end

test "warning when passing boolean attribute values that are not enclosed by {}" do
code = """
<div>
<div selected=true checked=false />
</div>
"""

{:warn, line, message} = run_compile(code, __ENV__)

assert message =~ """
passing unquoted attribute values has been deprecated and will be removed in future versions.

Hint: replace `selected=true` with `selected={true}`
"""

assert message =~ """
passing unquoted attribute values has been deprecated and will be removed in future versions.

Hint: replace `checked=false` with `checked={false}`
"""

assert line == 2
end

test "warning when a aliased component cannot be loaded" do
alias Components.But, warn: false

Expand Down
8 changes: 4 additions & 4 deletions test/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,8 @@ defmodule Surface.Compiler.ParserTest do
"""

attributes = [
{"prop1", 1, %{line: 2, file: "nofile", column: 3}},
{"prop2", 2, %{line: 3, file: "nofile", column: 3}}
{"prop1", 1, %{line: 2, file: "nofile", column: 3, unquoted_string?: true}},
{"prop2", 2, %{line: 3, file: "nofile", column: 3, unquoted_string?: true}}
]

assert parse!(code) ==
Expand All @@ -674,8 +674,8 @@ defmodule Surface.Compiler.ParserTest do

attributes = [
{"prop1", true, %{line: 2, file: "nofile", column: 3}},
{"prop2", true, %{line: 3, file: "nofile", column: 3}},
{"prop3", false, %{line: 4, file: "nofile", column: 3}},
{"prop2", true, %{line: 3, file: "nofile", column: 3, unquoted_string?: true}},
{"prop3", false, %{line: 4, file: "nofile", column: 3, unquoted_string?: true}},
{"prop4", true, %{line: 5, file: "nofile", column: 3}}
]

Expand Down