From 656efbb361e53c328cc5183bc4b383e476c7d84e Mon Sep 17 00:00:00 2001 From: Malian De Ron Date: Fri, 7 May 2021 15:30:12 +0200 Subject: [PATCH 1/4] Deprecated unquoted strings --- lib/surface/compiler.ex | 14 +++++++++++++- test/compiler_test.exs | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/surface/compiler.ex b/lib/surface/compiler.ex index 1c809c70..84dbd2a1 100644 --- a/lib/surface/compiler.ex +++ b/lib/surface/compiler.ex @@ -672,7 +672,19 @@ defmodule Surface.Compiler do } end - defp attr_value(name, type, value, meta) do + defp attr_value(name, type, value, meta) when is_boolean(value) or is_integer(value) do + message = """ + Passing attribute values as `#{name}=#{value}` has been deprecated and will be removed in future version. + + Hint: replace `#{name}=#{value}` by `#{name}={#{value}}` + """ + + IOHelper.warn(message, meta.caller, fn _ -> meta.line end) + + Surface.TypeHandler.literal_to_ast_node!(type, name, value, meta) + end + + defp attr_value(name, type, value, meta) when is_binary(value) do Surface.TypeHandler.literal_to_ast_node!(type, name, value, meta) end diff --git a/test/compiler_test.exs b/test/compiler_test.exs index 70e703b2..96d4bd6f 100644 --- a/test/compiler_test.exs +++ b/test/compiler_test.exs @@ -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 = """ +
+
+
+ """ + + {:warn, line, message} = run_compile(code, __ENV__) + + assert message =~ """ + Passing attribute values as `tabindex=1` has been deprecated and will be removed in future version. + + Hint: replace `tabindex=1` by `tabindex={1}` + """ + + assert line == 2 + end + + test "warning when passing boolean attribute values that are not enclosed by {}" do + code = """ +
+
+
+ """ + + {:warn, line, message} = run_compile(code, __ENV__) + + assert message =~ """ + Passing attribute values as `selected=true` has been deprecated and will be removed in future version. + + Hint: replace `selected=true` by `selected={true}` + """ + + assert message =~ """ + Passing attribute values as `checked=false` has been deprecated and will be removed in future version. + + Hint: replace `checked=false` by `checked={false}` + """ + + assert line == 2 + end + test "warning when a aliased component cannot be loaded" do alias Components.But, warn: false From 7158e2483e41506b47a1300d7e3e2bbf765c824b Mon Sep 17 00:00:00 2001 From: Malian De Ron Date: Fri, 7 May 2021 15:44:47 +0200 Subject: [PATCH 2/4] Set warning_as_error on github --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5c348f3d..8ee68f2a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: From 9f1db6462683403f946cebce9334893885f26131 Mon Sep 17 00:00:00 2001 From: Malian De Ron Date: Sat, 8 May 2021 06:52:36 +0200 Subject: [PATCH 3/4] Rephrase warning message --- lib/surface/compiler.ex | 4 ++-- test/compiler_test.exs | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/surface/compiler.ex b/lib/surface/compiler.ex index 84dbd2a1..3ee48384 100644 --- a/lib/surface/compiler.ex +++ b/lib/surface/compiler.ex @@ -674,9 +674,9 @@ defmodule Surface.Compiler do defp attr_value(name, type, value, meta) when is_boolean(value) or is_integer(value) do message = """ - Passing attribute values as `#{name}=#{value}` has been deprecated and will be removed in future version. + passing unquoted attribute values has been deprecated and will be removed in future versions. - Hint: replace `#{name}=#{value}` by `#{name}={#{value}}` + Hint: replace `#{name}=#{value}` with `#{name}={#{value}}` """ IOHelper.warn(message, meta.caller, fn _ -> meta.line end) diff --git a/test/compiler_test.exs b/test/compiler_test.exs index 96d4bd6f..ce66d4cb 100644 --- a/test/compiler_test.exs +++ b/test/compiler_test.exs @@ -693,9 +693,9 @@ defmodule Surface.CompilerSyncTest do {:warn, line, message} = run_compile(code, __ENV__) assert message =~ """ - Passing attribute values as `tabindex=1` has been deprecated and will be removed in future version. + passing unquoted attribute values has been deprecated and will be removed in future versions. - Hint: replace `tabindex=1` by `tabindex={1}` + Hint: replace `tabindex=1` with `tabindex={1}` """ assert line == 2 @@ -711,15 +711,15 @@ defmodule Surface.CompilerSyncTest do {:warn, line, message} = run_compile(code, __ENV__) assert message =~ """ - Passing attribute values as `selected=true` has been deprecated and will be removed in future version. + passing unquoted attribute values has been deprecated and will be removed in future versions. - Hint: replace `selected=true` by `selected={true}` + Hint: replace `selected=true` with `selected={true}` """ assert message =~ """ - Passing attribute values as `checked=false` has been deprecated and will be removed in future version. + passing unquoted attribute values has been deprecated and will be removed in future versions. - Hint: replace `checked=false` by `checked={false}` + Hint: replace `checked=false` with `checked={false}` """ assert line == 2 From caededb6c701f37b30c1be6e890713584eacb74c Mon Sep 17 00:00:00 2001 From: Malian De Ron Date: Sat, 8 May 2021 13:12:39 +0200 Subject: [PATCH 4/4] Add unquoted_string? as extra meta --- lib/surface/compiler.ex | 25 ++++++++++++------------- lib/surface/compiler/parser.ex | 3 +++ test/parser_test.exs | 8 ++++---- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/surface/compiler.ex b/lib/surface/compiler.ex index 3ee48384..13c65eff 100644 --- a/lib/surface/compiler.ex +++ b/lib/surface/compiler.ex @@ -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) @@ -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, @@ -672,19 +683,7 @@ defmodule Surface.Compiler do } end - defp attr_value(name, type, value, meta) when is_boolean(value) or is_integer(value) 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) - - Surface.TypeHandler.literal_to_ast_node!(type, name, value, meta) - end - - defp attr_value(name, type, value, meta) when is_binary(value) do + defp attr_value(name, type, value, meta) do Surface.TypeHandler.literal_to_ast_node!(type, name, value, meta) end diff --git a/lib/surface/compiler/parser.ex b/lib/surface/compiler/parser.ex index 3428c96d..04bd2e21 100644 --- a/lib/surface/compiler/parser.ex +++ b/lib/surface/compiler/parser.ex @@ -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)} _ -> diff --git a/test/parser_test.exs b/test/parser_test.exs index d1c56619..7c07b3af 100644 --- a/test/parser_test.exs +++ b/test/parser_test.exs @@ -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) == @@ -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}} ]