2020-04-03 16:50:37 +03:00
|
|
|
module NoUnused.Variables exposing (rule)
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2020-04-03 16:50:37 +03:00
|
|
|
{-| Report variables or types that are declared or imported but never used inside of a module.
|
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)
|
2020-05-14 22:01:53 +03:00
|
|
|
import Elm.Syntax.Declaration as Declaration exposing (Declaration)
|
|
|
|
import Elm.Syntax.Exposing as Exposing exposing (Exposing)
|
|
|
|
import Elm.Syntax.Expression as Expression exposing (Expression, Function, FunctionImplementation)
|
2018-11-22 21:19:19 +03:00
|
|
|
import Elm.Syntax.Import exposing (Import)
|
2020-05-14 22:01:53 +03:00
|
|
|
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)
|
2020-05-14 22:01:53 +03:00
|
|
|
import Elm.Syntax.TypeAnnotation as TypeAnnotation exposing (TypeAnnotation)
|
2020-04-03 16:50:37 +03:00
|
|
|
import NoUnused.NonemptyList as NonemptyList exposing (Nonempty)
|
2020-04-04 13:58:26 +03:00
|
|
|
import Review.Fix as Fix exposing (Fix)
|
2019-09-29 00:47:02 +03:00
|
|
|
import Review.Rule as Rule exposing (Direction, Error, Rule)
|
2018-11-22 21:19:19 +03:00
|
|
|
import Set exposing (Set)
|
|
|
|
|
|
|
|
|
2020-03-27 22:48:27 +03:00
|
|
|
{-| Report variables or types that are declared or imported but never used.
|
|
|
|
|
2019-06-26 01:34:57 +03:00
|
|
|
config =
|
2020-03-27 22:48:27 +03:00
|
|
|
[ NoUnused.Variables.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-03-27 22:48:27 +03:00
|
|
|
Rule.newModuleRuleSchema "NoUnused.Variables" 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
|
2020-03-27 22:48:27 +03:00
|
|
|
, inTheDeclarationOf : Maybe String
|
2019-06-24 01:32:27 +03:00
|
|
|
, 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
|
2020-03-27 22:48:27 +03:00
|
|
|
| ModuleAlias { originalNameOfTheImport : String, exposesSomething : Bool }
|
2018-11-26 20:39:46 +03:00
|
|
|
| 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 =
|
2020-03-27 22:48:27 +03:00
|
|
|
{ scopes = NonemptyList.fromElement emptyScope
|
|
|
|
, inTheDeclarationOf = Nothing
|
2019-06-24 01:32:27 +03:00
|
|
|
, 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-27 22:48:27 +03:00
|
|
|
error : Dict String VariableInfo -> VariableInfo -> String -> Error {}
|
|
|
|
error declaredModules variableInfo name =
|
2020-04-04 13:58:26 +03:00
|
|
|
Rule.errorWithFix
|
2020-03-27 22:48:27 +03:00
|
|
|
{ message = variableTypeToString variableInfo.variableType ++ " `" ++ name ++ "` is not used" ++ variableTypeWarning variableInfo.variableType
|
2020-04-01 23:40:23 +03:00
|
|
|
, details = [ "You should either use this value somewhere, or remove it at the location I pointed at." ]
|
2019-08-04 16:47:33 +03:00
|
|
|
}
|
2020-03-27 22:48:27 +03:00
|
|
|
variableInfo.under
|
2020-04-04 13:58:26 +03:00
|
|
|
(fix declaredModules variableInfo)
|
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"
|
|
|
|
|
2020-03-27 22:48:27 +03:00
|
|
|
ModuleAlias _ ->
|
2018-11-26 20:39:46 +03:00
|
|
|
"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
|
|
|
""
|
|
|
|
|
2020-03-27 22:48:27 +03:00
|
|
|
ModuleAlias _ ->
|
2018-11-26 21:09:18 +03:00
|
|
|
""
|
|
|
|
|
|
|
|
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-04-04 13:58:26 +03:00
|
|
|
fix : Dict String VariableInfo -> VariableInfo -> List Fix
|
|
|
|
fix declaredModules { variableType, rangeToRemove } =
|
2020-03-27 22:48:27 +03:00
|
|
|
let
|
|
|
|
shouldOfferFix : Bool
|
|
|
|
shouldOfferFix =
|
|
|
|
case variableType of
|
|
|
|
TopLevelVariable ->
|
|
|
|
True
|
|
|
|
|
|
|
|
LetVariable ->
|
|
|
|
True
|
|
|
|
|
|
|
|
ImportedModule ->
|
|
|
|
True
|
|
|
|
|
|
|
|
ImportedItem _ ->
|
|
|
|
True
|
|
|
|
|
|
|
|
ModuleAlias { originalNameOfTheImport, exposesSomething } ->
|
|
|
|
not exposesSomething
|
|
|
|
|| not (Dict.member originalNameOfTheImport declaredModules)
|
|
|
|
|
|
|
|
Type ->
|
|
|
|
True
|
|
|
|
|
|
|
|
Port ->
|
|
|
|
True
|
|
|
|
in
|
|
|
|
if shouldOfferFix then
|
2020-04-04 13:58:26 +03:00
|
|
|
[ Fix.removeRange rangeToRemove ]
|
2020-03-27 22:48:27 +03:00
|
|
|
|
|
|
|
else
|
2020-04-04 13:58:26 +03:00
|
|
|
[]
|
2020-03-27 22:48:27 +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
|
2020-05-14 22:01:53 +03:00
|
|
|
Exposing.All _ ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], { context | exposesEverything = True } )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
Exposing.Explicit list ->
|
2018-11-22 21:19:19 +03:00
|
|
|
let
|
|
|
|
names =
|
|
|
|
List.filterMap
|
2019-08-05 01:16:39 +03:00
|
|
|
(\(Node _ node) ->
|
|
|
|
case node of
|
2020-05-14 22:01:53 +03:00
|
|
|
Exposing.FunctionExpose name ->
|
2018-11-22 21:19:19 +03:00
|
|
|
Just name
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
Exposing.TypeOrAliasExpose name ->
|
2018-11-22 21:19:19 +03:00
|
|
|
Just name
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
Exposing.TypeExpose { name } ->
|
2018-11-22 21:19:19 +03:00
|
|
|
Just name
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
Exposing.InfixExpose name ->
|
2018-11-22 21:19:19 +03:00
|
|
|
-- 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-03-27 22:48:27 +03:00
|
|
|
importVisitor : Node Import -> Context -> ( List (Error {}), Context )
|
2020-05-14 22:01:53 +03:00
|
|
|
importVisitor ((Node _ import_) as node) context =
|
|
|
|
let
|
|
|
|
errors : List (Error {})
|
|
|
|
errors =
|
|
|
|
case import_.moduleAlias of
|
|
|
|
Just moduleAlias ->
|
|
|
|
if Node.value moduleAlias == Node.value import_.moduleName then
|
|
|
|
[ Rule.errorWithFix
|
|
|
|
{ message = "Module `Html` is aliased as `Html`"
|
|
|
|
, details = [ "The alias is the same as the module name, and brings no useful value" ]
|
|
|
|
}
|
|
|
|
(Node.range moduleAlias)
|
|
|
|
[ Fix.removeRange <| moduleAliasRange node (Node.range moduleAlias) ]
|
|
|
|
]
|
|
|
|
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
[]
|
|
|
|
in
|
|
|
|
case import_.exposingList of
|
2018-11-26 20:12:36 +03:00
|
|
|
Nothing ->
|
2020-05-14 22:01:53 +03:00
|
|
|
( errors, registerModuleNameOrAlias node context )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2018-11-26 20:12:36 +03:00
|
|
|
Just declaredImports ->
|
2020-05-14 22:01:53 +03:00
|
|
|
( errors
|
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_)
|
2020-03-27 22:48:27 +03:00
|
|
|
(registerModuleAlias node context)
|
2018-11-26 20:12:36 +03:00
|
|
|
(collectFromExposing declaredImports)
|
|
|
|
)
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
|
2020-03-27 22:48:27 +03:00
|
|
|
registerModuleNameOrAlias : Node Import -> Context -> Context
|
|
|
|
registerModuleNameOrAlias ((Node range { exposingList, moduleAlias, moduleName }) as node) context =
|
|
|
|
case moduleAlias of
|
|
|
|
Just _ ->
|
|
|
|
registerModuleAlias node context
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
register
|
|
|
|
{ variableType = ImportedModule
|
|
|
|
, under = Node.range moduleName
|
|
|
|
, rangeToRemove = untilStartOfNextLine range
|
|
|
|
}
|
|
|
|
(getModuleName <| Node.value moduleName)
|
|
|
|
context
|
|
|
|
|
|
|
|
|
|
|
|
registerModuleAlias : Node Import -> Context -> Context
|
|
|
|
registerModuleAlias ((Node range { exposingList, moduleAlias, moduleName }) as node) context =
|
|
|
|
case moduleAlias of
|
|
|
|
Just moduleAlias_ ->
|
|
|
|
register
|
|
|
|
{ variableType =
|
|
|
|
ModuleAlias
|
|
|
|
{ originalNameOfTheImport = getModuleName <| Node.value moduleName
|
|
|
|
, exposesSomething = exposingList /= Nothing
|
|
|
|
}
|
|
|
|
, under = Node.range moduleAlias_
|
|
|
|
, rangeToRemove =
|
|
|
|
case exposingList of
|
|
|
|
Nothing ->
|
|
|
|
untilStartOfNextLine range
|
|
|
|
|
|
|
|
Just _ ->
|
|
|
|
moduleAliasRange node (Node.range moduleAlias_)
|
|
|
|
}
|
|
|
|
(getModuleName <| Node.value moduleAlias_)
|
|
|
|
context
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
context
|
|
|
|
|
|
|
|
|
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
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnEnter, Expression.FunctionOrValue [] name ) ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], markAsUsed name context )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnEnter, Expression.FunctionOrValue moduleName name ) ->
|
2019-07-25 11:40:23 +03:00
|
|
|
( [], markModuleAsUsed (getModuleName moduleName) context )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnEnter, Expression.OperatorApplication name _ _ _ ) ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], markAsUsed name context )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnEnter, Expression.PrefixOperator name ) ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], markAsUsed name context )
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnEnter, Expression.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
|
2020-05-14 22:01:53 +03:00
|
|
|
Expression.LetFunction function ->
|
2020-03-27 22:48:27 +03:00
|
|
|
let
|
|
|
|
namesUsedInArgumentPatterns : { types : List String, modules : List String }
|
|
|
|
namesUsedInArgumentPatterns =
|
|
|
|
function.declaration
|
|
|
|
|> Node.value
|
|
|
|
|> .arguments
|
|
|
|
|> List.map getUsedVariablesFromPattern
|
|
|
|
|> foldUsedTypesAndModules
|
|
|
|
in
|
|
|
|
context_
|
|
|
|
|> registerFunction letBlockContext function
|
|
|
|
|> markUsedTypesAndModules namesUsedInArgumentPatterns
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
Expression.LetDestructuring pattern _ ->
|
2019-06-16 22:12:37 +03:00
|
|
|
context_
|
2018-11-26 13:22:43 +03:00
|
|
|
)
|
2020-03-27 22:48:27 +03:00
|
|
|
{ context | scopes = NonemptyList.cons emptyScope context.scopes }
|
2018-11-26 13:22:43 +03:00
|
|
|
declarations
|
|
|
|
in
|
|
|
|
( [], newContext )
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnEnter, Expression.LambdaExpression { args } ) ->
|
2020-03-27 22:48:27 +03:00
|
|
|
let
|
|
|
|
namesUsedInArgumentPatterns : { types : List String, modules : List String }
|
|
|
|
namesUsedInArgumentPatterns =
|
|
|
|
args
|
|
|
|
|> List.map getUsedVariablesFromPattern
|
|
|
|
|> foldUsedTypesAndModules
|
|
|
|
in
|
|
|
|
( [], markUsedTypesAndModules namesUsedInArgumentPatterns context )
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnExit, Expression.RecordUpdateExpression expr _ ) ->
|
2019-07-02 12:16:03 +03:00
|
|
|
( [], markAsUsed (Node.value expr) context )
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnExit, Expression.CaseExpression { cases } ) ->
|
2019-07-25 01:19:43 +03:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnExit, Expression.LetExpression _ ) ->
|
2018-11-26 13:22:43 +03:00
|
|
|
let
|
|
|
|
( errors, remainingUsed ) =
|
2020-03-27 22:48:27 +03:00
|
|
|
makeReport (NonemptyList.head context.scopes)
|
2018-11-26 13:22:43 +03:00
|
|
|
|
2019-06-16 22:12:37 +03:00
|
|
|
contextWithPoppedScope =
|
2020-03-27 22:48:27 +03:00
|
|
|
{ context | scopes = NonemptyList.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
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnEnter, Declaration.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
|
|
|
|
2020-03-27 22:48:27 +03:00
|
|
|
namesUsedInArgumentPatterns : { types : List String, modules : List String }
|
|
|
|
namesUsedInArgumentPatterns =
|
|
|
|
function.declaration
|
|
|
|
|> Node.value
|
|
|
|
|> .arguments
|
|
|
|
|> List.map getUsedVariablesFromPattern
|
|
|
|
|> foldUsedTypesAndModules
|
|
|
|
|
2019-07-05 01:33:51 +03:00
|
|
|
newContext : Context
|
2018-11-22 21:19:19 +03:00
|
|
|
newContext =
|
2020-03-27 22:48:27 +03:00
|
|
|
{ context | inTheDeclarationOf = Just <| Node.value functionImplementation.name }
|
2019-08-19 14:53:35 +03:00
|
|
|
|> register
|
|
|
|
{ variableType = TopLevelVariable
|
|
|
|
, under = Node.range functionImplementation.name
|
2020-03-27 22:48:27 +03:00
|
|
|
, rangeToRemove = rangeToRemoveForNodeWithDocumentation node function.documentation
|
2019-08-19 14:53:35 +03:00
|
|
|
}
|
|
|
|
(Node.value functionImplementation.name)
|
2019-07-25 11:40:23 +03:00
|
|
|
|> markUsedTypesAndModules namesUsedInSignature
|
2020-03-27 22:48:27 +03:00
|
|
|
|> markUsedTypesAndModules namesUsedInArgumentPatterns
|
2018-11-22 21:19:19 +03:00
|
|
|
in
|
|
|
|
( [], newContext )
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnEnter, Declaration.CustomTypeDeclaration { name, documentation, constructors } ) ->
|
2019-07-02 12:42:07 +03:00
|
|
|
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
|
2020-03-27 22:48:27 +03:00
|
|
|
, rangeToRemove = rangeToRemoveForNodeWithDocumentation node documentation
|
2019-08-19 14:53:35 +03:00
|
|
|
}
|
|
|
|
(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
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnEnter, Declaration.AliasDeclaration { name, typeAnnotation, documentation } ) ->
|
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
|
2020-03-27 22:48:27 +03:00
|
|
|
, rangeToRemove = rangeToRemoveForNodeWithDocumentation node documentation
|
2019-08-19 14:53:35 +03:00
|
|
|
}
|
|
|
|
(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
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnEnter, Declaration.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
|
|
|
)
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnEnter, Declaration.InfixDeclaration _ ) ->
|
2019-06-16 22:12:37 +03:00
|
|
|
( [], context )
|
2018-11-26 21:09:18 +03:00
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
( Rule.OnEnter, Declaration.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-27 22:48:27 +03:00
|
|
|
rangeToRemoveForNodeWithDocumentation : Node Declaration -> Maybe (Node a) -> Range
|
|
|
|
rangeToRemoveForNodeWithDocumentation (Node nodeRange _) documentation =
|
|
|
|
case documentation of
|
|
|
|
Nothing ->
|
|
|
|
untilStartOfNextLine nodeRange
|
|
|
|
|
|
|
|
Just (Node documentationRange _) ->
|
|
|
|
untilStartOfNextLine
|
|
|
|
{ start = documentationRange.start
|
|
|
|
, end = nodeRange.end
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 =
|
2020-03-27 22:48:27 +03:00
|
|
|
NonemptyList.head context.scopes
|
2019-07-05 01:33:51 +03:00
|
|
|
|
|
|
|
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
|
2020-03-27 22:48:27 +03:00
|
|
|
|> List.map (\( key, variableInfo ) -> error context.declaredModules 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
|
2020-05-14 22:01:53 +03:00
|
|
|
Exposing.All _ ->
|
2018-11-26 13:22:43 +03:00
|
|
|
[]
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
Exposing.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
|
2020-05-14 22:01:53 +03:00
|
|
|
Exposing.FunctionExpose name ->
|
|
|
|
Just
|
|
|
|
( name
|
|
|
|
, { variableType = ImportedItem ImportedVariable
|
|
|
|
, under = untilEndOfVariable name range
|
|
|
|
, rangeToRemove = rangeToRemove
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
Exposing.InfixExpose name ->
|
|
|
|
Just
|
|
|
|
( name
|
|
|
|
, { variableType = ImportedItem ImportedOperator
|
|
|
|
, under = untilEndOfVariable name range
|
|
|
|
, rangeToRemove = rangeToRemove
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
Exposing.TypeOrAliasExpose name ->
|
|
|
|
Just
|
|
|
|
( name
|
|
|
|
, { variableType = ImportedItem ImportedType
|
|
|
|
, under = untilEndOfVariable name range
|
|
|
|
, rangeToRemove = rangeToRemove
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
Exposing.TypeExpose { name, open } ->
|
2019-08-19 14:53:35 +03:00
|
|
|
case open of
|
|
|
|
Just openRange ->
|
2020-03-27 22:48:27 +03:00
|
|
|
-- TODO Change this behavior once we know the contents of the open range, using dependencies or the interfaces of the other modules
|
2019-08-19 14:53:35 +03:00
|
|
|
Nothing
|
|
|
|
|
|
|
|
Nothing ->
|
2020-05-14 22:01:53 +03:00
|
|
|
Just
|
|
|
|
( name
|
|
|
|
, { variableType = ImportedItem ImportedType
|
|
|
|
, under = range
|
|
|
|
, rangeToRemove = rangeToRemove
|
|
|
|
}
|
|
|
|
)
|
2019-08-19 14:53:35 +03:00
|
|
|
)
|
|
|
|
|> List.filterMap identity
|
2018-11-26 13:22:43 +03:00
|
|
|
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
untilEndOfVariable : String -> Range -> Range
|
|
|
|
untilEndOfVariable name range =
|
|
|
|
if range.start.row == range.end.row then
|
|
|
|
range
|
|
|
|
|
|
|
|
else
|
|
|
|
{ range | end = { row = range.start.row, column = range.start.column + String.length name } }
|
|
|
|
|
|
|
|
|
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
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.FunctionTypeAnnotation a b ->
|
2019-07-25 11:40:23 +03:00
|
|
|
collectTypesFromTypeAnnotation a ++ collectTypesFromTypeAnnotation b
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.Typed nameNode params ->
|
2019-07-25 11:40:23 +03:00
|
|
|
let
|
|
|
|
name : List String
|
|
|
|
name =
|
|
|
|
case Node.value nameNode of
|
|
|
|
( [], str ) ->
|
|
|
|
[ str ]
|
|
|
|
|
|
|
|
( moduleName, _ ) ->
|
|
|
|
[]
|
|
|
|
in
|
|
|
|
name ++ List.concatMap collectTypesFromTypeAnnotation params
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.Record list ->
|
2019-07-25 11:40:23 +03:00
|
|
|
list
|
|
|
|
|> List.map (Node.value >> Tuple.second)
|
|
|
|
|> List.concatMap collectTypesFromTypeAnnotation
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.GenericRecord name list ->
|
2019-07-25 11:40:23 +03:00
|
|
|
list
|
|
|
|
|> Node.value
|
|
|
|
|> List.map (Node.value >> Tuple.second)
|
|
|
|
|> List.concatMap collectTypesFromTypeAnnotation
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.Tupled list ->
|
2019-07-25 11:40:23 +03:00
|
|
|
List.concatMap collectTypesFromTypeAnnotation list
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.GenericType _ ->
|
2019-07-25 11:40:23 +03:00
|
|
|
[]
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.Unit ->
|
2019-07-25 11:40:23 +03:00
|
|
|
[]
|
|
|
|
|
|
|
|
|
|
|
|
collectModuleNamesFromTypeAnnotation : Node TypeAnnotation -> List String
|
|
|
|
collectModuleNamesFromTypeAnnotation node =
|
2019-06-24 13:32:07 +03:00
|
|
|
case Node.value node of
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.FunctionTypeAnnotation a b ->
|
2019-07-25 11:40:23 +03:00
|
|
|
collectModuleNamesFromTypeAnnotation a ++ collectModuleNamesFromTypeAnnotation b
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.Typed nameNode params ->
|
2018-11-22 21:19:19 +03:00
|
|
|
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
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.Record list ->
|
2018-11-22 21:19:19 +03:00
|
|
|
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
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.GenericRecord name list ->
|
2018-11-22 21:19:19 +03:00
|
|
|
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
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.Tupled list ->
|
2019-07-25 11:40:23 +03:00
|
|
|
List.concatMap collectModuleNamesFromTypeAnnotation list
|
2018-11-26 21:09:18 +03:00
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.GenericType _ ->
|
2018-11-26 21:09:18 +03:00
|
|
|
[]
|
|
|
|
|
2020-05-14 22:01:53 +03:00
|
|
|
TypeAnnotation.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 ->
|
2020-03-27 22:48:27 +03:00
|
|
|
-- The main function is "exposed" by default
|
|
|
|
if name == "main" then
|
|
|
|
context
|
|
|
|
|
|
|
|
else
|
|
|
|
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
|
|
|
|
|
2020-03-27 22:48:27 +03:00
|
|
|
ModuleAlias _ ->
|
2019-08-19 14:53:35 +03:00
|
|
|
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 =
|
2020-03-27 22:48:27 +03:00
|
|
|
NonemptyList.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 =
|
2020-03-27 22:48:27 +03:00
|
|
|
if context.inTheDeclarationOf == Just name then
|
|
|
|
context
|
|
|
|
|
|
|
|
else
|
|
|
|
let
|
|
|
|
scopes : Nonempty Scope
|
|
|
|
scopes =
|
|
|
|
NonemptyList.mapHead
|
|
|
|
(\scope ->
|
|
|
|
{ scope | used = Set.insert name scope.used }
|
|
|
|
)
|
|
|
|
context.scopes
|
|
|
|
in
|
|
|
|
{ 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
|
2020-03-27 22:48:27 +03:00
|
|
|
|> List.map (\( key, variableInfo ) -> error Dict.empty variableInfo key)
|
2018-11-22 21:19:19 +03:00
|
|
|
in
|
|
|
|
( errors, nonUsedVars )
|
|
|
|
|
|
|
|
|
2019-08-22 13:16:00 +03:00
|
|
|
|
|
|
|
-- RANGE MANIPULATION
|
|
|
|
|
|
|
|
|
2020-03-27 22:48:27 +03:00
|
|
|
{-| Include everything until the line after the end.
|
|
|
|
-}
|
|
|
|
untilStartOfNextLine : Range -> Range
|
|
|
|
untilStartOfNextLine range =
|
|
|
|
if range.end.column == 1 then
|
|
|
|
range
|
|
|
|
|
|
|
|
else
|
|
|
|
{ range | end = { row = range.end.row + 1, column = 1 } }
|
|
|
|
|
|
|
|
|
2019-08-22 13:16:00 +03:00
|
|
|
{-| 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 =
|
2020-03-27 22:48:27 +03:00
|
|
|
rangeUpUntil
|
2019-08-22 13:16:00 +03:00
|
|
|
(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
|