Init NoUnknownCssClasses

This commit is contained in:
Jeroen Engels 2024-03-19 22:04:16 +01:00
parent 4ad1dcfcdd
commit cedf1c240a
2 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,117 @@
module NoUnknownCssClasses exposing (rule)
{-|
@docs rule
-}
import Elm.Syntax.Expression exposing (Expression)
import Elm.Syntax.Node as Node exposing (Node)
import Review.ModuleNameLookupTable as ModuleNameLookupTable exposing (ModuleNameLookupTable)
import Review.Rule as Rule exposing (Rule)
import Set exposing (Set)
{-| Reports... REPLACEME
config =
[ NoUnknownCssClasses.rule
]
## Fail
a =
"REPLACEME example to replace"
## Success
a =
"REPLACEME example to replace"
## When (not) to enable this rule
This rule is useful when REPLACEME.
This rule is not useful when REPLACEME.
## Try it out
You can try this rule out by running the following command:
```bash
elm-review --template jfmengels/elm-review/example --rules NoUnknownCssClasses
```
-}
rule : Rule
rule =
Rule.newProjectRuleSchema "NoUnknownCssClasses" initialProjectContext
|> Rule.withModuleVisitor moduleVisitor
|> Rule.withModuleContextUsingContextCreator
{ fromProjectToModule = fromProjectToModule
, fromModuleToProject = fromModuleToProject
, foldProjectContexts = foldProjectContexts
}
-- Enable this if modules need to get information from other modules
-- |> Rule.withContextFromImportedModules
|> Rule.fromProjectRuleSchema
type alias ProjectContext =
{ knownClasses : Set String
}
type alias ModuleContext =
{ lookupTable : ModuleNameLookupTable
, knownClasses : Set String
}
moduleVisitor : Rule.ModuleRuleSchema schema ModuleContext -> Rule.ModuleRuleSchema { schema | hasAtLeastOneVisitor : () } ModuleContext
moduleVisitor schema =
schema
|> Rule.withExpressionEnterVisitor expressionVisitor
initialProjectContext : ProjectContext
initialProjectContext =
{ knownClasses = Set.empty
}
fromProjectToModule : Rule.ContextCreator ProjectContext ModuleContext
fromProjectToModule =
Rule.initContextCreator
(\lookupTable projectContext ->
{ lookupTable = lookupTable
, knownClasses = projectContext.knownClasses
}
)
|> Rule.withModuleNameLookupTable
fromModuleToProject : Rule.ContextCreator ModuleContext ProjectContext
fromModuleToProject =
Rule.initContextCreator
(\moduleContext ->
{ knownClasses = Set.empty
}
)
foldProjectContexts : ProjectContext -> ProjectContext -> ProjectContext
foldProjectContexts new previous =
{ knownClasses = previous.knownClasses
}
expressionVisitor : Node Expression -> ModuleContext -> ( List (Rule.Error {}), ModuleContext )
expressionVisitor node context =
case Node.value node of
_ ->
( [], context )

View File

@ -0,0 +1,31 @@
module NoUnknownCssClassesTest exposing (all)
import NoUnknownCssClasses exposing (rule)
import Review.Test
import Test exposing (Test, describe, test)
all : Test
all =
describe "NoUnknownCssClasses"
[ test "should not report an error when REPLACEME" <|
\() ->
"""module A exposing (..)
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectNoErrors
, test "should report an error when REPLACEME" <|
\() ->
"""module A exposing (..)
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "REPLACEME"
, details = [ "REPLACEME" ]
, under = "REPLACEME"
}
]
]