Skip to content

Commit bfb255d

Browse files
authored
Merge pull request purescript#2586 from michaelficarra/purescriptGH-2568
TypeLevelString/TypeConcat should not be quoted
2 parents 3f1a50e + d1a88d7 commit bfb255d

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/Language/PureScript/Errors.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Data.Functor.Identity (Identity(..))
2020
import Data.List (transpose, nub, nubBy, sortBy, partition)
2121
import Data.Maybe (maybeToList, fromMaybe, mapMaybe)
2222
import Data.Ord (comparing)
23+
import Data.String (fromString)
2324
import qualified Data.Map as M
2425
import qualified Data.Text as T
2526
import Data.Text (Text)
@@ -31,7 +32,8 @@ import Language.PureScript.Environment
3132
import Language.PureScript.Label (Label(..))
3233
import Language.PureScript.Names
3334
import Language.PureScript.Pretty
34-
import Language.PureScript.Pretty.Common (before, endWith)
35+
import Language.PureScript.Pretty.Common (endWith)
36+
import Language.PureScript.PSString (PSString, decodeStringWithReplacement)
3537
import Language.PureScript.Traversals
3638
import Language.PureScript.Types
3739
import qualified Language.PureScript.Publish.BoxesHelpers as BoxHelpers
@@ -1264,12 +1266,15 @@ renderBox = unlines
12641266
whiteSpace = all isSpace
12651267

12661268
toTypelevelString :: Type -> Maybe Box.Box
1267-
toTypelevelString (TypeLevelString s) = Just $ Box.text $ T.unpack $ prettyPrintString s
1268-
toTypelevelString (TypeApp (TypeConstructor f) x)
1269-
| f == primName "TypeString" = Just $ typeAsBox x
1270-
toTypelevelString (TypeApp (TypeApp (TypeConstructor f) x) ret)
1271-
| f == primName "TypeConcat" = before <$> (toTypelevelString x) <*> (toTypelevelString ret)
1272-
toTypelevelString _ = Nothing
1269+
toTypelevelString t = (Box.text . decodeStringWithReplacement) <$> toTypelevelString' t
1270+
where
1271+
toTypelevelString' :: Type -> Maybe PSString
1272+
toTypelevelString' (TypeLevelString s) = Just s
1273+
toTypelevelString' (TypeApp (TypeConstructor f) x)
1274+
| f == primName "TypeString" = Just $ fromString $ prettyPrintType x
1275+
toTypelevelString' (TypeApp (TypeApp (TypeConstructor f) x) ret)
1276+
| f == primName "TypeConcat" = toTypelevelString' x <> toTypelevelString' ret
1277+
toTypelevelString' _ = Nothing
12731278

12741279
-- | Rethrow an error with a more detailed error message in the case of failure
12751280
rethrow :: (MonadError e m) => (e -> e) -> m a -> m a

src/Language/PureScript/PSString.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Language.PureScript.PSString
55
, toUTF16CodeUnits
66
, decodeString
77
, decodeStringEither
8+
, decodeStringWithReplacement
89
, prettyPrintString
910
, prettyPrintStringJS
1011
, mkString
@@ -52,15 +53,24 @@ newtype PSString = PSString { toUTF16CodeUnits :: [Word16] }
5253
instance Show PSString where
5354
show = show . codePoints
5455

56+
-- |
5557
-- Decode a PSString to a String, representing any lone surrogates as the
5658
-- reserved code point with that index. Warning: if there are any lone
5759
-- surrogates, converting the result to Text via Data.Text.pack will result in
5860
-- loss of information as those lone surrogates will be replaced with U+FFFD
5961
-- REPLACEMENT CHARACTER. Because this function requires care to use correctly,
6062
-- we do not export it.
63+
--
6164
codePoints :: PSString -> String
6265
codePoints = map (either (chr . fromIntegral) id) . decodeStringEither
6366

67+
-- |
68+
-- Decode a PSString as UTF-16 text. Lone surrogates will be replaced with
69+
-- U+FFFD REPLACEMENT CHARACTER
70+
--
71+
decodeStringWithReplacement :: PSString -> String
72+
decodeStringWithReplacement = map (either (const '\xFFFD') id) . decodeStringEither
73+
6474
-- |
6575
-- Decode a PSString as UTF-16. Lone surrogates in the input are represented in
6676
-- the output with the Left constructor; characters which were successfully

0 commit comments

Comments
 (0)