@@ -20,6 +20,7 @@ import qualified Data.Foldable as F
2020import qualified Data.Map as M
2121import Data.Maybe (fromMaybe , isNothing )
2222import Data.Monoid ((<>) )
23+ import Data.String (fromString )
2324import Data.Text (Text )
2425import qualified Data.Text as T
2526
@@ -34,6 +35,7 @@ import Language.PureScript.Errors (ErrorMessageHint(..), SimpleErrorMessage(..),
3435 errorMessage , rethrowWithPosition , addHint )
3536import Language.PureScript.Names
3637import Language.PureScript.Options
38+ import Language.PureScript.PSString (PSString , mkString , codePoints )
3739import Language.PureScript.Traversals (sndM )
3840import qualified Language.PureScript.Constants as C
3941
@@ -65,8 +67,8 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign_ =
6567 let moduleBody = header : foreign' ++ jsImports ++ concat optimized
6668 let foreignExps = exps `intersect` (fst `map` foreigns)
6769 let standardExps = exps \\ foreignExps
68- let exps' = JSObjectLiteral Nothing $ map (runIdent &&& JSVar Nothing . identToJs) standardExps
69- ++ map (runIdent &&& foreignIdent) foreignExps
70+ let exps' = JSObjectLiteral Nothing $ map (mkString . runIdent &&& JSVar Nothing . identToJs) standardExps
71+ ++ map (mkString . runIdent &&& foreignIdent) foreignExps
7072 return $ moduleBody ++ [JSAssignment Nothing (JSAccessor Nothing " exports" (JSVar Nothing " module" )) exps']
7173
7274 where
@@ -108,7 +110,7 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign_ =
108110 importToJs :: M. Map ModuleName (Ann , ModuleName ) -> ModuleName -> m JS
109111 importToJs mnLookup mn' = do
110112 let ((ss, _, _, _), mnSafe) = fromMaybe (internalError " Missing value in mnLookup" ) $ M. lookup mn' mnLookup
111- let moduleBody = JSApp Nothing (JSVar Nothing " require" ) [JSStringLiteral Nothing (T. pack (" .." </> T. unpack (runModuleName mn')))]
113+ let moduleBody = JSApp Nothing (JSVar Nothing " require" ) [JSStringLiteral Nothing (fromString (" .." </> T. unpack (runModuleName mn')))]
112114 withPos ss $ JSVariableIntroduction Nothing (moduleNameToJs mnSafe) (Just moduleBody)
113115
114116 -- |
@@ -176,12 +178,13 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign_ =
176178 -- indexer is returned.
177179 --
178180 accessor :: Ident -> JS -> JS
179- accessor (Ident prop) = accessorString prop
181+ accessor (Ident prop) = accessorString $ mkString prop
180182 accessor (GenIdent _ _) = internalError " GenIdent in accessor"
181183
182- accessorString :: Text -> JS -> JS
183- accessorString prop | identNeedsEscaping prop = JSIndexer Nothing (JSStringLiteral Nothing prop)
184- | otherwise = JSAccessor Nothing prop
184+ accessorString :: PSString -> JS -> JS
185+ accessorString prop =
186+ let quoted = JSIndexer Nothing (JSStringLiteral Nothing prop) in
187+ either (const quoted) (\ t -> if identNeedsEscaping t then quoted else JSAccessor Nothing prop) $ codePoints prop
185188
186189 -- |
187190 -- Generate code in the simplified Javascript intermediate representation for a value or expression.
@@ -212,7 +215,7 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign_ =
212215 unAbs (Abs _ arg val) = arg : unAbs val
213216 unAbs _ = []
214217 assign :: Ident -> JS
215- assign name = JSAssignment Nothing (accessorString (runIdent name) (JSVar Nothing " this" ))
218+ assign name = JSAssignment Nothing (accessorString (mkString $ runIdent name) (JSVar Nothing " this" ))
216219 (var name)
217220 valueToJs' (Abs _ arg val) = do
218221 ret <- valueToJs val
@@ -256,7 +259,7 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign_ =
256259 (JSUnary Nothing JSNew $ JSApp Nothing (JSVar Nothing (properToJs ctor)) [] ) ]
257260 valueToJs' (Constructor _ _ (ProperName ctor) fields) =
258261 let constructor =
259- let body = [ JSAssignment Nothing (JSAccessor Nothing (identToJs f) (JSVar Nothing " this" )) (var f) | f <- fields ]
262+ let body = [ JSAssignment Nothing (JSAccessor Nothing (mkString $ identToJs f) (JSVar Nothing " this" )) (var f) | f <- fields ]
260263 in JSFunction Nothing (Just (properToJs ctor)) (identToJs `map` fields) (JSBlock Nothing body)
261264 createFn =
262265 let body = JSUnary Nothing JSNew $ JSApp Nothing (JSVar Nothing (properToJs ctor)) (var `map` fields)
@@ -272,15 +275,15 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign_ =
272275 literalToValueJS (NumericLiteral (Left i)) = return $ JSNumericLiteral Nothing (Left i)
273276 literalToValueJS (NumericLiteral (Right n)) = return $ JSNumericLiteral Nothing (Right n)
274277 literalToValueJS (StringLiteral s) = return $ JSStringLiteral Nothing s
275- literalToValueJS (CharLiteral c) = return $ JSStringLiteral Nothing (T. singleton c )
278+ literalToValueJS (CharLiteral c) = return $ JSStringLiteral Nothing (fromString [c] )
276279 literalToValueJS (BooleanLiteral b) = return $ JSBooleanLiteral Nothing b
277280 literalToValueJS (ArrayLiteral xs) = JSArrayLiteral Nothing <$> mapM valueToJs xs
278281 literalToValueJS (ObjectLiteral ps) = JSObjectLiteral Nothing <$> mapM (sndM valueToJs) ps
279282
280283 -- |
281284 -- Shallow copy an object.
282285 --
283- extendObj :: JS -> [(Text , JS )] -> m JS
286+ extendObj :: JS -> [(PSString , JS )] -> m JS
284287 extendObj obj sts = do
285288 newObj <- freshName
286289 key <- freshName
@@ -317,7 +320,7 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign_ =
317320 qualifiedToJS f (Qualified _ a) = JSVar Nothing $ identToJs (f a)
318321
319322 foreignIdent :: Ident -> JS
320- foreignIdent ident = accessorString (runIdent ident) (JSVar Nothing " $foreign" )
323+ foreignIdent ident = accessorString (mkString $ runIdent ident) (JSVar Nothing " $foreign" )
321324
322325 -- |
323326 -- Generate code in the simplified Javascript intermediate representation for pattern match binders
@@ -341,7 +344,7 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign_ =
341344 go _ _ _ = internalError " Invalid arguments to bindersToJs"
342345
343346 failedPatternError :: [Text ] -> JS
344- failedPatternError names = JSUnary Nothing JSNew $ JSApp Nothing (JSVar Nothing " Error" ) [JSBinary Nothing Add (JSStringLiteral Nothing failedPatternMessage) (JSArrayLiteral Nothing $ zipWith valueError names vals)]
347+ failedPatternError names = JSUnary Nothing JSNew $ JSApp Nothing (JSVar Nothing " Error" ) [JSBinary Nothing Add (JSStringLiteral Nothing $ mkString failedPatternMessage) (JSArrayLiteral Nothing $ zipWith valueError names vals)]
345348
346349 failedPatternMessage :: Text
347350 failedPatternMessage = " Failed pattern match" <> maybe " " (((" at " <> runModuleName mn <> " " ) <> ) . displayStartEndPos) maybeSpan <> " : "
@@ -391,7 +394,7 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign_ =
391394 argVar <- freshName
392395 done'' <- go remain done'
393396 js <- binderToJs argVar done'' binder
394- return (JSVariableIntroduction Nothing argVar (Just (JSAccessor Nothing (identToJs field) (JSVar Nothing varName))) : js)
397+ return (JSVariableIntroduction Nothing argVar (Just (JSAccessor Nothing (mkString $ identToJs field) (JSVar Nothing varName))) : js)
395398 binderToJs' _ _ ConstructorBinder {} =
396399 internalError " binderToJs: Invalid ConstructorBinder in binderToJs"
397400 binderToJs' varName done (NamedBinder _ ident binder) = do
@@ -402,7 +405,7 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign_ =
402405 literalToBinderJS varName done (NumericLiteral num) =
403406 return [JSIfElse Nothing (JSBinary Nothing EqualTo (JSVar Nothing varName) (JSNumericLiteral Nothing num)) (JSBlock Nothing done) Nothing ]
404407 literalToBinderJS varName done (CharLiteral c) =
405- return [JSIfElse Nothing (JSBinary Nothing EqualTo (JSVar Nothing varName) (JSStringLiteral Nothing (T. singleton c ))) (JSBlock Nothing done) Nothing ]
408+ return [JSIfElse Nothing (JSBinary Nothing EqualTo (JSVar Nothing varName) (JSStringLiteral Nothing (fromString [c] ))) (JSBlock Nothing done) Nothing ]
406409 literalToBinderJS varName done (StringLiteral str) =
407410 return [JSIfElse Nothing (JSBinary Nothing EqualTo (JSVar Nothing varName) (JSStringLiteral Nothing str)) (JSBlock Nothing done) Nothing ]
408411 literalToBinderJS varName done (BooleanLiteral True ) =
@@ -411,7 +414,7 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign_ =
411414 return [JSIfElse Nothing (JSUnary Nothing Not (JSVar Nothing varName)) (JSBlock Nothing done) Nothing ]
412415 literalToBinderJS varName done (ObjectLiteral bs) = go done bs
413416 where
414- go :: [JS ] -> [(Text , Binder Ann )] -> m [JS ]
417+ go :: [JS ] -> [(PSString , Binder Ann )] -> m [JS ]
415418 go done' [] = return done'
416419 go done' ((prop, binder): bs') = do
417420 propVar <- freshName
0 commit comments