on master help sign in
Fix bugs in import statements. Make sure JS imports work with any JS-minifier.
commitb4d563dff8a00323a98f65313a7adb33f79dfb49 authorevancz <info@elm-lang.org> dateSat 24 Nov 2012, 13:00 (14 years ago) parentd4012e4
3 files changed+30−20
elm/src/Gen/CompileToJS.hs+21−14 elm/src/Parse/Modules.hs+8−5 elm/src/Rename.hs+1−1
elm/src/Gen/CompileToJS.hs+21−14
@@ -14,6 +14,8 @@ import Initialize
1414import Rename (derename)
1515import Cases
1616import Guid
17+import Parse.Library (isOp)
18+import Rename (deprime)
1719
1820showErr :: String -> String
1921showErr err = mainEquals $ "Elm.Graphics.text(Elm.Text.monospace(" ++ msg ++ "))"
@@ -35,7 +37,7 @@ mainEquals s = globalAssign "Elm.main" (jsFunc "" (ret s))
3537globalAssign m s = "\n" ++ m ++ "=" ++ s ++ ";"
3638
3739tryBlock escapees names e =
38− concat [ "\n(function() {\ntry{\n" ++ e ++ "\n} catch (e) {"
40+ concat [ "\ntry{\n" ++ e ++ "\n} catch (e) {"
3941 , "\nElm.main=function() {"
4042 , "\nvar msg = ('<br/><h2>Your browser may not be supported. " ++
4143 "Are you using a modern browser?</h2>' +" ++
@@ -43,7 +45,7 @@ tryBlock escapees names e =
4345 intercalate "." names ++ " module:<br/>' + e + '" ++ msg ++ "</span>');"
4446 , "\ndocument.body.innerHTML = Elm.Text.monospace(msg);"
4547 , "throw e;"
46− , "};}}());"
48+ , "};}"
4749 ]
4850 where msg | escapees /= [] = concat [ "<br/><br/>The problem may stem from an improper usage of:<br/>"
4951 , intercalate ", " $ map (concatMap escape) escapees ]
@@ -55,14 +57,12 @@ tryBlock escapees names e =
5557
5658jsModule (escapees, Module names exports imports stmts) =
5759 tryBlock escapees (tail modNames) $ concat
58− [ assign "$op" "{}"
59− , "\nfor(this['i'] in Elm){eval('var '+this['i']+'=Elm[this.i];');}"
60− , concatMap (\n -> globalAssign n $ n ++ " || {}") .
60+ [ concatMap (\n -> globalAssign n $ n ++ " || {}") .
6161 map (intercalate ".") . drop 2 . inits $
6262 take (length modNames - 1) modNames
6363 , "\nif (" ++ modName ++ ") throw \"Module name collision, '" ++
6464 intercalate "." (tail modNames) ++ "' is already defined.\"; "
65− , globalAssign modName $ jsFunc "" (includes ++body++ export) ++ "()"
65+ , globalAssign modName $ jsFunc "" (defs ++ includes ++ body ++ export) ++ "()"
6666 , mainEquals $ modName ++ ".main" ]
6767 where modNames = if null names then ["Elm", "Main"]
6868 else "Elm" : names
@@ -71,9 +71,11 @@ jsModule (escapees, Module names exports imports stmts) =
7171 body = stmtsToJS stmts
7272 export = getExports exports stmts
7373 exps = if null exports then ["main"] else exports
74+ defs = concat [ assign "$op" "{}"
75+ , "\nfor(Elm['i'] in Elm){eval('var '+Elm['i']+'=Elm[Elm.i];');}" ]
7476
7577getExports names stmts = ret . braces $ intercalate ",\n" (op : map fnPair fns)
76− where exNames n = either id id n `elem` names
78+ where exNames n = either derename id n `elem` names
7779 exports | null names = concatMap get stmts
7880 | otherwise = filter exNames (concatMap get stmts)
7981
@@ -100,16 +102,21 @@ jsImport (modul, how) =
100102 jsImport' (modul, how)
101103
102104jsImport' (modul, As name) = assign name modul
103−jsImport' (modul, Importing vs) =
104− concatMap (\v -> assign v $ modul ++ "." ++ v) vs
105+jsImport' (modul, Importing vs) = concatMap def vs
106+ where def [] = []
107+ def (o:p) | isOp o = let v = "$op['" ++ o:p ++ "']" in
108+ "\n" ++ v ++ " = " ++ modul ++ "." ++ v ++ ";"
109+ | otherwise = let v = deprime (o:p) in
110+ assign v $ modul ++ "." ++ v
111+
105112jsImport' (modul, Hiding vs) =
106113 concat [ assign "hiddenVars" . ("{"++) . (++"}") . intercalate "," $
107− map (\v -> v ++ ":true") vs
108− , "\nfor (this['i'] in " ++ modul ++ ") "
114+ map (\v -> v ++ ":true") (map deprime vs)
115+ , "\nfor (Elm['i'] in " ++ modul ++ ") "
109116 , braces . indent . concat $
110− [ "\nif (hiddenVars[this['i']]) continue;"
111− , "\neval('var ' + this['i'] + ' = "
112− , modul, "[this.i];');" ]
117+ [ "\nif (hiddenVars[Elm['i']]) continue;"
118+ , "\neval('var ' + Elm['i'] + ' = "
119+ , modul, "[Elm.i];');" ]
113120 ]
114121
115122stmtsToJS :: [Statement] -> String
elm/src/Parse/Modules.hs+8−5
@@ -8,13 +8,16 @@ import Text.Parsec hiding (newline,spaces)
88import Ast
99import Parse.Library
1010
11+varList :: (Monad m) => ParsecT [Char] u m [String]
12+varList = parens $ commaSep1 (var <|> parens symOp)
13+
1114moduleDef :: (Monad m) => ParsecT [Char] u m ([String], [String])
1215moduleDef = do
1316 try (reserved "module")
14− whitespace <?> "name of module"
15− names <- dotSep1 capVar
1617 whitespace
17− exports <- option [] . parens $ commaSep var
18+ names <- dotSep1 capVar <?> "name of module"
19+ whitespace
20+ exports <- option [] varList
1821 whitespace <?> "reserved word 'where'"
1922 reserved "where"
2023 return (names, exports)
@@ -38,7 +41,7 @@ as' = reserved "as" >> whitespace >> As <$> capVar <?> "alias for module"
3841
3942hiding' :: (Monad m) => ParsecT [Char] u m ImportMethod
4043hiding' = reserved "hiding" >> whitespace >>
41− Hiding <$> parens (commaSep1 var) <?> "listing of hidden values"
44+ Hiding <$> varList <?> "listing of hidden values"
4245
4346importing' :: (Monad m) => ParsecT [Char] u m ImportMethod
44−importing' = Importing <$> parens (commaSep1 var) <?> "listing of imported values (x,y,z)"
47+importing' = Importing <$> varList <?> "listing of imported values (x,y,z)"
elm/src/Rename.hs+1−1
@@ -1,5 +1,5 @@
11{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−module Rename (renameModule, derename) where
2+module Rename (renameModule, derename, deprime) where
33
44import Ast
55import Control.Arrow (first)