dsherret / dsherret/ts-morph

getExportedDeclaration should work for all exports

Open
#1,215 0 comments 0 reactions 0 assignees View on GitHub
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.**

Similar to what `getExportedDeclarations` returns, I'd like to easily grab any single export from a file to determine its references whether it's a variable, function, etc. I'm not sure if I'm doing something wrong, but right now `getExportedDeclaration` seems to only work for named exports (`export { identifier } from './file'`) and won't be called for any other exports.

**Describe the solution you'd like**

Using a simple theme file as an example, I'd like to easily grab the `theme` export:

```ts
// theme.ts
export const colors = {
primary: 'tomato',
grey: '#333',
}

export const baseTheme = {
icons: {
sm: '16px',
md: '24px',
lg: '32px',
},
} as const

export const theme = {
colors,
fontSizes: [32, 18, 14],
} as const

```

```ts
// script.ts
const project = new Project({
tsConfigFilePath: path.resolve(__dirname, 'tsconfig.json'),
})
const themeSourceFile = project.getSourceFile('theme.ts')
const themeDeclaration = themeSourceFile.getDeclarationOrThrow('theme')
const themeReferences = exportDeclaration.findReferences()
```

**Describe alternatives you've considered**

The way I'm doing this right now is through `getExportedDeclarations`:

```ts
let exportDeclaration: VariableDeclaration | undefined

for (const [name, declarations] of themeSourceFile.getExportedDeclarations()) {
if (name === 'theme') {
exportDeclaration = declarations[0] as VariableDeclaration
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the theme.ts and script.ts example, comparing getExportedDeclaration with getExportedDeclarations. Trace how getExportedDeclaration handles the named-export case and determine the expected result for the theme variable export. Done means a single export such as theme can be retrieved and used to find its references, with the existing behavior preserved.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.