Class: Opal::Rewriters::Base
- Inherits:
-
Parser::AST::Processor
- Object
- Parser::AST::Processor
- Opal::Rewriters::Base
- Defined in:
- opal/lib/opal/rewriters/base.rb
Direct Known Subclasses
BinaryOperatorAssignment, BlockToIter, DeduplicateArgName, DotJsSyntax, DumpArgs, ForRewriter, ForRewriter::LocalVariableAssigns, ForwardArgs, Hashes::KeyDuplicatesRewriter, InlineArgs, InlineArgs::Initializer, JsReservedWords, LogicalOperatorAssignment, MlhsArgs, MlhsArgs::Arguments, MlhsArgs::MlhsRewriter, Numblocks, OpalEngineCheck, PatternMatching, PatternMatching::PatternConverter, ReturnableLogic, TargetedPatches, ThrowerFinder, Opal::Rubyspec::FiltersRewriter
Defined Under Namespace
Classes: DummyLocation
Constant Summary collapse
- DUMMY_LOCATION =
DummyLocation.new
Instance Attribute Summary collapse
-
#current_node ⇒ Object
Store the current node for reporting.
Class Method Summary collapse
Instance Method Summary collapse
-
#append_to_body(body, node) ⇒ Object
Appends given +node+ to +body+ node.
- #begin_with_stmts(stmts) ⇒ Object
-
#dynamic! ⇒ Object
Called when a given transformation is deemed to be dynamic, so that cache is conditionally disabled for a given file.
-
#error(msg) ⇒ Object
This is called when a rewriting error occurs.
- #on_top(node) ⇒ Object
-
#prepend_to_body(body, node) ⇒ Object
Prepends given +node+ to +body+ node.
-
#process(node) ⇒ Object
Intercept the main call and assign current node.
- #s(type, *children) ⇒ Object
- #stmts_of(node) ⇒ Object
Instance Attribute Details
#current_node ⇒ Object
Store the current node for reporting.
110 111 112 |
# File 'opal/lib/opal/rewriters/base.rb', line 110 def current_node @current_node end |
Class Method Details
.s(type, *children) ⇒ Object
49 50 51 |
# File 'opal/lib/opal/rewriters/base.rb', line 49 def self.s(type, *children) ::Opal::AST::Node.new(type, children, location: DUMMY_LOCATION) end |
Instance Method Details
#append_to_body(body, node) ⇒ Object
Appends given +node+ to +body+ node.
Supports +body+ to be one of:
- nil - empty body
- s(:begin) / s(:kwbegin) - multiline body
- s(:anything_else) - singleline body
Returns a new body with +node+ injected as a last statement.
83 84 85 86 |
# File 'opal/lib/opal/rewriters/base.rb', line 83 def append_to_body(body, node) stmts = stmts_of(body) + stmts_of(node) begin_with_stmts(stmts) end |
#begin_with_stmts(stmts) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'opal/lib/opal/rewriters/base.rb', line 98 def begin_with_stmts(stmts) case stmts.length when 0 nil when 1 stmts[0] else s(:begin, *stmts) end end |
#dynamic! ⇒ Object
Called when a given transformation is deemed to be dynamic, so that cache is conditionally disabled for a given file.
135 136 137 |
# File 'opal/lib/opal/rewriters/base.rb', line 135 def dynamic! @dynamic_cache_result = true end |
#error(msg) ⇒ Object
This is called when a rewriting error occurs.
121 122 123 124 125 |
# File 'opal/lib/opal/rewriters/base.rb', line 121 def error(msg) error = ::Opal::RewritingError.new(msg) error.location = current_node.loc if current_node raise error end |
#on_top(node) ⇒ Object
127 128 129 130 131 |
# File 'opal/lib/opal/rewriters/base.rb', line 127 def on_top(node) node = process_regular_node(node) node.[:dynamic_cache_result] = true if @dynamic_cache_result node end |
#prepend_to_body(body, node) ⇒ Object
Prepends given +node+ to +body+ node.
Supports +body+ to be one of:
- nil - empty body
- s(:begin) / s(:kwbegin) - multiline body
- s(:anything_else) - singleline body
Returns a new body with +node+ injected as a first statement.
69 70 71 72 |
# File 'opal/lib/opal/rewriters/base.rb', line 69 def prepend_to_body(body, node) stmts = stmts_of(node) + stmts_of(body) begin_with_stmts(stmts) end |
#process(node) ⇒ Object
Intercept the main call and assign current node.
113 114 115 116 117 118 |
# File 'opal/lib/opal/rewriters/base.rb', line 113 def process(node) self.current_node = node super ensure self.current_node = nil end |
#s(type, *children) ⇒ Object
44 45 46 47 |
# File 'opal/lib/opal/rewriters/base.rb', line 44 def s(type, *children) loc = current_node ? current_node.loc : DUMMY_LOCATION ::Opal::AST::Node.new(type, children, location: loc) end |
#stmts_of(node) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'opal/lib/opal/rewriters/base.rb', line 88 def stmts_of(node) if node.nil? [] elsif %i[begin kwbegin].include?(node.type) node.children else [node] end end |