Add ModuleNameLookupTable support for operators (#106)

This commit is contained in:
SiriusStarr 2021-10-03 02:03:00 -07:00 committed by GitHub
parent 8c8e708403
commit 2a8dbaf2a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 4 deletions

File diff suppressed because one or more lines are too long

View File

@ -33,11 +33,13 @@ type alias ModuleNameLookupTable =
Internal.ModuleNameLookupTable
{-| Returns the name of the module the type or value referred to by this [`Node`](https://package.elm-lang.org/packages/stil4m/elm-syntax/7.2.1/Elm-Syntax-Node#Node).
{-| Returns the name of the module the type, value, or operator referred to by this [`Node`](https://package.elm-lang.org/packages/stil4m/elm-syntax/7.2.1/Elm-Syntax-Node#Node) was defined in.
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):
- `Expression.FunctionOrValue`
- `Expression.PrefixOperator`
- `Expression.OperatorApplication`
- `nodeForTheName` in `Expression.RecordUpdateExpression nodeForTheName modifiers`
- `nodeForTheName` in `TypeAnnotation.Typed nodeForTheName args`
- `Pattern.NamedPattern`

View File

@ -5278,7 +5278,7 @@ registerImportExposed import_ innerContext =
|> Dict.fromList
in
{ innerContext
| importedFunctions = Dict.union innerContext.importedFunctions exposedValues
| importedFunctions = Dict.union exposedValues innerContext.importedFunctions
, importedTypes = Dict.union innerContext.importedTypes exposedTypes
}
@ -5299,7 +5299,7 @@ registerImportExposed import_ innerContext =
|> Dict.fromList
in
{ innerContext
| importedFunctions = Dict.union innerContext.importedFunctions exposedValues
| importedFunctions = Dict.union exposedValues innerContext.importedFunctions
, importedTypes = Dict.union innerContext.importedTypes exposedTypes
}
@ -5659,6 +5659,24 @@ scope_expressionEnterVisitor node context =
context.lookupTable
}
Expression.PrefixOperator op ->
{ context
| lookupTable =
ModuleNameLookupTableInternal.add
(Node.range node)
(moduleNameForValue context op [])
context.lookupTable
}
Expression.OperatorApplication op _ _ _ ->
{ context
| lookupTable =
ModuleNameLookupTableInternal.add
(Node.range node)
(moduleNameForValue context op [])
context.lookupTable
}
_ ->
context

View File

@ -12,6 +12,7 @@ import Review.ModuleNameLookupTable as ModuleNameLookupTable exposing (ModuleNam
import Review.Project as Project exposing (Project)
import Review.Rule as Rule exposing (Error, Rule)
import Review.Test
import Review.Test.Dependencies
import Test exposing (Test, test)
@ -38,6 +39,10 @@ import Html exposing (..)
import Http exposing (get)
import Something.B as Something
import Something.C as Something
import Url.Parser exposing (..)
-- NOTE: The behavior of the compiler if duplicate infix operators are imported is for the second-imported one to overwrite the first.
import Parser exposing ((|=))
import Parser.Advanced exposing ((|=))
localValue = 1
localValueValueToBeShadowed = 1
@ -68,6 +73,12 @@ a = localValue
True
Just
Cmd.none
(+)
(117 + 3)
(<?>)
("x" </> "y")
(|=)
("pars" |= "er")
b = case () of
VariantA -> ()
(ExposesEverything.VariantA as foo) -> foo
@ -124,6 +135,12 @@ Http.get -> Http.get
<nothing>.True -> Basics.True
<nothing>.Just -> Maybe.Just
Cmd.none -> Platform.Cmd.none
<nothing>.+ -> Basics.+
<nothing>.+ -> Basics.+
<nothing>.<?> -> Url.Parser.<?>
<nothing>.</> -> Url.Parser.</>
<nothing>.|= -> Parser.Advanced.|=
<nothing>.|= -> Parser.Advanced.|=
<nothing>.VariantA -> ExposesEverything.VariantA
ExposesEverything.VariantA -> ExposesEverything.VariantA
ExposesEverythingAlias.VariantA -> ExposesEverything.VariantA
@ -212,6 +229,8 @@ project =
Project.new
|> Project.addDependency Dependencies.elmCore
|> Project.addDependency Dependencies.elmHtml
|> Project.addDependency Review.Test.Dependencies.elmParser
|> Project.addDependency Review.Test.Dependencies.elmUrl
createRule : (Rule.ModuleRuleSchema {} ModuleContext -> Rule.ModuleRuleSchema { hasAtLeastOneVisitor : () } ModuleContext) -> Rule
@ -242,6 +261,12 @@ expressionVisitor node context =
Expression.RecordUpdateExpression (Node range name) _ ->
( [], { context | texts = context.texts ++ [ getRealName context [] range name ] } )
Expression.PrefixOperator op ->
( [], { context | texts = context.texts ++ [ getRealName context [] (Node.range node) op ] } )
Expression.OperatorApplication op _ _ _ ->
( [], { context | texts = context.texts ++ [ getRealName context [] (Node.range node) op ] } )
Expression.CaseExpression { cases } ->
let
texts : List String