git.iko.soy/rowtype-yoga/purescript-backend-optimizer
on main help sign in
fix(#114): "\xdc11" string causes "An error occurred while decoding a JSON value" -> parse Array CodePoint (#116)

fix(#114): "\xdc11" string causes "An error occurred while decoding a JSON value" -> parse Array CodePoint --------- Co-authored-by: Nathan Faubion <natefaubion@users.noreply.github.com>

commit8f13fc9fadffe2fdebb3efb2691029b32e177df7 authorRussians tortured my 18yo friend Ivan bc of ukr flag in mobile phone <srghma@gmail.com> dateSun 4 May 2025, 09:29 (1 year ago) parent347881a
3 files changed+16−2
backend-es/test/snapshots-out/Snapshot.StringLiteral01.js+2−1 backend-es/test/snapshots/Snapshot.StringLiteral01.purs+1 src/PureScript/Backend/Optimizer/CoreFn/Json.purs+13−1
backend-es/test/snapshots-out/Snapshot.StringLiteral01.js+2−1
@@ -1,4 +1,5 @@
1+const test4 = "\udc11";
12const test3 = "\u0000";
23const test2 = "\u0012";
34const test1 = "B";
4−export {test1, test2, test3};
5+export {test1, test2, test3, test4};
backend-es/test/snapshots/Snapshot.StringLiteral01.purs+1
@@ -3,3 +3,4 @@ module Snapshot.StringLiteral01 where
33test1 = "\x000042"
44test2 = "\x000012"
55test3 = "\x000000"
6+test4 = "\xdc11"
src/PureScript/Backend/Optimizer/CoreFn/Json.purs+13−1
@@ -15,9 +15,11 @@ import Data.Argonaut (Json, JsonDecodeError(..), caseJson, decodeJson, isNull)
1515import Data.Array as Array
1616import Data.Array.ST as STArray
1717import Data.Either (Either(..), note)
18+import Data.Enum (toEnum)
1819import Data.Foldable (intercalate)
1920import Data.Int as Int
2021import Data.Maybe (Maybe(..))
22+import Data.String.CodePoints (CodePoint, fromCodePointArray)
2123import Data.String.CodeUnits as SCU
2224import Data.Traversable (traverse)
2325import Data.Tuple (Tuple(..))
@@ -264,7 +266,7 @@ decodeLiteral dec json = do
264266 "NumberLiteral" ->
265267 LitNumber <$> getField decodeNumber obj "value"
266268 "StringLiteral" ->
267− LitString <$> getField decodeString obj "value"
269+ LitString <$> getField decodeStringLiteral obj "value"
268270 "CharLiteral" -> do
269271 str <- getField decodeString obj "value"
270272 LitChar <$> note (TypeMismatch "Char") do
@@ -358,12 +360,22 @@ decodeJArray = caseJson fail fail fail fail Right fail
358360 fail :: forall a. a -> JsonDecode (Array Json)
359361 fail _ = Left $ TypeMismatch "Array"
360362
363+decodeStringLiteral :: Json -> JsonDecode String
364+decodeStringLiteral json =
365+ decodeString json <|> \_ -> map fromCodePointArray (decodeCodePointArray json) <|> \_ -> throwError (TypeMismatch "StringLiteral")
366+
361367decodeString :: Json -> JsonDecode String
362368decodeString = caseJson fail fail fail Right fail fail
363369 where
364370 fail :: forall a. a -> JsonDecode String
365371 fail _ = Left $ TypeMismatch "String"
366372
373+decodeCodePointArray :: Json -> JsonDecode (Array CodePoint)
374+decodeCodePointArray = decodeArray decodeCodePoint
375+
376+decodeCodePoint :: Json -> JsonDecode CodePoint
377+decodeCodePoint = note (TypeMismatch "CodePoint") <<< toEnum <=< decodeInt
378+
367379decodeNumber :: Json -> JsonDecode Number
368380decodeNumber = caseJson fail fail Right fail fail fail
369381 where