git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #1648 from purescript/built-in-names

Revise reserved & built in names list

commit71c0a76031a9bd8d4eb6cc778547c96db4f06542 authorGary Burgess <gary.burgess@gmail.com> dateSun 20 Dec 2015, 00:41 (11 years ago) parent0c87efe9fecb2a
1 file changed+163−103
src/Language/PureScript/CodeGen/JS/Common.hs+163−103
src/Language/PureScript/CodeGen/JS/Common.hs+163−103
@@ -9,6 +9,11 @@ import Data.List (intercalate)
99import Language.PureScript.Crash
1010import Language.PureScript.Names
1111
12+moduleNameToJs :: ModuleName -> String
13+moduleNameToJs (ModuleName pns) =
14+ let name = intercalate "_" (runProperName `map` pns)
15+ in if nameIsJsBuiltIn name then "$$" ++ name else name
16+
1217-- |
1318-- Convert an Ident into a valid Javascript identifier:
1419--
@@ -19,8 +24,9 @@ import Language.PureScript.Names
1924-- * Symbols are prefixed with '$' followed by a symbol name or their ordinal value.
2025--
2126identToJs :: Ident -> String
22−identToJs (Ident name) | nameIsJsReserved name = "$$" ++ name
23−identToJs (Ident name) = concatMap identCharToString name
27+identToJs (Ident name)
28+ | nameIsJsReserved name || nameIsJsBuiltIn name = "$$" ++ name
29+ | otherwise = concatMap identCharToString name
2430identToJs (Op op) = concatMap identCharToString op
2531identToJs (GenIdent _ _) = internalError "GenIdent in identToJs"
2632
@@ -65,107 +71,161 @@ identCharToString c = '$' : show (ord c)
6571--
6672nameIsJsReserved :: String -> Bool
6773nameIsJsReserved name =
68− name `elem` [ "abstract"
69− , "arguments"
70− , "boolean"
71− , "break"
72− , "byte"
73− , "case"
74− , "catch"
75− , "char"
76− , "class"
77− , "const"
78− , "continue"
79− , "debugger"
80− , "default"
81− , "delete"
82− , "do"
83− , "double"
84− , "else"
85− , "enum"
86− , "eval"
87− , "export"
88− , "extends"
89− , "final"
90− , "finally"
91− , "float"
92− , "for"
93− , "function"
94− , "goto"
95− , "if"
96− , "implements"
97− , "import"
98− , "in"
99− , "instanceof"
100− , "int"
101− , "interface"
102− , "let"
103− , "long"
104− , "native"
105− , "new"
106− , "null"
107− , "package"
108− , "private"
109− , "protected"
110− , "public"
111− , "return"
112− , "short"
113− , "static"
114− , "super"
115− , "switch"
116− , "synchronized"
117− , "this"
118− , "throw"
119− , "throws"
120− , "transient"
121− , "try"
122− , "typeof"
123− , "var"
124− , "void"
125− , "volatile"
126− , "while"
127− , "with"
128− , "yield" ] || properNameIsJsReserved name
129−
130−moduleNameToJs :: ModuleName -> String
131−moduleNameToJs (ModuleName pns) =
132− let name = intercalate "_" (runProperName `map` pns)
133− in if properNameIsJsReserved name then "$$" ++ name else name
74+ name `elem` jsAnyReserved
13475
13576-- |
136−-- Checks whether a proper name is reserved in Javascript.
77+-- Checks whether a name matches a built-in value in Javascript.
13778--
138−properNameIsJsReserved :: String -> Bool
139−properNameIsJsReserved name =
140− name `elem` [ "Infinity"
141− , "NaN"
142− , "Object"
143− , "Function"
144− , "Boolean"
145− , "Error"
146− , "EvalError"
147− , "InternalError"
148− , "RangeError"
149− , "ReferenceError"
150− , "SyntaxError"
151− , "TypeError"
152− , "URIError"
153− , "Number"
154− , "Math"
155− , "Date"
156− , "String"
157− , "RegExp"
158− , "Array"
159− , "Int8Array"
160− , "Uint8Array"
161− , "Uint8ClampedArray"
162− , "Int16Array"
163− , "Uint16Array"
164− , "Int32Array"
165− , "Uint32Array"
166− , "Float32Array"
167− , "Float64Array"
168− , "ArrayBuffer"
169− , "DataView"
170− , "JSON"
171− , "Intl" ]
79+nameIsJsBuiltIn :: String -> Bool
80+nameIsJsBuiltIn name =
81+ elem name
82+ [ "arguments"
83+ , "Array"
84+ , "ArrayBuffer"
85+ , "Boolean"
86+ , "DataView"
87+ , "Date"
88+ , "decodeURI"
89+ , "decodeURIComponent"
90+ , "encodeURI"
91+ , "encodeURIComponent"
92+ , "Error"
93+ , "escape"
94+ , "eval"
95+ , "EvalError"
96+ , "Float32Array"
97+ , "Float64Array"
98+ , "Function"
99+ , "Infinity"
100+ , "Int16Array"
101+ , "Int32Array"
102+ , "Int8Array"
103+ , "Intl"
104+ , "isFinite"
105+ , "isNaN"
106+ , "JSON"
107+ , "Map"
108+ , "Math"
109+ , "NaN"
110+ , "Number"
111+ , "Object"
112+ , "parseFloat"
113+ , "parseInt"
114+ , "Promise"
115+ , "Proxy"
116+ , "RangeError"
117+ , "ReferenceError"
118+ , "Reflect"
119+ , "RegExp"
120+ , "Set"
121+ , "SIMD"
122+ , "String"
123+ , "Symbol"
124+ , "SyntaxError"
125+ , "TypeError"
126+ , "Uint16Array"
127+ , "Uint32Array"
128+ , "Uint8Array"
129+ , "Uint8ClampedArray"
130+ , "undefined"
131+ , "unescape"
132+ , "URIError"
133+ , "WeakMap"
134+ , "WeakSet"
135+ ]
136+
137+jsAnyReserved :: [String]
138+jsAnyReserved =
139+ concat
140+ [ jsKeywords
141+ , jsSometimesReserved
142+ , jsFutureReserved
143+ , jsFutureReservedStrict
144+ , jsOldReserved
145+ , jsLiterals
146+ ]
147+
148+jsKeywords :: [String]
149+jsKeywords =
150+ [ "break"
151+ , "case"
152+ , "catch"
153+ , "class"
154+ , "const"
155+ , "continue"
156+ , "debugger"
157+ , "default"
158+ , "delete"
159+ , "do"
160+ , "else"
161+ , "export"
162+ , "extends"
163+ , "finally"
164+ , "for"
165+ , "function"
166+ , "if"
167+ , "import"
168+ , "in"
169+ , "instanceof"
170+ , "new"
171+ , "return"
172+ , "super"
173+ , "switch"
174+ , "this"
175+ , "throw"
176+ , "try"
177+ , "typeof"
178+ , "var"
179+ , "void"
180+ , "while"
181+ , "with"
182+ ]
183+
184+jsSometimesReserved :: [String]
185+jsSometimesReserved =
186+ [ "await"
187+ , "let"
188+ , "static"
189+ , "yield"
190+ ]
191+
192+jsFutureReserved :: [String]
193+jsFutureReserved =
194+ [ "enum" ]
195+
196+jsFutureReservedStrict :: [String]
197+jsFutureReservedStrict =
198+ [ "implements"
199+ , "interface"
200+ , "package"
201+ , "private"
202+ , "protected"
203+ , "public"
204+ ]
205+
206+jsOldReserved :: [String]
207+jsOldReserved =
208+ [ "abstract"
209+ , "boolean"
210+ , "byte"
211+ , "char"
212+ , "double"
213+ , "final"
214+ , "float"
215+ , "goto"
216+ , "int"
217+ , "long"
218+ , "native"
219+ , "short"
220+ , "synchronized"
221+ , "throws"
222+ , "transient"
223+ , "volatile"
224+ ]
225+
226+jsLiterals :: [String]
227+jsLiterals =
228+ [ "null"
229+ , "true"
230+ , "false"
231+ ]