From 43edf174338073580c95fd955d86160cacee592f Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Sat, 23 Mar 2024 22:11:08 +0100 Subject: [PATCH] Change CssFunctions data structure --- tests/Css/NoUnknownClasses.elm | 25 ++++++++++++++----------- tests/Css/NoUnknownClassesTest.elm | 16 ++++++---------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/tests/Css/NoUnknownClasses.elm b/tests/Css/NoUnknownClasses.elm index b45d2e7d..70abfb20 100644 --- a/tests/Css/NoUnknownClasses.elm +++ b/tests/Css/NoUnknownClasses.elm @@ -257,13 +257,11 @@ Here is how `Html.Attributes.classList` is implemented (Reminder of an example u -} withCssUsingFunctions : - Dict - ( ModuleName, String ) - ({ firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List CssArgument) + List ( String, { firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List CssArgument ) -> Configuration -> Configuration withCssUsingFunctions newFunctions (Configuration configuration) = - Configuration { configuration | cssFunctions = Dict.union newFunctions configuration.cssFunctions } + Configuration { configuration | cssFunctions = List.foldl (\( key, fn ) acc -> Dict.insert key fn acc) configuration.cssFunctions newFunctions } type alias ProjectContext = @@ -346,7 +344,7 @@ reportStrayCssFunction cssFunctions context range name = else case ModuleNameLookupTable.moduleNameAt context.lookupTable range - |> Maybe.andThen (\moduleName -> Dict.get ( moduleName, name ) cssFunctions) + |> Maybe.andThen (\moduleName -> getCssFunction cssFunctions name moduleName) of Just _ -> [ Rule.error @@ -360,19 +358,24 @@ reportStrayCssFunction cssFunctions context range name = [] +getCssFunction : Dict String v -> String -> List String -> Maybe v +getCssFunction cssFunctions name moduleName = + Dict.get (String.join "." moduleName ++ "." ++ name) cssFunctions + + type alias CssFunctions = Dict - ( ModuleName, String ) + String ({ firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List CssArgument) baseCssFunctions : CssFunctions baseCssFunctions = Dict.fromList - [ ( ( [ "Html", "Attributes" ], "class" ), \{ firstArgument } -> [ fromLiteral firstArgument ] ) - , ( ( [ "Svg", "Attributes" ], "class" ), \{ firstArgument } -> [ fromLiteral firstArgument ] ) - , ( ( [ "Html", "Attributes" ], "classList" ), \{ firstArgument } -> htmlAttributesClassList firstArgument ) - , ( ( [ "Html", "Attributes" ], "attribute" ), attribute ) + [ ( "Html.Attributes.class", \{ firstArgument } -> [ fromLiteral firstArgument ] ) + , ( "Svg.Attributes.class", \{ firstArgument } -> [ fromLiteral firstArgument ] ) + , ( "Html.Attributes.classList", \{ firstArgument } -> htmlAttributesClassList firstArgument ) + , ( "Html.Attributes.attribute", attribute ) ] @@ -424,7 +427,7 @@ reportClasses : CssFunctions -> ModuleContext -> Range -> String -> Node Express reportClasses cssFunctions context fnRange name firstArgument restOfArguments = case ModuleNameLookupTable.moduleNameAt context.lookupTable fnRange - |> Maybe.andThen (\moduleName -> Dict.get ( moduleName, name ) cssFunctions) + |> Maybe.andThen (\moduleName -> getCssFunction cssFunctions name moduleName) of Just cssFunction -> ( errorsForCssFunction context.knownClasses cssFunction { firstArgument = firstArgument, restOfArguments = restOfArguments } diff --git a/tests/Css/NoUnknownClassesTest.elm b/tests/Css/NoUnknownClassesTest.elm index d14e1f60..be7e81d8 100644 --- a/tests/Css/NoUnknownClassesTest.elm +++ b/tests/Css/NoUnknownClassesTest.elm @@ -216,7 +216,7 @@ view model = |> Review.Test.run (cssFiles [ "*.css" ] |> addKnownClasses [ "known" ] - |> withCssUsingFunctions (Dict.fromList [ ( ( [ "Class" ], "fromString" ), classFromAttrFunction ) ]) + |> withCssUsingFunctions [ ( "Class.fromString", classFromAttrFunction ) ] |> rule ) |> Review.Test.expectNoErrors @@ -231,7 +231,7 @@ view model = |> Review.Test.run (cssFiles [ "*.css" ] |> addKnownClasses [ "known" ] - |> withCssUsingFunctions (Dict.fromList [ ( ( [ "Class" ], "fromString" ), classFromAttrFunction ) ]) + |> withCssUsingFunctions [ ( "Class.fromString", classFromAttrFunction ) ] |> rule ) |> Review.Test.expectErrors @@ -260,21 +260,17 @@ classListWithoutErrorsBeingReported = , test "should report an error when encountering a class function application with less arguments than where their class arguments are" <| \() -> """module A exposing (..) -import ClassFn +import Html.Attributes classFunctionWithoutErrorsBeingReported = - ClassFn.fnThatTakes5Args a b c d + Html.Attributes.attribute "class" """ - |> Review.Test.run - (cssFiles [ "*.css" ] - |> withCssUsingFunctions (Dict.fromList [ ( ( [ "ClassFn" ], "fnThatTakes5Args" ), always [] ) ]) - |> rule - ) + |> Review.Test.run (cssFiles [ "*.css" ] |> rule) |> Review.Test.expectErrors [ Review.Test.error { message = "Class using function is used without all of its class arguments" , details = [ "REPLACEME" ] - , under = "Html.Attributes.classList" + , under = "Html.Attributes.attribute" } ] , test "should report an error when being unable to parse a CSS file" <|