Copy tests

This commit is contained in:
Jeroen Engels 2024-05-07 11:04:09 +02:00
parent 14132fbf29
commit 955c3c56be
2 changed files with 54 additions and 19 deletions

View File

@ -1,17 +1,18 @@
module Css.NoUnusedCssClasses exposing (rule) module Css.NoUnusedCssClasses exposing (cssFiles, dontReport, rule, withCssUsingFunctions)
import Css.ClassFunction as ClassFunction exposing (CssArgument)
import Dict exposing (Dict) import Dict exposing (Dict)
import Elm.Syntax.Expression as Expression exposing (Expression) import Elm.Syntax.Expression as Expression exposing (Expression)
import Elm.Syntax.Node as Node exposing (Node) import Elm.Syntax.Node as Node exposing (Node)
import Elm.Syntax.Range exposing (Range) import Elm.Syntax.Range exposing (Range)
import Regex exposing (Regex) import Regex exposing (Regex)
import Review.FilePattern as FilePattern import Review.FilePattern as FilePattern exposing (FilePattern)
import Review.Rule as Rule exposing (Rule) import Review.Rule as Rule exposing (Rule)
import Set exposing (Set) import Set exposing (Set)
rule : Rule rule : Configuration -> Rule
rule = rule (Configuration configuration) =
Rule.newProjectRuleSchema "NoUnusedCssClasses" initialProjectContext Rule.newProjectRuleSchema "NoUnusedCssClasses" initialProjectContext
|> Rule.withExtraFilesProjectVisitor cssFilesVisitor |> Rule.withExtraFilesProjectVisitor cssFilesVisitor
[ FilePattern.include "**/*.css" ] [ FilePattern.include "**/*.css" ]
@ -25,12 +26,46 @@ rule =
|> Rule.fromProjectRuleSchema |> Rule.fromProjectRuleSchema
type Configuration
= Configuration
{ classesNotToReport : Set String
, cssFiles : List FilePattern
, cssFunctions : CssFunctions
}
type alias CssFunctions =
Dict String (ClassFunction.Arguments -> List CssArgument)
cssFiles : List FilePattern -> Configuration
cssFiles globs =
Configuration
{ classesNotToReport = Set.empty
, cssFiles = globs
, cssFunctions = Dict.fromList ClassFunction.baseCssFunctions
}
withCssUsingFunctions :
List ( String, ClassFunction.Arguments -> List CssArgument )
-> Configuration
-> Configuration
withCssUsingFunctions newFunctions (Configuration configuration) =
Configuration { configuration | cssFunctions = List.foldl (\( key, fn ) acc -> Dict.insert key fn acc) configuration.cssFunctions newFunctions }
moduleVisitor : Rule.ModuleRuleSchema {} ModuleContext -> Rule.ModuleRuleSchema { hasAtLeastOneVisitor : () } ModuleContext moduleVisitor : Rule.ModuleRuleSchema {} ModuleContext -> Rule.ModuleRuleSchema { hasAtLeastOneVisitor : () } ModuleContext
moduleVisitor schema = moduleVisitor schema =
schema schema
|> Rule.withExpressionEnterVisitor expressionVisitor |> Rule.withExpressionEnterVisitor expressionVisitor
dontReport : List String -> Configuration -> Configuration
dontReport classesNotToReport (Configuration configuration) =
Configuration { configuration | classesNotToReport = List.foldl Set.insert configuration.classesNotToReport classesNotToReport }
type alias ProjectContext = type alias ProjectContext =
{ cssFiles : { cssFiles :
Dict Dict

View File

@ -1,7 +1,7 @@
module Css.NoUnknownCssClassesTest exposing (all) module Css.NoUnusedCssClassesTest exposing (all)
import Css.ClassFunction as ClassFunction exposing (CssArgument, fromLiteral) import Css.ClassFunction as ClassFunction exposing (CssArgument, fromLiteral)
import Css.NoUnknownCssClasses exposing (addKnownClasses, cssFiles, rule, withCssUsingFunctions) import Css.NoUnusedCssClasses exposing (cssFiles, dontReport, rule, withCssUsingFunctions)
import Dict import Dict
import Elm.Syntax.Expression exposing (Expression) import Elm.Syntax.Expression exposing (Expression)
import Elm.Syntax.Node exposing (Node) import Elm.Syntax.Node exposing (Node)
@ -14,7 +14,7 @@ import Test exposing (Test, describe, test)
all : Test all : Test
all = all =
describe "NoUnknownCssClasses" describe "NoUnusedCssClasses"
[ test "should not report an error when strings don't seem to be CSS classes" <| [ test "should not report an error when strings don't seem to be CSS classes" <|
\() -> \() ->
"""module A exposing (..) """module A exposing (..)
@ -35,7 +35,7 @@ import Html.Attributes as Attr
view model = view model =
Html.span [ Attr.class "unknown" ] [] Html.span [ Attr.class "unknown" ] []
""" """
|> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> addKnownClasses [ "known", "bar", "unknown2" ] |> rule) |> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> dontReport [ "known", "bar", "unknown2" ] |> rule)
|> Review.Test.expectErrors |> Review.Test.expectErrors
[ Review.Test.error [ Review.Test.error
{ message = "Unknown CSS class \"unknown\"" { message = "Unknown CSS class \"unknown\""
@ -55,7 +55,7 @@ import Html.Attributes as Attr
view model = view model =
Html.span [ Attr.class "known" ] [] Html.span [ Attr.class "known" ] []
""" """
|> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> addKnownClasses [ "known" ] |> rule) |> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> dontReport [ "known" ] |> rule)
|> Review.Test.expectNoErrors |> Review.Test.expectNoErrors
, test "should not report an error when the class argument is empty" <| , test "should not report an error when the class argument is empty" <|
\() -> \() ->
@ -66,7 +66,7 @@ import Html.Attributes as Attr
view model = view model =
Html.span [ Attr.class "" ] [] Html.span [ Attr.class "" ] []
""" """
|> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> addKnownClasses [ "known" ] |> rule) |> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> dontReport [ "known" ] |> rule)
|> Review.Test.expectNoErrors |> Review.Test.expectNoErrors
, test "should not report an error when the class argument is only made out of spaces" <| , test "should not report an error when the class argument is only made out of spaces" <|
\() -> \() ->
@ -77,7 +77,7 @@ import Html.Attributes as Attr
view model = view model =
Html.span [ Attr.class " " ] [] Html.span [ Attr.class " " ] []
""" """
|> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> addKnownClasses [ "known" ] |> rule) |> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> dontReport [ "known" ] |> rule)
|> Review.Test.expectNoErrors |> Review.Test.expectNoErrors
, test "should report an error when encountering an unknown CSS class through Html.Attributes.class in <| pipe" <| , test "should report an error when encountering an unknown CSS class through Html.Attributes.class in <| pipe" <|
\() -> \() ->
@ -170,7 +170,7 @@ view model =
, ( variable, model.b ) , ( variable, model.b )
] ]
""" """
|> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> addKnownClasses [ "known" ] |> rule) |> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> dontReport [ "known" ] |> rule)
|> Review.Test.expectErrors |> Review.Test.expectErrors
[ Review.Test.error [ Review.Test.error
{ message = "Non-literal argument to CSS class function" { message = "Non-literal argument to CSS class function"
@ -204,7 +204,7 @@ import Html.Attributes as Attr
view model = view model =
Attr.attribute "class" "known" Attr.attribute "class" "known"
""" """
|> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> addKnownClasses [ "known" ] |> rule) |> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> dontReport [ "known" ] |> rule)
|> Review.Test.expectNoErrors |> Review.Test.expectNoErrors
, test "should not report an error when Html.Attributes.attribute is used with something else than \"class\"" <| , test "should not report an error when Html.Attributes.attribute is used with something else than \"class\"" <|
\() -> \() ->
@ -215,7 +215,7 @@ import Html.Attributes as Attr
view model = view model =
Attr.attribute "id" model.id Attr.attribute "id" model.id
""" """
|> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> addKnownClasses [ "known" ] |> rule) |> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> dontReport [ "known" ] |> rule)
|> Review.Test.expectNoErrors |> Review.Test.expectNoErrors
, test "should not report an error when Html.Attributes.attribute is used with a non-literal attribute name" <| , test "should not report an error when Html.Attributes.attribute is used with a non-literal attribute name" <|
\() -> \() ->
@ -226,7 +226,7 @@ import Html.Attributes as Attr
view model = view model =
Attr.attribute name model.name Attr.attribute name model.name
""" """
|> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> addKnownClasses [ "known" ] |> rule) |> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> dontReport [ "known" ] |> rule)
|> Review.Test.expectNoErrors |> Review.Test.expectNoErrors
, test "should not report an error when encountering a literal CSS class with a custom CSS function" <| , test "should not report an error when encountering a literal CSS class with a custom CSS function" <|
\() -> \() ->
@ -238,7 +238,7 @@ view model =
""" """
|> Review.Test.run |> Review.Test.run
(cssFiles [ FilePattern.include "*.css" ] (cssFiles [ FilePattern.include "*.css" ]
|> addKnownClasses [ "known" ] |> dontReport [ "known" ]
|> withCssUsingFunctions [ ( "Class.fromString", classFromAttrFunction ) ] |> withCssUsingFunctions [ ( "Class.fromString", classFromAttrFunction ) ]
|> rule |> rule
) )
@ -253,7 +253,7 @@ view model =
""" """
|> Review.Test.run |> Review.Test.run
(cssFiles [ FilePattern.include "*.css" ] (cssFiles [ FilePattern.include "*.css" ]
|> addKnownClasses [ "known" ] |> dontReport [ "known" ]
|> withCssUsingFunctions [ ( "Class.fromString", classFromAttrFunction ) ] |> withCssUsingFunctions [ ( "Class.fromString", classFromAttrFunction ) ]
|> rule |> rule
) )
@ -339,7 +339,7 @@ import Html.Attributes as Attr
view model = view model =
Attr.class <| if model.condition then "known" else nonLiteral Attr.class <| if model.condition then "known" else nonLiteral
""" """
|> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> addKnownClasses [ "known" ] |> withCssUsingFunctions [ ( "Html.Attributes.class", ClassFunction.smartFirstArgumentIsClass ) ] |> rule) |> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> dontReport [ "known" ] |> withCssUsingFunctions [ ( "Html.Attributes.class", ClassFunction.smartFirstArgumentIsClass ) ] |> rule)
|> Review.Test.expectErrors |> Review.Test.expectErrors
[ Review.Test.error [ Review.Test.error
{ message = "Non-literal argument to CSS class function" { message = "Non-literal argument to CSS class function"
@ -358,7 +358,7 @@ view model =
A -> "known" A -> "known"
B -> nonLiteral B -> nonLiteral
""" """
|> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> addKnownClasses [ "known" ] |> withCssUsingFunctions [ ( "Html.Attributes.class", ClassFunction.smartFirstArgumentIsClass ) ] |> rule) |> Review.Test.run (cssFiles [ FilePattern.include "*.css" ] |> dontReport [ "known" ] |> withCssUsingFunctions [ ( "Html.Attributes.class", ClassFunction.smartFirstArgumentIsClass ) ] |> rule)
|> Review.Test.expectErrors |> Review.Test.expectErrors
[ Review.Test.error [ Review.Test.error
{ message = "Non-literal argument to CSS class function" { message = "Non-literal argument to CSS class function"