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 :: String -> JS.Expression () -> JS.Statement ()
28 if Help.isOp name then
31 JS.VarRef () (JS.Id () "_op")
34 JS.LBracket () root (JS.StringLit () name)
36 JS.ExprStmt () (JS.AssignExpr () JS.OpAssign lvalue body)
39 JS.VarDeclStmt () [ JS.VarDecl () (JS.Id () (safe name)) (Just body) ]
42-- INSTANTIATE VARIABLES
44canonical :: Var.Canonical -> JS.Expression ()
45canonical (Var.Canonical home name) =
46 if Help.isOp name then
47 JS.BracketRef () (addRoot home "_op") (JS.string name)
50 addRoot home (safe name)
53addRoot :: Var.Home -> String -> JS.Expression ()
59 Var.TopLevel _moduleName ->
65 Var.Module moduleName ->
66 JS.DotRef () (JS.ref (canonicalModuleName moduleName)) (JS.Id () name)
69canonicalModuleName :: ModuleName.Canonical -> String
70canonicalModuleName (ModuleName.Canonical (Pkg.Name _user _project) moduleName) =
71 '$' : List.intercalate "$" moduleName
74swap :: Char -> Char -> Char -> Char
76 if c == from then to else c
81safe :: String -> String
84 if Set.member name jsReserveds then '$' : name else name
86 map (swap '\'' '$') saferName
89jsReserveds :: Set.Set String
93 [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"
94 , "arguments", "int", "byte", "char", "goto", "long", "final", "float"
95 , "short", "double", "native", "throws", "boolean", "abstract", "volatile"
96 , "transient", "synchronized", "function", "break", "case", "catch"
97 , "continue", "debugger", "default", "delete", "do", "else", "finally"
98 , "for", "function", "if", "in", "instanceof", "new", "return", "switch"
99 , "this", "throw", "try", "typeof", "var", "void", "while", "with", "class"
100 , "const", "enum", "export", "extends", "import", "super", "implements"
101 , "interface", "let", "package", "private", "protected", "public"
103 -- reserved by the Elm runtime system
104 , "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9"
105 , "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