getFirstAncestor with a lambda
See original GitHub issueIs 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 only getFirstAncestorByKind
and getFirstAncestorByKindOrThrow
.
Describe the solution you’d like
const method = methodBody.getFirstAncestor(ancestor => TypeGuards.isMethodDeclaration(ancestor) || TypeGuards.isConstructorDeclaration(ancestor))
Describe alternatives you’ve considered
The “workaround” is currently this:
const ancestors = methodBody.getAncestors()
const method = ancestors.find(ancestor => TypeGuards.isMethodDeclaration(ancestor) || TypeGuards.isConstructorDeclaration(ancestor))
It’s nothing drastic, but this first fetches all ancestors which could be computationally expensive.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Use parser.getFileContents in Atoum with Examples | LambdaTest
How to use getFileContents method of parser class · Run Atoum automation tests on LambdaTest cloud grid · Accelerate Your Automation Test Cycles...
Read more >org.sonar.python.checks.CheckUtils.isMethodDefinition java ...
public static boolean isMethodOfNonDerivedClass(AstNode node) { return isMethodDefinition(node) && !classHasInheritance(node.getFirstAncestor(PythonGrammar.
Read more >Lambda as "member" when class instance is captured
Basically I want to call a member function to which I pass a lambda, and I want to access class members from the...
Read more >Diff - 0df2607f98..0e821c2fa2 - chromium/src - Git at Google
CheckLongLines( + input_api, output_api, 80, lambda x: x. ... manifestURL; var partitionPath = getFirstAncestor(link, function(node) { - return !!node.
Read more >Python ConfigGrabber Examples, toolsconfiggrab.ConfigGrabber ...
... try: # filter all xode "world" objects from root, take only the first one world = filter(lambda x: isinstance(x, xode.parser. ... getFirstAncestor(ode....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Instead, only provide a function which accepts a lambda and let me pass in any kind of predicate.
Or shorter:
Or shorter:
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:
So my suggestion is to remove
getFirstAncestorByKind
completely as it only bloats the library with a use-case which can already be implemented in just as straight-forward manner and with even less typing.@cancerberoSgx I’m going to take a look. I need to re-evaluate the
getParentWhile
method. There’s already a.getFirstDescendant
method.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,
.getParameters()
uses thenode.parameters
array already and there are lots of existing methods on an array that people can use to find things. Additionally, there’s already a.getParameter(...)
and.getParameterOrThrow(...)
method. There’s also.getMethod(...)
method.I think doing a
.filter()
on.getDescendants()
would be about the same performance. For the best performance, people should use.forEachDescendant((node, traversal) => {});
using thetraversal
object (see https://dsherret.github.io/ts-simple-ast/navigation/).