bablr-lang / bablr-vm

A VM for enforcing language rules on agAST trees
MIT License
40 stars 2 forks source link

Scope #17

Open conartist6 opened 2 years ago

conartist6 commented 2 years ago

I need to create some pattern for understanding scope in CSTs. That will be critical to understanding the abstract portion of the tree so that it is truly useful for codemods.

conartist6 commented 2 years ago

Scope is understood in the context of grammar rules. But how will the information be stored and presented? Better take a look at some existing scope analysis tools

conartist6 commented 2 years ago

The matchNode tree is probably a decent place to keep a scope representation. It is safe to keep some graph-structured references in there, data stored in a matchNode is guaranteed to have been validated, and it would allow scope data to be used when traversing a matchNode tree, an operation which I expect to turn into the library's primary low-level API.

conartist6 commented 2 years ago

I'm going to close this for now. I may eventually build my own scope tools, but for now I'll let people use babel on my trees.

conartist6 commented 2 years ago

Ehhh I changed my mind I'm gonna leave it open. I think I can build some level of functionality without tackling everything.

conartist6 commented 2 years ago

I think the basic functionality I want is to establish which identifiers are the same symbol. I think the way to do this is by attaching a scope reference to the identifier token (either as a property or using a WeakMap). There will also need to be a way to find for a given scope id which node defined it. I would also like to consider differentiating between an identifier that defines a symbol and one that references a symbol.

This data would allow building a significant number of tools, yet it would not bless any particular API for manipulating scope. It would ideally allow the creation of many tools, each of which could support many languages out of the box by including a prebuilt grammar.

conartist6 commented 1 year ago

Another possibility, which I think may be better:

Instead of attaching a "identifier id" to each identifier, attach a "scope id" to each node.

conartist6 commented 1 year ago

What happens for nodes that define more than one new scope? Javascript doesn't have a problem as it uses the BlockStatement node type, but a language in which curly braces are not optional for control structures for example might not choose to do this, instead having some node type whose tokens are:

if (ref) { ref[] } else { ref[] }.