git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Escape built-in JS proper names
commit11cb4b62db0e1f612d4ccb822272c7f0d4bd20b8 authorGary Burgess <gary.burgess@gmail.com> dateTue 10 Feb 2015, 12:22 (12 years ago) parenta49ab17
1 file changed+42−2
src/Language/PureScript/CodeGen/JS/Common.hs+42−2
src/Language/PureScript/CodeGen/JS/Common.hs+42−2
@@ -135,7 +135,47 @@ nameIsJsReserved name =
135135 , "volatile"
136136 , "while"
137137 , "with"
138− , "yield" ]
138+ , "yield" ] || properNameIsJsReserved name
139139
140140moduleNameToJs :: ModuleName -> String
141−moduleNameToJs (ModuleName pns) = intercalate "_" (runProperName `map` pns)
141+moduleNameToJs (ModuleName pns) =
142+ let name = intercalate "_" (runProperName `map` pns)
143+ in if properNameIsJsReserved name then "$$" ++ name else name
144+
145+-- |
146+-- Checks whether a proper name is reserved in Javascript.
147+--
148+properNameIsJsReserved :: String -> Bool
149+properNameIsJsReserved name =
150+ name `elem` [ "Infinity"
151+ , "NaN"
152+ , "Object"
153+ , "Function"
154+ , "Boolean"
155+ , "Error"
156+ , "EvalError"
157+ , "InternalError"
158+ , "RangeError"
159+ , "ReferenceError"
160+ , "SyntaxError"
161+ , "TypeError"
162+ , "URIError"
163+ , "Number"
164+ , "Math"
165+ , "Date"
166+ , "String"
167+ , "RegExp"
168+ , "Array"
169+ , "Int8Array"
170+ , "Uint8Array"
171+ , "Uint8ClampedArray"
172+ , "Int16Array"
173+ , "Uint16Array"
174+ , "Int32Array"
175+ , "Uint32Array"
176+ , "Float32Array"
177+ , "Float64Array"
178+ , "ArrayBuffer"
179+ , "DataView"
180+ , "JSON"
181+ , "Intl" ]