on master help sign in
Make sure that generated JS does not use JS's reserved words as variables
commit40ea6df24b0354bfaf4659a8c134e06d7825a3a3 authorEvan Czaplicki <info@elm-lang.org> dateSat 24 Aug 2013, 15:00 (13 years ago) parent60e90c0
2 files changed+25−18
compiler/Parse/Declaration.hs+2−15 compiler/Parse/Helpers.hs+23−3
compiler/Parse/Declaration.hs+2−15
@@ -3,6 +3,7 @@ module Parse.Declaration where
33
44import Control.Applicative ((<$>), (<*>))
55import qualified Data.List as List
6+import qualified Data.Set as Set
67import Text.Parsec hiding (newline,spaces)
78import Text.Parsec.Indent
89import qualified Text.Pandoc as Pan
@@ -94,24 +95,10 @@ importEvent = do
9495jsVar :: IParser String
9596jsVar = betwixt '"' '"' $ do
9697 v <- (:) <$> (letter <|> char '_') <*> many (alphaNum <|> char '_')
97− if v `notElem` jsReserveds then return v else
98+ if Set.notMember v jsReserveds then return v else
9899 error $ "'" ++ v ++
99100 "' is not a good name for a importing or exporting JS values."
100101
101−jsReserveds :: [String]
102−jsReserveds =
103− [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"
104− , "arguments", "int", "byte", "char", "goto", "long", "final", "float"
105− , "short", "double", "native", "throws", "boolean", "abstract", "volatile"
106− , "transient", "synchronized", "function", "break", "case", "catch"
107− , "continue", "debugger", "default", "delete", "do", "else", "finally"
108− , "for", "function", "if", "in", "instanceof", "new", "return", "switch"
109− , "this", "throw", "try", "typeof", "var", "void", "while", "with", "class"
110− , "const", "enum", "export", "extends", "import", "super", "implements"
111− , "interface", "let", "package", "private", "protected", "public"
112− , "static", "yield"
113− ]
114−
115102isExportable tipe =
116103 case tipe of
117104 T.Lambda _ _ ->
compiler/Parse/Helpers.hs+23−3
@@ -4,6 +4,7 @@ import Control.Applicative ((<$>),(<*>))
44import Control.Monad
55import Control.Monad.State
66import Data.Char (isUpper)
7+import qualified Data.Set as Set
78import SourceSyntax.Helpers as Help
89import SourceSyntax.Location as Location
910import SourceSyntax.Expression
@@ -18,6 +19,20 @@ reserveds = [ "if", "then", "else"
1819 , "import", "as", "hiding", "open"
1920 , "export", "foreign" ]
2021
22+jsReserveds :: Set.Set String
23+jsReserveds = Set.fromList
24+ [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"
25+ , "arguments", "int", "byte", "char", "goto", "long", "final", "float"
26+ , "short", "double", "native", "throws", "boolean", "abstract", "volatile"
27+ , "transient", "synchronized", "function", "break", "case", "catch"
28+ , "continue", "debugger", "default", "delete", "do", "else", "finally"
29+ , "for", "function", "if", "in", "instanceof", "new", "return", "switch"
30+ , "this", "throw", "try", "typeof", "var", "void", "while", "with", "class"
31+ , "const", "enum", "export", "extends", "import", "super", "implements"
32+ , "interface", "let", "package", "private", "protected", "public"
33+ , "static", "yield"
34+ ]
35+
2136expecting = flip (<?>)
2237
2338type IParser a = ParsecT String () (State SourcePos) a
@@ -49,13 +64,18 @@ rLabel = lowVar
4964innerVarChar :: IParser Char
5065innerVarChar = alphaNum <|> char '_' <|> char '\'' <?> ""
5166
52−deprime :: String -> String
53−deprime = map (\c -> if c == '\'' then '$' else c)
67+makeSafe :: String -> String
68+makeSafe = dereserve . deprime
69+ where
70+ deprime = map (\c -> if c == '\'' then '$' else c)
71+ dereserve x = case Set.member x jsReserveds of
72+ False -> x
73+ True -> "$" ++ x
5474
5575makeVar :: IParser Char -> IParser String
5676makeVar p = do v <- (:) <$> p <*> many innerVarChar
5777 guard (v `notElem` reserveds)
58− return (deprime v)
78+ return (makeSafe v)
5979
6080reserved :: String -> IParser String
6181reserved word =