question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getFirstAncestor with a lambda

See original GitHub issue

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 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
lazarljubenoviccommented, Aug 14, 2018

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 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.

1reaction
dsherretcommented, Aug 13, 2018

@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 the node.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 the traversal object (see https://dsherret.github.io/ts-simple-ast/navigation/).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found