diff --git a/.chronus/changes/expose-get-node-for-target-2025-9-2-20-36-17.md b/.chronus/changes/expose-get-node-for-target-2025-9-2-20-36-17.md new file mode 100644 index 00000000000..60ad2138ed8 --- /dev/null +++ b/.chronus/changes/expose-get-node-for-target-2025-9-2-20-36-17.md @@ -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 diff --git a/packages/compiler/src/ast/index.ts b/packages/compiler/src/ast/index.ts index 1196010a119..8c126a4291e 100644 --- a/packages/compiler/src/ast/index.ts +++ b/packages/compiler/src/ast/index.ts @@ -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, diff --git a/packages/compiler/src/core/diagnostics.ts b/packages/compiler/src/core/diagnostics.ts index f23e3f791ba..1da09adebe9 100644 --- a/packages/compiler/src/core/diagnostics.ts +++ b/packages/compiler/src/core/diagnostics.ts @@ -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)) {