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

Allow for custom node type keys #139

Merged
merged 2 commits into from
Feb 27, 2023
Merged

Conversation

nzakas
Copy link
Contributor

@nzakas nzakas commented Feb 17, 2023

In ESLint, we are looking at being able to support different formats of ASTs, and as such, we can't guarantee that the node type will always be in the type key. This patch allows a custom nodeTypeKey option to be passed in that will alter be used instead of type when specified.

I wasn't sure what kinds of tests you'd be looking for here, so I added one as an example, but please let me know if you'd like me to add others.

Copy link
Member

@michaelficarra michaelficarra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This WFM. It's a bit weird that our "class" matchers (:function, :expression, etc) are aliases for a hard-coded list of estree types. But as long as they don't use these selectors, things should work fine. That begs the question, then: should we even update the class selector implementation to use nodeTypeKey if it's specific to estree which uses type?

@nzakas
Copy link
Contributor Author

nzakas commented Feb 27, 2023

That begs the question, then: should we even update the class selector implementation to use nodeTypeKey if it's specific to estree which uses type?

That's an excellent question. While some of them will definitely not work elsewhere, it seems like :expression, :statement, and :declaration would still most likely work in other ASTs so long as they follow a similar convention as ESTree.

I was going to follow up with another PR that externalizes the class name resolution so we can pass in a custom resolver that will take the place of the internal one. That would solve the problem completely for ESLint.

@michaelficarra
Copy link
Member

it seems like :expression, :statement, and :declaration would still most likely work in other ASTs so long as they follow a similar convention as ESTree

I don't really like the thought of someone relying on that. We currently take some shortcuts (like "ends with Expression") to avoid doing a comparison with every possible node type because we assume we know all the possible node types. I don't want this shortcut to become de facto spec.

I was going to follow up with another PR that externalizes the class name resolution

I think this is a good approach. Let's drop the class changes from this PR then until we have such a facility.

Design wise, I'm thinking the config is an object whose keys are class names that extend/override built-in class names and whose values are predicates. Not sure how much context is appropriate for the predicate parameters though. I would probably just start with passing the node for now.

@nzakas
Copy link
Contributor Author

nzakas commented Feb 27, 2023

Sounds good. I've disabled classes when nodeTypeKind is supplied. Once this is merged, I'll get to work on the custom class resolver.

@michaelficarra
Copy link
Member

Thanks @nzakas.

@michaelficarra michaelficarra merged commit a195660 into estools:master Feb 27, 2023
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

Successfully merging this pull request may close these issues.

None yet

2 participants