Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to Node#source or Location#source? #3467

Open
ioquatix opened this issue Feb 17, 2025 · 2 comments
Open

Access to Node#source or Location#source? #3467

ioquatix opened this issue Feb 17, 2025 · 2 comments

Comments

@ioquatix
Copy link
Member

I would like to access the source name, similar to how in the parser gem we can access node.source_buffer.name which is usually the file path.

Is that possible or can we make it possible? Maybe we can attach a name or some other metadata to the source object?

@kddnewton
Copy link
Collaborator

Do you need to do it through the node? It's available on the parse result, which is meant to funnel that kind of logic through that top-level interface. I was hoping to keep the fact that there is a source on the node/location as kind of opaque.

@ioquatix
Copy link
Member Author

ioquatix commented Feb 17, 2025

I understand your design and I'm okay if you don't want to expose it, but I don't store the parse results in my own AST, only the node.

Right now:

class Parser
  def self.parse(code, path)
    top = Prism.parse(code).value
    self.new.walk(top)
  end
  
  def walk(node, ...)
    if node.type == ...
      Thing.new(node)
    ...
end

class Thing
  def initialize(node)
    @node = node
  end

  def path
    # Previously node.source_buffer.name
    ???

I'd have to put the parse results into the instance of Thing which seems cumbersome, or alternatively pass in the path/source buffer as well as the node (was hoping to avoid this).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants