1module Generate.JavaScript.Variable (fresh, canonical, 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)
24-- INSTANTIATE VARIABLES
26canonical :: Var.Canonical -> JS.Expression ()
27canonical (Var.Canonical home name) =
28 if Help.isOp name then
29 JS.BracketRef () (JS.ref (addRoot home "_op")) (JS.string name)
32 JS.ref (addRoot home (safe name))
35addRoot :: Var.Home -> String -> String
41 Var.TopLevel moduleName ->
42 canonicalName moduleName name
47 Var.Module moduleName ->
48 canonicalName moduleName name
51canonicalName :: ModuleName.Canonical -> String -> String
52canonicalName (ModuleName.Canonical (Pkg.Name user project) moduleName) name =
53 map (swap '-' '_') user
54 ++ '$' : map (swap '-' '_') project
55 ++ '$' : List.intercalate "$" moduleName
59swap :: Char -> Char -> Char -> Char
61 if c == from then to else c
66safe :: String -> String
69 if Set.member name jsReserveds then '$' : name else name
71 map (swap '\'' '$') saferName
74jsReserveds :: Set.Set String
78 [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"
79 , "arguments", "int", "byte", "char", "goto", "long", "final", "float"
80 , "short", "double", "native", "throws", "boolean", "abstract", "volatile"
81 , "transient", "synchronized", "function", "break", "case", "catch"
82 , "continue", "debugger", "default", "delete", "do", "else", "finally"
83 , "for", "function", "if", "in", "instanceof", "new", "return", "switch"
84 , "this", "throw", "try", "typeof", "var", "void", "while", "with", "class"
85 , "const", "enum", "export", "extends", "import", "super", "implements"
86 , "interface", "let", "package", "private", "protected", "public"
88 -- reserved by the Elm runtime system
89 , "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9"
90 , "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