2017-01-07 20:29:26 +03:00
|
|
|
module Main exposing (main)
|
|
|
|
|
2018-11-05 21:06:03 +03:00
|
|
|
import Browser
|
2019-07-28 13:33:29 +03:00
|
|
|
import Html exposing (Html, button, div, input, label, p, text, textarea)
|
2019-07-03 15:45:05 +03:00
|
|
|
import Html.Attributes as Attr
|
|
|
|
import Html.Events as Events
|
2019-08-29 15:37:22 +03:00
|
|
|
import NoDebug
|
|
|
|
import NoUnused.CustomTypeConstructors
|
|
|
|
import NoUnused.Variables
|
2019-07-28 13:33:29 +03:00
|
|
|
import Reporter
|
2019-09-29 00:47:02 +03:00
|
|
|
import Review
|
|
|
|
import Review.Project as Project
|
|
|
|
import Review.Rule exposing (Rule)
|
2017-01-07 20:29:26 +03:00
|
|
|
|
|
|
|
|
2019-06-24 02:20:10 +03:00
|
|
|
|
2019-07-31 00:29:39 +03:00
|
|
|
-- MAIN
|
2017-01-07 20:29:26 +03:00
|
|
|
|
2019-07-25 15:35:58 +03:00
|
|
|
|
2019-07-31 00:29:39 +03:00
|
|
|
main : Program () Model Msg
|
|
|
|
main =
|
|
|
|
Browser.sandbox
|
|
|
|
{ init = init
|
|
|
|
, update = update
|
|
|
|
, view = view
|
|
|
|
}
|
2017-01-20 01:31:55 +03:00
|
|
|
|
|
|
|
|
2019-06-24 02:20:10 +03:00
|
|
|
|
|
|
|
-- MODEL
|
|
|
|
|
|
|
|
|
|
|
|
type alias Model =
|
|
|
|
{ sourceCode : String
|
2019-09-29 00:47:02 +03:00
|
|
|
, reviewErrors : List Review.Error
|
2019-07-03 15:45:05 +03:00
|
|
|
, noDebugEnabled : Bool
|
|
|
|
, noUnusedVariablesEnabled : Bool
|
2019-07-05 00:57:55 +03:00
|
|
|
, noUnusedTypeConstructorsEnabled : Bool
|
2019-07-03 19:11:38 +03:00
|
|
|
, showConfigurationAsText : Bool
|
2019-06-24 02:20:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-24 02:00:22 +03:00
|
|
|
init : Model
|
2017-01-07 20:29:26 +03:00
|
|
|
init =
|
2019-06-24 02:00:22 +03:00
|
|
|
let
|
|
|
|
sourceCode : String
|
|
|
|
sourceCode =
|
|
|
|
"""module Main exposing (f)
|
2017-01-19 23:02:21 +03:00
|
|
|
|
2019-06-24 02:00:22 +03:00
|
|
|
import NotUsed
|
|
|
|
import SomeModule exposing (notUsed)
|
2017-01-07 20:29:26 +03:00
|
|
|
|
2019-08-29 15:41:09 +03:00
|
|
|
type SomeCustomType
|
|
|
|
= UsedConstructor
|
|
|
|
| NotUsedConstructor
|
|
|
|
|
|
|
|
f : Int -> SomeCustomType
|
|
|
|
f x =
|
|
|
|
let
|
|
|
|
_ = Debug.log "x" x
|
|
|
|
in
|
|
|
|
UsedConstructor
|
2017-01-07 20:29:26 +03:00
|
|
|
|
2017-01-19 23:02:21 +03:00
|
|
|
g n = n + 1
|
2017-01-07 20:29:26 +03:00
|
|
|
"""
|
2019-06-24 02:00:22 +03:00
|
|
|
in
|
2019-07-31 01:08:16 +03:00
|
|
|
{ sourceCode = sourceCode
|
2019-09-29 00:47:02 +03:00
|
|
|
, reviewErrors = []
|
2019-07-31 01:08:16 +03:00
|
|
|
, noDebugEnabled = True
|
|
|
|
, noUnusedVariablesEnabled = True
|
|
|
|
, noUnusedTypeConstructorsEnabled = True
|
|
|
|
, showConfigurationAsText = False
|
|
|
|
}
|
2019-09-29 00:47:02 +03:00
|
|
|
|> runReview
|
2019-06-24 02:00:22 +03:00
|
|
|
|
2017-01-07 20:29:26 +03:00
|
|
|
|
2019-06-24 02:20:10 +03:00
|
|
|
|
2019-09-29 00:47:02 +03:00
|
|
|
-- REVIEW CONFIGURATION
|
2019-07-31 00:29:39 +03:00
|
|
|
|
|
|
|
|
|
|
|
config : Model -> List Rule
|
|
|
|
config model =
|
2019-08-29 15:37:22 +03:00
|
|
|
[ ( model.noDebugEnabled, NoDebug.rule )
|
|
|
|
, ( model.noUnusedVariablesEnabled, NoUnused.Variables.rule )
|
|
|
|
, ( model.noUnusedTypeConstructorsEnabled, NoUnused.CustomTypeConstructors.rule )
|
2019-07-31 00:29:39 +03:00
|
|
|
]
|
|
|
|
|> List.filter Tuple.first
|
|
|
|
|> List.map Tuple.second
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-06-24 02:20:10 +03:00
|
|
|
-- UPDATE
|
|
|
|
|
|
|
|
|
|
|
|
type Msg
|
|
|
|
= UserEditedSourceCode String
|
2019-07-03 15:45:05 +03:00
|
|
|
| UserToggledNoDebugRule
|
|
|
|
| UserToggledNoUnusedVariablesRule
|
2019-07-05 00:57:55 +03:00
|
|
|
| UserToggledNoUnusedTypeConstructorsRule
|
2019-07-03 19:11:38 +03:00
|
|
|
| UserToggledConfigurationAsText
|
2017-01-07 20:29:26 +03:00
|
|
|
|
2019-06-24 02:00:22 +03:00
|
|
|
|
|
|
|
update : Msg -> Model -> Model
|
2017-01-07 20:29:26 +03:00
|
|
|
update action model =
|
|
|
|
case action of
|
2019-06-24 02:11:50 +03:00
|
|
|
UserEditedSourceCode sourceCode ->
|
2019-07-31 01:08:16 +03:00
|
|
|
{ model | sourceCode = sourceCode }
|
2019-09-29 00:47:02 +03:00
|
|
|
|> runReview
|
2017-01-07 20:29:26 +03:00
|
|
|
|
2019-07-03 15:45:05 +03:00
|
|
|
UserToggledNoDebugRule ->
|
|
|
|
{ model | noDebugEnabled = not model.noDebugEnabled }
|
2019-09-29 00:47:02 +03:00
|
|
|
|> runReview
|
2019-07-03 15:45:05 +03:00
|
|
|
|
|
|
|
UserToggledNoUnusedVariablesRule ->
|
|
|
|
{ model | noUnusedVariablesEnabled = not model.noUnusedVariablesEnabled }
|
2019-09-29 00:47:02 +03:00
|
|
|
|> runReview
|
2019-07-03 15:45:05 +03:00
|
|
|
|
2019-07-05 00:57:55 +03:00
|
|
|
UserToggledNoUnusedTypeConstructorsRule ->
|
|
|
|
{ model | noUnusedTypeConstructorsEnabled = not model.noUnusedTypeConstructorsEnabled }
|
2019-09-29 00:47:02 +03:00
|
|
|
|> runReview
|
2019-07-05 00:57:55 +03:00
|
|
|
|
2019-07-03 19:11:38 +03:00
|
|
|
UserToggledConfigurationAsText ->
|
|
|
|
{ model | showConfigurationAsText = not model.showConfigurationAsText }
|
|
|
|
|
2019-07-03 15:45:05 +03:00
|
|
|
|
2019-09-29 00:47:02 +03:00
|
|
|
runReview : Model -> Model
|
|
|
|
runReview model =
|
|
|
|
{ model | reviewErrors = Review.review (config model) Project.new (file model.sourceCode) }
|
2019-07-03 15:45:05 +03:00
|
|
|
|
2017-01-07 20:29:26 +03:00
|
|
|
|
2018-11-06 15:15:23 +03:00
|
|
|
|
2019-06-24 02:20:10 +03:00
|
|
|
-- VIEW
|
2017-01-07 22:58:41 +03:00
|
|
|
|
|
|
|
|
2019-06-24 02:00:22 +03:00
|
|
|
view : Model -> Html Msg
|
2017-01-07 20:29:26 +03:00
|
|
|
view model =
|
2019-07-03 15:45:05 +03:00
|
|
|
div [ Attr.id "wrapper" ]
|
|
|
|
[ div [ Attr.id "left" ]
|
|
|
|
[ p [ Attr.class "title" ] [ text "Source code" ]
|
2019-06-24 02:11:18 +03:00
|
|
|
, div
|
2019-07-03 15:45:05 +03:00
|
|
|
[ Attr.style "display" "flex"
|
|
|
|
, Attr.style "flex-direction" "row"
|
2017-01-16 00:57:03 +03:00
|
|
|
]
|
2019-07-28 13:33:29 +03:00
|
|
|
[ div
|
|
|
|
[ Attr.style "width" "60%"
|
|
|
|
]
|
|
|
|
[ textarea
|
|
|
|
[ Attr.id "input"
|
|
|
|
, Events.onInput UserEditedSourceCode
|
|
|
|
, Attr.style "width" "100%"
|
|
|
|
, Attr.style "height" "500px"
|
|
|
|
]
|
|
|
|
[ text model.sourceCode ]
|
|
|
|
, div
|
|
|
|
[ Attr.style "border-radius" "4px"
|
|
|
|
, Attr.style "padding" "12px"
|
|
|
|
, Attr.style "max-width" "100%"
|
|
|
|
, Attr.style "width" "calc(100vw - 24px)"
|
|
|
|
, Attr.style "overflow-x" "auto"
|
|
|
|
, Attr.style "white-space" "pre"
|
|
|
|
, Attr.style "color" "white"
|
|
|
|
, Attr.style "font-family" "'Source Code Pro', monospace"
|
|
|
|
, Attr.style "font-size" "12px"
|
|
|
|
, Attr.style "background-color" "black"
|
|
|
|
]
|
2019-09-29 00:47:02 +03:00
|
|
|
[ viewReviewErrors model
|
2019-07-28 13:33:29 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
, div
|
|
|
|
[ Attr.style "margin-left" "2rem"
|
|
|
|
, Attr.style "width" "40%"
|
2019-06-24 02:11:18 +03:00
|
|
|
]
|
2019-07-03 15:45:05 +03:00
|
|
|
[ viewConfigurationPanel model
|
2019-07-03 19:11:38 +03:00
|
|
|
, viewConfigurationAsText model
|
2019-09-29 00:47:02 +03:00
|
|
|
, p [ Attr.class "title" ] [ text "Review errors" ]
|
2019-06-24 02:11:18 +03:00
|
|
|
]
|
2017-01-16 00:57:03 +03:00
|
|
|
]
|
2017-01-10 00:12:37 +03:00
|
|
|
]
|
|
|
|
]
|
2017-01-07 20:29:26 +03:00
|
|
|
|
|
|
|
|
2019-07-03 15:45:05 +03:00
|
|
|
viewConfigurationPanel : Model -> Html Msg
|
|
|
|
viewConfigurationPanel model =
|
|
|
|
div []
|
|
|
|
[ p [ Attr.class "title" ] [ text "Configuration" ]
|
|
|
|
, div
|
|
|
|
[ Attr.style "display" "flex"
|
|
|
|
, Attr.style "flex-direction" "column"
|
|
|
|
]
|
|
|
|
[ viewCheckbox UserToggledNoDebugRule "NoDebug" model.noDebugEnabled
|
|
|
|
, viewCheckbox UserToggledNoUnusedVariablesRule "NoUnusedVariables" model.noUnusedVariablesEnabled
|
2019-07-05 00:57:55 +03:00
|
|
|
, viewCheckbox UserToggledNoUnusedTypeConstructorsRule "NoUnusedTypeConstructors" model.noUnusedTypeConstructorsEnabled
|
2019-07-03 15:45:05 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-07-03 19:11:38 +03:00
|
|
|
viewConfigurationAsText : Model -> Html Msg
|
|
|
|
viewConfigurationAsText model =
|
|
|
|
if model.showConfigurationAsText then
|
|
|
|
div
|
|
|
|
[ Attr.style "display" "flex"
|
|
|
|
, Attr.style "flex-direction" "column"
|
2019-07-28 13:33:29 +03:00
|
|
|
, Attr.style "width" "100%"
|
2019-07-03 19:11:38 +03:00
|
|
|
]
|
|
|
|
[ button
|
|
|
|
[ Attr.style "margin-top" "2rem"
|
|
|
|
, Events.onClick UserToggledConfigurationAsText
|
|
|
|
]
|
|
|
|
[ text "Hide configuration as Elm code" ]
|
|
|
|
, textarea
|
|
|
|
[ Events.onInput UserEditedSourceCode
|
|
|
|
, Attr.style "height" "300px"
|
|
|
|
, Attr.style "width" "100%"
|
|
|
|
]
|
|
|
|
[ text <| configurationAsText model ]
|
|
|
|
]
|
|
|
|
|
|
|
|
else
|
|
|
|
button
|
|
|
|
[ Attr.style "margin-top" "2rem"
|
|
|
|
, Events.onClick UserToggledConfigurationAsText
|
|
|
|
]
|
|
|
|
[ text "Show configuration as Elm code" ]
|
|
|
|
|
|
|
|
|
|
|
|
configurationAsText : Model -> String
|
|
|
|
configurationAsText model =
|
|
|
|
let
|
|
|
|
rules : List { import_ : String, configExpression : String }
|
|
|
|
rules =
|
|
|
|
[ ( model.noDebugEnabled
|
2019-08-29 15:37:22 +03:00
|
|
|
, { import_ = "NoDebug"
|
|
|
|
, configExpression = "NoDebug.rule"
|
2019-07-03 19:11:38 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
, ( model.noUnusedVariablesEnabled
|
2019-08-29 15:37:22 +03:00
|
|
|
, { import_ = "NoUnused.Variables"
|
|
|
|
, configExpression = "NoUnused.Variables.rule"
|
2019-07-03 19:11:38 +03:00
|
|
|
}
|
|
|
|
)
|
2019-07-05 00:57:55 +03:00
|
|
|
, ( model.noUnusedTypeConstructorsEnabled
|
2019-08-29 15:37:22 +03:00
|
|
|
, { import_ = "NoUnused.CustomTypeConstructors"
|
|
|
|
, configExpression = "NoUnused.CustomTypeConstructors.rule"
|
2019-07-05 00:57:55 +03:00
|
|
|
}
|
|
|
|
)
|
2019-07-03 19:11:38 +03:00
|
|
|
]
|
|
|
|
|> List.filter Tuple.first
|
|
|
|
|> List.map Tuple.second
|
|
|
|
|
|
|
|
importStatements : String
|
|
|
|
importStatements =
|
|
|
|
rules
|
|
|
|
|> List.map (\{ import_ } -> "import " ++ import_)
|
|
|
|
|> String.join "\n"
|
|
|
|
|
|
|
|
configExpressions : String
|
|
|
|
configExpressions =
|
|
|
|
rules
|
2019-07-25 15:35:58 +03:00
|
|
|
|> List.map (\{ configExpression } -> " " ++ configExpression)
|
2019-07-03 19:11:38 +03:00
|
|
|
|> String.join "\n ,"
|
|
|
|
in
|
2019-09-29 00:47:02 +03:00
|
|
|
"""module ReviewConfig exposing (config)
|
2019-07-03 19:11:38 +03:00
|
|
|
|
2019-09-29 00:47:02 +03:00
|
|
|
import Review.Rule exposing (Rule)
|
2019-07-03 19:11:38 +03:00
|
|
|
""" ++ importStatements ++ """
|
|
|
|
|
2019-07-25 15:35:58 +03:00
|
|
|
config : List Rule
|
2019-07-03 19:11:38 +03:00
|
|
|
config =
|
|
|
|
[""" ++ configExpressions ++ """
|
|
|
|
]
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-07-03 15:45:05 +03:00
|
|
|
viewCheckbox : Msg -> String -> Bool -> Html Msg
|
|
|
|
viewCheckbox onClick name checked =
|
|
|
|
label
|
|
|
|
[]
|
|
|
|
[ input
|
|
|
|
[ Attr.type_ "checkbox"
|
|
|
|
, Attr.checked checked
|
|
|
|
, Events.onClick onClick
|
|
|
|
]
|
|
|
|
[]
|
|
|
|
, text name
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-09-29 00:47:02 +03:00
|
|
|
viewReviewErrors : Model -> Html msg
|
|
|
|
viewReviewErrors model =
|
|
|
|
reviewErrors model
|
2019-07-31 00:18:52 +03:00
|
|
|
|> List.map viewPart
|
|
|
|
|> Html.div []
|
|
|
|
|
|
|
|
|
2019-08-21 23:15:25 +03:00
|
|
|
viewPart : Reporter.TextContent -> Html msg
|
|
|
|
viewPart { str, color, backgroundColor } =
|
2019-07-31 00:18:52 +03:00
|
|
|
Html.span
|
|
|
|
[ case color of
|
|
|
|
Just ( red, green, blue ) ->
|
|
|
|
Attr.style "color" <| "rgb(" ++ String.fromInt red ++ "," ++ String.fromInt green ++ "," ++ String.fromInt blue ++ ")"
|
|
|
|
|
2019-08-21 23:15:25 +03:00
|
|
|
Nothing ->
|
|
|
|
Attr.classList []
|
|
|
|
, case backgroundColor of
|
|
|
|
Just ( red, green, blue ) ->
|
|
|
|
Attr.style "background-color" <| "rgb(" ++ String.fromInt red ++ "," ++ String.fromInt green ++ "," ++ String.fromInt blue ++ ")"
|
|
|
|
|
2019-07-31 00:18:52 +03:00
|
|
|
Nothing ->
|
|
|
|
Attr.classList []
|
|
|
|
]
|
|
|
|
(str
|
|
|
|
|> String.lines
|
|
|
|
|> List.map Html.text
|
|
|
|
|> List.intersperse (Html.br [] [])
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-09-29 00:47:02 +03:00
|
|
|
reviewErrors : Model -> List Reporter.TextContent
|
|
|
|
reviewErrors model =
|
2019-09-29 01:15:01 +03:00
|
|
|
Reporter.formatReport Reporter.Reviewing
|
2019-07-31 01:08:16 +03:00
|
|
|
[ ( file model.sourceCode
|
2019-09-29 00:47:02 +03:00
|
|
|
, model.reviewErrors
|
|
|
|
|> List.map fromReviewError
|
2019-07-31 00:18:52 +03:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-09-29 00:47:02 +03:00
|
|
|
fromReviewError : Review.Error -> Reporter.Error
|
|
|
|
fromReviewError error =
|
|
|
|
{ moduleName = Review.errorModuleName error
|
|
|
|
, ruleName = Review.errorRuleName error
|
|
|
|
, message = Review.errorMessage error
|
|
|
|
, details = Review.errorDetails error
|
|
|
|
, range = Review.errorRange error
|
|
|
|
, hasFix = Review.errorFixes error /= Nothing
|
2019-07-31 00:18:52 +03:00
|
|
|
}
|
2019-07-31 01:08:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
file : String -> { path : String, source : String }
|
|
|
|
file source =
|
|
|
|
{ path = "SOURCE CODE", source = source }
|