Skip to content

Commit

Permalink
add includeDependencies option to JavaScriptFileExport getText method
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Feb 4, 2025
1 parent 681cd4d commit 7788b7c
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 198 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-trees-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'renoun': minor
---

Adds `includeDependencies` option to `JavaScriptFileExport#getText` method. When enabled, this will include all dependencies of the export declaration in the returned text.
29 changes: 24 additions & 5 deletions packages/renoun/src/file-system/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CodeBlock, parsePreProps } from '../components/CodeBlock/index.js'
import { CodeInline } from '../components/CodeInline.js'
import { MDXRenderer } from '../components/MDXRenderer.js'
import type { MDXComponents } from '../mdx/index.js'
import { getFileExportMetadata } from '../project/client.js'
import { getFileExportMetadata, getFileExportText } from '../project/client.js'
import { createSlug, type SlugCasings } from '../utils/create-slug.js'
import { formatNameAsTitle } from '../utils/format-name-as-title.js'
import { getEditorUri } from '../utils/get-editor-uri.js'
Expand Down Expand Up @@ -663,9 +663,28 @@ export class JavaScriptFileExport<Value> {
return this.#metadata?.environment
}

/** Get the source text of the export. */
getText() {
return this.#metadata?.text
/**
* Get the source text of the export, optionally including dependencies.
*
* Note, including dependencies can be expensive to calculate, only use when necessary.
*/
async getText({
includeDependencies,
}: { includeDependencies?: boolean } = {}) {
const location = await this.#getLocation()

if (location === undefined) {
throw new Error(
`[renoun] Export cannot be statically analyzed at file path "${this.#file.getRelativePath()}".`
)
}

return getFileExportText(
location.path,
location.position,
location.kind,
includeDependencies
)
}

/** Get the start and end position of the export in the file system. */
Expand Down Expand Up @@ -712,7 +731,7 @@ export class JavaScriptFileExport<Value> {

if (location === undefined) {
throw new Error(
`[renoun] Export can not be statically analyzed at file path "${this.#file.getRelativePath()}".`
`[renoun] Export cannot not be statically analyzed at file path "${this.#file.getRelativePath()}".`
)
}

Expand Down
46 changes: 45 additions & 1 deletion packages/renoun/src/project/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export async function getFileExports(
}

/**
* Get a specific export of a file.
* Get a specific file export in a source file.
* @internal
*/
export async function getFileExportMetadata(
Expand Down Expand Up @@ -190,6 +190,50 @@ export async function getFileExportMetadata(
)
}

/**
* Get a specific file export's text by identifier, optionally including its dependencies.
* @internal
*/
export async function getFileExportText(
filePath: string,
position: number,
kind: SyntaxKind,
includeDependencies?: boolean,
projectOptions?: ProjectOptions
) {
if (client) {
return client.callMethod<
{
filePath: string
position: number
kind: SyntaxKind
includeDependencies?: boolean
projectOptions?: ProjectOptions
},
string
>('getFileExportText', {
filePath,
position,
kind,
includeDependencies,
projectOptions,
})
}

return import('../utils/get-file-export-text.js').then(
({ getFileExportText }) => {
const project = getProject(projectOptions)
return getFileExportText({
filePath,
position,
kind,
includeDependencies,
project,
})
}
)
}

/**
* Create a source file in the project.
* @internal
Expand Down
27 changes: 27 additions & 0 deletions packages/renoun/src/project/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getFileExports as baseGetFileExports,
getFileExportMetadata as baseGetFileExportMetadata,
} from '../utils/get-file-exports.js'
import { getFileExportText as baseGetFileExportText } from '../utils/get-file-export-text.js'
import { getRootDirectory } from '../utils/get-root-directory.js'
import { isFilePathGitIgnored } from '../utils/is-file-path-git-ignored.js'
import type { SymbolFilter } from '../utils/resolve-type.js'
Expand Down Expand Up @@ -156,6 +157,32 @@ export async function createServer(options?: { port?: number }) {
}
)

server.registerMethod(
'getFileExportText',
async function getFileExportText({
filePath,
position,
kind,
includeDependencies,
projectOptions,
}: {
filePath: string
position: number
kind: SyntaxKind
includeDependencies?: boolean
projectOptions?: ProjectOptions
}) {
const project = getProject(projectOptions)
return baseGetFileExportText({
filePath,
position,
kind,
includeDependencies,
project,
})
}
)

server.registerMethod(
'createSourceFile',
async function createSourceFile({
Expand Down
84 changes: 0 additions & 84 deletions packages/renoun/src/utils/extract-export-by-identifier.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { SourceFile } from 'ts-morph'
import type { Node } from 'ts-morph'
import tsMorph from 'ts-morph'

const { Node, ts } = tsMorph

/** Extract a single export and its local dependencies from a source file. */
export function extractExportByIdentifier(
sourceFile: SourceFile,
identifier: string
export function getExportDeclarationTextWithDependencies(
exportDeclaration: Node
) {
const identifier = exportDeclaration
.getFirstDescendantByKindOrThrow(tsMorph.ts.SyntaxKind.Identifier)
.getText()
let sourceFile = exportDeclaration.getSourceFile()

/** Copy the source file so it isn't mutated. */
const baseName = sourceFile.getBaseNameWithoutExtension()

Expand All @@ -23,13 +25,16 @@ export function extractExportByIdentifier(
/** Collect remaining exports and remove any declarations that don't have references. */
sourceFile.getExportedDeclarations().forEach((declarations) => {
declarations.forEach((declaration) => {
if (Node.isSourceFile(declaration) || Node.isExpression(declaration)) {
if (
tsMorph.Node.isSourceFile(declaration) ||
tsMorph.Node.isExpression(declaration)
) {
return
}

const exportIdentifier = declaration.getFirstDescendantByKind(
ts.SyntaxKind.Identifier
)!
const exportIdentifier = declaration.getFirstDescendantByKindOrThrow(
tsMorph.ts.SyntaxKind.Identifier
)

if (exportIdentifier.getText() !== identifier) {
declaration.remove()
Expand Down
46 changes: 0 additions & 46 deletions packages/renoun/src/utils/get-exported-declaration.test.ts

This file was deleted.

48 changes: 0 additions & 48 deletions packages/renoun/src/utils/get-exported-declaration.ts

This file was deleted.

Loading

0 comments on commit 7788b7c

Please sign in to comment.