Add Exception import when needed.

This commit is contained in:
Dillon Kearns 2023-01-02 11:31:59 -08:00
parent b1afe428e4
commit 0e4c33e01b
2 changed files with 43 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import Elm.Syntax.Expression as Expression exposing (Expression)
import Elm.Syntax.Import exposing (Import) import Elm.Syntax.Import exposing (Import)
import Elm.Syntax.ModuleName exposing (ModuleName) import Elm.Syntax.ModuleName exposing (ModuleName)
import Elm.Syntax.Node as Node exposing (Node) import Elm.Syntax.Node as Node exposing (Node)
import Elm.Syntax.Range exposing (Range)
import Review.Fix import Review.Fix
import Review.ModuleNameLookupTable as ModuleNameLookupTable exposing (ModuleNameLookupTable) import Review.ModuleNameLookupTable as ModuleNameLookupTable exposing (ModuleNameLookupTable)
import Review.Rule as Rule exposing (Error, Rule) import Review.Rule as Rule exposing (Error, Rule)
@ -15,6 +16,7 @@ import Review.Rule as Rule exposing (Error, Rule)
type alias Context = type alias Context =
{ lookupTable : ModuleNameLookupTable { lookupTable : ModuleNameLookupTable
, importContext : Dict (List String) ImportContext , importContext : Dict (List String) ImportContext
, firstImport : Maybe Range
} }
@ -66,15 +68,39 @@ rule =
|> Rule.withExpressionEnterVisitor expressionVisitor |> Rule.withExpressionEnterVisitor expressionVisitor
|> Rule.withDeclarationEnterVisitor declarationVisitor |> Rule.withDeclarationEnterVisitor declarationVisitor
|> Rule.withImportVisitor importVisitor |> Rule.withImportVisitor importVisitor
|> Rule.withFinalModuleEvaluation finalEvaluation
|> Rule.fromModuleRuleSchema |> Rule.fromModuleRuleSchema
finalEvaluation : Context -> List (Rule.Error {})
finalEvaluation context =
case Dict.get [ "Exception" ] context.importContext of
Nothing ->
let
importAddRange : { start : { row : Int, column : Int }, end : { row : Int, column : Int } }
importAddRange =
context.firstImport |> Maybe.withDefault { start = { row = 0, column = 0 }, end = { row = 0, column = 0 } }
in
[ Rule.errorWithFix
{ message = "Codemod"
, details = [ "" ]
}
importAddRange
[ Review.Fix.insertAt importAddRange.end "\nimport Exception\n"
]
]
_ ->
[]
initialContext : Rule.ContextCreator () Context initialContext : Rule.ContextCreator () Context
initialContext = initialContext =
Rule.initContextCreator Rule.initContextCreator
(\lookupTable () -> (\lookupTable () ->
{ lookupTable = lookupTable { lookupTable = lookupTable
, importContext = Dict.empty , importContext = Dict.empty
, firstImport = Nothing
} }
) )
|> Rule.withModuleNameLookupTable |> Rule.withModuleNameLookupTable
@ -91,6 +117,7 @@ importVisitor node context =
, { context , { context
| importContext = | importContext =
context.importContext |> Dict.insert key value context.importContext |> Dict.insert key value
, firstImport = context.firstImport |> Maybe.withDefault (Node.range node) |> Just
} }
) )

View File

@ -15,6 +15,7 @@ all =
import Server.Request as Request import Server.Request as Request
import BackendTask exposing (BackendTask) import BackendTask exposing (BackendTask)
import Exception
import RouteBuilder exposing (Page, StaticPayload, single) import RouteBuilder exposing (Page, StaticPayload, single)
import Pages.PageUrl exposing (PageUrl) import Pages.PageUrl exposing (PageUrl)
import Pages.Url import Pages.Url
@ -69,6 +70,7 @@ data =
import Server.Request as Request import Server.Request as Request
import BackendTask exposing (BackendTask) import BackendTask exposing (BackendTask)
import Exception
import RouteBuilder exposing (Page, StaticPayload, single) import RouteBuilder exposing (Page, StaticPayload, single)
import Pages.PageUrl exposing (PageUrl) import Pages.PageUrl exposing (PageUrl)
import Pages.Url import Pages.Url
@ -113,6 +115,7 @@ data =
"""module Route.Index exposing (Data, Model, Msg, route) """module Route.Index exposing (Data, Model, Msg, route)
import Server.Request as Request import Server.Request as Request
import Exception
import BackendTask as DS import BackendTask as DS
import RouteBuilder exposing (Page, StaticPayload, single) import RouteBuilder exposing (Page, StaticPayload, single)
import Pages.PageUrl exposing (PageUrl) import Pages.PageUrl exposing (PageUrl)
@ -166,6 +169,7 @@ data =
"""module Route.Index exposing (Data, Model, Msg, route) """module Route.Index exposing (Data, Model, Msg, route)
import Server.Request as Request import Server.Request as Request
import Exception
import BackendTask as DS import BackendTask as DS
import RouteBuilder exposing (Page, StaticPayload, single) import RouteBuilder exposing (Page, StaticPayload, single)
import Pages.PageUrl exposing (PageUrl) import Pages.PageUrl exposing (PageUrl)
@ -213,6 +217,7 @@ data =
import Server.Request as Request import Server.Request as Request
import BackendTask exposing (BackendTask) import BackendTask exposing (BackendTask)
import Exception
import RouteBuilder exposing (Page, StaticPayload) import RouteBuilder exposing (Page, StaticPayload)
import Pages.PageUrl exposing (PageUrl) import Pages.PageUrl exposing (PageUrl)
import Pages.Url import Pages.Url
@ -267,6 +272,7 @@ data =
import Server.Request as Request import Server.Request as Request
import BackendTask exposing (BackendTask) import BackendTask exposing (BackendTask)
import Exception
import RouteBuilder exposing (Page, StaticPayload) import RouteBuilder exposing (Page, StaticPayload)
import Pages.PageUrl exposing (PageUrl) import Pages.PageUrl exposing (PageUrl)
import Pages.Url import Pages.Url
@ -312,6 +318,7 @@ data =
"""module Route.Login exposing (Data, Model, Msg, route) """module Route.Login exposing (Data, Model, Msg, route)
import Server.Request as Request import Server.Request as Request
import Exception
type alias Model = type alias Model =
{} {}
@ -348,6 +355,7 @@ route =
"""module Route.Login exposing (Data, Model, Msg, route) """module Route.Login exposing (Data, Model, Msg, route)
import Server.Request as Request import Server.Request as Request
import Exception
type alias Model = type alias Model =
{} {}
@ -382,6 +390,7 @@ route =
"""module Route.Login exposing (Data, Model, Msg, route) """module Route.Login exposing (Data, Model, Msg, route)
import Server.Request as Request import Server.Request as Request
import Exception
type alias Model = type alias Model =
{} {}
@ -410,6 +419,7 @@ route =
"""module Route.Login exposing (Data, Model, Msg, route) """module Route.Login exposing (Data, Model, Msg, route)
import Server.Request import Server.Request
import Exception
type alias Model = type alias Model =
{} {}
@ -446,6 +456,7 @@ route =
"""module Route.Login exposing (Data, Model, Msg, route) """module Route.Login exposing (Data, Model, Msg, route)
import Server.Request import Server.Request
import Exception
type alias Model = type alias Model =
{} {}
@ -480,6 +491,7 @@ route =
"""module Route.Login exposing (Data, Model, Msg, route) """module Route.Login exposing (Data, Model, Msg, route)
import Server.Request import Server.Request
import Exception
type alias Model = type alias Model =
{} {}
@ -508,6 +520,7 @@ route =
"""module Route.Login exposing (Data, Model, Msg, route) """module Route.Login exposing (Data, Model, Msg, route)
import Server.Request as Request import Server.Request as Request
import Exception
type alias Model = type alias Model =
{} {}
@ -536,6 +549,7 @@ route =
"""module Route.Index exposing (Data, Model, Msg, route) """module Route.Index exposing (Data, Model, Msg, route)
import Server.Request as Request import Server.Request as Request
import Exception
import BackendTask exposing (BackendTask) import BackendTask exposing (BackendTask)
import RouteBuilder exposing (Page, StaticPayload) import RouteBuilder exposing (Page, StaticPayload)
@ -583,6 +597,7 @@ data =
"""module Shared exposing (Data, Model, Msg, template) """module Shared exposing (Data, Model, Msg, template)
import Server.Request as Request import Server.Request as Request
import Exception
import Browser.Navigation import Browser.Navigation
import BackendTask import BackendTask
@ -633,6 +648,7 @@ type alias Model =
"""module Shared exposing (Data, Model, Msg, template) """module Shared exposing (Data, Model, Msg, template)
import Server.Request as Request import Server.Request as Request
import Exception
import Browser.Navigation import Browser.Navigation
import BackendTask import BackendTask