Skip to content

Commit

Permalink
0.19 upgrade: upgrade Html.Attributes.style
Browse files Browse the repository at this point in the history
  • Loading branch information
avh4 committed May 17, 2018
1 parent 19a048c commit 49f52e9
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 77 deletions.
211 changes: 138 additions & 73 deletions src/ElmFormat/Render/Box.hs

Large diffs are not rendered by default.

44 changes: 42 additions & 2 deletions src/ElmFormat/Upgrade_0_19.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import qualified Reporting.Region as Region
import qualified ReversedList


transform :: Expr -> Expr
transform expr =
transform ::
Dict.Map LowercaseIdentifier [UppercaseIdentifier]
-> Dict.Map [UppercaseIdentifier] UppercaseIdentifier
-> Expr -> Expr
transform exposed importAliases expr =
let
basicsReplacements =
Dict.fromList
Expand Down Expand Up @@ -118,10 +121,47 @@ transform expr =
App (A _ (TupleFunction n)) args multiline ->
applyLambda (noRegion $ makeTuple n) args multiline

ExplicitList terms' trailing multiline ->
let
ha = (fmap UppercaseIdentifier ["Html", "Attributes"])
styleExposed = Dict.lookup (LowercaseIdentifier "style") exposed == Just ha
haAlias = Dict.lookup ha importAliases
in
noRegion $ ExplicitList (concat $ fmap (expandHtmlStyle styleExposed haAlias) $ terms') trailing multiline

_ ->
expr


expandHtmlStyle :: Bool -> Maybe UppercaseIdentifier -> (Comments, PreCommented (WithEol Expr)) -> [(Comments, PreCommented (WithEol Expr))]
expandHtmlStyle styleExposed importAlias (preComma, (pre, (term, eol))) =
let
lambda fRef =
Lambda
[([], noRegion $ AST.Pattern.Tuple [makeArg' "a", makeArg' "b"]) ] []
(noRegion $ App
(noRegion $ VarExpr $ fRef)
[ ([], makeVarRef "a")
, ([], makeVarRef "b")
]
(FAJoinFirst JoinAll)
)
False

isHtmlAttributesStyle var =
case var of
VarRef [UppercaseIdentifier "Html", UppercaseIdentifier "Attributes"] (LowercaseIdentifier "style") -> True
VarRef [alias] (LowercaseIdentifier "style") | Just alias == importAlias -> True
VarRef [] (LowercaseIdentifier "style") -> styleExposed
_ -> False
in
case RA.drop term of
App (A _ (VarExpr var)) [(xx, A _ (ExplicitList styles trailing multiline))] _ | isHtmlAttributesStyle var ->
fmap (\(preComma', (pre', (style, eol'))) -> (preComma', (pre', (applyLambda (noRegion $ lambda var) [([], style)] (FAJoinFirst JoinAll), eol')))) styles

_ ->
[(preComma, (pre, (term, eol)))]

--
-- Generic helpers
--
Expand Down
10 changes: 8 additions & 2 deletions tests/Parse/ExpressionTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import AST.Expression
import AST.Pattern (Pattern'(Anything))
import qualified AST.Pattern as P
import AST.Variable
import qualified Data.Map.Strict as Map
import Text.Parsec.Char (string)
import ElmVersion
import ElmFormat.Render.Box (formatExpression, ExpressionContext(..))
import ElmFormat.Render.Box (formatExpression, ExpressionContext(..), ImportInfo(..))
import qualified Box
import qualified Data.Text as Text
import Parse.TestHelpers
Expand All @@ -27,10 +28,15 @@ example name input expected =
assertParse expr input expected


importInfo :: ImportInfo
importInfo =
ImportInfo Map.empty Map.empty


example' :: String -> String -> String -> TestTree
example' name input expected =
testCase name $
assertParse (fmap (Text.unpack . Box.render . formatExpression Elm_0_18 SyntaxSeparated) expr) input expected
assertParse (fmap (Text.unpack . Box.render . formatExpression Elm_0_18 importInfo SyntaxSeparated) expr) input expected


commentedIntExpr (a,b,c,d) preComment postComment i =
Expand Down
2 changes: 2 additions & 0 deletions tests/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ checkUpgrade 0.19 Elm-0.19/RemoveBasicsFlip.elm
checkUpgrade 0.19 Elm-0.19/RemoveBasicsCurry.elm
checkUpgrade 0.19 Elm-0.19/RemoveBasicsUncurry.elm
checkUpgrade 0.19 Elm-0.19/ConvertBasicsRem.elm
checkUpgrade 0.19 Elm-0.19/ConvertHtmlAttributesStyle.elm
checkUpgrade 0.19 Elm-0.19/ConvertHtmlAttributesStyleAltImport.elm

checkGood 0.18 Simple.elm
checkGood 0.18 AllSyntax/0.18/AllSyntax.elm
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Html
import Html.Attributes exposing (style)

convertsStyle = Html.div [Html.Attributes.style [("a", "1")]] []
convertsExposedStyle = Html.div [style [("a", "1")]] []

doesntConvertOtherFunctions f = Html.div [ f [("a", "1")] ] []
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Main exposing (..)

import Html
import Html.Attributes exposing (style)


convertsStyle =
Html.div [ Html.Attributes.style "a" "1" ] []


convertsExposedStyle =
Html.div [ style "a" "1" ] []


doesntConvertOtherFunctions f =
Html.div [ f [ ( "a", "1" ) ] ] []
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Main exposing (..)

import Html
import Html.Attributes as HA


convertsStyle =
Html.div [ HA.style [ ( "a", "1" ) ] ] []


doesntConvertLocalStyle f =
Html.div [ style [ ( "a", "1" ) ] ] []


style x =
x
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Main exposing (..)

import Html
import Html.Attributes as HA


convertsStyle =
Html.div [ HA.style "a" "1" ] []


doesntConvertLocalStyle f =
Html.div [ style [ ( "a", "1" ) ] ] []


style x =
x

0 comments on commit 49f52e9

Please sign in to comment.