Skip to content

fix: bound value with constructor -> wrap in parens #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Language/PS/CST/Printers.purs
Original file line number Diff line number Diff line change
@@ -199,15 +199,19 @@ printInstanceBinding (InstanceBindingSignature { ident, type_ }) = (text <<< app
printInstanceBinding (InstanceBindingName valueBindingFields) = printValueBindingFields valueBindingFields

printValueBindingFields :: ValueBindingFields -> Doc Void
printValueBindingFields { name, binders, guarded } =
printValueBindingFields = \{ name, binders, guarded } ->
let
printedBinders =
if null binders
then mempty
else (paragraph $ map printBinder binders) <> space
else (paragraph $ map printBinderWithMaybeParens binders) <> space

printedHead = (text <<< appendUnderscoreIfReserved <<< unwrap) name <+> printedBinders <> text "="
in printGuarded printedHead guarded
where
printBinderWithMaybeParens x@(BinderConstructor { args: [] }) = printBinder x
printBinderWithMaybeParens x@(BinderConstructor { args }) = parens $ printBinder x
printBinderWithMaybeParens x = printBinder x

printGuarded :: Doc Void -> Guarded -> Doc Void
printGuarded printedHead guarded =
49 changes: 49 additions & 0 deletions test/Golden/BoundValue/Actual.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Test.Golden.BoundValue.Actual where

import Language.PS.CST (Binder(..), Declaration(..), Expr(..), Guarded(..), Ident(..), LetBinding(..), Module(..), ProperName(..), Type(..), mkModuleName, nonQualifiedName, (====>>))

import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Data.Array.NonEmpty as NonEmpty
import Prelude

actualModule :: Module
actualModule = Module
{ moduleName: mkModuleName $ NonEmpty.cons' "BoundValue" []
, imports: []
, exports: []
, declarations:
[ DeclSignature
{ comments: Nothing
, ident: Ident "myfunc"
, type_: (TypeConstructor $ nonQualifiedName $ ProperName "Int") ====>> (TypeConstructor $ nonQualifiedName $ ProperName "Int")
}
, DeclValue
{ comments: Nothing
, valueBindingFields:
{ name: Ident "myfunc"
, binders: [BinderNumber (Left 1)]
, guarded: Unconditional
{ expr: ExprLet
{ bindings:
NonEmpty.cons'
( LetBindingName
{ name: Ident "psModuleFile2"
, binders:
[ BinderConstructor { name: nonQualifiedName (ProperName "Path"), args: [BinderVar $ Ident "x"] }
]
, guarded: Unconditional
{ expr: ExprNumber (Left 1)
, whereBindings: []
}
}
)
[]
, body: (ExprIdent $ nonQualifiedName $ Ident "psModuleFile") `ExprApp` ExprNumber (Left 1)
}
, whereBindings: []
}
}
}
]
}
13 changes: 6 additions & 7 deletions test/Golden/BoundValue/Expected.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module BoundValue where

newtype Foo a
= Foo a

foo :: Int
foo = x
where
Foo x = let Foo y = Foo (Foo 1) in y
myfunc :: Int -> Int
myfunc 1 =
let
psModuleFile2 (Path x) = 1
in
psModuleFile 1
9 changes: 9 additions & 0 deletions test/Golden/BoundValue/Expected.txt_
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module BoundValue where

newtype Foo a
= Foo a

foo :: Int
foo = x
where
Foo x = let Foo y = Foo (Foo 1) in y
2 changes: 2 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ import Test.Golden.MultilinePatternMatchingInLet2.Actual as Test.Golden.Multilin
import Test.Golden.MultilinePatternMatchingInWhere.Actual as Test.Golden.MultilinePatternMatchingInWhere.Actual
import Test.Golden.MultilinePatternMatchingInWhere2.Actual as Test.Golden.MultilinePatternMatchingInWhere2.Actual
import Test.Golden.MultilinePatternMatchingInWhereAndLet2.Actual as Test.Golden.MultilinePatternMatchingInWhereAndLet2.Actual
import Test.Golden.BoundValue.Actual as Test.Golden.BoundValue.Actual
import Test.Spec as Test.Spec
import Test.Spec.Reporter as Test.Spec.Reporter
import Test.Spec.Runner as Test.Spec.Runner
@@ -80,6 +81,7 @@ goldenTests =
, { name: "ExprRecord", actualModule: Test.Golden.ExprRecord.Actual.actualModule }
, { name: "ExprArray", actualModule: Test.Golden.ExprArray.Actual.actualModule }
, { name: "Html", actualModule: Test.Golden.Html.Actual.actualModule }
, { name: "BoundValue", actualModule: Test.Golden.BoundValue.Actual.actualModule }
]

addText :: GoldenTest -> Aff GoldenTestWithExpected