Skip to content

Commit

Permalink
Add fullModuleNameAt and fullModuleNameFor in ModuleNameLookupTable
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed May 1, 2022
1 parent a422d44 commit 3ff9098
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 52 deletions.
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

36 changes: 33 additions & 3 deletions src/Review/ModuleNameLookupTable.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module Review.ModuleNameLookupTable exposing (ModuleNameLookupTable, moduleNameFor, moduleNameAt)
module Review.ModuleNameLookupTable exposing
( ModuleNameLookupTable, moduleNameFor, moduleNameAt
, fullModuleNameFor, fullModuleNameAt
)

{-| Looks up the name of the module a function or type comes from based on the position of the element in the module's AST.
Expand All @@ -14,6 +17,7 @@ doing it yourself.
type or value comes from.
@docs ModuleNameLookupTable, moduleNameFor, moduleNameAt
@docs fullModuleNameFor, fullModuleNameAt
Note: If you have been using [`elm-review-scope`](https://github.com/jfmengels/elm-review-scope) before, you should use this instead.
Expand Down Expand Up @@ -70,10 +74,23 @@ Note: If using a `Range` is easier in your situation than using a `Node`, use [`
-}
moduleNameFor : ModuleNameLookupTable -> Node a -> Maybe ModuleName
moduleNameFor (Internal.ModuleNameLookupTable dict) (Node range _) =
moduleNameFor (Internal.ModuleNameLookupTable _ dict) (Node range _) =
Dict.get (Internal.toRangeLike range) dict


{-| This is the same as [`moduleNameFor`](#moduleNameFor), except that the function will return the current module name
if the type or value was defined in this module, instead of `Just []`.
-}
fullModuleNameFor : ModuleNameLookupTable -> Node a -> Maybe ModuleName
fullModuleNameFor (Internal.ModuleNameLookupTable currentModuleName dict) (Node range _) =
case Dict.get (Internal.toRangeLike range) dict of
Just [] ->
Just currentModuleName

res ->
res


{-| Returns the name of the module the type, value, or operator referred to by this [`Range`](https://package.elm-lang.org/packages/stil4m/elm-syntax/7.2.1/Elm-Syntax-Range#Range).
The function returns `Just []` if the type or value was defined in this module. It returns `Just moduleName` if the Node is among these kinds of AST nodes (and `Nothing` for all the others):
Expand Down Expand Up @@ -105,5 +122,18 @@ Note: If using a `Node` is easier in your situation than using a `Range`, use [`
-}
moduleNameAt : ModuleNameLookupTable -> Range -> Maybe ModuleName
moduleNameAt (Internal.ModuleNameLookupTable dict) range =
moduleNameAt (Internal.ModuleNameLookupTable _ dict) range =
Dict.get (Internal.toRangeLike range) dict


{-| This is the same as [`moduleNameAt`](#moduleNameAt), except that the function will return the current module name
if the type or value was defined in this module, instead of `Just []`.
-}
fullModuleNameAt : ModuleNameLookupTable -> Range -> Maybe ModuleName
fullModuleNameAt (Internal.ModuleNameLookupTable currentModuleName dict) range =
case Dict.get (Internal.toRangeLike range) dict of
Just [] ->
Just currentModuleName

res ->
res
12 changes: 6 additions & 6 deletions src/Review/ModuleNameLookupTable/Internal.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import Elm.Syntax.Range exposing (Range)


type ModuleNameLookupTable
= ModuleNameLookupTable (Dict RangeLike ModuleName)
= ModuleNameLookupTable ModuleName (Dict RangeLike ModuleName)


empty : ModuleNameLookupTable
empty =
ModuleNameLookupTable Dict.empty
empty : ModuleName -> ModuleNameLookupTable
empty currentModuleName =
ModuleNameLookupTable currentModuleName Dict.empty


add : Range -> ModuleName -> ModuleNameLookupTable -> ModuleNameLookupTable
add range moduleName (ModuleNameLookupTable moduleNameLookupTable) =
ModuleNameLookupTable (Dict.insert (toRangeLike range) moduleName moduleNameLookupTable)
add range moduleName (ModuleNameLookupTable currentModuleName moduleNameLookupTable) =
ModuleNameLookupTable currentModuleName (Dict.insert (toRangeLike range) moduleName moduleNameLookupTable)


addMultiple : List ( Range, ModuleName ) -> ModuleNameLookupTable -> ModuleNameLookupTable
Expand Down
10 changes: 5 additions & 5 deletions src/Review/Rule.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ mergeModuleVisitors initialProjectContext maybeModuleContextCreator visitors =
, isInSourceDirectories = True
}
, moduleKey = ModuleKey "dummy"
, moduleNameLookupTable = ModuleNameLookupTableInternal.empty
, moduleNameLookupTable = ModuleNameLookupTableInternal.empty []
, extractSourceCode = always "dummy"
, filePath = "dummy file path"
}
Expand Down Expand Up @@ -4327,7 +4327,7 @@ computeModules projectVisitor ( moduleVisitor, moduleContextCreator ) project ex
, moduleKey = moduleKey
, moduleNameLookupTable =
Dict.get (Review.Project.Internal.getModuleName module_) moduleNameLookupTables
|> Maybe.withDefault ModuleNameLookupTableInternal.empty
|> Maybe.withDefault (ModuleNameLookupTableInternal.empty (Node.value (moduleNameNode module_.ast.moduleDefinition)))
, extractSourceCode =
let
(RequestedData requestedData) =
Expand Down Expand Up @@ -5393,8 +5393,8 @@ scope_initialProjectContext =
}
-}
scope_fromProjectToModule : a -> b -> ScopeProjectContext -> ScopeModuleContext
scope_fromProjectToModule _ _ projectContext =
scope_fromProjectToModule : a -> Node ModuleName -> ScopeProjectContext -> ScopeModuleContext
scope_fromProjectToModule _ moduleName projectContext =
{ scopes = nonemptyList_fromElement emptyScope
, localTypes = Set.empty
, importAliases = Dict.empty
Expand All @@ -5408,7 +5408,7 @@ scope_fromProjectToModule _ _ projectContext =
, exposedAliases = []
, exposedValues = []
, exposedBinops = []
, lookupTable = ModuleNameLookupTableInternal.empty
, lookupTable = ModuleNameLookupTableInternal.empty (Node.value moduleName)
}
|> registerPrelude

Expand Down
Loading

0 comments on commit 3ff9098

Please sign in to comment.