Skip to content

Commit d1d796e

Browse files
committed
improvement: helpful error message when mixing entity inline/block syntax
1 parent 9a44167 commit d1d796e

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/spark/dsl/extension.ex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,25 @@ defmodule Spark.Dsl.Extension do
12521252
end
12531253

12541254
@moduledoc false
1255+
1256+
# Generate mixed syntax macro only for entities without optional args
1257+
if not Enum.any?(entity.args, fn
1258+
{:optional, _, _} -> true
1259+
{:optional, _} -> true
1260+
_ -> false
1261+
end) do
1262+
defmacro unquote(entity.name)(unquote_splicing(args), extra_opts, [do: _] = _block) do
1263+
entity_name = unquote(entity.name)
1264+
1265+
raise Spark.Error.DslError,
1266+
module: __CALLER__.module,
1267+
message:
1268+
"Cannot use both inline syntax and block syntax for entity `#{entity_name}`. " <>
1269+
"Use block syntax `#{entity_name} args... do ... end`",
1270+
path: unquote(section_path ++ nested_entity_path)
1271+
end
1272+
end
1273+
12551274
defmacro unquote(entity.name)(unquote_splicing(args), opts \\ nil) do
12561275
section_path = unquote(Macro.escape(section_path))
12571276
entity_schema = unquote(Macro.escape(entity.schema))

test/dsl_test.exs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ defmodule Spark.DslTest do
396396
end
397397
end
398398

399-
describe "DSL section syntax validation" do
399+
describe "DSL section and entity syntax validation" do
400400
test "mixed inline/block syntax should provide helpful error" do
401401
assert_raise Spark.Error.DslError,
402402
~r/Cannot use both inline syntax and block syntax.*personal_details/,
@@ -424,6 +424,23 @@ defmodule Spark.DslTest do
424424
end
425425
end
426426
end
427+
428+
test "mixed inline/block syntax for entity should provide helpful error" do
429+
assert_raise Spark.Error.DslError,
430+
~r/Cannot use both inline syntax and block syntax.*preset/,
431+
fn ->
432+
defmodule EntityMixedSyntax do
433+
@moduledoc false
434+
use Spark.Test.Contact
435+
436+
presets do
437+
preset :einstein, default_message: "E=mc²" do
438+
contacter(fn x -> x end)
439+
end
440+
end
441+
end
442+
end
443+
end
427444
end
428445

429446
describe "optional entity arguments" do

0 commit comments

Comments
 (0)