elm-review/tests/ModuleNameForValueTest.elm

268 lines
7.8 KiB
Elm
Raw Normal View History

2020-05-16 23:33:36 +03:00
module ModuleNameForValueTest exposing (all)
2020-01-12 20:01:49 +03:00
import Elm.Syntax.Expression as Expression exposing (Expression)
import Elm.Syntax.Node as Node exposing (Node(..))
import Fixtures.Dependencies as Dependencies
2020-01-12 20:01:49 +03:00
import Review.Project as Project exposing (Project)
import Review.Rule as Rule exposing (Error, Rule)
2020-01-15 11:54:26 +03:00
import Review.Test
import Scope
2020-01-12 20:01:49 +03:00
import Test exposing (Test, test)
all : Test
all =
2020-05-16 23:33:36 +03:00
Test.describe "Scope.moduleNameForValue"
[ forModuleRule
, forProjectRule
]
2020-01-12 20:01:49 +03:00
2020-05-16 23:33:36 +03:00
forModuleRule : Test
forModuleRule =
Test.describe "module rule"
[ test "should return the module that defined the value" <|
2020-03-27 20:31:34 +03:00
\() ->
"""module A exposing (..)
import Bar as Baz exposing (baz)
import ExposesSomeThings exposing (..)
import ExposesEverything exposing (..)
import Foo.Bar
import Html exposing (..)
import Http exposing (get)
localValue = 1
a = localValue
unknownValue
exposedElement
nonExposedElement
elementFromExposesEverything
VariantA
Foo.bar
Foo.Bar
Baz.foo
baz
button
Http.get
get
always
True
Just
"""
|> Review.Test.runWithProjectData project moduleRule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = """
<nothing>.localValue -> <nothing>.localValue
<nothing>.unknownValue -> <nothing>.unknownValue
<nothing>.exposedElement -> <nothing>.exposedElement
<nothing>.nonExposedElement -> <nothing>.nonExposedElement
<nothing>.elementFromExposesEverything -> <nothing>.elementFromExposesEverything
<nothing>.VariantA -> <nothing>.VariantA
Foo.bar -> Foo.bar
Foo.Bar -> Foo.Bar
Baz.foo -> Bar.foo
<nothing>.baz -> Bar.baz
<nothing>.button -> Html.button
Http.get -> Http.get
<nothing>.get -> Http.get
<nothing>.always -> Basics.always
<nothing>.True -> Basics.True
<nothing>.Just -> Maybe.Just"""
, details = [ "details" ]
, under = "module"
}
]
]
2020-05-16 23:33:36 +03:00
forProjectRule : Test
forProjectRule =
Test.describe "project rule"
[ test "should return the module that defined the value" <|
\() ->
[ """module A exposing (..)
import Bar as Baz exposing (baz)
import ExposesSomeThings exposing (..)
import ExposesEverything exposing (..)
import Foo.Bar
import Html exposing (..)
import Http exposing (get)
import Something.B as Something
import Something.C as Something
localValue = 1
localValueValueToBeShadowed = 1
type Msg = SomeMsgToBeShadowed
a = localValue
localValueValueToBeShadowed
SomeMsgToBeShadowed
SomeOtherMsg
Something.b
Something.c
Something.BAlias
Something.Bar
unknownValue
exposedElement
nonExposedElement
elementFromExposesEverything
VariantA
Foo.bar
Foo.Bar
Baz.foo
baz
button
Http.get
get
always
True
Just
""", """module ExposesSomeThings exposing (SomeOtherTypeAlias, exposedElement)
type NonExposedCustomType = Variant
type alias SomeOtherTypeAlias = {}
exposedElement = 1
nonExposedElement = 2
""", """module ExposesEverything exposing (..)
type SomeCustomType = VariantA | VariantB
type alias SomeTypeAlias = {}
type Msg = SomeMsgToBeShadowed | SomeOtherMsg
elementFromExposesEverything = 1
localValueValueToBeShadowed = 1
""", """module Something.B exposing (..)
b = 1
type Foo = Bar
type alias BAlias = {}
""", """module Something.C exposing (..)
c = 1
""" ]
2020-03-27 20:31:34 +03:00
|> Review.Test.runOnModulesWithProjectData project projectRule
|> Review.Test.expectErrorsForModules
[ ( "A"
, [ Review.Test.error
{ message = """
<nothing>.localValue -> <nothing>.localValue
<nothing>.localValueValueToBeShadowed -> <nothing>.localValueValueToBeShadowed
<nothing>.SomeMsgToBeShadowed -> <nothing>.SomeMsgToBeShadowed
<nothing>.SomeOtherMsg -> ExposesEverything.SomeOtherMsg
Something.b -> Something.B.b
Something.c -> Something.C.c
Something.BAlias -> Something.B.BAlias
Something.Bar -> Something.B.Bar
<nothing>.unknownValue -> <nothing>.unknownValue
<nothing>.exposedElement -> ExposesSomeThings.exposedElement
<nothing>.nonExposedElement -> <nothing>.nonExposedElement
<nothing>.elementFromExposesEverything -> ExposesEverything.elementFromExposesEverything
<nothing>.VariantA -> ExposesEverything.VariantA
Foo.bar -> Foo.bar
Foo.Bar -> Foo.Bar
Baz.foo -> Bar.foo
<nothing>.baz -> Bar.baz
<nothing>.button -> Html.button
Http.get -> Http.get
<nothing>.get -> Http.get
<nothing>.always -> Basics.always
<nothing>.True -> Basics.True
<nothing>.Just -> Maybe.Just"""
, details = [ "details" ]
, under = "module"
}
]
)
]
2020-01-12 20:01:49 +03:00
]
type alias ModuleContext =
{ scope : Scope.ModuleContext
2020-05-16 23:33:36 +03:00
, texts : List String
}
project : Project
project =
Project.new
|> Project.addDependency Dependencies.elmCore
|> Project.addDependency Dependencies.elmHtml
2020-03-27 20:31:34 +03:00
projectRule : Rule
projectRule =
Rule.newProjectRuleSchema "TestRule" { scope = Scope.initialProjectContext }
2020-03-19 21:28:01 +03:00
|> Scope.addProjectVisitors
|> Rule.withModuleVisitor moduleVisitor
|> Rule.withModuleContext
{ fromProjectToModule =
\_ _ projectContext ->
{ scope = Scope.fromProjectToModule projectContext.scope
2020-05-16 23:33:36 +03:00
, texts = []
}
, fromModuleToProject =
\_ moduleNameNode moduleContext ->
{ scope = Scope.fromModuleToProject moduleNameNode moduleContext.scope
}
, foldProjectContexts = \a b -> { scope = Scope.foldProjectContexts a.scope b.scope }
}
2020-01-19 22:37:19 +03:00
|> Rule.fromProjectRuleSchema
2020-03-27 20:31:34 +03:00
moduleRule : Rule
moduleRule =
2020-05-16 23:33:36 +03:00
Rule.newModuleRuleSchema "TestRule" { scope = Scope.initialModuleContext, texts = [] }
2020-03-27 20:31:34 +03:00
|> Scope.addModuleVisitors
|> moduleVisitor
|> Rule.fromModuleRuleSchema
moduleVisitor : Rule.ModuleRuleSchema schemaState ModuleContext -> Rule.ModuleRuleSchema { schemaState | hasAtLeastOneVisitor : () } ModuleContext
moduleVisitor schema =
schema
|> Rule.withExpressionVisitor expressionVisitor
|> Rule.withFinalModuleEvaluation finalEvaluation
expressionVisitor : Node Expression -> Rule.Direction -> ModuleContext -> ( List nothing, ModuleContext )
expressionVisitor node direction context =
case ( direction, Node.value node ) of
( Rule.OnEnter, Expression.FunctionOrValue moduleName name ) ->
let
nameInCode : String
nameInCode =
case moduleName of
[] ->
"<nothing>." ++ name
_ ->
String.join "." moduleName ++ "." ++ name
realName : String
realName =
2020-05-16 23:33:36 +03:00
case Scope.moduleNameForValue context.scope name moduleName of
[] ->
"<nothing>." ++ name
moduleName_ ->
String.join "." moduleName_ ++ "." ++ name
in
2020-05-16 23:33:36 +03:00
( [], { context | texts = context.texts ++ [ nameInCode ++ " -> " ++ realName ] } )
_ ->
( [], context )
finalEvaluation : ModuleContext -> List (Error {})
finalEvaluation context =
2020-05-16 23:33:36 +03:00
if List.isEmpty context.texts then
[]
else
[ Rule.error
{ message = "\n" ++ String.join "\n" context.texts
, details = [ "details" ]
}
{ start = { row = 1, column = 1 }
, end = { row = 1, column = 7 }
}
]