2020-05-16 23:33:36 +03:00
|
|
|
module ModuleNameForTypeTest exposing (all)
|
|
|
|
|
|
|
|
import Elm.Syntax.Declaration as Declaration exposing (Declaration)
|
|
|
|
import Elm.Syntax.Node as Node exposing (Node(..))
|
|
|
|
import Elm.Syntax.TypeAnnotation as TypeAnnotation exposing (TypeAnnotation)
|
|
|
|
import Fixtures.Dependencies as Dependencies
|
2020-08-22 10:54:20 +03:00
|
|
|
import Review.ModuleNameLookupTable as ModuleNameLookupTable exposing (ModuleNameLookupTable)
|
2020-05-16 23:33:36 +03:00
|
|
|
import Review.Project as Project exposing (Project)
|
|
|
|
import Review.Rule as Rule exposing (Error, Rule)
|
|
|
|
import Review.Test
|
|
|
|
import Test exposing (Test, test)
|
|
|
|
|
|
|
|
|
|
|
|
all : Test
|
|
|
|
all =
|
|
|
|
Test.describe "Scope.moduleNameForType"
|
|
|
|
[ test "should return the module that defined the type" <|
|
|
|
|
\() ->
|
2020-09-22 19:55:29 +03:00
|
|
|
let
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
|
|
|
createRule (Rule.withDeclarationEnterVisitor declarationVisitor)
|
|
|
|
in
|
2020-05-16 23:33:36 +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)
|
|
|
|
import Something.B as Something
|
|
|
|
|
|
|
|
type A = B | C
|
|
|
|
type Role = NormalUser Bool | Admin (Maybe A)
|
|
|
|
type alias User =
|
|
|
|
{ role : Role
|
|
|
|
, age : ( Msg, Unknown )
|
|
|
|
}
|
|
|
|
type alias GenericRecord generic = { generic | foo : A }
|
|
|
|
|
|
|
|
a : SomeCustomType -> SomeTypeAlias -> SomeOtherTypeAlias -> NonExposedCustomType
|
|
|
|
a = 1
|
|
|
|
""", """module ExposesSomeThings exposing (SomeOtherTypeAlias)
|
|
|
|
type NonExposedCustomType = Variant
|
|
|
|
type alias SomeOtherTypeAlias = {}
|
|
|
|
""", """module ExposesEverything exposing (..)
|
|
|
|
type SomeCustomType = VariantA | VariantB
|
|
|
|
type alias SomeTypeAlias = {}
|
|
|
|
type Msg = SomeMsgToBeShadowed | SomeOtherMsg
|
|
|
|
""", """module Something.B exposing (..)
|
|
|
|
b = 1
|
|
|
|
type Foo = Bar
|
|
|
|
type alias BAlias = {}
|
|
|
|
""" ]
|
2020-09-22 19:55:29 +03:00
|
|
|
|> Review.Test.runOnModulesWithProjectData project rule
|
2020-05-16 23:33:36 +03:00
|
|
|
|> Review.Test.expectErrorsForModules
|
|
|
|
[ ( "A"
|
|
|
|
, [ Review.Test.error
|
|
|
|
{ message = """
|
|
|
|
<nothing>.Bool -> Basics.Bool
|
|
|
|
<nothing>.Maybe -> Maybe.Maybe
|
|
|
|
<nothing>.A -> <nothing>.A
|
|
|
|
<nothing>.Role -> <nothing>.Role
|
|
|
|
<nothing>.Msg -> ExposesEverything.Msg
|
|
|
|
<nothing>.Unknown -> <nothing>.Unknown
|
|
|
|
<nothing>.A -> <nothing>.A
|
|
|
|
<nothing>.SomeCustomType -> ExposesEverything.SomeCustomType
|
|
|
|
<nothing>.SomeTypeAlias -> ExposesEverything.SomeTypeAlias
|
|
|
|
<nothing>.SomeOtherTypeAlias -> ExposesSomeThings.SomeOtherTypeAlias
|
|
|
|
<nothing>.NonExposedCustomType -> <nothing>.NonExposedCustomType
|
|
|
|
"""
|
|
|
|
, details = [ "details" ]
|
|
|
|
, under = "module"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
type alias ModuleContext =
|
2020-08-22 10:54:20 +03:00
|
|
|
{ lookupTable : ModuleNameLookupTable
|
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-09-22 19:55:29 +03:00
|
|
|
createRule : (Rule.ModuleRuleSchema {} ModuleContext -> Rule.ModuleRuleSchema { hasAtLeastOneVisitor : () } ModuleContext) -> Rule
|
|
|
|
createRule visitor =
|
2020-08-22 10:54:20 +03:00
|
|
|
Rule.newModuleRuleSchemaUsingContextCreator "TestRule" contextCreator
|
2020-09-22 19:55:29 +03:00
|
|
|
|> visitor
|
2020-08-22 10:54:20 +03:00
|
|
|
|> Rule.withFinalModuleEvaluation finalEvaluation
|
|
|
|
|> Rule.fromModuleRuleSchema
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
|
2020-08-22 10:54:20 +03:00
|
|
|
contextCreator : Rule.ContextCreator () ModuleContext
|
|
|
|
contextCreator =
|
|
|
|
Rule.initContextCreator
|
|
|
|
(\lookupTable () ->
|
|
|
|
{ lookupTable = lookupTable
|
|
|
|
, texts = []
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|> Rule.withModuleNameLookupTable
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
|
2020-08-22 10:54:20 +03:00
|
|
|
declarationVisitor : Node Declaration -> ModuleContext -> ( List nothing, ModuleContext )
|
|
|
|
declarationVisitor node context =
|
|
|
|
case Node.value node of
|
|
|
|
Declaration.CustomTypeDeclaration { constructors } ->
|
2020-05-16 23:33:36 +03:00
|
|
|
let
|
|
|
|
types : List String
|
|
|
|
types =
|
|
|
|
constructors
|
|
|
|
|> List.concatMap (Node.value >> .arguments)
|
2020-08-22 10:54:20 +03:00
|
|
|
|> List.concatMap (typeAnnotationNames context)
|
2020-05-16 23:33:36 +03:00
|
|
|
in
|
|
|
|
( [], { context | texts = context.texts ++ types } )
|
|
|
|
|
2020-08-22 10:54:20 +03:00
|
|
|
Declaration.AliasDeclaration { typeAnnotation } ->
|
|
|
|
( [], { context | texts = context.texts ++ typeAnnotationNames context typeAnnotation } )
|
2020-05-16 23:33:36 +03:00
|
|
|
|
2020-08-22 10:54:20 +03:00
|
|
|
Declaration.FunctionDeclaration function ->
|
2020-05-16 23:33:36 +03:00
|
|
|
case function.signature |> Maybe.map (Node.value >> .typeAnnotation) of
|
|
|
|
Nothing ->
|
|
|
|
( [], context )
|
|
|
|
|
|
|
|
Just typeAnnotation ->
|
2020-08-22 10:54:20 +03:00
|
|
|
( [], { context | texts = context.texts ++ typeAnnotationNames context typeAnnotation } )
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
( [], context )
|
|
|
|
|
|
|
|
|
2020-08-22 10:54:20 +03:00
|
|
|
typeAnnotationNames : ModuleContext -> Node TypeAnnotation -> List String
|
|
|
|
typeAnnotationNames moduleContext typeAnnotation =
|
2020-05-16 23:33:36 +03:00
|
|
|
case Node.value typeAnnotation of
|
|
|
|
TypeAnnotation.GenericType name ->
|
|
|
|
[ "<nothing>." ++ name ++ " -> <generic>" ]
|
|
|
|
|
2020-08-22 10:54:20 +03:00
|
|
|
TypeAnnotation.Typed (Node typeRange ( moduleName, typeName )) typeParameters ->
|
2020-05-16 23:33:36 +03:00
|
|
|
let
|
|
|
|
nameInCode : String
|
|
|
|
nameInCode =
|
|
|
|
case moduleName of
|
|
|
|
[] ->
|
|
|
|
"<nothing>." ++ typeName
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
String.join "." moduleName ++ "." ++ typeName
|
|
|
|
|
|
|
|
realName : String
|
|
|
|
realName =
|
2020-08-22 10:54:20 +03:00
|
|
|
case ModuleNameLookupTable.moduleNameAt moduleContext.lookupTable typeRange of
|
|
|
|
Just [] ->
|
2020-05-16 23:33:36 +03:00
|
|
|
"<nothing>." ++ typeName
|
|
|
|
|
2020-08-22 10:54:20 +03:00
|
|
|
Just moduleName_ ->
|
2020-05-16 23:33:36 +03:00
|
|
|
String.join "." moduleName_ ++ "." ++ typeName
|
2020-08-22 10:54:20 +03:00
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
"!!! UNKNOWN !!!"
|
2020-05-16 23:33:36 +03:00
|
|
|
in
|
|
|
|
(nameInCode ++ " -> " ++ realName)
|
2020-08-22 10:54:20 +03:00
|
|
|
:: List.concatMap (typeAnnotationNames moduleContext) typeParameters
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
TypeAnnotation.Unit ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
TypeAnnotation.Tupled typeAnnotations ->
|
2020-08-22 10:54:20 +03:00
|
|
|
List.concatMap (typeAnnotationNames moduleContext) typeAnnotations
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
TypeAnnotation.Record typeAnnotations ->
|
2020-08-22 10:54:20 +03:00
|
|
|
List.concatMap (Node.value >> Tuple.second >> typeAnnotationNames moduleContext) typeAnnotations
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
TypeAnnotation.GenericRecord _ typeAnnotations ->
|
2020-08-22 10:54:20 +03:00
|
|
|
List.concatMap (Node.value >> Tuple.second >> typeAnnotationNames moduleContext) (Node.value typeAnnotations)
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
TypeAnnotation.FunctionTypeAnnotation arg returnType ->
|
2020-08-22 10:54:20 +03:00
|
|
|
typeAnnotationNames moduleContext arg ++ typeAnnotationNames moduleContext returnType
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
finalEvaluation : ModuleContext -> List (Error {})
|
|
|
|
finalEvaluation context =
|
|
|
|
if List.isEmpty context.texts then
|
|
|
|
[]
|
|
|
|
|
|
|
|
else
|
|
|
|
[ Rule.error
|
|
|
|
{ message = "\n" ++ String.join "\n" context.texts ++ "\n"
|
|
|
|
, details = [ "details" ]
|
|
|
|
}
|
|
|
|
{ start = { row = 1, column = 1 }
|
|
|
|
, end = { row = 1, column = 7 }
|
|
|
|
}
|
|
|
|
]
|