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)
|
2020-01-13 18:29:20 +03:00
|
|
|
import Elm.Syntax.Node as Node exposing (Node(..))
|
2020-04-03 17:06:44 +03:00
|
|
|
import Fixtures.Dependencies as Dependencies
|
2020-08-22 11:02:13 +03:00
|
|
|
import Review.ModuleNameLookupTable as ModuleNameLookupTable exposing (ModuleNameLookupTable)
|
2020-01-12 20:01:49 +03:00
|
|
|
import Review.Project as Project exposing (Project)
|
2020-03-25 20:02:37 +03:00
|
|
|
import Review.Rule as Rule exposing (Error, Rule)
|
2020-01-15 11:54:26 +03:00
|
|
|
import Review.Test
|
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"
|
|
|
|
[ test "should return the module that defined the value" <|
|
2020-01-13 01:43:21 +03:00
|
|
|
\() ->
|
|
|
|
[ """module A exposing (..)
|
|
|
|
import Bar as Baz exposing (baz)
|
2020-01-13 13:03:52 +03:00
|
|
|
import ExposesSomeThings exposing (..)
|
|
|
|
import ExposesEverything exposing (..)
|
2020-01-13 01:43:21 +03:00
|
|
|
import Foo.Bar
|
|
|
|
import Html exposing (..)
|
|
|
|
import Http exposing (get)
|
2020-04-22 00:01:47 +03:00
|
|
|
import Something.B as Something
|
|
|
|
import Something.C as Something
|
2020-01-13 01:43:21 +03:00
|
|
|
|
2020-01-13 18:29:20 +03:00
|
|
|
localValue = 1
|
2020-04-22 00:01:47 +03:00
|
|
|
localValueValueToBeShadowed = 1
|
|
|
|
type Msg = SomeMsgToBeShadowed
|
2020-01-13 18:29:20 +03:00
|
|
|
|
|
|
|
a = localValue
|
2020-04-22 00:01:47 +03:00
|
|
|
localValueValueToBeShadowed
|
|
|
|
SomeMsgToBeShadowed
|
|
|
|
SomeOtherMsg
|
|
|
|
Something.b
|
|
|
|
Something.c
|
|
|
|
Something.BAlias
|
|
|
|
Something.Bar
|
2020-01-13 18:29:20 +03:00
|
|
|
unknownValue
|
2020-01-13 13:03:52 +03:00
|
|
|
exposedElement
|
|
|
|
nonExposedElement
|
|
|
|
elementFromExposesEverything
|
2020-01-13 18:46:24 +03:00
|
|
|
VariantA
|
2020-01-13 01:43:21 +03:00
|
|
|
Foo.bar
|
|
|
|
Foo.Bar
|
|
|
|
Baz.foo
|
|
|
|
baz
|
2020-08-25 16:28:56 +03:00
|
|
|
{ baz | a = 1 }
|
2020-01-13 01:43:21 +03:00
|
|
|
button
|
|
|
|
Http.get
|
|
|
|
get
|
|
|
|
always
|
2020-01-13 18:46:24 +03:00
|
|
|
True
|
2020-01-13 01:43:21 +03:00
|
|
|
Just
|
2020-01-13 18:29:20 +03:00
|
|
|
""", """module ExposesSomeThings exposing (SomeOtherTypeAlias, exposedElement)
|
|
|
|
type NonExposedCustomType = Variant
|
|
|
|
type alias SomeOtherTypeAlias = {}
|
2020-01-13 13:03:52 +03:00
|
|
|
exposedElement = 1
|
|
|
|
nonExposedElement = 2
|
|
|
|
""", """module ExposesEverything exposing (..)
|
2020-01-13 18:29:20 +03:00
|
|
|
type SomeCustomType = VariantA | VariantB
|
|
|
|
type alias SomeTypeAlias = {}
|
2020-04-22 00:01:47 +03:00
|
|
|
type Msg = SomeMsgToBeShadowed | SomeOtherMsg
|
2020-01-13 13:03:52 +03:00
|
|
|
elementFromExposesEverything = 1
|
2020-04-22 00:01:47 +03:00
|
|
|
localValueValueToBeShadowed = 1
|
|
|
|
""", """module Something.B exposing (..)
|
|
|
|
b = 1
|
|
|
|
type Foo = Bar
|
|
|
|
type alias BAlias = {}
|
|
|
|
""", """module Something.C exposing (..)
|
|
|
|
c = 1
|
2020-01-13 01:43:21 +03:00
|
|
|
""" ]
|
2020-03-27 20:31:34 +03:00
|
|
|
|> Review.Test.runOnModulesWithProjectData project projectRule
|
2020-01-13 01:43:21 +03:00
|
|
|
|> Review.Test.expectErrorsForModules
|
|
|
|
[ ( "A"
|
|
|
|
, [ Review.Test.error
|
|
|
|
{ message = """
|
2020-01-13 18:29:20 +03:00
|
|
|
<nothing>.localValue -> <nothing>.localValue
|
2020-04-22 00:01:47 +03:00
|
|
|
<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
|
2020-01-13 18:29:20 +03:00
|
|
|
<nothing>.unknownValue -> <nothing>.unknownValue
|
2020-01-13 13:03:52 +03:00
|
|
|
<nothing>.exposedElement -> ExposesSomeThings.exposedElement
|
|
|
|
<nothing>.nonExposedElement -> <nothing>.nonExposedElement
|
|
|
|
<nothing>.elementFromExposesEverything -> ExposesEverything.elementFromExposesEverything
|
2020-01-13 18:46:24 +03:00
|
|
|
<nothing>.VariantA -> ExposesEverything.VariantA
|
2020-01-13 01:43:21 +03:00
|
|
|
Foo.bar -> Foo.bar
|
|
|
|
Foo.Bar -> Foo.Bar
|
|
|
|
Baz.foo -> Bar.foo
|
|
|
|
<nothing>.baz -> Bar.baz
|
2020-08-25 16:28:56 +03:00
|
|
|
<nothing>.baz -> Bar.baz
|
2020-01-13 01:43:21 +03:00
|
|
|
<nothing>.button -> Html.button
|
|
|
|
Http.get -> Http.get
|
|
|
|
<nothing>.get -> Http.get
|
|
|
|
<nothing>.always -> Basics.always
|
2020-01-13 18:46:24 +03:00
|
|
|
<nothing>.True -> Basics.True
|
2020-01-13 01:43:21 +03:00
|
|
|
<nothing>.Just -> Maybe.Just"""
|
|
|
|
, details = [ "details" ]
|
|
|
|
, under = "module"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
2020-01-12 20:01:49 +03:00
|
|
|
]
|
2020-01-13 01:43:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
type alias ModuleContext =
|
2020-08-22 11:02:13 +03:00
|
|
|
{ lookupTable : ModuleNameLookupTable
|
2020-05-16 23:33:36 +03:00
|
|
|
, texts : List String
|
2020-01-13 01:43:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
project : Project
|
|
|
|
project =
|
|
|
|
Project.new
|
2020-03-20 01:50:41 +03:00
|
|
|
|> Project.addDependency Dependencies.elmCore
|
|
|
|
|> Project.addDependency Dependencies.elmHtml
|
2020-01-13 01:43:21 +03:00
|
|
|
|
|
|
|
|
2020-03-27 20:31:34 +03:00
|
|
|
projectRule : Rule
|
|
|
|
projectRule =
|
2020-08-22 11:02:13 +03:00
|
|
|
Rule.newModuleRuleSchemaUsingContextCreator "TestRule" contextCreator
|
|
|
|
|> Rule.withExpressionEnterVisitor expressionVisitor
|
|
|
|
|> Rule.withFinalModuleEvaluation finalEvaluation
|
2020-03-27 20:31:34 +03:00
|
|
|
|> Rule.fromModuleRuleSchema
|
|
|
|
|
|
|
|
|
2020-08-22 11:02:13 +03:00
|
|
|
contextCreator : Rule.ContextCreator () ModuleContext
|
|
|
|
contextCreator =
|
|
|
|
Rule.initContextCreator
|
|
|
|
(\lookupTable () ->
|
|
|
|
{ lookupTable = lookupTable
|
|
|
|
, texts = []
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|> Rule.withModuleNameLookupTable
|
2020-03-19 21:21:49 +03:00
|
|
|
|
|
|
|
|
2020-08-22 11:02:13 +03:00
|
|
|
expressionVisitor : Node Expression -> ModuleContext -> ( List nothing, ModuleContext )
|
|
|
|
expressionVisitor node context =
|
|
|
|
case Node.value node of
|
|
|
|
Expression.FunctionOrValue moduleName name ->
|
2020-01-13 01:43:21 +03:00
|
|
|
let
|
|
|
|
nameInCode : String
|
|
|
|
nameInCode =
|
|
|
|
case moduleName of
|
|
|
|
[] ->
|
|
|
|
"<nothing>." ++ name
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
String.join "." moduleName ++ "." ++ name
|
|
|
|
|
|
|
|
realName : String
|
|
|
|
realName =
|
2020-08-22 11:02:13 +03:00
|
|
|
case ModuleNameLookupTable.moduleNameFor context.lookupTable node of
|
|
|
|
Just [] ->
|
2020-03-27 20:51:42 +03:00
|
|
|
"<nothing>." ++ name
|
2020-01-13 01:43:21 +03:00
|
|
|
|
2020-08-22 11:02:13 +03:00
|
|
|
Just moduleName_ ->
|
2020-03-27 20:51:42 +03:00
|
|
|
String.join "." moduleName_ ++ "." ++ name
|
2020-08-22 11:02:13 +03:00
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
"!!! UNKNOWN !!!"
|
2020-01-13 01:43:21 +03:00
|
|
|
in
|
2020-05-16 23:33:36 +03:00
|
|
|
( [], { context | texts = context.texts ++ [ nameInCode ++ " -> " ++ realName ] } )
|
2020-01-13 01:43:21 +03:00
|
|
|
|
2020-08-25 16:28:56 +03:00
|
|
|
Expression.RecordUpdateExpression (Node range name) _ ->
|
|
|
|
let
|
|
|
|
realName : String
|
|
|
|
realName =
|
|
|
|
case ModuleNameLookupTable.moduleNameAt context.lookupTable range of
|
|
|
|
Just [] ->
|
|
|
|
"<nothing>." ++ name
|
|
|
|
|
|
|
|
Just moduleName_ ->
|
|
|
|
String.join "." moduleName_ ++ "." ++ name
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
"!!! UNKNOWN !!!"
|
|
|
|
in
|
|
|
|
( [], { context | texts = context.texts ++ [ "<nothing>." ++ name ++ " -> " ++ realName ] } )
|
|
|
|
|
2020-01-13 01:43:21 +03:00
|
|
|
_ ->
|
|
|
|
( [], context )
|
|
|
|
|
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
finalEvaluation : ModuleContext -> List (Error {})
|
2020-01-13 01:43:21 +03:00
|
|
|
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 }
|
|
|
|
}
|
|
|
|
]
|