Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@typespec/compiler"
---

[API] Export `getNodeForTarget` in `@typespec/compiler/ast` exports
1 change: 1 addition & 0 deletions packages/compiler/src/ast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

export { NodeFlags, SyntaxKind } from "../core/types.js";

export { getNodeForTarget } from "../core/diagnostics.js";
export { printTypeSpecNode } from "../core/formatter.js";
export {
exprIsBareIdentifier,
Expand Down
14 changes: 12 additions & 2 deletions packages/compiler/src/core/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,18 @@ export function getRelatedLocations(diagnostic: Diagnostic): RelatedSourceLocati
}

/**
* Find the syntax node for a typespec diagnostic target.
* Returns undefined if target is a type or symbol without a node.
* Find the syntax node for a TypeSpec diagnostic target.
*
* This function extracts the AST node from various types of diagnostic targets:
* - For template instance targets: returns the node of the template declaration
* - For symbols: returns the first declaration node (or symbol source for using symbols)
* - For AST nodes: returns the node itself
* - For types: returns the node associated with the type
*
* @param target The diagnostic target to extract a node from. Can be a template instance,
* symbol, AST node, or type.
* @returns The AST node associated with the target, or undefined if the target is a type
* or symbol that doesn't have an associated node.
*/
export function getNodeForTarget(target: TypeSpecDiagnosticTarget): Node | undefined {
if (!("kind" in target) && !("entityKind" in target)) {
Expand Down
Loading