Class: Opal::Nodes::XStringNode
Instance Attribute Summary
Attributes inherited from Base
Attributes included from Closure::NodeSupport
Class Method Summary collapse
-
.single_line?(children) ⇒ Boolean
Check if there's only one child or if they're all part of the same line (e.g. because of interpolations).
-
.strip_empty_children(children) ⇒ Object
Will remove empty :str lines coming from cosmetic newlines in x-strings.
Instance Method Summary collapse
Methods inherited from Base
#add_gvar, #add_ivar, #add_local, #add_temp, children, #children, #class_variable_owner, #class_variable_owner_nesting_level, #comments, #compile_to_fragments, #error, #expr, #expr?, #expr_or_empty, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #source_location, #stmt, #stmt?, #top_scope, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap
Methods included from Closure::NodeSupport
#closure_is?, #compile_catcher, #generate_thrower, #generate_thrower_without_catcher, #in_closure, #pop_closure, #push_closure, #select_closure, #thrower
Methods included from Helpers
#current_indent, #empty_line, #indent, #js_truthy, #js_truthy_optimize, #line, #mid_to_jsid, #property, #valid_name?
Constructor Details
This class inherits a constructor from Opal::Nodes::Base
Class Method Details
.single_line?(children) ⇒ Boolean
Check if there's only one child or if they're all part of the same line (e.g. because of interpolations)
48 49 50 51 52 |
# File 'lib/opal/nodes/x_string.rb', line 48 def self.single_line?(children) (children.size == 1) || children.none? do |c| c.type == :str && c.loc.expression.source.end_with?("\n") end end |
.strip_empty_children(children) ⇒ Object
Will remove empty :str lines coming from cosmetic newlines in x-strings
62 63 64 65 66 67 68 69 70 |
# File 'lib/opal/nodes/x_string.rb', line 62 def self.strip_empty_children(children) children = children.dup empty_line = ->(child) { child.nil? || (child.type == :str && child.loc.expression.source.rstrip.empty?) } children.shift while children.any? && empty_line[children.first] children.pop while children.any? && empty_line[children.last] children end |
Instance Method Details
#compile ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/opal/nodes/x_string.rb', line 8 def compile if compiler.backtick_javascript_or_warn? compile_javascript else compile_send end end |
#compile_javascript ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/opal/nodes/x_string.rb', line 21 def compile_javascript @should_add_semicolon = false unpacked_children = unpack_return(children) stripped_children = XStringNode.strip_empty_children(unpacked_children) if XStringNode.single_line?(stripped_children) # If it's a single line we'll try to: # # - strip empty lines # - remove a trailing `;` # - detect an embedded `return` # - prepend a `return` when needed # - append a `;` when needed # - warn the user not to use the semicolon in single-line x-strings compile_single_line(stripped_children) else # Here we leave to the user the responsibility to add # a return where it's due. unpacked_children.each { |c| compile_child(c) } end wrap '(', ')' if recv? push ';' if @should_add_semicolon end |
#compile_send ⇒ Object
16 17 18 19 |
# File 'lib/opal/nodes/x_string.rb', line 16 def compile_send sexp = s(:send, nil, :`, s(:dstr, *children)) push process(sexp, @level) end |