2019-09-29 00:47:02 +03:00
|
|
|
module Review.Rule.NoUnusedVariables exposing (rule)
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2019-07-03 15:19:29 +03:00
|
|
|
{-| Forbid variables or types that are declared or imported but never used.
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
|
2019-07-03 15:19:29 +03:00
|
|
|
# Rule
|
|
|
|
|
|
|
|
@docs rule
|
|
|
|
|
2018-11-22 21:19:19 +03:00
|
|
|
-}
|
|
|
|
|
|
|
|
import Dict exposing (Dict)
|
|
|
|
import Elm.Syntax.Declaration exposing (Declaration(..))
|
|
|
|
import Elm.Syntax.Exposing exposing (Exposing(..), TopLevelExpose(..))
|
2019-07-02 12:55:49 +03:00
|
|
|
import Elm.Syntax.Expression exposing (Expression(..), Function, FunctionImplementation, LetDeclaration(..))
|
2018-11-22 21:19:19 +03:00
|
|
|
import Elm.Syntax.Import exposing (Import)
|
|
|
|
import Elm.Syntax.Module as Module exposing (Module(..))
|
2019-08-05 01:16:39 +03:00
|
|
|
import Elm.Syntax.Node as Node exposing (Node(..))
|
2019-07-25 01:19:43 +03:00
|
|
|
import Elm.Syntax.Pattern as Pattern exposing (Pattern)
|
2018-11-22 21:19:19 +03:00
|
|
|
import Elm.Syntax.Range exposing (Range)
|
|
|
|
import Elm.Syntax.TypeAnnotation exposing (TypeAnnotation(..))
|
2019-08-22 13:32:35 +03:00
|
|
|
import NonemptyList as Nonempty exposing (Nonempty)
|
2019-09-29 00:47:02 +03:00
|
|
|
import Review.Fix as Fix
|
|
|
|
import Review.Rule as Rule exposing (Direction, Error, Rule)
|
2018-11-22 21:19:19 +03:00
|
|
|
import Set exposing (Set)
|
|
|
|
|
|
|
|
|
2019-07-03 15:19:29 +03:00
|
|
|
{-| Forbid variables or types that are declared or imported but never used.
|
2018-11-26 13:22:43 +03:00
|
|
|
|
2019-06-26 01:34:57 +03:00
|
|
|
config =
|
2019-07-25 15:35:58 +03:00
|
|
|
[ NoUnusedVariables.rule
|
2018-11-26 13:22:43 +03:00
|
|
|
]
|
|
|
|
|
2019-07-23 00:38:39 +03:00
|
|
|
|
|
|
|
## Fail
|
|
|
|
|
2019-08-27 20:18:56 +03:00
|
|
|
module A exposing (a)
|
|
|
|
|
2019-07-23 00:38:39 +03:00
|
|
|
a n =
|
|
|
|
n + 1
|
|
|
|
|
|
|
|
b =
|
|
|
|
a 2
|
|
|
|
|
|
|
|
|
|
|
|
## Success
|
|
|
|
|
2019-08-27 20:18:56 +03:00
|
|
|
module A exposing (a)
|
|
|
|
|
2019-07-23 00:38:39 +03:00
|
|
|
a n =
|
|
|
|
n + 1
|
|
|
|
|
2018-11-26 13:22:43 +03:00
|
|
|
-}
|
|
|
|
rule : Rule
|
2019-06-15 22:14:40 +03:00
|
|
|
rule =
|
2020-01-19 22:37:19 +03:00
|
|
|
Rule.newModuleRuleSchema "NoUnusedVariables" initialContext
|
2019-06-24 01:32:27 +03:00
|
|
|
|> Rule.withModuleDefinitionVisitor moduleDefinitionVisitor
|
|
|
|
|> Rule.withImportVisitor importVisitor
|
|
|
|
|> Rule.withExpressionVisitor expressionVisitor
|
|
|
|
|> Rule.withDeclarationVisitor declarationVisitor
|
2020-01-26 15:28:57 +03:00
|
|
|
|> Rule.withFinalModuleEvaluation finalEvaluation
|
2020-01-19 22:37:19 +03:00
|
|
|
|> Rule.fromModuleRuleSchema
|
2019-06-24 01:32:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
type alias Context =
|
|
|
|
{ scopes : Nonempty Scope
|
|
|
|
, exposesEverything : Bool
|
2019-07-05 01:33:51 +03:00
|
|
|
, constructorNameToTypeName : Dict String String
|
2019-08-19 14:53:35 +03:00
|
|
|
, declaredModules : Dict String VariableInfo
|
2019-07-25 11:40:23 +03:00
|
|
|
, usedModules : Set String
|
2019-06-24 01:32:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type alias Scope =
|
2019-08-19 14:53:35 +03:00
|
|
|
{ declared : Dict String VariableInfo
|
2019-06-24 01:32:27 +03:00
|
|
|
, used : Set String
|
|
|
|
}
|
2018-11-26 13:22:43 +03:00
|
|
|
|
|
|
|
|
2019-08-19 14:53:35 +03:00
|
|
|
type alias VariableInfo =
|
|
|
|
{ variableType : VariableType
|
|
|
|
, under : Range
|
|
|
|
, rangeToRemove : Range
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-26 21:16:04 +03:00
|
|
|
type VariableType
|
2019-08-19 14:53:35 +03:00
|
|
|
= TopLevelVariable
|
|
|
|
| LetVariable
|
2018-11-26 20:39:46 +03:00
|
|
|
| ImportedModule
|
2019-08-19 14:53:35 +03:00
|
|
|
| ImportedItem ImportType
|
2018-11-26 20:39:46 +03:00
|
|
|
| ModuleAlias
|
|
|
|
| Type
|
2018-11-26 21:09:18 +03:00
|
|
|
| Port
|
2018-11-26 20:39:46 +03:00
|
|
|
|
|
|
|
|
2019-08-09 20:04:12 +03:00
|
|
|
type LetBlockContext
|
|
|
|
= HasMultipleDeclarations
|
|
|
|
| HasNoOtherDeclarations Range
|
|
|
|
|
|
|
|
|
2019-08-19 14:53:35 +03:00
|
|
|
type ImportType
|
|
|
|
= ImportedVariable
|
|
|
|
| ImportedType
|
|
|
|
| ImportedOperator
|
|
|
|
|
|
|
|
|
2019-06-24 01:32:27 +03:00
|
|
|
initialContext : Context
|
|
|
|
initialContext =
|
|
|
|
{ scopes = Nonempty.fromElement emptyScope
|
|
|
|
, exposesEverything = False
|
2019-07-05 01:33:51 +03:00
|
|
|
, constructorNameToTypeName = Dict.empty
|
2019-07-25 11:40:23 +03:00
|
|
|
, declaredModules = Dict.empty
|
|
|
|
, usedModules = Set.empty
|
2018-11-22 21:19:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
emptyScope : Scope
|
|
|
|
emptyScope =
|
2019-07-25 11:40:23 +03:00
|
|
|
{ declared = Dict.empty
|
|
|
|
, used = Set.empty
|
|
|
|
}
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
error : VariableInfo -> String -> Error {}
|
2019-08-19 14:53:35 +03:00
|
|
|
error { variableType, under, rangeToRemove } name =
|
2019-08-04 16:47:33 +03:00
|
|
|
Rule.error
|
|
|
|
{ message = variableTypeToString variableType ++ " `" ++ name ++ "` is not used" ++ variableTypeWarning variableType
|
|
|
|
, details = [ "Since it is not being used, I recommend removing it. It should make the code clearer to read for other people." ]
|
|
|
|
}
|
2019-08-19 14:53:35 +03:00
|
|
|
under
|
|
|
|
|> Rule.withFixes [ Fix.removeRange rangeToRemove ]
|
2018-11-26 20:39:46 +03:00
|
|
|
|
|
|
|
|
2018-11-26 21:16:04 +03:00
|
|
|
variableTypeToString : VariableType -> String
|
2019-08-19 14:53:35 +03:00
|
|
|
variableTypeToString variableType =
|
|
|
|
case variableType of
|
|
|
|
TopLevelVariable ->
|
2019-08-09 20:04:12 +03:00
|
|
|
"Top-level variable"
|
|
|
|
|
2019-08-19 14:53:35 +03:00
|
|
|
LetVariable ->
|
2019-08-09 20:04:12 +03:00
|
|
|
"`let in` variable"
|
2018-11-26 20:39:46 +03:00
|
|
|
|
|
|
|
ImportedModule ->
|
|
|
|
"Imported module"
|
|
|
|
|
2019-08-19 14:53:35 +03:00
|
|
|
ImportedItem ImportedVariable ->
|
2018-11-26 21:16:04 +03:00
|
|
|
"Imported variable"
|
|
|
|
|
2019-08-19 14:53:35 +03:00
|
|
|
ImportedItem ImportedType ->
|
2018-11-26 21:16:04 +03:00
|
|
|
"Imported type"
|
|
|
|
|
2019-08-19 14:53:35 +03:00
|
|
|
ImportedItem ImportedOperator ->
|
2018-11-26 21:16:04 +03:00
|
|
|
"Imported operator"
|
|
|
|
|
2018-11-26 20:39:46 +03:00
|
|
|
ModuleAlias ->
|
|
|
|
"Module alias"
|
|
|
|
|
|
|
|
Type ->
|
|
|
|
"Type"
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2018-11-26 21:09:18 +03:00
|
|
|
Port ->
|
|
|
|
"Port"
|
|
|
|
|
|
|
|
|
2018-11-26 21:16:04 +03:00
|
|
|
variableTypeWarning : VariableType -> String
|
2018-11-26 21:09:18 +03:00
|
|
|
variableTypeWarning value =
|
|
|
|
case value of
|
2019-08-19 14:53:35 +03:00
|
|
|
TopLevelVariable ->
|
2019-08-09 20:04:12 +03:00
|
|
|
""
|
|
|
|
|
2019-08-19 14:53:35 +03:00
|
|
|
LetVariable ->
|
2018-11-26 21:09:18 +03:00
|
|
|
""
|
|
|
|
|
|
|
|
ImportedModule ->
|
|
|
|
""
|
|
|
|
|
2019-08-19 14:53:35 +03:00
|
|
|
ImportedItem _ ->
|
2018-11-26 21:16:04 +03:00
|
|
|
""
|
|
|
|
|
2018-11-26 21:09:18 +03:00
|
|
|
ModuleAlias ->
|
|
|
|
""
|
|
|
|
|
|
|
|
Type ->
|
|
|
|
""
|
|
|
|
|
|
|
|
Port ->
|
|
|
|
" (Warning: Removing this port may break your application if it is used in the JS code)"
|
|
|
|
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2020-02-28 19:11:15 +03:00
|
|
|
moduleDefinitionVisitor : Node Module -> Context -> ( List nothing, Context )
|
2019-08-05 01:16:39 +03:00
|
|
|
moduleDefinitionVisitor (Node _ moduleNode) context =
|
|
|
|
case Module.exposingList moduleNode of
|
2018-11-22 21:19:19 +03:00
|
|
|
All _ ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], { context | exposesEverything = True } )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
Explicit list ->
|
|
|
|
let
|
|
|
|
names =
|
|
|
|
List.filterMap
|
2019-08-05 01:16:39 +03:00
|
|
|
(\(Node _ node) ->
|
|
|
|
case node of
|
2018-11-22 21:19:19 +03:00
|
|
|
FunctionExpose name ->
|
|
|
|
Just name
|
|
|
|
|
|
|
|
TypeOrAliasExpose name ->
|
|
|
|
Just name
|
|
|
|
|
|
|
|
TypeExpose { name } ->
|
|
|
|
Just name
|
|
|
|
|
|
|
|
InfixExpose name ->
|
|
|
|
-- Just name
|
|
|
|
Nothing
|
|
|
|
)
|
|
|
|
list
|
|
|
|
in
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], markAllAsUsed names context )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
|
2020-02-28 19:11:15 +03:00
|
|
|
importVisitor : Node Import -> Context -> ( List nothing, Context )
|
2019-08-19 14:53:35 +03:00
|
|
|
importVisitor ((Node range { exposingList, moduleAlias, moduleName }) as importNode) context =
|
|
|
|
case exposingList of
|
2018-11-26 20:12:36 +03:00
|
|
|
Nothing ->
|
|
|
|
let
|
2019-08-19 14:53:35 +03:00
|
|
|
( variableType, Node nameNodeRange nameNodeValue, rangeToRemove ) =
|
2019-08-05 01:16:39 +03:00
|
|
|
case moduleAlias of
|
|
|
|
Just moduleAlias_ ->
|
2019-08-19 14:53:35 +03:00
|
|
|
( ModuleAlias, moduleAlias_, moduleAliasRange importNode (Node.range moduleAlias_) )
|
2018-11-26 20:39:46 +03:00
|
|
|
|
|
|
|
Nothing ->
|
2019-08-19 14:53:35 +03:00
|
|
|
( ImportedModule, moduleName, range )
|
2018-11-26 20:12:36 +03:00
|
|
|
in
|
|
|
|
( []
|
|
|
|
, register
|
2019-08-19 14:53:35 +03:00
|
|
|
{ variableType = variableType, under = nameNodeRange, rangeToRemove = rangeToRemove }
|
2019-08-05 01:16:39 +03:00
|
|
|
(getModuleName nameNodeValue)
|
2019-06-16 22:12:37 +03:00
|
|
|
context
|
2018-11-26 20:12:36 +03:00
|
|
|
)
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2018-11-26 20:12:36 +03:00
|
|
|
Just declaredImports ->
|
2019-07-25 10:14:33 +03:00
|
|
|
let
|
|
|
|
contextWithoutImports : Context
|
|
|
|
contextWithoutImports =
|
2019-08-05 01:16:39 +03:00
|
|
|
case moduleAlias of
|
2019-08-19 14:53:35 +03:00
|
|
|
Just (Node moduleAliasRange_ value) ->
|
2019-07-25 10:14:33 +03:00
|
|
|
register
|
2019-08-19 14:53:35 +03:00
|
|
|
{ variableType = ModuleAlias
|
|
|
|
, under = moduleAliasRange_
|
|
|
|
, rangeToRemove = moduleAliasRange importNode moduleAliasRange_
|
|
|
|
}
|
2019-08-05 01:16:39 +03:00
|
|
|
(getModuleName value)
|
2019-07-25 10:14:33 +03:00
|
|
|
context
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
context
|
|
|
|
in
|
2018-11-26 20:12:36 +03:00
|
|
|
( []
|
|
|
|
, List.foldl
|
2019-08-19 14:53:35 +03:00
|
|
|
(\( name, variableInfo ) context_ -> register variableInfo name context_)
|
2019-07-25 10:14:33 +03:00
|
|
|
contextWithoutImports
|
2018-11-26 20:12:36 +03:00
|
|
|
(collectFromExposing declaredImports)
|
|
|
|
)
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
|
2019-08-19 14:53:35 +03:00
|
|
|
moduleAliasRange : Node Import -> Range -> Range
|
|
|
|
moduleAliasRange (Node _ { moduleName }) range =
|
|
|
|
{ range | start = (Node.range moduleName).end }
|
|
|
|
|
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
expressionVisitor : Node Expression -> Direction -> Context -> ( List (Error {}), Context )
|
2019-08-09 20:04:12 +03:00
|
|
|
expressionVisitor (Node range value) direction context =
|
|
|
|
case ( direction, value ) of
|
2019-06-26 12:42:17 +03:00
|
|
|
( Rule.OnEnter, FunctionOrValue [] name ) ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], markAsUsed name context )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2019-06-26 12:42:17 +03:00
|
|
|
( Rule.OnEnter, FunctionOrValue moduleName name ) ->
|
2019-07-25 11:40:23 +03:00
|
|
|
( [], markModuleAsUsed (getModuleName moduleName) context )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2019-06-26 12:42:17 +03:00
|
|
|
( Rule.OnEnter, OperatorApplication name _ _ _ ) ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], markAsUsed name context )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2019-06-26 12:42:17 +03:00
|
|
|
( Rule.OnEnter, PrefixOperator name ) ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], markAsUsed name context )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2019-08-09 20:04:12 +03:00
|
|
|
( Rule.OnEnter, LetExpression { declarations, expression } ) ->
|
2018-11-26 13:22:43 +03:00
|
|
|
let
|
2019-08-09 20:04:12 +03:00
|
|
|
letBlockContext : LetBlockContext
|
|
|
|
letBlockContext =
|
|
|
|
if List.length declarations == 1 then
|
2019-08-22 13:16:00 +03:00
|
|
|
HasNoOtherDeclarations <| rangeUpUntil range (Node.range expression |> .start)
|
2019-08-09 20:04:12 +03:00
|
|
|
|
|
|
|
else
|
|
|
|
HasMultipleDeclarations
|
|
|
|
|
2019-07-02 12:55:49 +03:00
|
|
|
newContext : Context
|
2018-11-26 13:22:43 +03:00
|
|
|
newContext =
|
|
|
|
List.foldl
|
2019-06-16 22:12:37 +03:00
|
|
|
(\declaration context_ ->
|
2019-06-24 13:32:07 +03:00
|
|
|
case Node.value declaration of
|
2018-11-26 13:22:43 +03:00
|
|
|
LetFunction function ->
|
2019-08-09 20:04:12 +03:00
|
|
|
registerFunction letBlockContext function context_
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2018-11-26 13:22:43 +03:00
|
|
|
LetDestructuring pattern _ ->
|
2019-06-16 22:12:37 +03:00
|
|
|
context_
|
2018-11-26 13:22:43 +03:00
|
|
|
)
|
2019-06-16 22:12:37 +03:00
|
|
|
{ context | scopes = Nonempty.cons emptyScope context.scopes }
|
2018-11-26 13:22:43 +03:00
|
|
|
declarations
|
|
|
|
in
|
|
|
|
( [], newContext )
|
|
|
|
|
2019-07-02 12:16:03 +03:00
|
|
|
( Rule.OnExit, RecordUpdateExpression expr _ ) ->
|
|
|
|
( [], markAsUsed (Node.value expr) context )
|
|
|
|
|
2019-07-25 01:19:43 +03:00
|
|
|
( Rule.OnExit, CaseExpression { cases } ) ->
|
|
|
|
let
|
2019-07-25 11:40:23 +03:00
|
|
|
usedVariables : { types : List String, modules : List String }
|
2019-07-25 01:19:43 +03:00
|
|
|
usedVariables =
|
2019-07-25 11:40:23 +03:00
|
|
|
cases
|
|
|
|
|> List.map
|
|
|
|
(\( patternNode, expressionNode ) ->
|
|
|
|
getUsedVariablesFromPattern patternNode
|
|
|
|
)
|
|
|
|
|> foldUsedTypesAndModules
|
2019-07-25 01:19:43 +03:00
|
|
|
in
|
|
|
|
( []
|
2019-07-25 11:40:23 +03:00
|
|
|
, markUsedTypesAndModules usedVariables context
|
2019-07-25 01:19:43 +03:00
|
|
|
)
|
|
|
|
|
2019-06-26 12:42:17 +03:00
|
|
|
( Rule.OnExit, LetExpression _ ) ->
|
2018-11-26 13:22:43 +03:00
|
|
|
let
|
|
|
|
( errors, remainingUsed ) =
|
2019-06-16 22:12:37 +03:00
|
|
|
makeReport (Nonempty.head context.scopes)
|
2018-11-26 13:22:43 +03:00
|
|
|
|
2019-06-16 22:12:37 +03:00
|
|
|
contextWithPoppedScope =
|
|
|
|
{ context | scopes = Nonempty.pop context.scopes }
|
2018-11-26 13:22:43 +03:00
|
|
|
in
|
|
|
|
( errors
|
2019-06-16 22:12:37 +03:00
|
|
|
, markAllAsUsed remainingUsed contextWithPoppedScope
|
2018-11-26 13:22:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
_ ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], context )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
|
2019-07-25 11:40:23 +03:00
|
|
|
getUsedVariablesFromPattern : Node Pattern -> { types : List String, modules : List String }
|
2019-07-25 01:19:43 +03:00
|
|
|
getUsedVariablesFromPattern patternNode =
|
2019-07-25 11:40:23 +03:00
|
|
|
{ types = getUsedTypesFromPattern patternNode
|
|
|
|
, modules = getUsedModulesFromPattern patternNode
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getUsedTypesFromPattern : Node Pattern -> List String
|
|
|
|
getUsedTypesFromPattern patternNode =
|
2019-07-25 01:19:43 +03:00
|
|
|
case Node.value patternNode of
|
|
|
|
Pattern.AllPattern ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.UnitPattern ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.CharPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.StringPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.IntPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.HexPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.FloatPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.TuplePattern patterns ->
|
2019-07-25 11:40:23 +03:00
|
|
|
List.concatMap getUsedTypesFromPattern patterns
|
2019-07-25 01:19:43 +03:00
|
|
|
|
|
|
|
Pattern.RecordPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.UnConsPattern pattern1 pattern2 ->
|
2019-07-25 11:40:23 +03:00
|
|
|
List.concatMap getUsedTypesFromPattern [ pattern1, pattern2 ]
|
2019-07-25 01:19:43 +03:00
|
|
|
|
|
|
|
Pattern.ListPattern patterns ->
|
2019-07-25 11:40:23 +03:00
|
|
|
List.concatMap getUsedTypesFromPattern patterns
|
2019-07-25 01:19:43 +03:00
|
|
|
|
|
|
|
Pattern.VarPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.NamedPattern qualifiedNameRef patterns ->
|
|
|
|
let
|
2019-07-25 11:40:23 +03:00
|
|
|
usedVariable : List String
|
2019-07-25 01:19:43 +03:00
|
|
|
usedVariable =
|
|
|
|
case qualifiedNameRef.moduleName of
|
|
|
|
[] ->
|
2019-07-25 11:40:23 +03:00
|
|
|
[ qualifiedNameRef.name ]
|
2019-07-25 01:19:43 +03:00
|
|
|
|
|
|
|
moduleName ->
|
2019-07-25 11:40:23 +03:00
|
|
|
[]
|
2019-07-25 01:19:43 +03:00
|
|
|
in
|
2019-07-25 11:40:23 +03:00
|
|
|
usedVariable ++ List.concatMap getUsedTypesFromPattern patterns
|
2019-07-25 01:19:43 +03:00
|
|
|
|
|
|
|
Pattern.AsPattern pattern alias_ ->
|
2019-07-25 11:40:23 +03:00
|
|
|
getUsedTypesFromPattern pattern
|
2019-07-25 01:19:43 +03:00
|
|
|
|
|
|
|
Pattern.ParenthesizedPattern pattern ->
|
2019-07-25 11:40:23 +03:00
|
|
|
getUsedTypesFromPattern pattern
|
|
|
|
|
|
|
|
|
|
|
|
getUsedModulesFromPattern : Node Pattern -> List String
|
|
|
|
getUsedModulesFromPattern patternNode =
|
|
|
|
case Node.value patternNode of
|
|
|
|
Pattern.AllPattern ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.UnitPattern ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.CharPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.StringPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.IntPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.HexPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.FloatPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.TuplePattern patterns ->
|
|
|
|
List.concatMap getUsedModulesFromPattern patterns
|
|
|
|
|
|
|
|
Pattern.RecordPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.UnConsPattern pattern1 pattern2 ->
|
|
|
|
List.concatMap getUsedModulesFromPattern [ pattern1, pattern2 ]
|
|
|
|
|
|
|
|
Pattern.ListPattern patterns ->
|
|
|
|
List.concatMap getUsedModulesFromPattern patterns
|
|
|
|
|
|
|
|
Pattern.VarPattern _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Pattern.NamedPattern qualifiedNameRef patterns ->
|
|
|
|
let
|
|
|
|
usedVariable : List String
|
|
|
|
usedVariable =
|
|
|
|
case qualifiedNameRef.moduleName of
|
|
|
|
[] ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
moduleName ->
|
|
|
|
[ getModuleName moduleName ]
|
|
|
|
in
|
|
|
|
usedVariable ++ List.concatMap getUsedModulesFromPattern patterns
|
|
|
|
|
|
|
|
Pattern.AsPattern pattern alias_ ->
|
|
|
|
getUsedModulesFromPattern pattern
|
|
|
|
|
|
|
|
Pattern.ParenthesizedPattern pattern ->
|
|
|
|
getUsedModulesFromPattern pattern
|
2019-07-25 01:19:43 +03:00
|
|
|
|
|
|
|
|
2020-02-28 19:11:15 +03:00
|
|
|
declarationVisitor : Node Declaration -> Direction -> Context -> ( List nothing, Context )
|
2019-06-24 01:32:27 +03:00
|
|
|
declarationVisitor node direction context =
|
2019-06-24 13:32:07 +03:00
|
|
|
case ( direction, Node.value node ) of
|
2019-06-26 12:42:17 +03:00
|
|
|
( Rule.OnEnter, FunctionDeclaration function ) ->
|
2018-11-22 21:19:19 +03:00
|
|
|
let
|
2019-07-05 01:33:51 +03:00
|
|
|
functionImplementation : FunctionImplementation
|
|
|
|
functionImplementation =
|
2019-06-24 13:32:07 +03:00
|
|
|
Node.value function.declaration
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2019-07-25 11:40:23 +03:00
|
|
|
namesUsedInSignature : { types : List String, modules : List String }
|
2018-11-22 21:19:19 +03:00
|
|
|
namesUsedInSignature =
|
|
|
|
function.signature
|
2019-06-24 13:32:07 +03:00
|
|
|
|> Maybe.map (Node.value >> .typeAnnotation >> collectNamesFromTypeAnnotation)
|
2019-07-25 11:40:23 +03:00
|
|
|
|> Maybe.withDefault { types = [], modules = [] }
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2019-07-05 01:33:51 +03:00
|
|
|
newContext : Context
|
2018-11-22 21:19:19 +03:00
|
|
|
newContext =
|
2019-06-16 22:12:37 +03:00
|
|
|
context
|
2019-08-19 14:53:35 +03:00
|
|
|
|> register
|
|
|
|
{ variableType = TopLevelVariable
|
|
|
|
, under = Node.range functionImplementation.name
|
|
|
|
, rangeToRemove = Node.range node
|
|
|
|
}
|
|
|
|
(Node.value functionImplementation.name)
|
2019-07-25 11:40:23 +03:00
|
|
|
|> markUsedTypesAndModules namesUsedInSignature
|
2018-11-22 21:19:19 +03:00
|
|
|
in
|
|
|
|
( [], newContext )
|
|
|
|
|
2019-07-02 12:42:07 +03:00
|
|
|
( Rule.OnEnter, CustomTypeDeclaration { name, constructors } ) ->
|
|
|
|
let
|
2019-07-25 11:40:23 +03:00
|
|
|
variablesFromConstructorArguments : { types : List String, modules : List String }
|
2019-07-02 12:42:07 +03:00
|
|
|
variablesFromConstructorArguments =
|
|
|
|
constructors
|
|
|
|
|> List.concatMap (Node.value >> .arguments)
|
2019-07-25 11:40:23 +03:00
|
|
|
|> List.map collectNamesFromTypeAnnotation
|
|
|
|
|> foldUsedTypesAndModules
|
2019-07-05 01:33:51 +03:00
|
|
|
|
|
|
|
typeName : String
|
|
|
|
typeName =
|
|
|
|
Node.value name
|
|
|
|
|
|
|
|
constructorsForType : Dict String String
|
|
|
|
constructorsForType =
|
|
|
|
constructors
|
|
|
|
|> List.map (Node.value >> .name >> Node.value)
|
|
|
|
|> List.map (\constructorName -> ( constructorName, typeName ))
|
|
|
|
|> Dict.fromList
|
2019-07-02 12:42:07 +03:00
|
|
|
in
|
|
|
|
( []
|
2019-07-05 01:33:51 +03:00
|
|
|
, { context | constructorNameToTypeName = Dict.union constructorsForType context.constructorNameToTypeName }
|
2019-08-19 14:53:35 +03:00
|
|
|
|> register
|
|
|
|
{ variableType = Type
|
|
|
|
, under = Node.range name
|
|
|
|
, rangeToRemove = Node.range node
|
|
|
|
}
|
|
|
|
(Node.value name)
|
2019-07-25 11:40:23 +03:00
|
|
|
|> markUsedTypesAndModules variablesFromConstructorArguments
|
2019-07-02 12:42:07 +03:00
|
|
|
)
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2019-07-02 12:42:07 +03:00
|
|
|
( Rule.OnEnter, AliasDeclaration { name, typeAnnotation } ) ->
|
2019-07-25 11:40:23 +03:00
|
|
|
let
|
|
|
|
namesUsedInTypeAnnotation : { types : List String, modules : List String }
|
|
|
|
namesUsedInTypeAnnotation =
|
|
|
|
collectNamesFromTypeAnnotation typeAnnotation
|
|
|
|
in
|
2019-07-02 12:42:07 +03:00
|
|
|
( []
|
|
|
|
, context
|
2019-08-19 14:53:35 +03:00
|
|
|
|> register
|
|
|
|
{ variableType = Type
|
|
|
|
, under = Node.range name
|
|
|
|
, rangeToRemove = Node.range node
|
|
|
|
}
|
|
|
|
(Node.value name)
|
2019-07-25 11:40:23 +03:00
|
|
|
|> markUsedTypesAndModules namesUsedInTypeAnnotation
|
2019-07-02 12:42:07 +03:00
|
|
|
)
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2019-06-26 12:42:17 +03:00
|
|
|
( Rule.OnEnter, PortDeclaration { name, typeAnnotation } ) ->
|
2019-07-25 11:40:23 +03:00
|
|
|
let
|
|
|
|
namesUsedInTypeAnnotation : { types : List String, modules : List String }
|
|
|
|
namesUsedInTypeAnnotation =
|
|
|
|
collectNamesFromTypeAnnotation typeAnnotation
|
|
|
|
in
|
2018-11-26 21:09:18 +03:00
|
|
|
( []
|
2019-06-16 22:12:37 +03:00
|
|
|
, context
|
2019-07-25 11:40:23 +03:00
|
|
|
|> markUsedTypesAndModules namesUsedInTypeAnnotation
|
2019-08-19 14:53:35 +03:00
|
|
|
|> register
|
|
|
|
{ variableType = Port
|
|
|
|
, under = Node.range name
|
|
|
|
, rangeToRemove = Node.range node
|
|
|
|
}
|
|
|
|
(Node.value name)
|
2018-11-26 21:09:18 +03:00
|
|
|
)
|
|
|
|
|
2019-06-26 12:42:17 +03:00
|
|
|
( Rule.OnEnter, InfixDeclaration _ ) ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], context )
|
2018-11-26 21:09:18 +03:00
|
|
|
|
2019-06-26 12:42:17 +03:00
|
|
|
( Rule.OnEnter, Destructuring _ _ ) ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], context )
|
2018-11-26 21:09:18 +03:00
|
|
|
|
2019-06-26 12:42:17 +03:00
|
|
|
( Rule.OnExit, _ ) ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], context )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
|
2019-07-25 11:40:23 +03:00
|
|
|
foldUsedTypesAndModules : List { types : List String, modules : List String } -> { types : List String, modules : List String }
|
|
|
|
foldUsedTypesAndModules =
|
|
|
|
List.foldl (\a b -> { types = a.types ++ b.types, modules = a.modules ++ b.modules }) { types = [], modules = [] }
|
|
|
|
|
|
|
|
|
|
|
|
markUsedTypesAndModules : { types : List String, modules : List String } -> Context -> Context
|
|
|
|
markUsedTypesAndModules { types, modules } context =
|
|
|
|
context
|
|
|
|
|> markAllAsUsed types
|
|
|
|
|> markAllModulesAsUsed modules
|
|
|
|
|
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
finalEvaluation : Context -> List (Error {})
|
2019-06-16 22:12:37 +03:00
|
|
|
finalEvaluation context =
|
|
|
|
if context.exposesEverything then
|
2019-06-15 22:13:40 +03:00
|
|
|
[]
|
|
|
|
|
|
|
|
else
|
2019-07-05 01:33:51 +03:00
|
|
|
let
|
|
|
|
rootScope : Scope
|
|
|
|
rootScope =
|
|
|
|
Nonempty.head context.scopes
|
|
|
|
|
|
|
|
namesOfCustomTypesUsedByCallingAConstructor : Set String
|
|
|
|
namesOfCustomTypesUsedByCallingAConstructor =
|
|
|
|
context.constructorNameToTypeName
|
|
|
|
|> Dict.filter (\usedName _ -> Set.member usedName rootScope.used)
|
|
|
|
|> Dict.values
|
|
|
|
|> Set.fromList
|
|
|
|
|
|
|
|
newRootScope : Scope
|
|
|
|
newRootScope =
|
|
|
|
{ rootScope | used = Set.union namesOfCustomTypesUsedByCallingAConstructor rootScope.used }
|
2019-07-25 11:40:23 +03:00
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
moduleErrors : List (Error {})
|
2019-07-25 11:40:23 +03:00
|
|
|
moduleErrors =
|
|
|
|
context.declaredModules
|
|
|
|
|> Dict.filter (\key _ -> not <| Set.member key context.usedModules)
|
|
|
|
|> Dict.toList
|
2019-08-19 14:53:35 +03:00
|
|
|
|> List.map (\( key, variableInfo ) -> error variableInfo key)
|
2019-07-05 01:33:51 +03:00
|
|
|
in
|
2019-07-25 11:40:23 +03:00
|
|
|
List.concat
|
|
|
|
[ newRootScope
|
|
|
|
|> makeReport
|
|
|
|
|> Tuple.first
|
|
|
|
, moduleErrors
|
|
|
|
]
|
2018-11-26 13:22:43 +03:00
|
|
|
|
|
|
|
|
2019-08-09 20:04:12 +03:00
|
|
|
registerFunction : LetBlockContext -> Function -> Context -> Context
|
|
|
|
registerFunction letBlockContext function context =
|
2018-11-26 13:22:43 +03:00
|
|
|
let
|
2019-07-02 12:55:49 +03:00
|
|
|
declaration : FunctionImplementation
|
2018-11-26 13:22:43 +03:00
|
|
|
declaration =
|
2019-06-24 13:32:07 +03:00
|
|
|
Node.value function.declaration
|
2019-07-02 12:55:49 +03:00
|
|
|
|
2019-07-25 11:40:23 +03:00
|
|
|
namesUsedInSignature : { types : List String, modules : List String }
|
2019-07-02 12:55:49 +03:00
|
|
|
namesUsedInSignature =
|
|
|
|
case Maybe.map Node.value function.signature of
|
|
|
|
Just signature ->
|
|
|
|
collectNamesFromTypeAnnotation signature.typeAnnotation
|
|
|
|
|
|
|
|
Nothing ->
|
2019-07-25 11:40:23 +03:00
|
|
|
{ types = [], modules = [] }
|
2019-08-04 12:14:04 +03:00
|
|
|
|
|
|
|
functionRange : Range
|
|
|
|
functionRange =
|
2019-08-04 12:42:59 +03:00
|
|
|
case function.signature of
|
|
|
|
Just signature ->
|
2019-08-22 13:16:00 +03:00
|
|
|
mergeRanges
|
2019-08-04 12:42:59 +03:00
|
|
|
(Node.range function.declaration)
|
|
|
|
(Node.range signature)
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Node.range function.declaration
|
2018-11-26 13:22:43 +03:00
|
|
|
in
|
2019-07-02 12:55:49 +03:00
|
|
|
context
|
2019-08-09 20:04:12 +03:00
|
|
|
|> register
|
2019-08-19 14:53:35 +03:00
|
|
|
{ variableType = LetVariable
|
|
|
|
, under = Node.range declaration.name
|
|
|
|
, rangeToRemove =
|
|
|
|
case letBlockContext of
|
|
|
|
HasMultipleDeclarations ->
|
|
|
|
functionRange
|
|
|
|
|
|
|
|
HasNoOtherDeclarations letDeclarationsRange ->
|
|
|
|
-- If there are no other declarations in the let in block,
|
|
|
|
-- we also need to remove the `let in` keywords.
|
|
|
|
letDeclarationsRange
|
|
|
|
}
|
2019-08-09 20:04:12 +03:00
|
|
|
(Node.value declaration.name)
|
2019-07-25 11:40:23 +03:00
|
|
|
|> markUsedTypesAndModules namesUsedInSignature
|
2018-11-26 13:22:43 +03:00
|
|
|
|
|
|
|
|
2019-08-19 14:53:35 +03:00
|
|
|
collectFromExposing : Node Exposing -> List ( String, VariableInfo )
|
|
|
|
collectFromExposing exposingNode =
|
|
|
|
case Node.value exposingNode of
|
2018-11-26 13:22:43 +03:00
|
|
|
All _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Explicit list ->
|
2019-08-19 14:53:35 +03:00
|
|
|
let
|
|
|
|
listWithPreviousRange : List (Maybe Range)
|
|
|
|
listWithPreviousRange =
|
|
|
|
Nothing
|
|
|
|
:: (list
|
|
|
|
|> List.map (Node.range >> Just)
|
|
|
|
|> List.take (List.length list - 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
listWithNextRange : List Range
|
|
|
|
listWithNextRange =
|
|
|
|
(list
|
|
|
|
|> List.map Node.range
|
|
|
|
|> List.drop 1
|
|
|
|
)
|
|
|
|
++ [ { start = { row = 0, column = 0 }, end = { row = 0, column = 0 } } ]
|
|
|
|
in
|
|
|
|
list
|
|
|
|
|> List.map3 (\prev next current -> ( prev, current, next )) listWithPreviousRange listWithNextRange
|
|
|
|
|> List.indexedMap
|
|
|
|
(\index ( maybePreviousRange, Node range value, nextRange ) ->
|
|
|
|
let
|
|
|
|
rangeToRemove : Range
|
|
|
|
rangeToRemove =
|
|
|
|
if List.length list == 1 then
|
|
|
|
Node.range exposingNode
|
|
|
|
|
|
|
|
else if index == 0 then
|
|
|
|
{ range | end = nextRange.start }
|
|
|
|
|
|
|
|
else
|
|
|
|
case maybePreviousRange of
|
|
|
|
Nothing ->
|
|
|
|
range
|
|
|
|
|
|
|
|
Just previousRange ->
|
|
|
|
{ range | start = previousRange.end }
|
|
|
|
in
|
|
|
|
case value of
|
|
|
|
FunctionExpose name ->
|
|
|
|
Just ( name, { variableType = ImportedItem ImportedVariable, under = range, rangeToRemove = rangeToRemove } )
|
|
|
|
|
|
|
|
InfixExpose name ->
|
|
|
|
Just ( name, { variableType = ImportedItem ImportedOperator, under = range, rangeToRemove = rangeToRemove } )
|
|
|
|
|
|
|
|
TypeOrAliasExpose name ->
|
|
|
|
Just ( name, { variableType = ImportedItem ImportedType, under = range, rangeToRemove = rangeToRemove } )
|
|
|
|
|
|
|
|
TypeExpose { name, open } ->
|
|
|
|
case open of
|
|
|
|
Just openRange ->
|
|
|
|
Nothing
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Just ( name, { variableType = ImportedItem ImportedType, under = range, rangeToRemove = rangeToRemove } )
|
|
|
|
)
|
|
|
|
|> List.filterMap identity
|
2018-11-26 13:22:43 +03:00
|
|
|
|
|
|
|
|
2019-07-25 11:40:23 +03:00
|
|
|
collectNamesFromTypeAnnotation : Node TypeAnnotation -> { types : List String, modules : List String }
|
2018-11-22 21:19:19 +03:00
|
|
|
collectNamesFromTypeAnnotation node =
|
2019-07-25 11:40:23 +03:00
|
|
|
{ types = collectTypesFromTypeAnnotation node
|
|
|
|
, modules = collectModuleNamesFromTypeAnnotation node
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
collectTypesFromTypeAnnotation : Node TypeAnnotation -> List String
|
|
|
|
collectTypesFromTypeAnnotation node =
|
|
|
|
case Node.value node of
|
|
|
|
FunctionTypeAnnotation a b ->
|
|
|
|
collectTypesFromTypeAnnotation a ++ collectTypesFromTypeAnnotation b
|
|
|
|
|
|
|
|
Typed nameNode params ->
|
|
|
|
let
|
|
|
|
name : List String
|
|
|
|
name =
|
|
|
|
case Node.value nameNode of
|
|
|
|
( [], str ) ->
|
|
|
|
[ str ]
|
|
|
|
|
|
|
|
( moduleName, _ ) ->
|
|
|
|
[]
|
|
|
|
in
|
|
|
|
name ++ List.concatMap collectTypesFromTypeAnnotation params
|
|
|
|
|
|
|
|
Record list ->
|
|
|
|
list
|
|
|
|
|> List.map (Node.value >> Tuple.second)
|
|
|
|
|> List.concatMap collectTypesFromTypeAnnotation
|
|
|
|
|
|
|
|
GenericRecord name list ->
|
|
|
|
list
|
|
|
|
|> Node.value
|
|
|
|
|> List.map (Node.value >> Tuple.second)
|
|
|
|
|> List.concatMap collectTypesFromTypeAnnotation
|
|
|
|
|
|
|
|
Tupled list ->
|
|
|
|
List.concatMap collectTypesFromTypeAnnotation list
|
|
|
|
|
|
|
|
GenericType _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Unit ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
|
|
|
|
collectModuleNamesFromTypeAnnotation : Node TypeAnnotation -> List String
|
|
|
|
collectModuleNamesFromTypeAnnotation node =
|
2019-06-24 13:32:07 +03:00
|
|
|
case Node.value node of
|
2018-11-22 21:19:19 +03:00
|
|
|
FunctionTypeAnnotation a b ->
|
2019-07-25 11:40:23 +03:00
|
|
|
collectModuleNamesFromTypeAnnotation a ++ collectModuleNamesFromTypeAnnotation b
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
Typed nameNode params ->
|
|
|
|
let
|
2019-07-25 11:40:23 +03:00
|
|
|
name : List String
|
2018-11-22 21:19:19 +03:00
|
|
|
name =
|
2019-06-24 13:32:07 +03:00
|
|
|
case Node.value nameNode of
|
2018-11-26 21:09:18 +03:00
|
|
|
( [], str ) ->
|
2019-07-25 11:40:23 +03:00
|
|
|
[]
|
2018-11-26 21:09:18 +03:00
|
|
|
|
|
|
|
( moduleName, _ ) ->
|
2019-07-25 11:40:23 +03:00
|
|
|
[ getModuleName moduleName ]
|
2018-11-22 21:19:19 +03:00
|
|
|
in
|
2019-07-25 11:40:23 +03:00
|
|
|
name ++ List.concatMap collectModuleNamesFromTypeAnnotation params
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
Record list ->
|
|
|
|
list
|
2019-06-24 13:32:07 +03:00
|
|
|
|> List.map (Node.value >> Tuple.second)
|
2019-07-25 11:40:23 +03:00
|
|
|
|> List.concatMap collectModuleNamesFromTypeAnnotation
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
GenericRecord name list ->
|
|
|
|
list
|
2019-06-24 13:32:07 +03:00
|
|
|
|> Node.value
|
|
|
|
|> List.map (Node.value >> Tuple.second)
|
2019-07-25 11:40:23 +03:00
|
|
|
|> List.concatMap collectModuleNamesFromTypeAnnotation
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2018-11-26 21:09:18 +03:00
|
|
|
Tupled list ->
|
2019-07-25 11:40:23 +03:00
|
|
|
List.concatMap collectModuleNamesFromTypeAnnotation list
|
2018-11-26 21:09:18 +03:00
|
|
|
|
|
|
|
GenericType _ ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Unit ->
|
2018-11-22 21:19:19 +03:00
|
|
|
[]
|
|
|
|
|
|
|
|
|
2019-08-19 14:53:35 +03:00
|
|
|
register : VariableInfo -> String -> Context -> Context
|
|
|
|
register variableInfo name context =
|
|
|
|
case variableInfo.variableType of
|
|
|
|
TopLevelVariable ->
|
|
|
|
registerVariable variableInfo name context
|
2019-07-25 11:40:23 +03:00
|
|
|
|
2019-08-19 14:53:35 +03:00
|
|
|
LetVariable ->
|
|
|
|
registerVariable variableInfo name context
|
|
|
|
|
|
|
|
ImportedModule ->
|
|
|
|
registerModule variableInfo name context
|
|
|
|
|
|
|
|
ImportedItem _ ->
|
|
|
|
registerVariable variableInfo name context
|
|
|
|
|
|
|
|
ModuleAlias ->
|
|
|
|
registerModule variableInfo name context
|
|
|
|
|
|
|
|
Type ->
|
|
|
|
registerVariable variableInfo name context
|
|
|
|
|
|
|
|
Port ->
|
|
|
|
registerVariable variableInfo name context
|
|
|
|
|
|
|
|
|
|
|
|
registerModule : VariableInfo -> String -> Context -> Context
|
|
|
|
registerModule variableInfo name context =
|
|
|
|
{ context | declaredModules = Dict.insert name variableInfo context.declaredModules }
|
|
|
|
|
|
|
|
|
|
|
|
registerVariable : VariableInfo -> String -> Context -> Context
|
|
|
|
registerVariable variableInfo name context =
|
|
|
|
let
|
|
|
|
scopes : Nonempty Scope
|
|
|
|
scopes =
|
2019-08-22 13:32:35 +03:00
|
|
|
Nonempty.mapHead
|
2019-08-19 14:53:35 +03:00
|
|
|
(\scope ->
|
|
|
|
{ scope | declared = Dict.insert name variableInfo scope.declared }
|
|
|
|
)
|
|
|
|
context.scopes
|
|
|
|
in
|
|
|
|
{ context | scopes = scopes }
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
|
2018-11-26 13:22:43 +03:00
|
|
|
markAllAsUsed : List String -> Context -> Context
|
2019-06-16 22:12:37 +03:00
|
|
|
markAllAsUsed names context =
|
|
|
|
List.foldl markAsUsed context names
|
2018-11-26 13:22:43 +03:00
|
|
|
|
|
|
|
|
2018-11-22 21:19:19 +03:00
|
|
|
markAsUsed : String -> Context -> Context
|
2019-06-16 22:12:37 +03:00
|
|
|
markAsUsed name context =
|
2018-11-22 21:19:19 +03:00
|
|
|
let
|
2019-07-05 01:33:51 +03:00
|
|
|
scopes : Nonempty Scope
|
2018-11-22 21:19:19 +03:00
|
|
|
scopes =
|
2019-08-22 13:32:35 +03:00
|
|
|
Nonempty.mapHead
|
2018-11-22 21:19:19 +03:00
|
|
|
(\scope ->
|
|
|
|
{ scope | used = Set.insert name scope.used }
|
|
|
|
)
|
2019-06-16 22:12:37 +03:00
|
|
|
context.scopes
|
2018-11-22 21:19:19 +03:00
|
|
|
in
|
2019-06-16 22:12:37 +03:00
|
|
|
{ context | scopes = scopes }
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
|
2019-07-25 11:40:23 +03:00
|
|
|
markAllModulesAsUsed : List String -> Context -> Context
|
|
|
|
markAllModulesAsUsed names context =
|
|
|
|
{ context | usedModules = Set.union (Set.fromList names) context.usedModules }
|
|
|
|
|
|
|
|
|
|
|
|
markModuleAsUsed : String -> Context -> Context
|
|
|
|
markModuleAsUsed name context =
|
|
|
|
{ context | usedModules = Set.insert name context.usedModules }
|
|
|
|
|
|
|
|
|
2018-11-26 13:22:43 +03:00
|
|
|
getModuleName : List String -> String
|
|
|
|
getModuleName name =
|
|
|
|
String.join "." name
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
makeReport : Scope -> ( List (Error {}), List String )
|
2018-11-22 21:19:19 +03:00
|
|
|
makeReport { declared, used } =
|
|
|
|
let
|
2019-06-24 01:32:27 +03:00
|
|
|
nonUsedVars : List String
|
2018-11-22 21:19:19 +03:00
|
|
|
nonUsedVars =
|
|
|
|
Set.diff used (Set.fromList <| Dict.keys declared)
|
|
|
|
|> Set.toList
|
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
errors : List (Error {})
|
2018-11-22 21:19:19 +03:00
|
|
|
errors =
|
|
|
|
Dict.filter (\key _ -> not <| Set.member key used) declared
|
|
|
|
|> Dict.toList
|
2019-08-19 14:53:35 +03:00
|
|
|
|> List.map (\( key, variableInfo ) -> error variableInfo key)
|
2018-11-22 21:19:19 +03:00
|
|
|
in
|
|
|
|
( errors, nonUsedVars )
|
|
|
|
|
|
|
|
|
2019-08-22 13:16:00 +03:00
|
|
|
|
|
|
|
-- RANGE MANIPULATION
|
|
|
|
|
|
|
|
|
|
|
|
{-| Create a new range that starts at the start of the range that starts first,
|
|
|
|
and ends at the end of the range that starts last. If the two ranges are distinct
|
|
|
|
and there is code in between, that code will be included in the resulting range.
|
|
|
|
|
|
|
|
range : Range
|
|
|
|
range =
|
|
|
|
Fix.mergeRanges
|
|
|
|
(Node.range node1)
|
|
|
|
(Node.range node2)
|
|
|
|
|
|
|
|
-}
|
|
|
|
mergeRanges : Range -> Range -> Range
|
|
|
|
mergeRanges a b =
|
|
|
|
let
|
|
|
|
start : { row : Int, column : Int }
|
|
|
|
start =
|
|
|
|
case comparePosition a.start b.start of
|
|
|
|
LT ->
|
|
|
|
a.start
|
|
|
|
|
|
|
|
EQ ->
|
|
|
|
a.start
|
|
|
|
|
|
|
|
GT ->
|
|
|
|
b.start
|
|
|
|
|
|
|
|
end : { row : Int, column : Int }
|
|
|
|
end =
|
|
|
|
case comparePosition a.end b.end of
|
|
|
|
LT ->
|
|
|
|
b.end
|
|
|
|
|
|
|
|
EQ ->
|
|
|
|
b.end
|
|
|
|
|
|
|
|
GT ->
|
|
|
|
a.end
|
|
|
|
in
|
|
|
|
{ start = start, end = end }
|
|
|
|
|
|
|
|
|
|
|
|
{-| Make a range stop at a position. If the position is not inside the range,
|
|
|
|
then the range won't change.
|
|
|
|
|
|
|
|
range : Range
|
|
|
|
range =
|
|
|
|
Fix.rangeUpUntil
|
|
|
|
(Node.range node)
|
|
|
|
(node |> Node.value |> .typeAnnotation |> Node.range |> .start)
|
|
|
|
|
|
|
|
-}
|
|
|
|
rangeUpUntil : Range -> { row : Int, column : Int } -> Range
|
|
|
|
rangeUpUntil range position =
|
|
|
|
let
|
|
|
|
positionAsInt_ : Int
|
|
|
|
positionAsInt_ =
|
|
|
|
positionAsInt position
|
|
|
|
in
|
|
|
|
if positionAsInt range.start <= positionAsInt_ && positionAsInt range.end >= positionAsInt_ then
|
|
|
|
{ range | end = position }
|
|
|
|
|
|
|
|
else
|
|
|
|
range
|
|
|
|
|
|
|
|
|
|
|
|
positionAsInt : { row : Int, column : Int } -> Int
|
|
|
|
positionAsInt { row, column } =
|
|
|
|
-- This is a quick and simple heuristic to be able to sort ranges.
|
|
|
|
-- It is entirely based on the assumption that no line is longer than
|
|
|
|
-- 1.000.000 characters long. Then, as long as ranges don't overlap,
|
|
|
|
-- this should work fine.
|
|
|
|
row * 1000000 + column
|
|
|
|
|
|
|
|
|
|
|
|
comparePosition : { row : Int, column : Int } -> { row : Int, column : Int } -> Order
|
|
|
|
comparePosition a b =
|
|
|
|
let
|
|
|
|
order : Order
|
|
|
|
order =
|
|
|
|
compare a.row b.row
|
|
|
|
in
|
|
|
|
case order of
|
|
|
|
EQ ->
|
|
|
|
compare a.column b.column
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
order
|