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

feat: add getChildren and getChildFields methods to Node #249

Merged
merged 1 commit into from
Jul 22, 2017

Conversation

alangpierce
Copy link
Member

This should make it easier to traverse ASTs when depending only on
decaffeinate-parser.

Also change test traversal to use this new method, so that our tests make sure
every node is covered. (stripExtraInfo also had the wrong type before, so this
makes it so the param is actually always Node.)

Traverse function and parent pointers coming soon.

This should make it easier to traverse ASTs when depending only on
decaffeinate-parser.

Also change test traversal to use this new method, so that our tests make sure
every node is covered. (`stripExtraInfo` also had the wrong type before, so this
makes it so the param is actually always `Node`.)

Traverse function and parent pointers coming soon.
let children: Array<Node> = [];
for (let childField of this.getChildFields()) {
if (Array.isArray(childField)) {
children.push(...childField.filter<Node>((node): node is Node => node !== null));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm surprised this node is Node assertion filter is needed. What does just filter(node => node) do?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, it was necessary. I think it used to be that filter couldn't be used for type refinement at all, but now the current state of things is that if the callback is a type guard, then it'll work. But it won't infer your callback to be a type guard, so you need to specify the return type explicitly.

(Also, minor, but filter(node => node) also doesn't typecheck because the predicate needs to return a boolean.)

This issue has some discussion about it: microsoft/TypeScript#7657

@eventualbuddha
Copy link
Collaborator

Should we just delete decaffeinate-traverse? In theory such a package should be able to provide more fully-featured AST traversal with mutation support and everything (like babel-traverse), but I don’t see it being needed for decaffeinate itself and I don’t really want to do any CoffeeScript-based tooling, so… I’m inclined to just delete the repo.

@alangpierce
Copy link
Member Author

Should we just delete decaffeinate-traverse?

Yeah, that would be fine with me.

My reason for cleaning up decaffeinate-parser today is so that I can make a tool to get soak operation statistics for CoffeeScript projects, which might help inform the discussion at https://github.com/tc39/proposal-optional-chaining . But other than that, I don't really plan on working on any CoffeeScript tooling.

@alangpierce alangpierce merged commit 442c8c4 into decaffeinate:master Jul 22, 2017
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