Last active
May 18, 2023 12:29
-
-
Save flyerhzm/5807442e7dbc99ca50c4d1789eb0d4b1 to your computer and use it in GitHub Desktop.
Use node-query and node-mutation via syntax-tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'syntax_tree' | |
gem 'syntax_tree_ext' | |
gem 'node_query' | |
gem 'node_mutation' | |
end | |
NodeQuery.configure(adapter: NodeQuery::SyntaxTreeAdapter.new) | |
NodeMutation.configure(adapter: NodeMutation::SyntaxTreeAdapter.new) | |
source = "foo\nputs foo\n" | |
node = SyntaxTree::Parser.new(source).parse.statements | |
query = NodeQuery.new('.Command[message=puts]') | |
mutation = NodeMutation.new(source) | |
query.query_nodes(node).each do |matching_node| | |
mutation.remove(matching_node) | |
end | |
result = mutation.process | |
puts result.new_source # foo\n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment