git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Rename `isIdent` to be more meaningful
06bd87eGary Burgess
src/Language/PureScript/CodeGen/Common.hs+6−6
@@ -33,6 +33,12 @@ identToJs (Ident name) | nameIsJsReserved name = "$$" ++ name
33identToJs (Ident name) = concatMap identCharToString name
33identToJs (Ident name) = concatMap identCharToString name
34identToJs (Op op) = concatMap identCharToString op
34identToJs (Op op) = concatMap identCharToString op
35
35
36+-- |
37+-- Test if a string is a valid JS identifier without escaping.
38+--
39+identNeedsEscaping :: String -> Bool
40+identNeedsEscaping s = s /= identToJs (Ident s)
41+
36-- |
42-- |
37-- Attempts to find a human-readable name for a symbol, if none has been specified returns the
43-- Attempts to find a human-readable name for a symbol, if none has been specified returns the
38-- ordinal value.
44-- ordinal value.
@@ -130,11 +136,5 @@ nameIsJsReserved name =
130 , "with"
136 , "with"
131 , "yield" ]
137 , "yield" ]
132
138
133−-- |
134−-- Test if a string is a valid JS identifier.
135−--
136−isIdent :: String -> Bool
137−isIdent s = identToJs (Ident s) == s
138−
139moduleNameToJs :: ModuleName -> String
139moduleNameToJs :: ModuleName -> String
140moduleNameToJs (ModuleName pns) = intercalate "_" (runProperName `map` pns)
140moduleNameToJs (ModuleName pns) = intercalate "_" (runProperName `map` pns)
src/Language/PureScript/CodeGen/JS.hs+3−3
@@ -20,7 +20,7 @@ module Language.PureScript.CodeGen.JS (
20 ModuleType(..),
20 ModuleType(..),
21 declToJs,
21 declToJs,
22 moduleToJs,
22 moduleToJs,
23− isIdent
23+ identNeedsEscaping
24) where
24) where
25
25
26import Data.Maybe (catMaybes, fromJust, fromMaybe)
26import Data.Maybe (catMaybes, fromJust, fromMaybe)
@@ -179,8 +179,8 @@ accessor (Ident prop) = accessorString prop
179accessor (Op op) = JSIndexer (JSStringLiteral op)
179accessor (Op op) = JSIndexer (JSStringLiteral op)
180
180
181accessorString :: String -> JS -> JS
181accessorString :: String -> JS -> JS
182−accessorString prop | isIdent prop = JSAccessor prop
183− | otherwise = JSIndexer (JSStringLiteral prop)
182+accessorString prop | identNeedsEscaping prop = JSIndexer (JSStringLiteral prop)
183+ | otherwise = JSAccessor prop
184
184
185-- |
185-- |
186-- Generate code in the simplified Javascript intermediate representation for a value or expression.
186-- Generate code in the simplified Javascript intermediate representation for a value or expression.
src/Language/PureScript/Pretty/JS.hs+3−3
@@ -18,7 +18,7 @@ module Language.PureScript.Pretty.JS (
18) where
18) where
19
19
20import Language.PureScript.Pretty.Common
20import Language.PureScript.Pretty.Common
21−import Language.PureScript.CodeGen.JS (isIdent)
21+import Language.PureScript.CodeGen.JS (identNeedsEscaping)
22import Language.PureScript.CodeGen.JS.AST
22import Language.PureScript.CodeGen.JS.AST
23
23
24import Data.List
24import Data.List
@@ -56,8 +56,8 @@ literals = mkPattern' match
56 ]
56 ]
57 where
57 where
58 objectPropertyToString :: String -> String
58 objectPropertyToString :: String -> String
59− objectPropertyToString s | isIdent s = s
59+ objectPropertyToString s | identNeedsEscaping s = show s
60− | otherwise = show s
60+ | otherwise = s
61 match (JSBlock sts) = fmap concat $ sequence
61 match (JSBlock sts) = fmap concat $ sequence
62 [ return "{\n"
62 [ return "{\n"
63 , withIndent $ prettyStatements sts
63 , withIndent $ prettyStatements sts