-
Notifications
You must be signed in to change notification settings - Fork 189
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
getFirstAncestor with a lambda #390
Comments
+1 and also for getDescendants (if it's not there yet). Ideally for all getters that might need refined filtering for example anInterface.getMethods() (signatures overrides have same name), getParameters, etc. @dsherret Can I take this ? Also do you think the name should be getFirstAncestor or findAncestor ? What about filtering ( I don't think is important though since writing getDescendants().filter() is kinda intuitive will end up with the same performance? |
@cancerberoSgx I'm going to take a look. I need to re-evaluate the I definitely don't want to add it on everything because then the library becomes too bloated. This should be a case-by-case addition. For example, I think doing a |
Indeed, I think that it's more straight-forward for people to do The places where these help are most useful are when there are potentially a lot of nodes and when a regular way to do it would be via a And with getByKind(SyntaxKind.Something)
getBy(TypeGuard.isSomething) |
I don't think I agree with that because I could be filtering using other information besides kind, for example, nodes with a certain Identifier text (name) - no matter its kind - or am I missing something ? |
Exactly. That's why I meant to remove "getters" which get only by syntax kind -- instead, always use a lambda -- and that lambda can be a type guard. In the concrete example, my idea is to remove this type of call completely. getFirstAncestorByKind(SyntaxKind.ClassDeclaration) Instead, only provide a function which accepts a lambda and let me pass in any kind of predicate. getFirstAncestorBy(ancestor => ancestor.getKind() == SyntaxKind.ClassDeclaration) Or shorter: getFirstAncestorBy(ancestor => TypeGuards.isClassDeclaration(ancestor)) Or shorter: getFirstAncestorBy(TypeGuads.isClassDeclaration) The point is, if you look at current thing and the last snippet above, the last one is more flexibile and shorter for the same use case: getFirstAncestorByKind(SyntaxKind.ClassDeclaration)
getFirstAncestorBy(TypeGuads.isClassDeclaration) So my suggestion is to remove |
@lazarljubenovic agreed, sorry I misunderstood your previous comment. IMO just the name "findAncestor" would be better than getFirstAncestorBy since Array.prototype.find returns the first item found and is the behavior people would expect for something named "find". Thanks! |
Indeed, |
Will be in the next release. I don't want to use |
Is your feature request related to a problem? Please describe.
I need to get the enclosing method in a class -- but this could be a constructor as well. Looks like we're missing a way to provide a custom lambda for
getFirstAncestor
, as there is onlygetFirstAncestorByKind
andgetFirstAncestorByKindOrThrow
.Describe the solution you'd like
Describe alternatives you've considered
The "workaround" is currently this:
It's nothing drastic, but this first fetches all ancestors which could be computationally expensive.
The text was updated successfully, but these errors were encountered: