Skip to content

Commit

Permalink
0.19 upgrade: replace Basics.flip
Browse files Browse the repository at this point in the history
  • Loading branch information
avh4 committed Apr 13, 2018
1 parent b90d203 commit 6c2dc03
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 4 deletions.
1 change: 1 addition & 0 deletions elm-format.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ library
ElmFormat.InputConsole
ElmFormat.Operation
ElmFormat.OutputConsole
ElmFormat.Upgrade_0_19
ElmFormat.Version
Flags
Messages.Formatter.Format
Expand Down
8 changes: 8 additions & 0 deletions src/ElmFormat/Render/Box.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Data.Maybe (fromMaybe, maybeToList)
import qualified Data.Text as Text
import qualified ElmFormat.Render.ElmStructure as ElmStructure
import qualified ElmFormat.Render.Markdown
import qualified ElmFormat.Upgrade_0_19 as Upgrade_0_19
import qualified ElmFormat.Version
import qualified ElmVersion
import qualified Parse.Parse as Parse
Expand Down Expand Up @@ -972,6 +973,13 @@ expressionParens inner outer =

formatExpression :: ElmVersion -> ExpressionContext -> AST.Expression.Expr -> Box
formatExpression elmVersion context aexpr =
case elmVersion of
Elm_0_19_Upgrade -> formatExpression' elmVersion context (Upgrade_0_19.transform aexpr)
_ -> formatExpression' elmVersion context aexpr


formatExpression' :: ElmVersion -> ExpressionContext -> AST.Expression.Expr -> Box
formatExpression' elmVersion context aexpr =
case RA.drop aexpr of
AST.Expression.Literal lit ->
formatLiteral elmVersion lit
Expand Down
85 changes: 85 additions & 0 deletions src/ElmFormat/Upgrade_0_19.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{-# OPTIONS_GHC -Wall #-}
module ElmFormat.Upgrade_0_19 (transform) where

import AST.V0_16
import AST.Expression
import AST.Pattern
import AST.Variable
import Reporting.Annotation (Located(A))

import qualified Reporting.Annotation as RA
import qualified Reporting.Region as Region


transform :: Expr -> Expr
transform expr =
case RA.drop expr of
VarExpr var | isBasics "flip" var ->
noRegion $ Lambda
[makeArg "f", makeArg "b", makeArg "a"] []
(noRegion $ App
(makeVarRef "f")
[([], makeVarRef "a"), ([], makeVarRef "b")]
(FAJoinFirst JoinAll)
)
False

App (A _ (VarExpr var)) [(pre, arg1)] _ | isBasics "flip" var ->
noRegion $ Lambda
[makeArg "b", makeArg "a"] pre
(noRegion $ App
arg1
[([], makeVarRef "a"), ([], makeVarRef "b")]
(FAJoinFirst JoinAll)
)
False

App (A _ (VarExpr var)) [(pre, arg1), arg2] _ | isBasics "flip" var ->
noRegion $ Lambda
[makeArg "a"] pre
(noRegion $ App
arg1
[([], makeVarRef "a"), arg2]
(FAJoinFirst JoinAll)
)
False

App (A _ (VarExpr var)) ((pre, arg1):arg2:arg3:argRest) multiline | isBasics "flip" var ->
noRegion $ Parens
(Commented pre (noRegion $ App arg1 (arg3:arg2:argRest) multiline) [])

_ ->
expr


--
-- Generic helpers
--


nowhere :: Region.Position
nowhere =
Region.Position 0 0


noRegion :: a -> RA.Located a
noRegion =
RA.at nowhere nowhere


isBasics :: String -> Ref -> Bool
isBasics targetName var =
case var of
VarRef [] (LowercaseIdentifier name) | name == targetName -> True
VarRef [(UppercaseIdentifier "Basics")] (LowercaseIdentifier name) | name == targetName -> True
_ -> False


makeArg :: String -> (Comments, Pattern)
makeArg varName =
([], noRegion $ VarPattern $ LowercaseIdentifier varName)


makeVarRef :: String -> Expr
makeVarRef varName =
noRegion $ VarExpr $ VarRef [] $ LowercaseIdentifier varName
9 changes: 5 additions & 4 deletions tests/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ checkWaysToRun

checkGood 0.19 AllSyntax/0.19/Literals.elm

checkUpgrade 0.19 Elm-0.19/CommaFunctionsBecomeLambdas.elm
checkUpgrade 0.19 Elm-0.19/RemoveBangOperator.elm
checkUpgrade 0.19 Elm-0.19/NewStringEscapeSyntax.elm
checkUpgrade 0.19 Elm-0.19/RemoveBasicsFlip.elm

checkGood 0.18 Simple.elm
checkGood 0.18 AllSyntax/0.18/AllSyntax.elm
checkGoodAllSyntax 0.18 Module
Expand Down Expand Up @@ -383,10 +388,6 @@ checkUpgrade 0.18 Elm-0.18/BackticksBecomeFunctionCalls.elm
checkUpgrade 0.18 Elm-0.18/SpecialBackticksBecomePipelines.elm
checkUpgrade 0.18 Elm-0.18/RenameTupleFunctions.elm

checkUpgrade 0.19 Elm-0.19/CommaFunctionsBecomeLambdas.elm
checkUpgrade 0.19 Elm-0.19/RemoveBangOperator.elm
checkUpgrade 0.19 Elm-0.19/NewStringEscapeSyntax.elm

checkValidationOutputFormat

echo
Expand Down
10 changes: 10 additions & 0 deletions tests/test-files/transform/Elm-0.19/RemoveBasicsFlip.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fullyApplied f = flip f "2" 1
fullyApplied_comments f = flip{-A-}f{-B-}"2"{-C-}1
partiallyApplied f = flip f "2"
partiallyApplied_comments f = flip{-A-}f{-B-}"2"
functionOnly f = flip f
functionOnly_comments f = flip{-A-}f
unapplied = flip
extraArgs f = flip f 1 2 3 4 5

qualified f = Basics.flip f "2" 1
41 changes: 41 additions & 0 deletions tests/test-files/transform/Elm-0.19/RemoveBasicsFlip.formatted.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Main exposing (..)


fullyApplied f =
f 1 "2"


fullyApplied_comments f =
({- A -} f {- C -} 1 {- B -} "2")


partiallyApplied f =
\a -> f a "2"


partiallyApplied_comments f =
\a ->
{- A -}
f a {- B -} "2"


functionOnly f =
\b a -> f a b


functionOnly_comments f =
\b a ->
{- A -}
f a b


unapplied =
\f b a -> f a b


extraArgs f =
f 2 1 3 4 5


qualified f =
f 1 "2"

0 comments on commit 6c2dc03

Please sign in to comment.