1module Generate.JavaScript.Variable (fresh, canonical, define, safe) where
3import qualified Control.Monad.State as State
4import qualified Data.List as List
5import qualified Data.Set as Set
6import qualified Language.ECMAScript3.Syntax as JS
8import qualified AST.Helpers as Help
9import qualified AST.Module.Name as ModuleName
10import qualified AST.Variable as Var
11import qualified Elm.Package as Pkg
12import qualified Generate.JavaScript.Helpers as JS
17fresh :: State.State Int String
21 return ("_v" ++ show n)
26define :: Maybe ModuleName.Canonical -> String -> JS.Expression () -> JS.Statement ()
27define maybeHome name body =
30 JS.VarDeclStmt () [ JS.VarDecl () (JS.Id () safeName) (Just body) ]
37 if Help.isOp name then
40 JS.VarRef () (JS.Id () (canonicalPrefix home "_op"))
43 JS.LBracket () root (JS.StringLit () name)
45 JS.ExprStmt () (JS.AssignExpr () JS.OpAssign lvalue body)
48 varDecl (canonicalPrefix home (safe name))
51-- INSTANTIATE VARIABLES
53canonical :: Var.Canonical -> JS.Expression ()
54canonical (Var.Canonical home name) =
55 if Help.isOp name then
56 JS.BracketRef () (JS.ref (addRoot home "_op")) (JS.string name)
59 JS.ref (addRoot home (safe name))
62addRoot :: Var.Home -> String -> String
68 Var.TopLevel moduleName ->
69 canonicalPrefix moduleName name
74 Var.Module moduleName ->
75 canonicalPrefix moduleName name
78canonicalPrefix :: ModuleName.Canonical -> String -> String
79canonicalPrefix (ModuleName.Canonical (Pkg.Name user project) moduleName) name =
80 map (swap '-' '_') user
81 ++ '$' : map (swap '-' '_') project
82 ++ '$' : List.intercalate "$" moduleName
86swap :: Char -> Char -> Char -> Char
88 if c == from then to else c
93safe :: String -> String
96 if Set.member name jsReserveds then '$' : name else name
98 map (swap '\'' '$') saferName
101jsReserveds :: Set.Set String
105 [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"
106 , "arguments", "int", "byte", "char", "goto", "long", "final", "float"
107 , "short", "double", "native", "throws", "boolean", "abstract", "volatile"
108 , "transient", "synchronized", "function", "break", "case", "catch"
109 , "continue", "debugger", "default", "delete", "do", "else", "finally"
110 , "for", "function", "if", "in", "instanceof", "new", "return", "switch"
111 , "this", "throw", "try", "typeof", "var", "void", "while", "with", "class"
112 , "const", "enum", "export", "extends", "import", "super", "implements"
113 , "interface", "let", "package", "private", "protected", "public"
115 -- reserved by the Elm runtime system
116 , "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9"
117 , "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list