dsherret / dsherret/ts-morph

Getting a concrete definition: getDefinitionNodeOfKind(OrThrow)

Open
#701 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**Is your feature request related to a problem? Please describe.**

I find myself almost always expecting to find a definition of a certain kind. I expect that most use-cases have such a need.

**Describe the solution you'd like**

From:

```ts
const elementDefinitions = element.getDefinitionNodes()
const classDeclaration = elementDefinitions.find(TypeGuards.isClassDeclaration)
if (classDeclaration == null) throw new Error(`All elements of @NgModule.imports are expected to reference a class.`)
```

To:

```ts
const classDeclaration = element.getDefinitionNodeByKindOrThrow(SyntaxKind.ClassDeclaration)
```

**Describe alternatives you've considered**

I can obviously wrap this myself in a function :smile:

Contributor guide

Open the contributing guide

Research direction

Read CONTRIBUTING.md, then inspect the existing getDefinitionNodes() API and the SyntaxKind and TypeGuards usage shown in the issue. Determine the intended behavior for a matching definition and for no match, and verify that the new convenience API supports the class-declaration example without changing existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.