-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
tests/test-files/transform/Elm-0.19/RemoveBasicsFlip.formatted.elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |