Module: Opal::Nodes::Helpers
- Included in:
- Base
- Defined in:
- opal/lib/opal/nodes/helpers.rb
Constant Summary
- RESERVED =
Reserved javascript keywords - we cannot create variables with the same name
%w[ arguments break case catch char class const continue debugger default delete do else enum export extends false finally for function if import in instanceof let native new return static switch super this throw try true typeof var void while with undefined ]
Instance Method Summary collapse
- #current_indent ⇒ Object
- #empty_line ⇒ Object
- #indent(&block) ⇒ Object
- #js_falsy(sexp) ⇒ Object
- #js_truthy(sexp) ⇒ Object
- #js_truthy_optimize(sexp) ⇒ Object
- #line(*strs) ⇒ Object
-
#lvar_to_js(var) ⇒ Object
Converts a ruby lvar/arg name to a js identifier.
-
#mid_to_jsid(mid) ⇒ Object
Converts a ruby method name into its javascript equivalent for a method/function call.
- #property(name) ⇒ Object
- #reserved?(name) ⇒ Boolean
- #variable(name) ⇒ Object
Instance Method Details
#current_indent ⇒ Object
51 52 53 |
# File 'opal/lib/opal/nodes/helpers.rb', line 51 def current_indent compiler.parser_indent end |
#empty_line ⇒ Object
60 61 62 |
# File 'opal/lib/opal/nodes/helpers.rb', line 60 def empty_line push "\n" end |
#indent(&block) ⇒ Object
47 48 49 |
# File 'opal/lib/opal/nodes/helpers.rb', line 47 def indent(&block) compiler.indent(&block) end |
#js_falsy(sexp) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'opal/lib/opal/nodes/helpers.rb', line 74 def js_falsy(sexp) if sexp.type == :call mid = sexp[2] if mid == :block_given? scope.uses_block! return "#{scope.block_name} === nil" end end with_temp do |tmp| [fragment("(#{tmp} = "), expr(sexp), fragment(") === false || #{tmp} === nil")] end end |
#js_truthy(sexp) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'opal/lib/opal/nodes/helpers.rb', line 64 def js_truthy(sexp) if optimize = js_truthy_optimize(sexp) return optimize end with_temp do |tmp| [fragment("(#{tmp} = "), expr(sexp), fragment(") !== false && #{tmp} !== nil")] end end |
#js_truthy_optimize(sexp) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'opal/lib/opal/nodes/helpers.rb', line 88 def js_truthy_optimize(sexp) if sexp.type == :call mid = sexp[2] if mid == :block_given? expr(sexp) elsif Compiler::COMPARE.include? mid.to_s expr(sexp) elsif mid == :==" expr(sexp) end elsif [:lvar, :self].include? sexp.type [expr(sexp.dup), fragment(" !== false && "), expr(sexp.dup), fragment(" !== nil")] end end |
#line(*strs) ⇒ Object
55 56 57 58 |
# File 'opal/lib/opal/nodes/helpers.rb', line 55 def line(*strs) push "\n#{current_indent}" push(*strs) end |
#lvar_to_js(var) ⇒ Object
Converts a ruby lvar/arg name to a js identifier. Not all ruby names are valid in javascript. A $ suffix is added to non-valid names. varibales
29 30 31 32 |
# File 'opal/lib/opal/nodes/helpers.rb', line 29 def lvar_to_js(var) var = "#{var}$" if RESERVED.include? var.to_s var.to_sym end |
#mid_to_jsid(mid) ⇒ Object
Converts a ruby method name into its javascript equivalent for a method/function call. All ruby method names get prefixed with a '$', and if the name is a valid javascript identifier, it will have a '.' prefix (for dot-calling), otherwise it will be wrapped in brackets to use reference notation calling.
39 40 41 42 43 44 45 |
# File 'opal/lib/opal/nodes/helpers.rb', line 39 def mid_to_jsid(mid) if /\=|\+|\-|\*|\/|\!|\?|\<|\>|\&|\||\^|\%|\~|\[/ =~ mid.to_s "['$#{mid}']" else '.$' + mid end end |
#property(name) ⇒ Object
14 15 16 |
# File 'opal/lib/opal/nodes/helpers.rb', line 14 def property(name) reserved?(name) ? "['#{name}']" : ".#{name}" end |
#reserved?(name) ⇒ Boolean
18 19 20 |
# File 'opal/lib/opal/nodes/helpers.rb', line 18 def reserved?(name) RESERVED.include? name end |
#variable(name) ⇒ Object
22 23 24 |
# File 'opal/lib/opal/nodes/helpers.rb', line 22 def variable(name) reserved?(name.to_s) ? "#{name}$" : name end |