2020-09-22 20:01:02 +03:00
|
|
|
module ModuleNameLookupTableTest exposing (all)
|
2020-05-16 23:33:36 +03:00
|
|
|
|
2022-08-23 18:09:29 +03:00
|
|
|
import Elm.Docs
|
|
|
|
import Elm.License
|
|
|
|
import Elm.Package
|
|
|
|
import Elm.Project
|
2020-05-16 23:33:36 +03:00
|
|
|
import Elm.Syntax.Declaration as Declaration exposing (Declaration)
|
2020-09-22 19:59:34 +03:00
|
|
|
import Elm.Syntax.Expression as Expression exposing (Expression)
|
2020-09-22 20:04:24 +03:00
|
|
|
import Elm.Syntax.ModuleName exposing (ModuleName)
|
2020-05-16 23:33:36 +03:00
|
|
|
import Elm.Syntax.Node as Node exposing (Node(..))
|
2020-09-23 10:01:00 +03:00
|
|
|
import Elm.Syntax.Pattern as Pattern
|
2020-09-22 20:02:48 +03:00
|
|
|
import Elm.Syntax.Range exposing (Range)
|
2020-05-16 23:33:36 +03:00
|
|
|
import Elm.Syntax.TypeAnnotation as TypeAnnotation exposing (TypeAnnotation)
|
2022-08-23 18:09:29 +03:00
|
|
|
import Elm.Version
|
2020-05-16 23:33:36 +03:00
|
|
|
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)
|
2022-08-23 18:09:29 +03:00
|
|
|
import Review.Project.Dependency as Dependency exposing (Dependency)
|
2022-06-21 18:35:20 +03:00
|
|
|
import Review.Rule as Rule exposing (Rule)
|
2020-05-16 23:33:36 +03:00
|
|
|
import Review.Test
|
2021-10-03 12:03:00 +03:00
|
|
|
import Review.Test.Dependencies
|
2022-08-23 18:09:29 +03:00
|
|
|
import Review.Test.Dependencies.Unsafe as Unsafe
|
2022-05-01 18:02:55 +03:00
|
|
|
import Test exposing (Test, describe, test)
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
all : Test
|
|
|
|
all =
|
2022-05-01 18:02:55 +03:00
|
|
|
describe "ModuleNameLookupTable"
|
|
|
|
[ moduleNameAtTest
|
|
|
|
, fullModuleNameAtest
|
2022-08-23 18:09:29 +03:00
|
|
|
, dependenciesTest
|
2022-05-01 18:02:55 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
moduleNameAtTest : Test
|
|
|
|
moduleNameAtTest =
|
|
|
|
describe "ModuleNameLookupTable.moduleNameAt"
|
2020-09-22 19:59:34 +03:00
|
|
|
[ test "should return the module that defined the value" <|
|
|
|
|
\() ->
|
|
|
|
let
|
2022-05-01 18:02:55 +03:00
|
|
|
lookupFunction : ModuleNameLookupTable -> Range -> Maybe ModuleName
|
|
|
|
lookupFunction =
|
|
|
|
ModuleNameLookupTable.moduleNameAt
|
|
|
|
|
2020-09-22 19:59:34 +03:00
|
|
|
rule : Rule
|
|
|
|
rule =
|
2020-09-23 11:25:40 +03:00
|
|
|
createRule
|
2022-05-01 18:02:55 +03:00
|
|
|
(Rule.withExpressionEnterVisitor (expressionVisitor lookupFunction)
|
|
|
|
>> Rule.withDeclarationEnterVisitor (declarationVisitor lookupFunction)
|
2020-09-23 11:25:40 +03:00
|
|
|
)
|
2020-09-22 19:59:34 +03:00
|
|
|
in
|
|
|
|
[ """module A exposing (..)
|
|
|
|
import Bar as Baz exposing (baz)
|
|
|
|
import ExposesSomeThings exposing (..)
|
|
|
|
import ExposesEverything exposing (..)
|
2020-12-06 16:30:39 +03:00
|
|
|
import ExposesEverything as ExposesEverythingAlias
|
2020-09-22 19:59:34 +03:00
|
|
|
import Foo.Bar
|
|
|
|
import Html exposing (..)
|
|
|
|
import Http exposing (get)
|
|
|
|
import Something.B as Something
|
|
|
|
import Something.C as Something
|
2021-10-03 12:03:00 +03:00
|
|
|
import Url.Parser exposing (..)
|
|
|
|
-- NOTE: The behavior of the compiler if duplicate infix operators are imported is for the second-imported one to overwrite the first.
|
|
|
|
import Parser exposing ((|=))
|
|
|
|
import Parser.Advanced exposing ((|=))
|
2020-09-22 19:59:34 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
{ baz | a = 1 }
|
|
|
|
button
|
|
|
|
Http.get
|
|
|
|
get
|
|
|
|
always
|
|
|
|
True
|
|
|
|
Just
|
2020-11-18 12:07:16 +03:00
|
|
|
Cmd.none
|
2023-06-17 20:47:43 +03:00
|
|
|
(let foo get = get
|
|
|
|
in
|
|
|
|
get
|
|
|
|
)
|
2021-10-03 12:03:00 +03:00
|
|
|
(+)
|
|
|
|
(117 + 3)
|
|
|
|
(<?>)
|
|
|
|
("x" </> "y")
|
|
|
|
(|=)
|
|
|
|
("pars" |= "er")
|
2024-04-09 12:03:35 +03:00
|
|
|
(let (exposedElement, _) = 1 in exposedElement)
|
2020-09-23 10:01:00 +03:00
|
|
|
b = case () of
|
|
|
|
VariantA -> ()
|
2020-09-23 11:21:35 +03:00
|
|
|
(ExposesEverything.VariantA as foo) -> foo
|
2020-12-06 16:30:39 +03:00
|
|
|
ExposesEverythingAlias.VariantA -> ()
|
2020-09-23 11:27:10 +03:00
|
|
|
|
|
|
|
someFunction Something.B.Bar =
|
2020-09-23 11:38:39 +03:00
|
|
|
let Something.B.Bar = ()
|
2020-09-23 11:27:10 +03:00
|
|
|
in ()
|
2020-09-22 19:59:34 +03:00
|
|
|
""", """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
|
|
|
|
""" ]
|
|
|
|
|> Review.Test.runOnModulesWithProjectData project rule
|
|
|
|
|> 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>.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
|
2020-11-18 12:07:16 +03:00
|
|
|
Cmd.none -> Platform.Cmd.none
|
2023-06-17 20:47:43 +03:00
|
|
|
<nothing>.get -> <nothing>.get
|
|
|
|
<nothing>.get -> Http.get
|
2021-10-03 12:03:00 +03:00
|
|
|
<nothing>.+ -> Basics.+
|
|
|
|
<nothing>.+ -> Basics.+
|
|
|
|
<nothing>.<?> -> Url.Parser.<?>
|
|
|
|
<nothing>.</> -> Url.Parser.</>
|
|
|
|
<nothing>.|= -> Parser.Advanced.|=
|
|
|
|
<nothing>.|= -> Parser.Advanced.|=
|
2024-04-09 12:03:35 +03:00
|
|
|
<nothing>.exposedElement -> <nothing>.exposedElement
|
2020-09-23 10:01:00 +03:00
|
|
|
<nothing>.VariantA -> ExposesEverything.VariantA
|
|
|
|
ExposesEverything.VariantA -> ExposesEverything.VariantA
|
2020-12-06 16:30:39 +03:00
|
|
|
ExposesEverythingAlias.VariantA -> ExposesEverything.VariantA
|
2020-09-23 11:21:35 +03:00
|
|
|
<nothing>.foo -> <nothing>.foo
|
2020-09-23 11:29:02 +03:00
|
|
|
Something.B.Bar -> Something.B.Bar
|
2020-09-23 11:38:39 +03:00
|
|
|
Something.B.Bar -> Something.B.Bar
|
2020-09-22 19:59:34 +03:00
|
|
|
"""
|
|
|
|
, details = [ "details" ]
|
|
|
|
, under = "module"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
, test "should return the module that defined the type" <|
|
2020-05-16 23:33:36 +03:00
|
|
|
\() ->
|
2020-09-22 19:55:29 +03:00
|
|
|
let
|
2022-05-01 18:02:55 +03:00
|
|
|
lookupFunction : ModuleNameLookupTable -> Range -> Maybe ModuleName
|
|
|
|
lookupFunction =
|
|
|
|
ModuleNameLookupTable.moduleNameAt
|
|
|
|
|
2020-09-22 19:55:29 +03:00
|
|
|
rule : Rule
|
|
|
|
rule =
|
2022-05-01 18:02:55 +03:00
|
|
|
createRule
|
|
|
|
(Rule.withDeclarationEnterVisitor (declarationVisitor lookupFunction))
|
2020-09-22 19:55:29 +03:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-05-01 18:02:55 +03:00
|
|
|
fullModuleNameAtest : Test
|
|
|
|
fullModuleNameAtest =
|
|
|
|
describe "ModuleNameLookupTable.fullModuleNameAt"
|
|
|
|
[ test "should return the module that defined the value" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
lookupFunction : ModuleNameLookupTable -> Range -> Maybe ModuleName
|
|
|
|
lookupFunction =
|
|
|
|
ModuleNameLookupTable.fullModuleNameAt
|
|
|
|
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
|
|
|
createRule
|
|
|
|
(Rule.withExpressionEnterVisitor (expressionVisitor lookupFunction)
|
|
|
|
>> Rule.withDeclarationEnterVisitor (declarationVisitor lookupFunction)
|
|
|
|
)
|
|
|
|
in
|
|
|
|
[ """module Abc.Xyz exposing (..)
|
|
|
|
import Bar as Baz exposing (baz)
|
|
|
|
import ExposesSomeThings exposing (..)
|
|
|
|
import ExposesEverything exposing (..)
|
|
|
|
import ExposesEverything as ExposesEverythingAlias
|
|
|
|
import Foo.Bar
|
|
|
|
import Html exposing (..)
|
|
|
|
import Http exposing (get)
|
|
|
|
import Something.B as Something
|
|
|
|
import Something.C as Something
|
|
|
|
import Url.Parser exposing (..)
|
|
|
|
-- NOTE: The behavior of the compiler if duplicate infix operators are imported is for the second-imported one to overwrite the first.
|
|
|
|
import Parser exposing ((|=))
|
|
|
|
import Parser.Advanced exposing ((|=))
|
|
|
|
|
|
|
|
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
|
|
|
|
{ baz | a = 1 }
|
|
|
|
button
|
|
|
|
Http.get
|
|
|
|
get
|
|
|
|
always
|
|
|
|
True
|
|
|
|
Just
|
|
|
|
Cmd.none
|
|
|
|
(+)
|
|
|
|
(117 + 3)
|
|
|
|
(<?>)
|
|
|
|
("x" </> "y")
|
|
|
|
(|=)
|
|
|
|
("pars" |= "er")
|
2024-04-09 12:03:35 +03:00
|
|
|
(let (exposedElement, _) = 1 in exposedElement)
|
2022-05-01 18:02:55 +03:00
|
|
|
b = case () of
|
|
|
|
VariantA -> ()
|
|
|
|
(ExposesEverything.VariantA as foo) -> foo
|
|
|
|
ExposesEverythingAlias.VariantA -> ()
|
|
|
|
|
|
|
|
someFunction Something.B.Bar =
|
|
|
|
let Something.B.Bar = ()
|
|
|
|
in ()
|
|
|
|
""", """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
|
|
|
|
""" ]
|
|
|
|
|> Review.Test.runOnModulesWithProjectData project rule
|
|
|
|
|> Review.Test.expectErrorsForModules
|
|
|
|
[ ( "Abc.Xyz"
|
|
|
|
, [ Review.Test.error
|
|
|
|
{ message = """
|
|
|
|
<nothing>.localValue -> Abc.Xyz.localValue
|
|
|
|
<nothing>.localValueValueToBeShadowed -> Abc.Xyz.localValueValueToBeShadowed
|
|
|
|
<nothing>.SomeMsgToBeShadowed -> Abc.Xyz.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 -> Abc.Xyz.unknownValue
|
|
|
|
<nothing>.exposedElement -> ExposesSomeThings.exposedElement
|
|
|
|
<nothing>.nonExposedElement -> Abc.Xyz.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>.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
|
|
|
|
Cmd.none -> Platform.Cmd.none
|
|
|
|
<nothing>.+ -> Basics.+
|
|
|
|
<nothing>.+ -> Basics.+
|
|
|
|
<nothing>.<?> -> Url.Parser.<?>
|
|
|
|
<nothing>.</> -> Url.Parser.</>
|
|
|
|
<nothing>.|= -> Parser.Advanced.|=
|
|
|
|
<nothing>.|= -> Parser.Advanced.|=
|
2024-04-09 12:03:35 +03:00
|
|
|
<nothing>.exposedElement -> Abc.Xyz.exposedElement
|
2022-05-01 18:02:55 +03:00
|
|
|
<nothing>.VariantA -> ExposesEverything.VariantA
|
|
|
|
ExposesEverything.VariantA -> ExposesEverything.VariantA
|
|
|
|
ExposesEverythingAlias.VariantA -> ExposesEverything.VariantA
|
|
|
|
<nothing>.foo -> Abc.Xyz.foo
|
|
|
|
Something.B.Bar -> Something.B.Bar
|
|
|
|
Something.B.Bar -> Something.B.Bar
|
|
|
|
"""
|
|
|
|
, details = [ "details" ]
|
|
|
|
, under = "module"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
, test "should return the module that defined the type" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
lookupFunction : ModuleNameLookupTable -> Range -> Maybe ModuleName
|
|
|
|
lookupFunction =
|
|
|
|
ModuleNameLookupTable.fullModuleNameAt
|
|
|
|
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
|
|
|
createRule
|
|
|
|
(Rule.withDeclarationEnterVisitor (declarationVisitor lookupFunction))
|
|
|
|
in
|
|
|
|
[ """module Abc.Xyz 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 = {}
|
|
|
|
""" ]
|
|
|
|
|> Review.Test.runOnModulesWithProjectData project rule
|
|
|
|
|> Review.Test.expectErrorsForModules
|
|
|
|
[ ( "Abc.Xyz"
|
|
|
|
, [ Review.Test.error
|
|
|
|
{ message = """
|
|
|
|
<nothing>.Bool -> Basics.Bool
|
|
|
|
<nothing>.Maybe -> Maybe.Maybe
|
|
|
|
<nothing>.A -> Abc.Xyz.A
|
|
|
|
<nothing>.Role -> Abc.Xyz.Role
|
|
|
|
<nothing>.Msg -> ExposesEverything.Msg
|
|
|
|
<nothing>.Unknown -> Abc.Xyz.Unknown
|
|
|
|
<nothing>.A -> Abc.Xyz.A
|
|
|
|
<nothing>.SomeCustomType -> ExposesEverything.SomeCustomType
|
|
|
|
<nothing>.SomeTypeAlias -> ExposesEverything.SomeTypeAlias
|
|
|
|
<nothing>.SomeOtherTypeAlias -> ExposesSomeThings.SomeOtherTypeAlias
|
|
|
|
<nothing>.NonExposedCustomType -> Abc.Xyz.NonExposedCustomType
|
|
|
|
"""
|
|
|
|
, details = [ "details" ]
|
|
|
|
, under = "module"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-08-23 18:09:29 +03:00
|
|
|
dependenciesTest : Test
|
|
|
|
dependenciesTest =
|
|
|
|
describe "Dependencies"
|
|
|
|
[ test "should not confuse a function from a local module with a module from an indirect dependency" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
lookupFunction : ModuleNameLookupTable -> Range -> Maybe ModuleName
|
|
|
|
lookupFunction =
|
|
|
|
ModuleNameLookupTable.moduleNameAt
|
|
|
|
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
|
|
|
createRule
|
|
|
|
(Rule.withExpressionEnterVisitor (expressionVisitor lookupFunction))
|
|
|
|
in
|
|
|
|
[ """module A exposing (..)
|
|
|
|
import Element exposing (..)
|
|
|
|
|
|
|
|
a = value
|
|
|
|
""", """module Element exposing (value)
|
|
|
|
value = 1
|
|
|
|
""" ]
|
|
|
|
|> Review.Test.runOnModulesWithProjectData project rule
|
|
|
|
|> Review.Test.expectErrorsForModules
|
|
|
|
[ ( "A"
|
|
|
|
, [ Review.Test.error
|
|
|
|
{ message = """
|
|
|
|
<nothing>.value -> Element.value
|
|
|
|
"""
|
|
|
|
, details = [ "details" ]
|
|
|
|
, under = "module"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-05-16 23:33:36 +03:00
|
|
|
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
|
2022-08-23 18:09:29 +03:00
|
|
|
|> Project.addElmJson applicationElmJson
|
2020-05-16 23:33:36 +03:00
|
|
|
|> Project.addDependency Dependencies.elmCore
|
|
|
|
|> Project.addDependency Dependencies.elmHtml
|
2021-10-03 12:03:00 +03:00
|
|
|
|> Project.addDependency Review.Test.Dependencies.elmParser
|
|
|
|
|> Project.addDependency Review.Test.Dependencies.elmUrl
|
2022-08-23 18:09:29 +03:00
|
|
|
|> Project.addDependency xyz2dependency
|
|
|
|
|
|
|
|
|
|
|
|
applicationElmJson : { path : String, raw : String, project : Elm.Project.Project }
|
|
|
|
applicationElmJson =
|
|
|
|
{ path = "elm.json"
|
|
|
|
, raw = """{
|
|
|
|
"type": "application",
|
|
|
|
"source-directories": [
|
|
|
|
"src"
|
|
|
|
],
|
|
|
|
"elm-version": "0.19.1",
|
|
|
|
"dependencies": {
|
|
|
|
"direct": {
|
|
|
|
"elm/core": "1.0.0",
|
|
|
|
"elm/html": "1.0.0",
|
|
|
|
"elm/parser": "1.0.0",
|
|
|
|
"elm/url": "1.0.0",
|
|
|
|
"abc/xyz": "1.0.0"
|
|
|
|
},
|
|
|
|
"indirect": {
|
|
|
|
"abc/xyz2": "1.0.0"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"test-dependencies": {
|
|
|
|
"direct": {},
|
|
|
|
"indirect": {}
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
, project =
|
|
|
|
Elm.Project.Application
|
|
|
|
{ elm = Elm.Version.one
|
|
|
|
, dirs = []
|
|
|
|
, depsDirect =
|
|
|
|
[ ( unsafePackageName "elm/core", Elm.Version.one )
|
|
|
|
, ( unsafePackageName "elm/html", Elm.Version.one )
|
|
|
|
, ( unsafePackageName "elm/parser", Elm.Version.one )
|
|
|
|
, ( unsafePackageName "elm/url", Elm.Version.one )
|
|
|
|
, ( unsafePackageName "abc/xyz", Elm.Version.one )
|
|
|
|
]
|
|
|
|
, depsIndirect = [ ( unsafePackageName "abc/xyz2", Elm.Version.one ) ]
|
|
|
|
, testDepsDirect = []
|
|
|
|
, testDepsIndirect = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xyz2dependency : Dependency
|
|
|
|
xyz2dependency =
|
|
|
|
Dependency.create "abc/xyz2"
|
|
|
|
xyz2elmJson
|
|
|
|
xyz2dependencyModules
|
|
|
|
|
|
|
|
|
|
|
|
xyz2elmJson : Elm.Project.Project
|
|
|
|
xyz2elmJson =
|
|
|
|
Elm.Project.Package
|
|
|
|
{ elm = Unsafe.constraint "0.19.0 <= v < 0.20.0"
|
|
|
|
, exposed = Elm.Project.ExposedList [ Unsafe.moduleName "Element" ]
|
|
|
|
, license = Elm.License.fromString "BSD-3-Clause" |> Maybe.withDefault Elm.License.bsd3
|
|
|
|
, name = Unsafe.packageName "elm/xyz"
|
|
|
|
, summary = "Fake stuff"
|
|
|
|
, deps = [ ( Unsafe.packageName "elm/core", Unsafe.constraint "1.0.0 <= v < 2.0.0" ) ]
|
|
|
|
, testDeps = []
|
|
|
|
, version = Elm.Version.fromString "1.0.0" |> Maybe.withDefault Elm.Version.one
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xyz2dependencyModules : List Elm.Docs.Module
|
|
|
|
xyz2dependencyModules =
|
|
|
|
[ { name = "Element"
|
|
|
|
, comment = ""
|
|
|
|
, unions = []
|
|
|
|
, aliases = []
|
|
|
|
, values = []
|
|
|
|
, binops = []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
unsafePackageName : String -> Elm.Package.Name
|
|
|
|
unsafePackageName packageName =
|
|
|
|
case Elm.Package.fromString packageName of
|
|
|
|
Just name ->
|
|
|
|
name
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
-- unsafe, but if the generation went well, it should all be good.
|
|
|
|
unsafePackageName packageName
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2022-05-01 18:02:55 +03:00
|
|
|
expressionVisitor : (ModuleNameLookupTable -> Range -> Maybe ModuleName) -> Node Expression -> ModuleContext -> ( List nothing, ModuleContext )
|
|
|
|
expressionVisitor lookupFunction node context =
|
2020-09-22 19:59:34 +03:00
|
|
|
case Node.value node of
|
|
|
|
Expression.FunctionOrValue moduleName name ->
|
2022-05-01 18:02:55 +03:00
|
|
|
( [], { context | texts = context.texts ++ [ getRealName lookupFunction context moduleName (Node.range node) name ] } )
|
2020-09-22 19:59:34 +03:00
|
|
|
|
|
|
|
Expression.RecordUpdateExpression (Node range name) _ ->
|
2022-05-01 18:02:55 +03:00
|
|
|
( [], { context | texts = context.texts ++ [ getRealName lookupFunction context [] range name ] } )
|
2020-09-22 19:59:34 +03:00
|
|
|
|
2021-10-03 12:03:00 +03:00
|
|
|
Expression.PrefixOperator op ->
|
2022-05-01 18:02:55 +03:00
|
|
|
( [], { context | texts = context.texts ++ [ getRealName lookupFunction context [] (Node.range node) op ] } )
|
2021-10-03 12:03:00 +03:00
|
|
|
|
|
|
|
Expression.OperatorApplication op _ _ _ ->
|
2022-05-01 18:02:55 +03:00
|
|
|
( [], { context | texts = context.texts ++ [ getRealName lookupFunction context [] (Node.range node) op ] } )
|
2021-10-03 12:03:00 +03:00
|
|
|
|
2020-09-23 10:01:00 +03:00
|
|
|
Expression.CaseExpression { cases } ->
|
|
|
|
let
|
|
|
|
texts : List String
|
|
|
|
texts =
|
2022-05-01 18:02:55 +03:00
|
|
|
List.concatMap (Tuple.first >> collectPatterns lookupFunction context) cases
|
2020-09-23 10:01:00 +03:00
|
|
|
in
|
|
|
|
( [], { context | texts = context.texts ++ texts } )
|
|
|
|
|
2020-09-23 11:38:39 +03:00
|
|
|
Expression.LetExpression { declarations } ->
|
|
|
|
let
|
|
|
|
texts : List String
|
|
|
|
texts =
|
|
|
|
List.concatMap
|
|
|
|
(\declaration ->
|
|
|
|
case Node.value declaration of
|
|
|
|
Expression.LetFunction function ->
|
|
|
|
let
|
|
|
|
typeAnnotationTexts : List String
|
|
|
|
typeAnnotationTexts =
|
|
|
|
case function.signature |> Maybe.map (Node.value >> .typeAnnotation) of
|
|
|
|
Nothing ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Just typeAnnotation ->
|
2022-05-01 18:02:55 +03:00
|
|
|
typeAnnotationNames lookupFunction context typeAnnotation
|
2020-09-23 11:38:39 +03:00
|
|
|
|
|
|
|
signatureTexts : List String
|
|
|
|
signatureTexts =
|
|
|
|
function.declaration
|
|
|
|
|> Node.value
|
|
|
|
|> .arguments
|
2022-05-01 18:02:55 +03:00
|
|
|
|> List.concatMap (collectPatterns lookupFunction context)
|
2020-09-23 11:38:39 +03:00
|
|
|
in
|
|
|
|
typeAnnotationTexts ++ signatureTexts
|
|
|
|
|
|
|
|
Expression.LetDestructuring pattern _ ->
|
2022-05-01 18:02:55 +03:00
|
|
|
collectPatterns lookupFunction context pattern
|
2020-09-23 11:38:39 +03:00
|
|
|
)
|
|
|
|
declarations
|
|
|
|
in
|
|
|
|
( [], { context | texts = context.texts ++ texts } )
|
|
|
|
|
2020-09-22 19:59:34 +03:00
|
|
|
_ ->
|
|
|
|
( [], context )
|
|
|
|
|
|
|
|
|
2022-05-01 18:02:55 +03:00
|
|
|
collectPatterns : (ModuleNameLookupTable -> Range -> Maybe ModuleName) -> ModuleContext -> Node Pattern.Pattern -> List String
|
|
|
|
collectPatterns lookupFunction context node =
|
2020-09-23 11:21:35 +03:00
|
|
|
case Node.value node of
|
|
|
|
Pattern.NamedPattern { moduleName, name } _ ->
|
2022-05-01 18:02:55 +03:00
|
|
|
[ getRealName lookupFunction context moduleName (Node.range node) name ]
|
2020-09-23 11:21:35 +03:00
|
|
|
|
|
|
|
Pattern.ParenthesizedPattern subPattern ->
|
2022-05-01 18:02:55 +03:00
|
|
|
collectPatterns lookupFunction context subPattern
|
2020-09-23 11:21:35 +03:00
|
|
|
|
|
|
|
Pattern.AsPattern subPattern _ ->
|
2022-05-01 18:02:55 +03:00
|
|
|
collectPatterns lookupFunction context subPattern
|
2020-09-23 11:21:35 +03:00
|
|
|
|
2023-06-17 20:47:43 +03:00
|
|
|
Pattern.VarPattern _ ->
|
|
|
|
[]
|
|
|
|
|
2024-04-09 12:03:35 +03:00
|
|
|
Pattern.TuplePattern subPatterns ->
|
|
|
|
List.concatMap (collectPatterns lookupFunction context) subPatterns
|
|
|
|
|
|
|
|
Pattern.RecordPattern subPatterns ->
|
|
|
|
List.map Node.value subPatterns
|
|
|
|
|
|
|
|
Pattern.AllPattern ->
|
|
|
|
[]
|
|
|
|
|
2020-09-23 11:21:35 +03:00
|
|
|
_ ->
|
2023-06-17 20:47:35 +03:00
|
|
|
Debug.todo ("Other patterns in case expressions are not handled: " ++ Debug.toString node)
|
2020-09-23 11:21:35 +03:00
|
|
|
|
|
|
|
|
2022-05-01 18:02:55 +03:00
|
|
|
getRealName : (ModuleNameLookupTable -> Range -> Maybe ModuleName) -> ModuleContext -> ModuleName -> Range -> String -> String
|
|
|
|
getRealName lookupFunction context moduleName range name =
|
2020-09-22 20:02:48 +03:00
|
|
|
let
|
2020-09-22 20:04:24 +03:00
|
|
|
nameInCode : String
|
|
|
|
nameInCode =
|
|
|
|
case moduleName of
|
|
|
|
[] ->
|
|
|
|
"<nothing>." ++ name
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
String.join "." moduleName ++ "." ++ name
|
|
|
|
|
2020-09-22 20:02:48 +03:00
|
|
|
resultingName : String
|
|
|
|
resultingName =
|
2022-05-01 18:02:55 +03:00
|
|
|
case lookupFunction context.lookupTable range of
|
2020-09-22 20:02:48 +03:00
|
|
|
Just [] ->
|
|
|
|
"<nothing>." ++ name
|
|
|
|
|
|
|
|
Just moduleName_ ->
|
|
|
|
String.join "." moduleName_ ++ "." ++ name
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
"!!! UNKNOWN !!!"
|
|
|
|
in
|
2020-09-22 20:04:24 +03:00
|
|
|
nameInCode ++ " -> " ++ resultingName
|
2020-09-22 20:02:48 +03:00
|
|
|
|
|
|
|
|
2022-05-01 18:02:55 +03:00
|
|
|
declarationVisitor : (ModuleNameLookupTable -> Range -> Maybe ModuleName) -> Node Declaration -> ModuleContext -> ( List nothing, ModuleContext )
|
|
|
|
declarationVisitor lookupFunction node context =
|
2020-08-22 10:54:20 +03:00
|
|
|
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)
|
2022-05-01 18:02:55 +03:00
|
|
|
|> List.concatMap (typeAnnotationNames lookupFunction 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 } ->
|
2022-05-01 18:02:55 +03:00
|
|
|
( [], { context | texts = context.texts ++ typeAnnotationNames lookupFunction context typeAnnotation } )
|
2020-05-16 23:33:36 +03:00
|
|
|
|
2020-08-22 10:54:20 +03:00
|
|
|
Declaration.FunctionDeclaration function ->
|
2020-09-23 11:27:10 +03:00
|
|
|
let
|
|
|
|
typeAnnotationTexts : List String
|
|
|
|
typeAnnotationTexts =
|
|
|
|
case function.signature |> Maybe.map (Node.value >> .typeAnnotation) of
|
|
|
|
Nothing ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Just typeAnnotation ->
|
2022-05-01 18:02:55 +03:00
|
|
|
typeAnnotationNames lookupFunction context typeAnnotation
|
2020-09-23 11:29:02 +03:00
|
|
|
|
|
|
|
signatureTexts : List String
|
|
|
|
signatureTexts =
|
|
|
|
function.declaration
|
|
|
|
|> Node.value
|
|
|
|
|> .arguments
|
2022-05-01 18:02:55 +03:00
|
|
|
|> List.concatMap (collectPatterns lookupFunction context)
|
2020-09-23 11:27:10 +03:00
|
|
|
in
|
2020-09-23 11:29:02 +03:00
|
|
|
( [], { context | texts = context.texts ++ typeAnnotationTexts ++ signatureTexts } )
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
( [], context )
|
|
|
|
|
|
|
|
|
2022-05-01 18:02:55 +03:00
|
|
|
typeAnnotationNames : (ModuleNameLookupTable -> Range -> Maybe ModuleName) -> ModuleContext -> Node TypeAnnotation -> List String
|
|
|
|
typeAnnotationNames lookupFunction 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 ->
|
2022-05-01 18:02:55 +03:00
|
|
|
getRealName lookupFunction moduleContext moduleName typeRange typeName
|
|
|
|
:: List.concatMap (typeAnnotationNames lookupFunction moduleContext) typeParameters
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
TypeAnnotation.Unit ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
TypeAnnotation.Tupled typeAnnotations ->
|
2022-05-01 18:02:55 +03:00
|
|
|
List.concatMap (typeAnnotationNames lookupFunction moduleContext) typeAnnotations
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
TypeAnnotation.Record typeAnnotations ->
|
2022-05-01 18:02:55 +03:00
|
|
|
List.concatMap (Node.value >> Tuple.second >> typeAnnotationNames lookupFunction moduleContext) typeAnnotations
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
TypeAnnotation.GenericRecord _ typeAnnotations ->
|
2022-05-01 18:02:55 +03:00
|
|
|
List.concatMap (Node.value >> Tuple.second >> typeAnnotationNames lookupFunction moduleContext) (Node.value typeAnnotations)
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
TypeAnnotation.FunctionTypeAnnotation arg returnType ->
|
2022-05-01 18:02:55 +03:00
|
|
|
typeAnnotationNames lookupFunction moduleContext arg ++ typeAnnotationNames lookupFunction moduleContext returnType
|
2020-05-16 23:33:36 +03:00
|
|
|
|
|
|
|
|
2022-06-21 18:35:20 +03:00
|
|
|
finalEvaluation : ModuleContext -> List (Rule.Error {})
|
2020-05-16 23:33:36 +03:00
|
|
|
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 }
|
|
|
|
}
|
|
|
|
]
|