Results for: "Logger"

The dispatcher class fires events for nodes that are found while walking an AST to all registered listeners. It’s useful for performing different types of analysis on the AST while only having to walk the tree once.

To use the dispatcher, you would first instantiate it and register listeners for the events you’re interested in:

class OctalListener
  def on_integer_node_enter(node)
    if node.octal? && !node.slice.start_with?("0o")
      warn("Octal integers should be written with the 0o prefix")
    end
  end
end

listener = OctalListener.new
dispatcher = Prism::Dispatcher.new
dispatcher.register(listener, :on_integer_node_enter)

Then, you can walk any number of trees and dispatch events to the listeners:

result = Prism.parse("001 + 002 + 003")
dispatcher.dispatch(result.value)

Optionally, you can also use ‘#dispatch_once` to dispatch enter and leave events for a single node without recursing further down the tree. This can be useful in circumstances where you want to reuse the listeners you already have registers but want to stop walking the tree at a certain point.

integer = result.value.statements.body.first.receiver.receiver
dispatcher.dispatch_once(integer)

Here we are going to patch StringQuery to put in the class-level methods so that it can maintain a consistent interface

Query methods that allow categorizing strings based on their context for where they could be valid in a Ruby syntax tree.

This visitor walks through the tree and copies each node as it is being visited. This is useful for consumers that want to mutate the tree, as you can change subtrees in place without effecting the rest of the tree.

Represents the use of the ‘alias` keyword to alias a global variable.

alias $foo $bar
^^^^^^^^^^^^^^^

Represents an array pattern in pattern matching.

foo in 1, 2
^^^^^^^^^^^

foo in [1, 2]
^^^^^^^^^^^^^

foo in *bar
^^^^^^^^^^^

foo in Bar[]
^^^^^^^^^^^^

foo in Bar[1, 2, 3]
^^^^^^^^^^^^^^^^^^^

Represents a block argument using ‘&`.

bar(&args)
^^^^^^^^^^

Represents a block of ruby code.

[1, 2, 3].each { |i| puts x }
               ^^^^^^^^^^^^^^

Represents the use of the ‘||=` operator on a call.

foo.bar ||= value
^^^^^^^^^^^^^^^^^

Represents assigning to a method call.

foo.bar, = 1
^^^^^^^

begin
rescue => foo.bar
          ^^^^^^^
end

for foo.bar in baz do end
    ^^^^^^^

Represents assigning to a local variable in pattern matching.

foo => [bar => baz]
       ^^^^^^^^^^^^

Represents referencing a class variable.

@@foo
^^^^^

Represents writing to a class variable in a context that doesn’t have an explicit value.

@@foo, @@bar = baz
^^^^^  ^^^^^

Represents assigning to a constant path using an operator that isn’t ‘=`.

Parent::Child += value
^^^^^^^^^^^^^^^^^^^^^^

Represents writing to a constant path in a context that doesn’t have an explicit value.

Foo::Foo, Bar::Bar = baz
^^^^^^^^  ^^^^^^^^

Represents writing to a constant in a context that doesn’t have an explicit value.

Foo, Bar = baz
^^^  ^^^

Represents a find pattern in pattern matching.

foo in *bar, baz, *qux
       ^^^^^^^^^^^^^^^

foo in [*bar, baz, *qux]
       ^^^^^^^^^^^^^^^^^

foo in Foo(*bar, baz, *qux)
       ^^^^^^^^^^^^^^^^^^^^

Represents the use of the ‘..` or `…` operators to create flip flops.

baz if foo .. bar
       ^^^^^^^^^^

Represents a floating point number literal.

1.0
^^^

Represents the use of the forwarding parameter in a method, block, or lambda declaration.

def foo(...)
        ^^^
end

Represents the use of the ‘super` keyword without parentheses or arguments.

super
^^^^^

Represents writing to a global variable.

$foo = 1
^^^^^^^^

Represents a hash pattern in pattern matching.

foo => { a: 1, b: 2 }
       ^^^^^^^^^^^^^^

foo => { a: 1, b: 2, **c }
       ^^^^^^^^^^^^^^^^^^^

Represents the use of an assignment operator on a call to ‘[]`.

foo.bar[baz] += value
^^^^^^^^^^^^^^^^^^^^^

Represents assigning to an index.

foo[bar], = 1
^^^^^^^^

begin
rescue => foo[bar]
          ^^^^^^^^
end

for foo[bar] in baz do end
    ^^^^^^^^

Represents referencing an instance variable.

@foo
^^^^
Search took: 2ms  ·  Total Results: 3406