Skip to content

Instantly share code, notes, and snippets.

@flyerhzm
Last active May 18, 2023 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flyerhzm/5807442e7dbc99ca50c4d1789eb0d4b1 to your computer and use it in GitHub Desktop.
Save flyerhzm/5807442e7dbc99ca50c4d1789eb0d4b1 to your computer and use it in GitHub Desktop.
Use node-query and node-mutation via syntax-tree
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