Add ModuleNameLookupTable to arguments

This commit is contained in:
Jeroen Engels 2024-03-26 22:39:03 +01:00
parent 00c344b88c
commit c955278351
3 changed files with 36 additions and 30 deletions

View File

@ -2,7 +2,7 @@ module Css.ClassFunction exposing
( CssArgument(..)
, fromLiteral
, baseCssFunctions
, firstArgumentIsClass, htmlAttributesAttribute, htmlAttributesClassList
, Arguments, firstArgumentIsClass, htmlAttributesAttribute, htmlAttributesClassList
, smartFirstArgumentIsClass
)
@ -11,7 +11,7 @@ module Css.ClassFunction exposing
@docs CssArgument
@docs fromLiteral
@docs baseCssFunctions
@docs firstArgumentIsClass, htmlAttributesAttribute, htmlAttributesClassList
@docs Arguments, firstArgumentIsClass, htmlAttributesAttribute, htmlAttributesClassList
@docs smartFirstArgumentIsClass
-}
@ -19,6 +19,7 @@ module Css.ClassFunction exposing
import Elm.Syntax.Expression as Expression exposing (Expression)
import Elm.Syntax.Node as Node exposing (Node)
import Elm.Syntax.Range exposing (Range)
import Review.ModuleNameLookupTable exposing (ModuleNameLookupTable)
type CssArgument
@ -28,6 +29,13 @@ type CssArgument
| MissingArgument Int
type alias Arguments =
{ firstArgument : Node Expression
, restOfArguments : List (Node Expression)
, lookupTable : ModuleNameLookupTable
}
fromLiteral : Node Expression -> CssArgument
fromLiteral node =
case Node.value node of
@ -38,13 +46,13 @@ fromLiteral node =
UngraspableExpression (Node.range node)
fromExpression : Node Expression -> List CssArgument
fromExpression node =
fromExpressionHelp [ node ] []
fromExpression : ModuleNameLookupTable -> Node Expression -> List CssArgument
fromExpression lookupTable node =
fromExpressionHelp lookupTable [ node ] []
fromExpressionHelp : List (Node Expression) -> List CssArgument -> List CssArgument
fromExpressionHelp nodes acc =
fromExpressionHelp : ModuleNameLookupTable -> List (Node Expression) -> List CssArgument -> List CssArgument
fromExpressionHelp lookupTable nodes acc =
case nodes of
[] ->
acc
@ -52,22 +60,22 @@ fromExpressionHelp nodes acc =
node :: rest ->
case Node.value node of
Expression.Literal str ->
fromExpressionHelp rest (Literal str :: acc)
fromExpressionHelp lookupTable rest (Literal str :: acc)
Expression.ParenthesizedExpression expr ->
fromExpressionHelp (expr :: rest) acc
fromExpressionHelp lookupTable (expr :: rest) acc
Expression.IfBlock _ then_ else_ ->
fromExpressionHelp (then_ :: else_ :: rest) acc
fromExpressionHelp lookupTable (then_ :: else_ :: rest) acc
Expression.CaseExpression { cases } ->
fromExpressionHelp (List.foldl (\( _, expr ) nodesAcc -> expr :: nodesAcc) rest cases) acc
fromExpressionHelp lookupTable (List.foldl (\( _, expr ) nodesAcc -> expr :: nodesAcc) rest cases) acc
_ ->
fromExpressionHelp rest (UngraspableExpression (Node.range node) :: acc)
fromExpressionHelp lookupTable rest (UngraspableExpression (Node.range node) :: acc)
baseCssFunctions : List ( String, { firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List CssArgument )
baseCssFunctions : List ( String, Arguments -> List CssArgument )
baseCssFunctions =
[ ( "Html.Attributes.class", \{ firstArgument } -> [ fromLiteral firstArgument ] )
, ( "Svg.Attributes.class", \{ firstArgument } -> [ fromLiteral firstArgument ] )
@ -76,17 +84,17 @@ baseCssFunctions =
]
firstArgumentIsClass : { firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List CssArgument
firstArgumentIsClass : Arguments -> List CssArgument
firstArgumentIsClass { firstArgument } =
[ fromLiteral firstArgument ]
smartFirstArgumentIsClass : { firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List CssArgument
smartFirstArgumentIsClass { firstArgument } =
fromExpression firstArgument
smartFirstArgumentIsClass : Arguments -> List CssArgument
smartFirstArgumentIsClass { lookupTable, firstArgument } =
fromExpression lookupTable firstArgument
htmlAttributesAttribute : { firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List CssArgument
htmlAttributesAttribute : Arguments -> List CssArgument
htmlAttributesAttribute { firstArgument, restOfArguments } =
case Node.value firstArgument of
Expression.Literal "class" ->
@ -101,7 +109,7 @@ htmlAttributesAttribute { firstArgument, restOfArguments } =
[]
htmlAttributesClassList : { firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List CssArgument
htmlAttributesClassList : Arguments -> List CssArgument
htmlAttributesClassList { firstArgument } =
case Node.value firstArgument of
Expression.ListExpr list ->

View File

@ -217,11 +217,11 @@ This example is for adding `Class.fromAttr`.
|> Css.NoUnknownClasses.rule
]
cssUsingFunctions : Dict ( ModuleName, String ) ({ firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List ClassFunction.CssArgument)
cssUsingFunctions : Dict ( ModuleName, String ) (ClassFunction.Arguments -> List ClassFunction.CssArgument)
cssUsingFunctions =
Dict.fromList [ ( ( [ "Class" ], "fromString" ), classFromStringFunction ) ]
classFromStringFunction : { firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List ClassFunction.CssArgument
classFromStringFunction : ClassFunction.Arguments -> List ClassFunction.CssArgument
classFromStringFunction { firstArgument } =
[ ClassFunction.fromLiteral firstArgument ]
@ -231,7 +231,7 @@ Here is how `Html.Attributes.classList` is implemented (Reminder of an example u
import Css.ClassFunction
-- TODO Add missing imports
cssUsingFunctions : Dict ( ModuleName, String ) ({ firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List ClassFunction.CssArgument)
cssUsingFunctions : Dict ( ModuleName, String ) (ClassFunction.Arguments -> List ClassFunction.CssArgument)
cssUsingFunctions =
Dict.fromList
[ ( ( [ "Html", "Attributes" ], "classList" ), \{ firstArgument } -> htmlAttributesClassList firstArgument )
@ -260,7 +260,7 @@ Here is how `Html.Attributes.classList` is implemented (Reminder of an example u
-}
withCssUsingFunctions :
List ( String, { firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List CssArgument )
List ( String, ClassFunction.Arguments -> List CssArgument )
-> Configuration
-> Configuration
withCssUsingFunctions newFunctions (Configuration configuration) =
@ -367,9 +367,7 @@ getCssFunction cssFunctions name moduleName =
type alias CssFunctions =
Dict
String
({ firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List CssArgument)
Dict String (ClassFunction.Arguments -> List CssArgument)
reportClasses : CssFunctions -> ModuleContext -> Range -> String -> Node Expression -> List (Node Expression) -> ( List (Rule.Error {}), ModuleContext )
@ -379,7 +377,7 @@ reportClasses cssFunctions context fnRange name firstArgument restOfArguments =
|> Maybe.andThen (\moduleName -> getCssFunction cssFunctions name moduleName)
of
Just cssFunction ->
( errorsForCssFunction context.knownClasses cssFunction fnRange { firstArgument = firstArgument, restOfArguments = restOfArguments }
( errorsForCssFunction context.knownClasses cssFunction fnRange { lookupTable = context.lookupTable, firstArgument = firstArgument, restOfArguments = restOfArguments }
, { context | functionOrValuesToIgnore = RangeDict.insert fnRange () context.functionOrValuesToIgnore }
)
@ -389,9 +387,9 @@ reportClasses cssFunctions context fnRange name firstArgument restOfArguments =
errorsForCssFunction :
Set String
-> ({ firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List CssArgument)
-> (ClassFunction.Arguments -> List CssArgument)
-> Range
-> { firstArgument : Node Expression, restOfArguments : List (Node Expression) }
-> ClassFunction.Arguments
-> List (Rule.Error {})
errorsForCssFunction knownClasses cssFunction fnRange target =
cssFunction target

View File

@ -345,7 +345,7 @@ view model =
]
classFromAttrFunction : { firstArgument : Node Expression, restOfArguments : List (Node Expression) } -> List CssArgument
classFromAttrFunction : ClassFunction.Arguments -> List CssArgument
classFromAttrFunction { firstArgument } =
[ fromLiteral firstArgument ]