From 0efe84ab3366d963fc3e716380ab7fa79495a380 Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Fri, 8 Nov 2024 16:54:59 +0100 Subject: [PATCH 1/3] Add customTypeWith --- src/Elm/Arg.elm | 17 +++++++++++++++-- src/Internal/Arg.elm | 33 +++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/Elm/Arg.elm b/src/Elm/Arg.elm index f39cd18..545b1e8 100644 --- a/src/Elm/Arg.elm +++ b/src/Elm/Arg.elm @@ -5,7 +5,7 @@ module Elm.Arg exposing , aliasAs , ignore, string, char, int , list, item, items, listRemaining - , customType + , customType, customTypeWith ) {-| An `Arg` can be used to pattern match on the arguments of a function. @@ -64,7 +64,7 @@ Will generate @docs list, item, items, listRemaining -@docs customType +@docs customType, customTypeWith -} @@ -283,3 +283,16 @@ Which will generate customType : String -> a -> Arg a customType = Internal.Arg.customType + + +{-| Same as `customType`, but allows to specify the module name and type name. +-} +customTypeWith : + { importFrom : List String + , typeName : String + , variantName : String + } + -> a + -> Arg a +customTypeWith = + Internal.Arg.customTypeWith diff --git a/src/Internal/Arg.elm b/src/Internal/Arg.elm index 130a583..1a939f0 100644 --- a/src/Internal/Arg.elm +++ b/src/Internal/Arg.elm @@ -7,6 +7,7 @@ module Internal.Arg exposing , customType , item, items, list, listRemaining, record, field , ignore, unit + , customTypeWith ) {-| @@ -531,34 +532,54 @@ list toList = customType : String -> a -> Arg a customType name toType = + customTypeWith + { typeName = Format.formatValue name + , variantName = name + , importFrom = [] + } + toType + + +customTypeWith : + { importFrom : List String + , typeName : String + , variantName : String + } + -> a + -> Arg a +customTypeWith { typeName, variantName, importFrom } toType = Arg (\index -> let annotation = Ok - { type_ = Internal.Types.custom [] (Format.formatValue name) [] + { type_ = Internal.Types.custom importFrom (Format.formatType typeName) [] , inferences = Dict.empty , aliases = Compiler.emptyAliases } + imports : List (List String) imports = - [] + if List.isEmpty importFrom then + [] + + else + [ importFrom ] in { details = { imports = imports , pattern = Compiler.nodify (Pattern.NamedPattern - { moduleName = [] - , name = Format.formatType name + { moduleName = importFrom + , name = Format.formatType variantName } [] ) , annotation = annotation } , index = Index.dive index - , value = - toType + , value = toType } ) From 8f182d8e0f134f8a7f4e922e1ba1b604a9b070da Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Sat, 30 Nov 2024 19:51:14 +0100 Subject: [PATCH 2/3] Add test for custom pattern with type --- tests/Pattern.elm | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Pattern.elm b/tests/Pattern.elm index 50fd2e4..47f2793 100644 --- a/tests/Pattern.elm +++ b/tests/Pattern.elm @@ -179,6 +179,18 @@ suite = Nothing -> "Oh, it's nothing." """ + , test "custom type helpers - with imports" <| + \() -> + Elm.Expect.renderedAs + (Elm.fn + (Arg.customTypeWith { importFrom = [ "From", "Here" ], variantName = "Variant", typeName = "SomeType" } identity + |> Arg.item (Arg.char 'c') + ) + (\_ -> Elm.string "There is 1 item") + ) + """ + \\(From.Here.Variant 'c') -> "There is 1 item" + """ , describe "literal patterns" [ test "unit" <| \() -> From bf01e81971f965262c4689fbb7e79e71a85915fc Mon Sep 17 00:00:00 2001 From: Leonardo Taglialegne Date: Sat, 30 Nov 2024 19:55:09 +0100 Subject: [PATCH 3/3] Check type qualification for `Arg.customWithType` patterns --- tests/Pattern.elm | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Pattern.elm b/tests/Pattern.elm index 47f2793..4006b42 100644 --- a/tests/Pattern.elm +++ b/tests/Pattern.elm @@ -191,6 +191,21 @@ suite = """ \\(From.Here.Variant 'c') -> "There is 1 item" """ + , test "custom type helpers - type qualification" <| + \() -> + Elm.Expect.declarationAs + (Elm.declaration "fn" <| + Elm.fn + (Arg.customTypeWith { importFrom = [ "From", "Here" ], variantName = "Variant", typeName = "SomeType" } identity + |> Arg.item (Arg.char 'c') + ) + (\_ -> Elm.string "There is 1 item") + ) + """ + fn : From.Here.SomeType -> String + fn (From.Here.Variant 'c') = + "There is 1 item" + """ , describe "literal patterns" [ test "unit" <| \() ->