Skip to content

Commit

Permalink
Fix module name lookup table giving a wrong result
Browse files Browse the repository at this point in the history
Let destructuring variables that shadowed imports were not correctly
registered, and looking them up could lead to incorrectly point to
the imported module that was shadowed.

Thanks @matzko for discovering this.
  • Loading branch information
jfmengels committed Apr 9, 2024
1 parent 5573f0a commit 19eda49
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

## [2.13.2] - 2024-04-09

Fixed an issue where the result of a module name lookup for let variables was sometimes incorrect.

## [2.13.1] - 2023-06-17

Fixed an issue where the module name lookup table would yield an incorrect result. [#159](https://github.com/jfmengels/elm-review/pull/159)
Expand Down
10 changes: 5 additions & 5 deletions src/Review/ModuleNameLookupTable/Compute.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ popScopeExit node context =
expressionEnterVisitor : Node Expression -> Context -> Context
expressionEnterVisitor node context =
case Node.value node of
Expression.LetExpression { declarations } ->
Expression.LetExpression letExpression ->
let
newContext : Context
newContext =
Expand Down Expand Up @@ -1246,11 +1246,11 @@ expressionEnterVisitor node context =
in
NonEmpty.mapHead (\scope -> { scope | cases = ( expression, names ) :: scope.cases }) withLetVariable

Expression.LetDestructuring _ _ ->
scopes
Expression.LetDestructuring pattern _ ->
NonEmpty.mapHead (\scope -> { scope | names = collectNamesFromPattern LetVariable [ pattern ] scope.names }) scopes
)
(NonEmpty.cons emptyScope context.scopes)
declarations
letExpression.declarations
|> updateScope context

lookupTable : ModuleNameLookupTable
Expand Down Expand Up @@ -1280,7 +1280,7 @@ expressionEnterVisitor node context =
collectModuleNamesFromPattern newContext [ pattern ] acc
)
newContext.lookupTable
declarations
letExpression.declarations
in
{ newContext | lookupTable = lookupTable }

Expand Down
13 changes: 13 additions & 0 deletions tests/ModuleNameLookupTableTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ a = localValue
("x" </> "y")
(|=)
("pars" |= "er")
(let (exposedElement, _) = 1 in exposedElement)
b = case () of
VariantA -> ()
(ExposesEverything.VariantA as foo) -> foo
Expand Down Expand Up @@ -167,6 +168,7 @@ Cmd.none -> Platform.Cmd.none
<nothing>.</> -> Url.Parser.</>
<nothing>.|= -> Parser.Advanced.|=
<nothing>.|= -> Parser.Advanced.|=
<nothing>.exposedElement -> <nothing>.exposedElement
<nothing>.VariantA -> ExposesEverything.VariantA
ExposesEverything.VariantA -> ExposesEverything.VariantA
ExposesEverythingAlias.VariantA -> ExposesEverything.VariantA
Expand Down Expand Up @@ -316,6 +318,7 @@ a = localValue
("x" </> "y")
(|=)
("pars" |= "er")
(let (exposedElement, _) = 1 in exposedElement)
b = case () of
VariantA -> ()
(ExposesEverything.VariantA as foo) -> foo
Expand Down Expand Up @@ -378,6 +381,7 @@ Cmd.none -> Platform.Cmd.none
<nothing>.</> -> Url.Parser.</>
<nothing>.|= -> Parser.Advanced.|=
<nothing>.|= -> Parser.Advanced.|=
<nothing>.exposedElement -> Abc.Xyz.exposedElement
<nothing>.VariantA -> ExposesEverything.VariantA
ExposesEverything.VariantA -> ExposesEverything.VariantA
ExposesEverythingAlias.VariantA -> ExposesEverything.VariantA
Expand Down Expand Up @@ -698,6 +702,15 @@ collectPatterns lookupFunction context node =
Pattern.VarPattern _ ->
[]

Pattern.TuplePattern subPatterns ->
List.concatMap (collectPatterns lookupFunction context) subPatterns

Pattern.RecordPattern subPatterns ->
List.map Node.value subPatterns

Pattern.AllPattern ->
[]

_ ->
Debug.todo ("Other patterns in case expressions are not handled: " ++ Debug.toString node)

Expand Down

0 comments on commit 19eda49

Please sign in to comment.